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