Implemented loadable filters.
[idzebra-moved-to-github.git] / index / index.h
1 /* $Id: index.h,v 1.121 2004-09-27 10:44:49 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 <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_LEVEL_MAX 4
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);
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 {
187     char *term;
188     char *db;
189     zint sysno;
190     int score;
191 } *ZebraPosSet;
192
193 typedef struct zebra_set *ZebraSet;
194
195 typedef struct zebra_rank_class {
196     struct rank_control *control;
197     int init_flag;
198     void *class_handle;
199     struct zebra_rank_class *next;
200 } *ZebraRankClass;
201
202 struct recKeys {
203     int buf_used;
204     int buf_max;
205     char *buf;
206     void *codec_handle;
207 };
208
209 struct sortKeys {
210     int buf_used;
211     int buf_max;
212     char *buf;
213 };
214
215 struct zebra_register {
216     char *name;
217     
218     ISAMS isams;
219     ISAMC isamc;
220     ISAMB isamb;
221     Dict dict;
222     Dict matchDict;
223     SortIdx sortIdx;
224     int registerState; /* 0 (no commit pages), 1 (use commit pages) */
225     time_t registerChange;
226     BFiles bfs;
227     Records records;
228     ZebraExplainInfo zei;
229
230     char *server_path_prefix;
231     data1_handle dh;
232     ZebraMaps zebra_maps;
233     ZebraRankClass rank_classes;
234     RecTypes recTypes;
235     int seqno;
236     int last_val;
237     int stop_flag;
238     int active; /* 0=shutdown, 1=enabled and inactive, 2=activated */
239
240     struct recKeys keys;
241 #if 1
242     struct sortKeys sortKeys;
243 #else
244     struct sortKey *sortKeys;
245 #endif
246     char **key_buf;
247     size_t ptr_top;
248     size_t ptr_i;
249     size_t key_buf_used;
250     int key_file_no;
251 };
252
253 struct zebra_service {
254     int stop_flag;
255     Res global_res;
256     char *configName;
257     struct zebra_session *sessions;
258     struct zebra_register *regs;
259     Zebra_mutex_cond session_lock;
260     Passwd_db passwd_db;
261     const char *path_root;
262     RecTypeClass record_classes;
263     NMEM nmem;
264 };
265
266
267 struct zebra_session {
268     struct zebra_session *next;
269     struct zebra_service *service;
270     struct zebra_register *reg;
271
272     char *xadmin_databaseName;
273
274     char **basenames;
275     int num_basenames;
276     char *reg_name;
277     char *path_reg;
278
279     ZebraLockHandle lock_normal;
280     ZebraLockHandle lock_shadow;
281
282     int trans_no;
283     int trans_w_no;
284
285     int destroyed;
286     ZebraSet sets;
287     Res res;
288     char *user_perm;
289     int errCode;
290     zint hits;
291     char *errString;
292 #if HAVE_SYS_TIMES_H
293     struct tms tms1;
294     struct tms tms2;    
295 #endif
296     int  shadow_enable;
297
298     zint records_inserted;
299     zint records_updated;
300     zint records_deleted;
301     zint records_processed;
302     char *record_encoding;
303
304     yaz_iconv_t iconv_to_utf8;
305     yaz_iconv_t iconv_from_utf8;
306
307     int m_follow_links;
308     const char *m_group;
309     const char *m_record_id;
310     const char *m_record_type;
311     int m_store_data;
312     int m_store_keys;
313     int m_explain_database;
314     int m_flag_rw;
315     int m_file_verbose_limit;
316 };
317
318 struct rank_control {
319     char *name;
320     void *(*create)(ZebraHandle zh);
321     void (*destroy)(struct zebra_register *reg, void *class_handle);
322     void *(*begin)(struct zebra_register *reg, void *class_handle, RSET rset);
323     /* ### Could add parameters to begin:
324      *  char *index;    // author, title, etc.
325      *  int dbsize;     // number of records in database
326      *  int rssize;     // number of records in result set (estimate?)
327      */
328     void (*end)(struct zebra_register *reg, void *set_handle);
329     int (*calc)(void *set_handle, zint sysno);
330     void (*add)(void *set_handle, int seqno, int term_index);
331 };
332
333 struct term_set_entry {
334     char *term;
335     struct term_set_entry *next;
336 };
337
338 struct term_set_list {
339     struct term_set_entry *first;
340     struct term_set_entry *last;
341 };
342
343 RSET rpn_search (ZebraHandle zh, NMEM mem, NMEM rset_nmem,
344                  Z_RPNQuery *rpn, int num_bases, char **basenames, 
345                  const char *setname, ZebraSet sset);
346
347
348 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
349                oid_value attributeset,
350                int num_bases, char **basenames,
351                int *position, int *num_entries, ZebraScanEntry **list,
352                int *is_partial, RSET limit_set, int return_zero);
353
354 RSET rset_trunc (ZebraHandle zh, ISAMS_P *isam_p, int no,
355                  const char *term, int length_term, const char *flags,
356                  int preserve_position, int term_type, NMEM rset_nmem,
357                  const struct key_control *kctrl, int scope);
358
359 void resultSetAddTerm (ZebraHandle zh, ZebraSet s, int reg_type,
360                        const char *db, int set,
361                        int use, const char *term);
362 ZebraSet resultSetAdd (ZebraHandle zh, const char *name, int ov);
363 ZebraSet resultSetGet (ZebraHandle zh, const char *name);
364 ZebraSet resultSetAddRPN (ZebraHandle zh, NMEM m, Z_RPNQuery *rpn,
365                           int num_bases, char **basenames,
366                           const char *setname);
367 RSET resultSetRef (ZebraHandle zh, const char *resultSetId);
368 void resultSetDestroy (ZebraHandle zh, int num_names, char **names,
369                        int *statuses);
370
371
372 ZebraPosSet zebraPosSetCreate (ZebraHandle zh, const char *name,
373                                int num, int *positions);
374 void zebraPosSetDestroy (ZebraHandle zh, ZebraPosSet records, int num);
375
376 void resultSetSort (ZebraHandle zh, NMEM nmem,
377                     int num_input_setnames, const char **input_setnames,
378                     const char *output_setname,
379                     Z_SortKeySpecList *sort_sequence, int *sort_status);
380 void resultSetSortSingle (ZebraHandle zh, NMEM nmem,
381                           ZebraSet sset, RSET rset,
382                           Z_SortKeySpecList *sort_sequence, int *sort_status);
383 void resultSetRank (ZebraHandle zh, ZebraSet zebraSet, RSET rset);
384 void resultSetInvalidate (ZebraHandle zh);
385
386 int zebra_server_lock_init (ZebraService zh);
387 int zebra_server_lock_destroy (ZebraService zh);
388 int zebra_server_lock (ZebraService zh, int lockCommit);
389 void zebra_server_unlock (ZebraService zh, int commitPhase);
390 int zebra_server_lock_get_state (ZebraService zh, time_t *timep);
391
392 typedef struct attent
393 {
394     int attset_ordinal;
395     data1_local_attribute *local_attributes;
396 } attent;
397
398 void zebraRankInstall (struct zebra_register *reg, struct rank_control *ctrl);
399 ZebraRankClass zebraRankLookup (ZebraHandle zh, const char *name);
400 void zebraRankDestroy (struct zebra_register *reg);
401
402 int att_getentbyatt(ZebraHandle zh, attent *res, oid_value set, int att,
403                 const char *sattr);
404
405 extern struct rank_control *rank1_class;
406 extern struct rank_control *rankzv_class;
407 extern struct rank_control *rankliv_class;
408
409 int zebra_record_fetch (ZebraHandle zh, SYSNO sysno, int score, ODR stream,
410                         oid_value input_format, Z_RecordComposition *comp,
411                         oid_value *output_format, char **rec_bufp,
412                         int *rec_lenp, char **basenamep);
413
414 void extract_get_fname_tmp (ZebraHandle zh, char *fname, int no);
415
416 void zebra_index_merge (ZebraHandle zh);
417
418 int buffer_extract_record (ZebraHandle zh, 
419                            const char *buf, size_t buf_size,
420                            int delete_flag,
421                            int test_mode, 
422                            const char *recordType,
423                            SYSNO *sysno,
424                            const char *match_criteria,
425                            const char *fname,
426                            int force_update,
427                            int allow_update);
428
429 #if 0
430 int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
431                         const char *buf, size_t buf_size,
432                         const char *databaseName, int delete_flag,
433                         int test_mode, int *sysno,
434                         int store_keys, int store_data,
435                         const char *match_criteria);
436 #endif
437 void extract_flushWriteKeys (ZebraHandle zh, int final);
438
439 struct zebra_fetch_control {
440     off_t offset_end;
441     off_t record_offset;
442     off_t record_int_pos;
443     const char *record_int_buf;
444     int record_int_len;
445     int fd;
446 };
447
448 int zebra_record_ext_read (void *fh, char *buf, size_t count);
449 off_t zebra_record_ext_seek (void *fh, off_t offset);
450 off_t zebra_record_ext_tell (void *fh);
451 off_t zebra_record_int_seek (void *fh, off_t offset);
452 off_t zebra_record_int_tell (void *fh);
453 int zebra_record_int_read (void *fh, char *buf, size_t count);
454 void zebra_record_int_end (void *fh, off_t offset);
455
456 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
457                               int cmd, struct recKeys *reckeys);
458 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
459                             int cmd, struct sortKeys *skp);
460 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid);
461 void extract_token_add (RecWord *p);
462 int explain_extract (void *handle, Record rec, data1_node *n);
463
464 int fileExtract (ZebraHandle zh, SYSNO *sysno, const char *fname,
465                  int deleteFlag);
466
467 int zebra_begin_read (ZebraHandle zh);
468 int zebra_end_read (ZebraHandle zh);
469
470 int zebra_file_stat (const char *file_name, struct stat *buf,
471                      int follow_links);
472
473 void zebra_livcode_transform(ZebraHandle zh, Z_RPNQuery *query);
474
475 void *iscz1_start ();
476 void iscz1_reset (void *vp);
477 void iscz1_stop (void *p);
478 void iscz1_decode (void *vp, char **dst, const char **src);
479 void iscz1_encode (void *vp, char **dst, const char **src);
480
481 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
482                     int compact_flag, Res res);
483
484 YAZ_END_CDECL
485
486 #endif