3e532ffb80f8b1ac409f9218ac1339dbbc78ad29
[idzebra-moved-to-github.git] / index / index.h
1 /*
2  * Copyright (C) 1995-2002, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss, Heikki Levanto
5  * $Id: index.h,v 1.84 2002-07-25 13:06:43 adam Exp $
6  */
7
8 #ifndef INDEX_H
9 #define INDEX_H
10
11 #include <time.h>
12 #include <zebraver.h>
13 #include <zebrautl.h>
14 #include <zebramap.h>
15 #include <sortidx.h>
16
17 #if HAVE_SYS_TIMES_H
18 #include <sys/times.h>
19 #endif
20
21 #if HAVE_ICONV_H
22 #include <iconv.h>
23 #endif
24
25 #include <dict.h>
26 #include <isams.h>
27 #include <isam.h>
28 #include <isamc.h>
29 #include <isamd.h>
30 #include <isamb.h>
31 #define ISAM_DEFAULT "c"
32 #include <yaz/data1.h>
33 #include <recctrl.h>
34 #include "recindex.h"
35 #include "zebraapi.h"
36 #include "zinfo.h"
37 #include <passwddb.h>
38 #include <rset.h>
39
40 YAZ_BEGIN_CDECL
41
42 #define SU_SCHEME 1
43
44 #define IT_MAX_WORD 256
45 #define IT_KEY_HAVE_SEQNO 1
46 #define IT_KEY_HAVE_FIELD 0
47
48 typedef int SYSNO;
49
50 struct it_key {
51     int  sysno;
52     int  seqno;
53 };
54
55 enum dirsKind { dirs_dir, dirs_file };
56
57 struct dir_entry {
58     enum dirsKind kind;
59     char *name;
60     time_t mtime;
61 };
62
63 struct dirs_entry {
64     enum dirsKind kind;
65     char path[256];
66     SYSNO sysno;
67     time_t mtime;
68 };
69
70 void getFnameTmp (Res res, char *fname, int no);
71         
72 struct dirs_info *dirs_open (Dict dict, const char *rep, int rw);
73 struct dirs_info *dirs_fopen (Dict dict, const char *path);
74 struct dirs_entry *dirs_read (struct dirs_info *p);
75 struct dirs_entry *dirs_last (struct dirs_info *p);
76 void dirs_mkdir (struct dirs_info *p, const char *src, time_t mtime);
77 void dirs_rmdir (struct dirs_info *p, const char *src);
78 void dirs_add (struct dirs_info *p, const char *src, int sysno, time_t mtime);
79 void dirs_del (struct dirs_info *p, const char *src);
80 void dirs_free (struct dirs_info **pp);
81
82 struct dir_entry *dir_open (const char *rep, const char *base);
83 void dir_sort (struct dir_entry *e);
84 void dir_free (struct dir_entry **e_p);
85
86 void repositoryUpdate (ZebraHandle zh);
87 void repositoryAdd (ZebraHandle zh);
88 void repositoryDelete (ZebraHandle zh);
89 void repositoryShow (ZebraHandle zh);
90
91 int key_open (ZebraHandle zh, int mem);
92 int key_close (ZebraHandle zh);
93 int key_compare (const void *p1, const void *p2);
94 char *key_print_it (const void *p, char *buf);
95 int key_get_pos (const void *p);
96 int key_compare_it (const void *p1, const void *p2);
97 int key_qsort_compare (const void *p1, const void *p2);
98 void key_logdump (int mask, const void *p);
99 void inv_prstat (ZebraHandle zh);
100 void inv_compact (BFiles bfs);
101 void key_input (ZebraHandle zh, int nkeys, int cache, Res res);
102 ISAMS_M key_isams_m (Res res, ISAMS_M me);
103 ISAMC_M key_isamc_m (Res res, ISAMC_M me);
104 ISAMD_M key_isamd_m (Res res, ISAMD_M me);
105 int merge_sort (char **buf, int from, int to);
106 int key_SU_code (int ch, char *out);
107
108 #define FNAME_DICT "dict"
109 #define FNAME_ISAM "isam"
110 #define FNAME_ISAMC "isamc"
111 #define FNAME_ISAMS "isams"
112 #define FNAME_ISAMH "isamh"
113 #define FNAME_ISAMD "isamd"
114 #define FNAME_CONFIG "zebra.cfg"
115
116 #define GMATCH_DICT "gmatch"
117 #define FMATCH_DICT "fmatch"
118
119 struct strtab *strtab_mk (void);
120 int strtab_src (struct strtab *t, const char *name, void ***infop);
121 void strtab_del (struct strtab *t,
122                  void (*func)(const char *name, void *info, void *data),
123                  void *data);
124 int index_char_cvt (int c);
125 int index_word_prefix (char *string, int attset_ordinal,
126                        int local_attribute, const char *databaseName);
127
128
129 void zebraIndexLockMsg (ZebraHandle zh, const char *str);
130 void zebraIndexUnlock (ZebraHandle zh);
131 int zebraIndexLock (BFiles bfs, ZebraHandle zh, int commitNow, const char *rval);
132 int zebraIndexWait (ZebraHandle zh, int commitPhase);
133
134 #define FNAME_MAIN_LOCK   "zebraidx.LCK"
135 #define FNAME_COMMIT_LOCK "zebracmt.LCK"
136 #define FNAME_ORG_LOCK    "zebraorg.LCK"
137 #define FNAME_TOUCH_TIME  "zebraidx.time"
138
139 typedef struct zebra_lock_info *ZebraLockHandle;
140 ZebraLockHandle zebra_lock_create(const char *dir,
141                                   const char *file, int excl_flag);
142 void zebra_lock_destroy (ZebraLockHandle h);
143 int zebra_lock (ZebraLockHandle h);
144 int zebra_lock_nb (ZebraLockHandle h);
145 int zebra_unlock (ZebraLockHandle h);
146 int zebra_lock_fd (ZebraLockHandle h);
147 void zebra_lock_prefix (Res res, char *dst);
148 char *zebra_mk_fname (const char *dir, const char *name);
149
150 int zebra_lock_w (ZebraLockHandle h);
151 int zebra_lock_r (ZebraLockHandle h);
152
153 void zebra_load_atts (data1_handle dh, Res res);
154
155 int key_SU_decode (int *ch, const unsigned char *out);
156 int key_SU_encode (int ch, char *out);
157
158 // extern Res common_resource;
159
160 struct encode_info {
161     int  sysno;
162     int  seqno;
163     int  cmd;
164     char buf[768];
165 };
166
167 void encode_key_init (struct encode_info *i);
168 char *encode_key_int (int d, char *bp);
169 void encode_key_write (char *k, struct encode_info *i, FILE *outf);
170
171 typedef struct {
172     char *term;
173     char *db;
174     int sysno;
175     int score;
176 } *ZebraPosSet;
177
178 typedef struct zebra_set *ZebraSet;
179
180 typedef struct zebra_rank_class {
181     struct rank_control *control;
182     int init_flag;
183     void *class_handle;
184     struct zebra_rank_class *next;
185 } *ZebraRankClass;
186
187 struct recKeys {
188     int buf_used;
189     int buf_max;
190     char *buf;
191     char prevAttrSet;
192     short prevAttrUse;
193     int prevSeqNo;
194 };
195
196 struct sortKey {
197     char *string;
198     int length;
199     int attrSet;
200     int attrUse;
201     struct sortKey *next;
202 };
203
204 struct zebra_register {
205     char *name;
206     
207     ISAMS isams;
208     ISAM isam;
209     ISAMC isamc;
210     ISAMD isamd;
211     ISAMB isamb;
212     Dict dict;
213     Dict matchDict;
214     SortIdx sortIdx;
215     int registerState; /* 0 (no commit pages), 1 (use commit pages) */
216     time_t registerChange;
217     BFiles bfs;
218     Records records;
219     ZebraExplainInfo zei;
220
221     char *server_path_prefix;
222     data1_handle dh;
223     ZebraMaps zebra_maps;
224     ZebraRankClass rank_classes;
225     RecTypes recTypes;
226     int seqno;
227     int last_val;
228     int stop_flag;
229     int active; /* 0=shutdown, 1=enabled and inactive, 2=activated */
230
231
232
233     struct recKeys keys;
234     struct sortKey *sortKeys;
235
236     char **key_buf;
237     size_t ptr_top;
238     size_t ptr_i;
239     size_t key_buf_used;
240     int key_file_no;
241 };
242
243 struct zebra_service {
244     int stop_flag;
245     Res global_res;
246     char *configName;
247     struct zebra_session *sessions;
248     struct zebra_register *regs;
249     Zebra_mutex_cond session_lock;
250     Passwd_db passwd_db;
251     char *path_root;
252 };
253
254
255 struct zebra_session {
256     struct zebra_session *next;
257     struct zebra_service *service;
258     struct zebra_register *reg;
259
260     char *admin_databaseName;
261
262     char **basenames;
263     int num_basenames;
264     char *reg_name;
265     char *path_reg;
266
267     ZebraLockHandle lock_normal;
268     ZebraLockHandle lock_shadow;
269
270     int trans_no;
271     int destroyed;
272     ZebraSet sets;
273     Res res;
274     int errCode;
275     int hits;
276     char *errString;
277 #if HAVE_SYS_TIMES_H
278     struct tms tms1;
279     struct tms tms2;    
280 #endif
281     struct recordGroup rGroup;
282     int  shadow_enable;
283
284     int records_inserted;
285     int records_updated;
286     int records_deleted;
287     int records_processed;
288     char *record_encoding;
289 #if HAVE_ICONV_H
290     iconv_t iconv_to_utf8;
291     iconv_t iconv_from_utf8;
292 #endif
293 };
294
295 struct rank_control {
296     char *name;
297     void *(*create)(struct zebra_register *reg);
298     void (*destroy)(struct zebra_register *reg, void *class_handle);
299     void *(*begin)(struct zebra_register *reg, void *class_handle, RSET rset);
300     void (*end)(struct zebra_register *reg, void *set_handle);
301     int (*calc)(void *set_handle, int sysno);
302     void (*add)(void *set_handle, int seqno, int term_index);
303 };
304
305 struct term_set_entry {
306     char *term;
307     struct term_set_entry *next;
308 };
309
310 struct term_set_list {
311     struct term_set_entry *first;
312     struct term_set_entry *last;
313 };
314
315 RSET rpn_search (ZebraHandle zh, NMEM mem,
316                  Z_RPNQuery *rpn, int num_bases, char **basenames, 
317                  const char *setname, ZebraSet sset);
318
319
320 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
321                oid_value attributeset,
322                int num_bases, char **basenames,
323                int *position, int *num_entries, ZebraScanEntry **list,
324                int *is_partial);
325
326 RSET rset_trunc (ZebraHandle zh, ISAMS_P *isam_p, int no,
327                  const char *term, int length_term, const char *flags,
328                  int preserve_position, int term_type);
329
330 void resultSetAddTerm (ZebraHandle zh, ZebraSet s, int reg_type,
331                        const char *db, int set,
332                        int use, const char *term);
333 ZebraSet resultSetAdd (ZebraHandle zh, const char *name, int ov);
334 ZebraSet resultSetGet (ZebraHandle zh, const char *name);
335 ZebraSet resultSetAddRPN (ZebraHandle zh, ODR stream, ODR decode,
336                           Z_RPNQuery *rpn, int num_bases,
337                           char **basenames, const char *setname);
338 RSET resultSetRef (ZebraHandle zh, Z_ResultSetId *resultSetId);
339 void resultSetDestroy (ZebraHandle zh, int num_names, char **names,
340                        int *statuses);
341
342
343 ZebraPosSet zebraPosSetCreate (ZebraHandle zh, const char *name,
344                                int num, int *positions);
345 void zebraPosSetDestroy (ZebraHandle zh, ZebraPosSet records, int num);
346
347 void resultSetSort (ZebraHandle zh, NMEM nmem,
348                     int num_input_setnames, const char **input_setnames,
349                     const char *output_setname,
350                     Z_SortKeySpecList *sort_sequence, int *sort_status);
351 void resultSetSortSingle (ZebraHandle zh, NMEM nmem,
352                           ZebraSet sset, RSET rset,
353                           Z_SortKeySpecList *sort_sequence, int *sort_status);
354 void resultSetRank (ZebraHandle zh, ZebraSet zebraSet, RSET rset);
355 void resultSetInvalidate (ZebraHandle zh);
356
357 int zebra_server_lock_init (ZebraService zh);
358 int zebra_server_lock_destroy (ZebraService zh);
359 int zebra_server_lock (ZebraService zh, int lockCommit);
360 void zebra_server_unlock (ZebraService zh, int commitPhase);
361 int zebra_server_lock_get_state (ZebraService zh, time_t *timep);
362
363 typedef struct attent
364 {
365     int attset_ordinal;
366     data1_local_attribute *local_attributes;
367 } attent;
368
369 void zebraRankInstall (struct zebra_register *reg, struct rank_control *ctrl);
370 ZebraRankClass zebraRankLookup (ZebraHandle zh, const char *name);
371 void zebraRankDestroy (struct zebra_register *reg);
372
373 int att_getentbyatt(ZebraHandle zh, attent *res, oid_value set, int att);
374
375 extern struct rank_control *rank1_class;
376
377 int zebra_record_fetch (ZebraHandle zh, int sysno, int score, ODR stream,
378                         oid_value input_format, Z_RecordComposition *comp,
379                         oid_value *output_format, char **rec_bufp,
380                         int *rec_lenp, char **basenamep);
381
382 void extract_get_fname_tmp (ZebraHandle zh, char *fname, int no);
383 void zebra_index_merge (ZebraHandle zh);
384
385
386 int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
387                         const char *buf, size_t buf_size,
388                         const char *databaseName, int delete_flag,
389                         int test_mode, int *sysno,
390                         int store_keys, int store_data,
391                         const char *match_criteria);
392
393 void extract_flushWriteKeys (ZebraHandle zh);
394
395 struct zebra_fetch_control {
396     off_t offset_end;
397     off_t record_offset;
398     off_t record_int_pos;
399     const char *record_int_buf;
400     int record_int_len;
401     int fd;
402 };
403
404 int zebra_record_ext_read (void *fh, char *buf, size_t count);
405 off_t zebra_record_ext_seek (void *fh, off_t offset);
406 off_t zebra_record_ext_tell (void *fh);
407 off_t zebra_record_int_seek (void *fh, off_t offset);
408 off_t zebra_record_int_tell (void *fh);
409 int zebra_record_int_read (void *fh, char *buf, size_t count);
410 void zebra_record_int_end (void *fh, off_t offset);
411
412 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
413                               int cmd, struct recKeys *reckeys);
414 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
415                             int cmd, struct sortKey **skp);
416 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid);
417 void extract_token_add (RecWord *p);
418 int explain_extract (void *handle, Record rec, data1_node *n);
419
420 int fileExtract (ZebraHandle zh, SYSNO *sysno, const char *fname,
421                  const struct recordGroup *rGroup, int deleteFlag);
422
423 int zebra_begin_read (ZebraHandle zh);
424 void zebra_end_read (ZebraHandle zh);
425
426
427 YAZ_END_CDECL
428
429 #endif