Using the key_control block for the first key-handling functions in all
[idzebra-moved-to-github.git] / index / index.h
1 /* $Id: index.h,v 1.117 2004-09-01 15:01:32 heikki Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 #ifndef INDEX_H
24 #define INDEX_H
25
26 #include <time.h>
27 #include <idzebra/version.h>
28 #include <zebrautl.h>
29 #include <zebramap.h>
30 #include <sortidx.h>
31
32 #if HAVE_SYS_TIMES_H
33 #include <sys/times.h>
34 #endif
35 #include <sys/stat.h>
36
37 #include <dict.h>
38 #include <isams.h>
39 #include <isamc.h>
40 #include <isamb.h>
41 #include <data1.h>
42 #include <recctrl.h>
43 #include "recindex.h"
44 #include <idzebra/api.h>
45 #include "zinfo.h"
46 #include <passwddb.h>
47 #include <rset.h>
48
49 YAZ_BEGIN_CDECL
50
51 #define ISAM_DEFAULT "b"
52
53 #define SU_SCHEME 1
54
55 #define IT_MAX_WORD 256
56
57 #define IT_KEY_NEW 1
58
59 #if IT_KEY_NEW
60
61 #endif
62
63 #if IT_KEY_NEW
64 #define IT_KEY_LEVEL_MAX 4
65 struct it_key {
66     int  len;
67     zint mem[IT_KEY_LEVEL_MAX];
68 };
69 #else
70 struct it_key {
71     int  sysno;
72     int  seqno;
73 };
74 #endif
75
76
77 enum dirsKind { dirs_dir, dirs_file };
78
79 struct dir_entry {
80     enum dirsKind kind;
81     char *name;
82     time_t mtime;
83 };
84
85 struct dirs_entry {
86     enum dirsKind kind;
87     char path[256];
88     SYSNO sysno;
89     time_t mtime;
90 };
91
92 void getFnameTmp (Res res, char *fname, int no);
93         
94 struct dirs_info *dirs_open (Dict dict, const char *rep, int rw);
95 struct dirs_info *dirs_fopen (Dict dict, const char *path);
96 struct dirs_entry *dirs_read (struct dirs_info *p);
97 struct dirs_entry *dirs_last (struct dirs_info *p);
98 void dirs_mkdir (struct dirs_info *p, const char *src, time_t mtime);
99 void dirs_rmdir (struct dirs_info *p, const char *src);
100 void dirs_add (struct dirs_info *p, const char *src, SYSNO sysno, time_t mtime);
101 void dirs_del (struct dirs_info *p, const char *src);
102 void dirs_free (struct dirs_info **pp);
103
104 struct dir_entry *dir_open (const char *rep, const char *base,
105                             int follow_links);
106 void dir_sort (struct dir_entry *e);
107 void dir_free (struct dir_entry **e_p);
108
109 void repositoryUpdate (ZebraHandle zh, const char *path);
110 void repositoryAdd (ZebraHandle zh, const char *path);
111 void repositoryDelete (ZebraHandle zh, const char *path);
112 void repositoryShow (ZebraHandle zh, const char *path);
113
114 int key_open (ZebraHandle zh, int mem);
115 int key_close (ZebraHandle zh);
116 int key_compare (const void *p1, const void *p2);
117 void key_init(struct it_key *k);
118 char *key_print_it (const void *p, char *buf);
119 zint key_get_seq (const void *p);
120 int key_compare_it (const void *p1, const void *p2);
121 int key_qsort_compare (const void *p1, const void *p2);
122 void key_logdump (int mask, const void *p);
123 void key_logdump_txt (int logmask, const void *p, const char *txt);
124 void inv_prstat (ZebraHandle zh);
125 void inv_compact (BFiles bfs);
126 void key_input (ZebraHandle zh, int nkeys, int cache, Res res);
127 ISAMS_M *key_isams_m (Res res, ISAMS_M *me);
128 ISAMC_M *key_isamc_m (Res res, ISAMC_M *me);
129 int merge_sort (char **buf, int from, int to);
130 int key_SU_code (int ch, char *out);
131
132 #define FNAME_DICT "dict"
133 #define FNAME_ISAM "isam"
134 #define FNAME_ISAMC "isamc"
135 #define FNAME_ISAMS "isams"
136 #define FNAME_ISAMH "isamh"
137 #define FNAME_ISAMD "isamd"
138 #define FNAME_CONFIG "zebra.cfg"
139
140 #define GMATCH_DICT "gmatch"
141 #define FMATCH_DICT "fmatch"
142
143 struct strtab *strtab_mk (void);
144 int strtab_src (struct strtab *t, const char *name, void ***infop);
145 void strtab_del (struct strtab *t,
146                  void (*func)(const char *name, void *info, void *data),
147                  void *data);
148 int index_char_cvt (int c);
149 int index_word_prefix (char *string, int attset_ordinal,
150                        int local_attribute, const char *databaseName);
151
152
153 void zebraIndexLockMsg (ZebraHandle zh, const char *str);
154 void zebraIndexUnlock (ZebraHandle zh);
155 int zebraIndexLock (BFiles bfs, ZebraHandle zh, int commitNow, const char *rval);
156 int zebraIndexWait (ZebraHandle zh, int commitPhase);
157
158 #define FNAME_MAIN_LOCK   "zebraidx.LCK"
159 #define FNAME_COMMIT_LOCK "zebracmt.LCK"
160 #define FNAME_ORG_LOCK    "zebraorg.LCK"
161 #define FNAME_TOUCH_TIME  "zebraidx.time"
162
163 typedef struct zebra_lock_info *ZebraLockHandle;
164 ZebraLockHandle zebra_lock_create(const char *dir,
165                                   const char *file, int excl_flag);
166 void zebra_lock_destroy (ZebraLockHandle h);
167 int zebra_lock (ZebraLockHandle h);
168 int zebra_lock_nb (ZebraLockHandle h);
169 int zebra_unlock (ZebraLockHandle h);
170 int zebra_lock_fd (ZebraLockHandle h);
171 void zebra_lock_prefix (Res res, char *dst);
172 char *zebra_mk_fname (const char *dir, const char *name);
173
174 int zebra_lock_w (ZebraLockHandle h);
175 int zebra_lock_r (ZebraLockHandle h);
176
177 void zebra_load_atts (data1_handle dh, Res res);
178
179 int key_SU_decode (int *ch, const unsigned char *out);
180 int key_SU_encode (int ch, char *out);
181
182 #define ENCODE_BUFLEN 768
183 struct encode_info {
184     int  sysno;  /* previously written values for delta-compress */
185     int  seqno;
186     int  cmd;
187     int prevsys; /* buffer for skipping insert/delete pairs */
188     int prevseq;
189     int prevcmd;
190     int keylen; /* tells if we have an unwritten key in buf, and how long*/
191 #if IT_KEY_NEW
192     void *encode_handle;
193 #endif
194     char buf[ENCODE_BUFLEN];
195 };
196
197 void encode_key_init (struct encode_info *i);
198 char *encode_key_int (int d, char *bp);
199 void encode_key_write (char *k, struct encode_info *i, FILE *outf);
200 void encode_key_flush (struct encode_info *i, FILE *outf);
201
202 typedef struct {
203     char *term;
204     char *db;
205     zint sysno;
206     int score;
207 } *ZebraPosSet;
208
209 typedef struct zebra_set *ZebraSet;
210
211 typedef struct zebra_rank_class {
212     struct rank_control *control;
213     int init_flag;
214     void *class_handle;
215     struct zebra_rank_class *next;
216 } *ZebraRankClass;
217
218 struct recKeys {
219     int buf_used;
220     int buf_max;
221     char *buf;
222 #if IT_KEY_NEW
223     void *codec_handle;
224 #else
225     int prevSeqNo;
226     char prevAttrSet;
227     short prevAttrUse;
228 #endif
229 };
230
231 struct sortKeys {
232     int buf_used;
233     int buf_max;
234     char *buf;
235 };
236
237 struct zebra_register {
238     char *name;
239     
240     ISAMS isams;
241     ISAMC isamc;
242     ISAMB isamb;
243     Dict dict;
244     Dict matchDict;
245     SortIdx sortIdx;
246     int registerState; /* 0 (no commit pages), 1 (use commit pages) */
247     time_t registerChange;
248     BFiles bfs;
249     Records records;
250     ZebraExplainInfo zei;
251
252     char *server_path_prefix;
253     data1_handle dh;
254     ZebraMaps zebra_maps;
255     ZebraRankClass rank_classes;
256     RecTypes recTypes;
257     int seqno;
258     int last_val;
259     int stop_flag;
260     int active; /* 0=shutdown, 1=enabled and inactive, 2=activated */
261
262     struct recKeys keys;
263 #if 1
264     struct sortKeys sortKeys;
265 #else
266     struct sortKey *sortKeys;
267 #endif
268     char **key_buf;
269     size_t ptr_top;
270     size_t ptr_i;
271     size_t key_buf_used;
272     int key_file_no;
273 };
274
275 struct zebra_service {
276     int stop_flag;
277     Res global_res;
278     char *configName;
279     struct zebra_session *sessions;
280     struct zebra_register *regs;
281     Zebra_mutex_cond session_lock;
282     Passwd_db passwd_db;
283     const char *path_root;
284 };
285
286
287 struct zebra_session {
288     struct zebra_session *next;
289     struct zebra_service *service;
290     struct zebra_register *reg;
291
292     char *xadmin_databaseName;
293
294     char **basenames;
295     int num_basenames;
296     char *reg_name;
297     char *path_reg;
298
299     ZebraLockHandle lock_normal;
300     ZebraLockHandle lock_shadow;
301
302     int trans_no;
303     int trans_w_no;
304
305     int destroyed;
306     ZebraSet sets;
307     Res res;
308     char *user_perm;
309     int errCode;
310     zint hits;
311     char *errString;
312 #if HAVE_SYS_TIMES_H
313     struct tms tms1;
314     struct tms tms2;    
315 #endif
316     int  shadow_enable;
317
318     zint records_inserted;
319     zint records_updated;
320     zint records_deleted;
321     zint records_processed;
322     char *record_encoding;
323
324     yaz_iconv_t iconv_to_utf8;
325     yaz_iconv_t iconv_from_utf8;
326
327     int m_follow_links;
328     const char *m_group;
329     const char *m_record_id;
330     const char *m_record_type;
331     int m_store_data;
332     int m_store_keys;
333     int m_explain_database;
334     int m_flag_rw;
335     int m_file_verbose_limit;
336 };
337
338 struct rank_control {
339     char *name;
340     void *(*create)(ZebraHandle zh);
341     void (*destroy)(struct zebra_register *reg, void *class_handle);
342     void *(*begin)(struct zebra_register *reg, void *class_handle, RSET rset);
343     /* ### Could add parameters to begin:
344      *  char *index;    // author, title, etc.
345      *  int dbsize;     // number of records in database
346      *  int rssize;     // number of records in result set (estimate?)
347      */
348     void (*end)(struct zebra_register *reg, void *set_handle);
349     int (*calc)(void *set_handle, zint sysno);
350     void (*add)(void *set_handle, int seqno, int term_index);
351 };
352
353 struct term_set_entry {
354     char *term;
355     struct term_set_entry *next;
356 };
357
358 struct term_set_list {
359     struct term_set_entry *first;
360     struct term_set_entry *last;
361 };
362
363 RSET rpn_search (ZebraHandle zh, NMEM mem, NMEM rset_nmem,
364                  Z_RPNQuery *rpn, int num_bases, char **basenames, 
365                  const char *setname, ZebraSet sset);
366
367
368 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
369                oid_value attributeset,
370                int num_bases, char **basenames,
371                int *position, int *num_entries, ZebraScanEntry **list,
372                int *is_partial, RSET limit_set, int return_zero);
373
374 RSET rset_trunc (ZebraHandle zh, ISAMS_P *isam_p, int no,
375                  const char *term, int length_term, const char *flags,
376                  int preserve_position, int term_type, NMEM rset_nmem,
377                  const struct key_control *kctrl);
378
379 void resultSetAddTerm (ZebraHandle zh, ZebraSet s, int reg_type,
380                        const char *db, int set,
381                        int use, const char *term);
382 ZebraSet resultSetAdd (ZebraHandle zh, const char *name, int ov);
383 ZebraSet resultSetGet (ZebraHandle zh, const char *name);
384 ZebraSet resultSetAddRPN (ZebraHandle zh, NMEM m, Z_RPNQuery *rpn,
385                           int num_bases, char **basenames,
386                           const char *setname);
387 RSET resultSetRef (ZebraHandle zh, const char *resultSetId);
388 void resultSetDestroy (ZebraHandle zh, int num_names, char **names,
389                        int *statuses);
390
391
392 ZebraPosSet zebraPosSetCreate (ZebraHandle zh, const char *name,
393                                int num, int *positions);
394 void zebraPosSetDestroy (ZebraHandle zh, ZebraPosSet records, int num);
395
396 void resultSetSort (ZebraHandle zh, NMEM nmem,
397                     int num_input_setnames, const char **input_setnames,
398                     const char *output_setname,
399                     Z_SortKeySpecList *sort_sequence, int *sort_status);
400 void resultSetSortSingle (ZebraHandle zh, NMEM nmem,
401                           ZebraSet sset, RSET rset,
402                           Z_SortKeySpecList *sort_sequence, int *sort_status);
403 void resultSetRank (ZebraHandle zh, ZebraSet zebraSet, RSET rset);
404 void resultSetInvalidate (ZebraHandle zh);
405
406 int zebra_server_lock_init (ZebraService zh);
407 int zebra_server_lock_destroy (ZebraService zh);
408 int zebra_server_lock (ZebraService zh, int lockCommit);
409 void zebra_server_unlock (ZebraService zh, int commitPhase);
410 int zebra_server_lock_get_state (ZebraService zh, time_t *timep);
411
412 typedef struct attent
413 {
414     int attset_ordinal;
415     data1_local_attribute *local_attributes;
416 } attent;
417
418 void zebraRankInstall (struct zebra_register *reg, struct rank_control *ctrl);
419 ZebraRankClass zebraRankLookup (ZebraHandle zh, const char *name);
420 void zebraRankDestroy (struct zebra_register *reg);
421
422 int att_getentbyatt(ZebraHandle zh, attent *res, oid_value set, int att,
423                 const char *sattr);
424
425 extern struct rank_control *rank1_class;
426 extern struct rank_control *rankzv_class;
427 extern struct rank_control *rankliv_class;
428
429 int zebra_record_fetch (ZebraHandle zh, SYSNO sysno, int score, ODR stream,
430                         oid_value input_format, Z_RecordComposition *comp,
431                         oid_value *output_format, char **rec_bufp,
432                         int *rec_lenp, char **basenamep);
433
434 void extract_get_fname_tmp (ZebraHandle zh, char *fname, int no);
435
436 void zebra_index_merge (ZebraHandle zh);
437
438 int buffer_extract_record (ZebraHandle zh, 
439                            const char *buf, size_t buf_size,
440                            int delete_flag,
441                            int test_mode, 
442                            const char *recordType,
443                            SYSNO *sysno,
444                            const char *match_criteria,
445                            const char *fname,
446                            int force_update,
447                            int allow_update);
448
449 #if 0
450 int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
451                         const char *buf, size_t buf_size,
452                         const char *databaseName, int delete_flag,
453                         int test_mode, int *sysno,
454                         int store_keys, int store_data,
455                         const char *match_criteria);
456 #endif
457 void extract_flushWriteKeys (ZebraHandle zh, int final);
458
459 struct zebra_fetch_control {
460     off_t offset_end;
461     off_t record_offset;
462     off_t record_int_pos;
463     const char *record_int_buf;
464     int record_int_len;
465     int fd;
466 };
467
468 int zebra_record_ext_read (void *fh, char *buf, size_t count);
469 off_t zebra_record_ext_seek (void *fh, off_t offset);
470 off_t zebra_record_ext_tell (void *fh);
471 off_t zebra_record_int_seek (void *fh, off_t offset);
472 off_t zebra_record_int_tell (void *fh);
473 int zebra_record_int_read (void *fh, char *buf, size_t count);
474 void zebra_record_int_end (void *fh, off_t offset);
475
476 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
477                               int cmd, struct recKeys *reckeys);
478 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
479                             int cmd, struct sortKeys *skp);
480 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid);
481 void extract_token_add (RecWord *p);
482 int explain_extract (void *handle, Record rec, data1_node *n);
483
484 int fileExtract (ZebraHandle zh, SYSNO *sysno, const char *fname,
485                  int deleteFlag);
486
487 int zebra_begin_read (ZebraHandle zh);
488 int zebra_end_read (ZebraHandle zh);
489
490 int zebra_file_stat (const char *file_name, struct stat *buf,
491                      int follow_links);
492
493 void zebra_livcode_transform(ZebraHandle zh, Z_RPNQuery *query);
494
495 void *iscz1_start ();
496 void iscz1_reset (void *vp);
497 void iscz1_stop (void *p);
498 void iscz1_decode (void *vp, char **dst, const char **src);
499 void iscz1_encode (void *vp, char **dst, const char **src);
500
501 YAZ_END_CDECL
502
503 #endif