Remove isamd. It's not been in use for a long time and isamb is better
[idzebra-moved-to-github.git] / index / index.h
1 /* $Id: index.h,v 1.110 2004-08-04 08:35:23 adam 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 <zebraver.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 <isam.h>
40 #include <isamc.h>
41 #include <isamb.h>
42 #define ISAM_DEFAULT "c"
43 #include <data1.h>
44 #include <recctrl.h>
45 #include "recindex.h"
46 #include "zebraapi.h"
47 #include "zinfo.h"
48 #include <passwddb.h>
49 #include <rset.h>
50
51 YAZ_BEGIN_CDECL
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 enum dirsKind { dirs_dir, dirs_file };
77
78 struct dir_entry {
79     enum dirsKind kind;
80     char *name;
81     time_t mtime;
82 };
83
84 struct dirs_entry {
85     enum dirsKind kind;
86     char path[256];
87     SYSNO sysno;
88     time_t mtime;
89 };
90
91 void getFnameTmp (Res res, char *fname, int no);
92         
93 struct dirs_info *dirs_open (Dict dict, const char *rep, int rw);
94 struct dirs_info *dirs_fopen (Dict dict, const char *path);
95 struct dirs_entry *dirs_read (struct dirs_info *p);
96 struct dirs_entry *dirs_last (struct dirs_info *p);
97 void dirs_mkdir (struct dirs_info *p, const char *src, time_t mtime);
98 void dirs_rmdir (struct dirs_info *p, const char *src);
99 void dirs_add (struct dirs_info *p, const char *src, SYSNO sysno, time_t mtime);
100 void dirs_del (struct dirs_info *p, const char *src);
101 void dirs_free (struct dirs_info **pp);
102
103 struct dir_entry *dir_open (const char *rep, const char *base,
104                             int follow_links);
105 void dir_sort (struct dir_entry *e);
106 void dir_free (struct dir_entry **e_p);
107
108 void repositoryUpdate (ZebraHandle zh, const char *path);
109 void repositoryAdd (ZebraHandle zh, const char *path);
110 void repositoryDelete (ZebraHandle zh, const char *path);
111 void repositoryShow (ZebraHandle zh, const char *path);
112
113 int key_open (ZebraHandle zh, int mem);
114 int key_close (ZebraHandle zh);
115 int key_compare (const void *p1, const void *p2);
116 void key_init(struct it_key *k);
117 char *key_print_it (const void *p, char *buf);
118 int key_get_seq (const void *p);
119 int key_compare_it (const void *p1, const void *p2);
120 int key_qsort_compare (const void *p1, const void *p2);
121 void key_logdump (int mask, const void *p);
122 void key_logdump_txt (int logmask, const void *p, const char *txt);
123 void inv_prstat (ZebraHandle zh);
124 void inv_compact (BFiles bfs);
125 void key_input (ZebraHandle zh, int nkeys, int cache, Res res);
126 ISAMS_M *key_isams_m (Res res, ISAMS_M *me);
127 ISAMC_M *key_isamc_m (Res res, ISAMC_M *me);
128 int merge_sort (char **buf, int from, int to);
129 int key_SU_code (int ch, char *out);
130
131 #define FNAME_DICT "dict"
132 #define FNAME_ISAM "isam"
133 #define FNAME_ISAMC "isamc"
134 #define FNAME_ISAMS "isams"
135 #define FNAME_ISAMH "isamh"
136 #define FNAME_ISAMD "isamd"
137 #define FNAME_CONFIG "zebra.cfg"
138
139 #define GMATCH_DICT "gmatch"
140 #define FMATCH_DICT "fmatch"
141
142 struct strtab *strtab_mk (void);
143 int strtab_src (struct strtab *t, const char *name, void ***infop);
144 void strtab_del (struct strtab *t,
145                  void (*func)(const char *name, void *info, void *data),
146                  void *data);
147 int index_char_cvt (int c);
148 int index_word_prefix (char *string, int attset_ordinal,
149                        int local_attribute, const char *databaseName);
150
151
152 void zebraIndexLockMsg (ZebraHandle zh, const char *str);
153 void zebraIndexUnlock (ZebraHandle zh);
154 int zebraIndexLock (BFiles bfs, ZebraHandle zh, int commitNow, const char *rval);
155 int zebraIndexWait (ZebraHandle zh, int commitPhase);
156
157 #define FNAME_MAIN_LOCK   "zebraidx.LCK"
158 #define FNAME_COMMIT_LOCK "zebracmt.LCK"
159 #define FNAME_ORG_LOCK    "zebraorg.LCK"
160 #define FNAME_TOUCH_TIME  "zebraidx.time"
161
162 typedef struct zebra_lock_info *ZebraLockHandle;
163 ZebraLockHandle zebra_lock_create(const char *dir,
164                                   const char *file, int excl_flag);
165 void zebra_lock_destroy (ZebraLockHandle h);
166 int zebra_lock (ZebraLockHandle h);
167 int zebra_lock_nb (ZebraLockHandle h);
168 int zebra_unlock (ZebraLockHandle h);
169 int zebra_lock_fd (ZebraLockHandle h);
170 void zebra_lock_prefix (Res res, char *dst);
171 char *zebra_mk_fname (const char *dir, const char *name);
172
173 int zebra_lock_w (ZebraLockHandle h);
174 int zebra_lock_r (ZebraLockHandle h);
175
176 void zebra_load_atts (data1_handle dh, Res res);
177
178 int key_SU_decode (int *ch, const unsigned char *out);
179 int key_SU_encode (int ch, char *out);
180
181 #define ENCODE_BUFLEN 768
182 struct encode_info {
183     int  sysno;  /* previously written values for delta-compress */
184     int  seqno;
185     int  cmd;
186     int prevsys; /* buffer for skipping insert/delete pairs */
187     int prevseq;
188     int prevcmd;
189     int keylen; /* tells if we have an unwritten key in buf, and how long*/
190 #if IT_KEY_NEW
191     void *encode_handle;
192 #endif
193     char buf[ENCODE_BUFLEN];
194 };
195
196 void encode_key_init (struct encode_info *i);
197 char *encode_key_int (int d, char *bp);
198 void encode_key_write (char *k, struct encode_info *i, FILE *outf);
199 void encode_key_flush (struct encode_info *i, FILE *outf);
200
201 typedef struct {
202     char *term;
203     char *db;
204     int sysno;
205     int score;
206 } *ZebraPosSet;
207
208 typedef struct zebra_set *ZebraSet;
209
210 typedef struct zebra_rank_class {
211     struct rank_control *control;
212     int init_flag;
213     void *class_handle;
214     struct zebra_rank_class *next;
215 } *ZebraRankClass;
216
217 struct recKeys {
218     int buf_used;
219     int buf_max;
220     char *buf;
221 #if IT_KEY_NEW
222     void *codec_handle;
223 #else
224     int prevSeqNo;
225     char prevAttrSet;
226     short prevAttrUse;
227 #endif
228 };
229
230 struct sortKeys {
231     int buf_used;
232     int buf_max;
233     char *buf;
234 };
235
236 struct zebra_register {
237     char *name;
238     
239     ISAMS isams;
240     ISAM isam;
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     int 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     int records_inserted;
319     int records_updated;
320     int records_deleted;
321     int 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,
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);
377
378 void resultSetAddTerm (ZebraHandle zh, ZebraSet s, int reg_type,
379                        const char *db, int set,
380                        int use, const char *term);
381 ZebraSet resultSetAdd (ZebraHandle zh, const char *name, int ov);
382 ZebraSet resultSetGet (ZebraHandle zh, const char *name);
383 ZebraSet resultSetAddRPN (ZebraHandle zh, NMEM m, Z_RPNQuery *rpn,
384                           int num_bases, char **basenames,
385                           const char *setname);
386 RSET resultSetRef (ZebraHandle zh, const char *resultSetId);
387 void resultSetDestroy (ZebraHandle zh, int num_names, char **names,
388                        int *statuses);
389
390
391 ZebraPosSet zebraPosSetCreate (ZebraHandle zh, const char *name,
392                                int num, int *positions);
393 void zebraPosSetDestroy (ZebraHandle zh, ZebraPosSet records, int num);
394
395 void resultSetSort (ZebraHandle zh, NMEM nmem,
396                     int num_input_setnames, const char **input_setnames,
397                     const char *output_setname,
398                     Z_SortKeySpecList *sort_sequence, int *sort_status);
399 void resultSetSortSingle (ZebraHandle zh, NMEM nmem,
400                           ZebraSet sset, RSET rset,
401                           Z_SortKeySpecList *sort_sequence, int *sort_status);
402 void resultSetRank (ZebraHandle zh, ZebraSet zebraSet, RSET rset);
403 void resultSetInvalidate (ZebraHandle zh);
404
405 int zebra_server_lock_init (ZebraService zh);
406 int zebra_server_lock_destroy (ZebraService zh);
407 int zebra_server_lock (ZebraService zh, int lockCommit);
408 void zebra_server_unlock (ZebraService zh, int commitPhase);
409 int zebra_server_lock_get_state (ZebraService zh, time_t *timep);
410
411 typedef struct attent
412 {
413     int attset_ordinal;
414     data1_local_attribute *local_attributes;
415 } attent;
416
417 void zebraRankInstall (struct zebra_register *reg, struct rank_control *ctrl);
418 ZebraRankClass zebraRankLookup (ZebraHandle zh, const char *name);
419 void zebraRankDestroy (struct zebra_register *reg);
420
421 int att_getentbyatt(ZebraHandle zh, attent *res, oid_value set, int att,
422                 const char *sattr);
423
424 extern struct rank_control *rank1_class;
425 extern struct rank_control *rankzv_class;
426 extern struct rank_control *rankliv_class;
427
428 int zebra_record_fetch (ZebraHandle zh, SYSNO sysno, int score, ODR stream,
429                         oid_value input_format, Z_RecordComposition *comp,
430                         oid_value *output_format, char **rec_bufp,
431                         int *rec_lenp, char **basenamep);
432
433 void extract_get_fname_tmp (ZebraHandle zh, char *fname, int no);
434
435 void zebra_index_merge (ZebraHandle zh);
436
437 int buffer_extract_record (ZebraHandle zh, 
438                            const char *buf, size_t buf_size,
439                            int delete_flag,
440                            int test_mode, 
441                            const char *recordType,
442                            SYSNO *sysno,
443                            const char *match_criteria,
444                            const char *fname,
445                            int force_update,
446                            int allow_update);
447
448 #if 0
449 int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
450                         const char *buf, size_t buf_size,
451                         const char *databaseName, int delete_flag,
452                         int test_mode, int *sysno,
453                         int store_keys, int store_data,
454                         const char *match_criteria);
455 #endif
456 void extract_flushWriteKeys (ZebraHandle zh, int final);
457
458 struct zebra_fetch_control {
459     off_t offset_end;
460     off_t record_offset;
461     off_t record_int_pos;
462     const char *record_int_buf;
463     int record_int_len;
464     int fd;
465 };
466
467 int zebra_record_ext_read (void *fh, char *buf, size_t count);
468 off_t zebra_record_ext_seek (void *fh, off_t offset);
469 off_t zebra_record_ext_tell (void *fh);
470 off_t zebra_record_int_seek (void *fh, off_t offset);
471 off_t zebra_record_int_tell (void *fh);
472 int zebra_record_int_read (void *fh, char *buf, size_t count);
473 void zebra_record_int_end (void *fh, off_t offset);
474
475 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
476                               int cmd, struct recKeys *reckeys);
477 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
478                             int cmd, struct sortKeys *skp);
479 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid);
480 void extract_token_add (RecWord *p);
481 int explain_extract (void *handle, Record rec, data1_node *n);
482
483 int fileExtract (ZebraHandle zh, SYSNO *sysno, const char *fname,
484                  int deleteFlag);
485
486 int zebra_begin_read (ZebraHandle zh);
487 int zebra_end_read (ZebraHandle zh);
488
489 int zebra_file_stat (const char *file_name, struct stat *buf,
490                      int follow_links);
491
492 void zebra_livcode_transform(ZebraHandle zh, Z_RPNQuery *query);
493
494 void *iscz1_start ();
495 void iscz1_reset (void *vp);
496 void iscz1_stop (void *p);
497 void iscz1_decode (void *vp, char **dst, const char **src);
498 void iscz1_encode (void *vp, char **dst, const char **src);
499
500 YAZ_END_CDECL
501
502 #endif