5f15228fe9ccdbb2161f68f8463c5eadeae9e452
[idzebra-moved-to-github.git] / index / index.h
1 /* $Id: index.h,v 1.153 2005-10-28 07:25:30 adam Exp $
2    Copyright (C) 1995-2005
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 <stdlib.h>
28 #include <idzebra/version.h>
29 #include <idzebra/util.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 <idzebra/dict.h>
38 #include <idzebra/isams.h>
39 #include <idzebra/isamc.h>
40 #include <idzebra/isamb.h>
41 #include <d1_absyn.h>
42 #include <idzebra/recgrs.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_LEVEL_MAX 5
58 struct it_key {
59     int  len;
60     zint mem[IT_KEY_LEVEL_MAX];
61 };
62
63 enum dirsKind { dirs_dir, dirs_file };
64
65 struct dir_entry {
66     enum dirsKind kind;
67     char *name;
68     time_t mtime;
69 };
70
71 struct dirs_entry {
72     enum dirsKind kind;
73     char path[256];
74     SYSNO sysno;
75     time_t mtime;
76 };
77
78 void getFnameTmp (Res res, char *fname, int no);
79         
80 struct dirs_info *dirs_open (Dict dict, const char *rep, int rw);
81 struct dirs_info *dirs_fopen (Dict dict, const char *path, int rw);
82 struct dirs_entry *dirs_read (struct dirs_info *p);
83 struct dirs_entry *dirs_last (struct dirs_info *p);
84 void dirs_mkdir (struct dirs_info *p, const char *src, time_t mtime);
85 void dirs_rmdir (struct dirs_info *p, const char *src);
86 void dirs_add (struct dirs_info *p, const char *src, SYSNO sysno, time_t mtime);
87 void dirs_del (struct dirs_info *p, const char *src);
88 void dirs_free (struct dirs_info **pp);
89
90 struct dir_entry *dir_open (const char *rep, const char *base,
91                             int follow_links);
92 void dir_sort (struct dir_entry *e);
93 void dir_free (struct dir_entry **e_p);
94
95 void repositoryUpdate (ZebraHandle zh, const char *path);
96 void repositoryAdd (ZebraHandle zh, const char *path);
97 void repositoryDelete (ZebraHandle zh, const char *path);
98 void repositoryShow (ZebraHandle zh, const char *path);
99
100 int key_open (ZebraHandle zh, int mem);
101 int key_close (ZebraHandle zh);
102 int key_compare (const void *p1, const void *p2);
103 void key_init(struct it_key *k);
104 char *key_print_it (const void *p, char *buf);
105 zint key_get_seq (const void *p);
106 int key_compare_it (const void *p1, const void *p2);
107 int key_qsort_compare (const void *p1, const void *p2);
108 void key_logdump (int mask, const void *p);
109 void key_logdump_txt (int logmask, const void *p, const char *txt);
110 void inv_prstat (ZebraHandle zh);
111 void inv_compact (BFiles bfs);
112 void key_input (ZebraHandle zh, int nkeys, int cache, Res res);
113 ISAMS_M *key_isams_m (Res res, ISAMS_M *me);
114 ISAMC_M *key_isamc_m (Res res, ISAMC_M *me);
115 int merge_sort (char **buf, int from, int to);
116 int key_SU_code (int ch, char *out);
117
118 #define FNAME_DICT "dict"
119 #define FNAME_ISAM "isam"
120 #define FNAME_ISAMC "isamc"
121 #define FNAME_ISAMS "isams"
122 #define FNAME_ISAMH "isamh"
123 #define FNAME_ISAMD "isamd"
124 #define FNAME_CONFIG "zebra.cfg"
125
126 #define GMATCH_DICT "gmatch"
127 #define FMATCH_DICT "fmatch"
128
129 struct strtab *strtab_mk (void);
130 int strtab_src (struct strtab *t, const char *name, void ***infop);
131 void strtab_del (struct strtab *t,
132                  void (*func)(const char *name, void *info, void *data),
133                  void *data);
134 int index_char_cvt (int c);
135 int index_word_prefix (char *string, int attset_ordinal,
136                        int local_attribute, const char *databaseName);
137
138
139 void zebraIndexLockMsg (ZebraHandle zh, const char *str);
140 void zebraIndexUnlock (ZebraHandle zh);
141 int zebraIndexLock (BFiles bfs, ZebraHandle zh, int commitNow, const char *rval);
142 int zebraIndexWait (ZebraHandle zh, int commitPhase);
143
144 #define FNAME_MAIN_LOCK   "zebraidx.LCK"
145 #define FNAME_COMMIT_LOCK "zebracmt.LCK"
146 #define FNAME_ORG_LOCK    "zebraorg.LCK"
147 #define FNAME_TOUCH_TIME  "zebraidx.time"
148
149 typedef struct zebra_lock_info *ZebraLockHandle;
150 ZebraLockHandle zebra_lock_create(const char *dir,
151                                   const char *file, int excl_flag);
152 void zebra_lock_destroy (ZebraLockHandle h);
153 int zebra_lock (ZebraLockHandle h);
154 int zebra_lock_nb (ZebraLockHandle h);
155 int zebra_unlock (ZebraLockHandle h);
156 int zebra_lock_fd (ZebraLockHandle h);
157 void zebra_lock_prefix (Res res, char *dst);
158 char *zebra_mk_fname (const char *dir, const char *name);
159
160 int zebra_lock_w (ZebraLockHandle h);
161 int zebra_lock_r (ZebraLockHandle h);
162
163 void zebra_load_atts (data1_handle dh, Res res);
164
165 int key_SU_decode (int *ch, const unsigned char *out);
166 int key_SU_encode (int ch, char *out);
167
168 #define ENCODE_BUFLEN 768
169 struct encode_info {
170     int  sysno;  /* previously written values for delta-compress */
171     int  seqno;
172     int  cmd;
173     int prevsys; /* buffer for skipping insert/delete pairs */
174     int prevseq;
175     int prevcmd;
176     int keylen; /* tells if we have an unwritten key in buf, and how long*/
177     void *encode_handle;
178     char buf[ENCODE_BUFLEN];
179 };
180
181 void encode_key_init (struct encode_info *i);
182 char *encode_key_int (int d, char *bp);
183 void encode_key_write (char *k, struct encode_info *i, FILE *outf);
184 void encode_key_flush (struct encode_info *i, FILE *outf);
185
186 typedef struct zebra_set *ZebraSet;
187
188 typedef struct zebra_rank_class {
189     struct rank_control *control;
190     int init_flag;
191     void *class_handle;
192     struct zebra_rank_class *next;
193 } *ZebraRankClass;
194
195 #define NEW_REC_KEYS 1
196
197 #if NEW_REC_KEYS
198 #include "reckeys.h"
199 #else
200 struct recKeys {
201     int buf_used;
202     int buf_max;
203     char *buf;
204     void *codec_handle;
205 };
206 #endif
207
208 #if NATTR
209
210 #else
211 struct sortKeys {
212     int buf_used;
213     int buf_max;
214     char *buf;
215 };
216 #endif
217
218 struct zebra_register {
219     char *name;
220     
221     ISAMS isams;
222     ISAMC isamc;
223     ISAMB isamb;
224     Dict dict;
225     Dict matchDict;
226     SortIdx sortIdx;
227     int registerState; /* 0 (no commit pages), 1 (use commit pages) */
228     time_t registerChange;
229     BFiles bfs;
230     Records records;
231     ZebraExplainInfo zei;
232
233     char *server_path_prefix;
234     data1_handle dh;
235     ZebraMaps zebra_maps;
236     ZebraRankClass rank_classes;
237     RecTypes recTypes;
238     int seqno;
239     int last_val;
240     int stop_flag;
241     int active; /* 0=shutdown, 1=enabled and inactive, 2=activated */
242
243 #if NEW_REC_KEYS
244     zebra_rec_keys_t keys;
245 #else
246     struct recKeys keys;
247 #endif
248
249 #if NATTR
250     struct recKeys sortKeys;
251 #else
252     struct sortKeys sortKeys;
253 #endif
254     char **key_buf;
255     size_t ptr_top;
256     size_t ptr_i;
257     size_t key_buf_used;
258     int key_file_no;
259 };
260
261 struct zebra_service {
262     int stop_flag;
263     Res global_res;
264     struct zebra_session *sessions;
265     struct zebra_register *regs;
266     Zebra_mutex_cond session_lock;
267     Passwd_db passwd_db;
268     Res dbaccess;
269     const char *path_root;
270     RecTypeClass record_classes;
271     NMEM nmem;
272 };
273
274
275 struct zebra_session {
276     struct zebra_session *next;
277     struct zebra_service *service;
278     struct zebra_register *reg;
279
280     char *xadmin_databaseName;
281
282     char **basenames;
283     int num_basenames;
284     zint approx_limit;
285     char *reg_name;
286     char *path_reg;
287
288     ZebraLockHandle lock_normal;
289     ZebraLockHandle lock_shadow;
290
291     int trans_no;
292     int trans_w_no;
293
294     int destroyed;
295     ZebraSet sets;
296     Res res;
297     Res session_res;
298     char *user_perm;
299     char *dbaccesslist;
300     int errCode;
301     zint hits;
302     char *errString;
303 #if HAVE_SYS_TIMES_H
304     struct tms tms1;
305     struct tms tms2;    
306 #endif
307     int  shadow_enable;
308
309     int m_staticrank;
310
311     zint records_inserted;
312     zint records_updated;
313     zint records_deleted;
314     zint records_processed;
315     char *record_encoding;
316
317     yaz_iconv_t iconv_to_utf8;
318     yaz_iconv_t iconv_from_utf8;
319
320     int m_follow_links;
321     const char *m_group;
322     const char *m_record_id;
323     const char *m_record_type;
324     int m_store_data;
325     int m_store_keys;
326     int m_explain_database;
327     int m_flag_rw;
328     int m_file_verbose_limit;
329
330     void *store_data_buf;
331     size_t store_data_size;
332     NMEM nmem_error;
333
334     struct zebra_limit *m_limit;
335 };
336
337 struct rank_control {
338     char *name;
339     void *(*create)(ZebraHandle zh);
340     void (*destroy)(struct zebra_register *reg, void *class_handle);
341     void *(*begin)(struct zebra_register *reg, 
342                    void *class_handle, RSET rset, NMEM nmem,
343                    TERMID *terms, int numterms);
344     /* ### Could add parameters to begin:
345      *  char *index;    // author, title, etc.
346      *  int dbsize;     // number of records in database
347      *  int rssize;     // number of records in result set (estimate?)
348      */
349     void (*end)(struct zebra_register *reg, void *set_handle);
350     int (*calc)(void *set_handle, zint sysno, zint staticrank,
351                 int *stop_flag);
352     void (*add)(void *set_handle, int seqno, TERMID term);
353 };
354
355 struct term_set_entry {
356     char *term;
357     struct term_set_entry *next;
358 };
359
360 struct term_set_list {
361     struct term_set_entry *first;
362     struct term_set_entry *last;
363 };
364
365
366 void zebra_limit_destroy(struct zebra_limit *zl);
367 struct zebra_limit *zebra_limit_create(int exclude_flag, zint *ids);
368 void zebra_limit_for_rset(struct zebra_limit *zl,
369                           int (**filter_func)(const void *buf, void *data),
370                           void (**filter_destroy)(void *data),
371                           void **filter_data);
372
373 struct rset_key_control *zebra_key_control_create(ZebraHandle zh);
374
375 ZEBRA_RES rpn_search_top(ZebraHandle zh, Z_RPNStructure *zs,
376                          oid_value attributeSet, 
377                          NMEM stream, NMEM rset_nmem,
378                          Z_SortKeySpecList *sort_sequence,
379                          int num_bases, char **basenames,
380                          RSET *result_set);
381
382 ZEBRA_RES rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
383                     oid_value attributeset,
384                     int num_bases, char **basenames,
385                     int *position, int *num_entries, ZebraScanEntry **list,
386                     int *is_partial, RSET limit_set, int return_zero);
387
388 RSET rset_trunc(ZebraHandle zh, ISAM_P *isam_p, int no,
389                 const char *term, int length_term, const char *flags,
390                 int preserve_position, int term_type, NMEM rset_nmem,
391                 struct rset_key_control *kctrl, int scope,
392                 struct ord_list *ol, int reg_type,
393                 zint hits_limit, const char *term_ref_id);
394
395 void resultSetAddTerm (ZebraHandle zh, ZebraSet s, int reg_type,
396                        const char *db, int set,
397                        int use, const char *term);
398 ZebraSet resultSetAdd (ZebraHandle zh, const char *name, int ov);
399 ZebraSet resultSetGet (ZebraHandle zh, const char *name);
400 ZEBRA_RES resultSetAddRPN (ZebraHandle zh, NMEM m, Z_RPNQuery *rpn,
401                      int num_bases, char **basenames,
402                      const char *setname);
403 RSET resultSetRef (ZebraHandle zh, const char *resultSetId);
404 void resultSetDestroy (ZebraHandle zh, int num_names, char **names,
405                        int *statuses);
406 ZEBRA_RES resultSetSort (ZebraHandle zh, NMEM nmem,
407                          int num_input_setnames, const char **input_setnames,
408                          const char *output_setname,
409                          Z_SortKeySpecList *sort_sequence, int *sort_status);
410 ZEBRA_RES resultSetSortSingle (ZebraHandle zh, NMEM nmem,
411                                ZebraSet sset, RSET rset,
412                                Z_SortKeySpecList *sort_sequence,
413                                int *sort_status);
414 ZEBRA_RES resultSetRank (ZebraHandle zh, ZebraSet zebraSet, RSET rset,
415                          NMEM nmem);
416 void resultSetInvalidate (ZebraHandle zh);
417
418 int zebra_server_lock_init (ZebraService zh);
419 int zebra_server_lock_destroy (ZebraService zh);
420 int zebra_server_lock (ZebraService zh, int lockCommit);
421 void zebra_server_unlock (ZebraService zh, int commitPhase);
422 int zebra_server_lock_get_state (ZebraService zh, time_t *timep);
423
424 typedef struct attent
425 {
426     int attset_ordinal;
427     data1_local_attribute *local_attributes;
428 } attent;
429
430 void zebraRankInstall (struct zebra_register *reg, struct rank_control *ctrl);
431 ZebraRankClass zebraRankLookup (ZebraHandle zh, const char *name);
432 void zebraRankDestroy (struct zebra_register *reg);
433
434 int att_getentbyatt(ZebraHandle zh, attent *res, oid_value set, int att,
435                 const char *sattr);
436
437 extern struct rank_control *rank_1_class;
438 extern struct rank_control *rank_zv_class;
439 extern struct rank_control *rank_static_class;
440
441 int zebra_record_fetch (ZebraHandle zh, SYSNO sysno, int score, 
442                         zebra_snippets *hit_snippet, ODR stream,
443                         oid_value input_format, Z_RecordComposition *comp,
444                         oid_value *output_format, char **rec_bufp,
445                         int *rec_lenp, char **basenamep,
446                         char **addinfo);
447
448 void extract_get_fname_tmp (ZebraHandle zh, char *fname, int no);
449
450 void zebra_index_merge (ZebraHandle zh);
451
452 ZEBRA_RES buffer_extract_record (ZebraHandle zh, 
453                                  const char *buf, size_t buf_size,
454                                  int delete_flag,
455                                  int test_mode, 
456                                  const char *recordType,
457                                  SYSNO *sysno,
458                                  const char *match_criteria,
459                                  const char *fname,
460                                  int force_update,
461                                  int allow_update);
462
463 #if 0
464 int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
465                         const char *buf, size_t buf_size,
466                         const char *databaseName, int delete_flag,
467                         int test_mode, int *sysno,
468                         int store_keys, int store_data,
469                         const char *match_criteria);
470 #endif
471 void extract_flushWriteKeys (ZebraHandle zh, int final);
472
473 struct zebra_fetch_control {
474     off_t offset_end;
475     off_t record_offset;
476     off_t record_int_pos;
477     const char *record_int_buf;
478     int record_int_len;
479     int fd;
480 };
481
482 int zebra_record_ext_read (void *fh, char *buf, size_t count);
483 off_t zebra_record_ext_seek (void *fh, off_t offset);
484 off_t zebra_record_ext_tell (void *fh);
485 off_t zebra_record_int_seek (void *fh, off_t offset);
486 off_t zebra_record_int_tell (void *fh);
487 int zebra_record_int_read (void *fh, char *buf, size_t count);
488 void zebra_record_int_end (void *fh, off_t offset);
489
490 #if NEW_REC_KEYS
491 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys);
492 #else
493 void print_rec_keys(ZebraHandle zh, struct recKeys *reckeys);
494 #endif
495
496 #if NEW_REC_KEYS
497 ZEBRA_RES zebra_snippets_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys,
498                                   zebra_snippets *snippets);
499 #else
500 ZEBRA_RES zebra_snippets_rec_keys(ZebraHandle zh, struct recKeys *reckeys,
501                                   zebra_snippets *snippets);
502 #endif
503 ZEBRA_RES zebra_snippets_hit_vector(ZebraHandle zh, const char *setname,
504                                     zint sysno, zebra_snippets *snippets);
505
506 #if NEW_REC_KEYS
507 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
508                               int cmd, zebra_rec_keys_t reckeys,
509                               zint staticrank);
510 #else
511 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
512                               int cmd, struct recKeys *reckeys,
513                               zint staticrank);
514 #endif
515 #if NATTR
516 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
517                             int cmd, struct recKeys *skp);
518 #else
519 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
520                             int cmd, struct sortKeys *skp);
521 #endif
522 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid);
523 void extract_token_add (RecWord *p);
524 int explain_extract (void *handle, Record rec, data1_node *n);
525
526 int fileExtract (ZebraHandle zh, SYSNO *sysno, const char *fname,
527                  int deleteFlag);
528
529 ZEBRA_RES zebra_begin_read (ZebraHandle zh);
530 ZEBRA_RES zebra_end_read (ZebraHandle zh);
531
532 int zebra_file_stat (const char *file_name, struct stat *buf,
533                      int follow_links);
534
535 void zebra_livcode_transform(ZebraHandle zh, Z_RPNQuery *query);
536
537 void *iscz1_start ();
538 void iscz1_reset (void *vp);
539 void iscz1_stop (void *p);
540 void iscz1_decode (void *vp, char **dst, const char **src);
541 void iscz1_encode (void *vp, char **dst, const char **src);
542
543 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
544                     int compact_flag, Res res);
545
546 void zebra_setError(ZebraHandle zh, int code, const char *addinfo);
547 void zebra_setError_zint(ZebraHandle zh, int code, zint i);
548
549 void zebra_term_untrans_iconv(ZebraHandle zh, NMEM stream, int reg_type,
550                               char **dst, const char *src);
551
552 ZEBRA_RES zebra_get_hit_vector(ZebraHandle zh, const char *setname, zint sysno);
553
554 void zebra_term_untrans(ZebraHandle zh, int reg_type,
555                         char *dst, const char *src);
556
557 YAZ_END_CDECL
558
559 #endif