Multiple registers (alpha early)
[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.75 2002-04-04 14:14:13 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 #if ZMBOL
24 #include <isam.h>
25 #include <isamc.h>
26 #include <isamd.h>
27 #define ISAM_DEFAULT "c"
28 #else
29 #define ISAM_DEFAULT "s"
30 #endif
31 #include <yaz/data1.h>
32 #include <recctrl.h>
33 #include "recindex.h"
34 #include "zebraapi.h"
35 #include "zinfo.h"
36 #include <passwddb.h>
37 #include <rset.h>
38
39 YAZ_BEGIN_CDECL
40
41 #define SU_SCHEME 1
42
43 #define IT_MAX_WORD 256
44 #define IT_KEY_HAVE_SEQNO 1
45 #define IT_KEY_HAVE_FIELD 0
46
47 typedef int SYSNO;
48
49 struct it_key {
50     int  sysno;
51     int  seqno;
52 };
53
54 enum dirsKind { dirs_dir, dirs_file };
55
56 struct dir_entry {
57     enum dirsKind kind;
58     char *name;
59     time_t mtime;
60 };
61
62 struct dirs_entry {
63     enum dirsKind kind;
64     char path[256];
65     SYSNO sysno;
66     time_t mtime;
67 };
68
69 void getFnameTmp (Res res, char *fname, int no);
70         
71 struct dirs_info *dirs_open (Dict dict, const char *rep, int rw);
72 struct dirs_info *dirs_fopen (Dict dict, const char *path);
73 struct dirs_entry *dirs_read (struct dirs_info *p);
74 struct dirs_entry *dirs_last (struct dirs_info *p);
75 void dirs_mkdir (struct dirs_info *p, const char *src, time_t mtime);
76 void dirs_rmdir (struct dirs_info *p, const char *src);
77 void dirs_add (struct dirs_info *p, const char *src, int sysno, time_t mtime);
78 void dirs_del (struct dirs_info *p, const char *src);
79 void dirs_free (struct dirs_info **pp);
80
81 struct dir_entry *dir_open (const char *rep);
82 void dir_sort (struct dir_entry *e);
83 void dir_free (struct dir_entry **e_p);
84
85 void repositoryUpdate (ZebraHandle zh);
86 void repositoryAdd (ZebraHandle zh);
87 void repositoryDelete (ZebraHandle zh);
88 void repositoryShow (ZebraHandle zh);
89
90 int key_open (ZebraHandle zh, int mem);
91 int key_close (ZebraHandle zh);
92 int key_compare (const void *p1, const void *p2);
93 int key_get_pos (const void *p);
94 int key_compare_it (const void *p1, const void *p2);
95 int key_qsort_compare (const void *p1, const void *p2);
96 void key_logdump (int mask, const void *p);
97 void inv_prstat (ZebraHandle zh);
98 void inv_compact (BFiles bfs);
99 void key_input (ZebraHandle zh, int nkeys, int cache, Res res);
100 ISAMS_M key_isams_m (Res res, ISAMS_M me);
101 #if ZMBOL
102 ISAMC_M key_isamc_m (Res res, ISAMC_M me);
103 ISAMD_M key_isamd_m (Res res, ISAMD_M me);
104 #endif
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 #if ZMBOL
209     ISAM isam;
210     ISAMC isamc;
211     ISAMD isamd;
212 #endif
213     Dict dict;
214     Dict matchDict;
215     SortIdx sortIdx;
216     int registerState; /* 0 (no commit pages), 1 (use commit pages) */
217     time_t registerChange;
218     BFiles bfs;
219     Records records;
220     ZebraExplainInfo zei;
221
222     char *server_path_prefix;
223     data1_handle dh;
224     ZebraMaps zebra_maps;
225     ZebraRankClass rank_classes;
226     RecTypes recTypes;
227     int seqno;
228     int last_val;
229     int stop_flag;
230     int active; /* 0=shutdown, 1=enabled and inactive, 2=activated */
231
232
233
234     struct recKeys keys;
235     struct sortKey *sortKeys;
236
237     char **key_buf;
238     size_t ptr_top;
239     size_t ptr_i;
240     size_t key_buf_used;
241     int key_file_no;
242 };
243
244 struct zebra_service {
245     int stop_flag;
246     Res global_res;
247     char *configName;
248     struct zebra_session *sessions;
249     struct zebra_register *regs;
250     Zebra_mutex_cond session_lock;
251     Passwd_db passwd_db;
252     char *path_root;
253 };
254
255
256 struct zebra_session {
257     struct zebra_session *next;
258     struct zebra_service *service;
259     struct zebra_register *reg;
260
261     char *admin_databaseName;
262
263     char **basenames;
264     int num_basenames;
265     char *reg_name;
266     char *path_reg;
267
268     ZebraLockHandle lock_normal;
269     ZebraLockHandle lock_shadow;
270
271     int trans_no;
272     int destroyed;
273     ZebraSet sets;
274     Res res;
275     int errCode;
276     int hits;
277     char *errString;
278 #if HAVE_SYS_TIMES_H
279     struct tms tms1;
280     struct tms tms2;    
281 #endif
282     struct recordGroup rGroup;
283 };
284
285 struct rank_control {
286     char *name;
287     void *(*create)(struct zebra_register *reg);
288     void (*destroy)(struct zebra_register *reg, void *class_handle);
289     void *(*begin)(struct zebra_register *reg, void *class_handle, RSET rset);
290     void (*end)(struct zebra_register *reg, void *set_handle);
291     int (*calc)(void *set_handle, int sysno);
292     void (*add)(void *set_handle, int seqno, int term_index);
293 };
294
295 struct term_set_entry {
296     char *term;
297     struct term_set_entry *next;
298 };
299
300 struct term_set_list {
301     struct term_set_entry *first;
302     struct term_set_entry *last;
303 };
304
305 RSET rpn_search (ZebraHandle zh, NMEM mem,
306                  Z_RPNQuery *rpn, int num_bases, char **basenames, 
307                  const char *setname, ZebraSet sset);
308
309
310 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
311                oid_value attributeset,
312                int num_bases, char **basenames,
313                int *position, int *num_entries, ZebraScanEntry **list,
314                int *is_partial);
315
316 RSET rset_trunc (ZebraHandle zh, ISAMS_P *isam_p, int no,
317                  const char *term, int length_term, const char *flags);
318
319 void resultSetAddTerm (ZebraHandle zh, ZebraSet s, int reg_type,
320                        const char *db, int set,
321                        int use, const char *term);
322 ZebraSet resultSetAdd (ZebraHandle zh, const char *name, int ov);
323 ZebraSet resultSetGet (ZebraHandle zh, const char *name);
324 ZebraSet resultSetAddRPN (ZebraHandle zh, ODR stream, ODR decode,
325                           Z_RPNQuery *rpn, int num_bases,
326                           char **basenames, const char *setname);
327 RSET resultSetRef (ZebraHandle zh, Z_ResultSetId *resultSetId);
328 void resultSetDestroy (ZebraHandle zh, int num_names, char **names,
329                        int *statuses);
330
331
332 ZebraPosSet zebraPosSetCreate (ZebraHandle zh, const char *name,
333                                int num, int *positions);
334 void zebraPosSetDestroy (ZebraHandle zh, ZebraPosSet records, int num);
335
336 void resultSetSort (ZebraHandle zh, NMEM nmem,
337                     int num_input_setnames, const char **input_setnames,
338                     const char *output_setname,
339                     Z_SortKeySpecList *sort_sequence, int *sort_status);
340 void resultSetSortSingle (ZebraHandle zh, NMEM nmem,
341                           ZebraSet sset, RSET rset,
342                           Z_SortKeySpecList *sort_sequence, int *sort_status);
343 void resultSetRank (ZebraHandle zh, ZebraSet zebraSet, RSET rset);
344 void resultSetInvalidate (ZebraHandle zh);
345
346 int zebra_server_lock_init (ZebraService zh);
347 int zebra_server_lock_destroy (ZebraService zh);
348 int zebra_server_lock (ZebraService zh, int lockCommit);
349 void zebra_server_unlock (ZebraService zh, int commitPhase);
350 int zebra_server_lock_get_state (ZebraService zh, time_t *timep);
351
352 typedef struct attent
353 {
354     int attset_ordinal;
355     data1_local_attribute *local_attributes;
356 } attent;
357
358 void zebraRankInstall (struct zebra_register *reg, struct rank_control *ctrl);
359 ZebraRankClass zebraRankLookup (ZebraHandle zh, const char *name);
360 void zebraRankDestroy (struct zebra_register *reg);
361
362 int att_getentbyatt(ZebraHandle zh, attent *res, oid_value set, int att);
363
364 extern struct rank_control *rank1_class;
365
366 int zebra_record_fetch (ZebraHandle zh, int sysno, int score, ODR stream,
367                         oid_value input_format, Z_RecordComposition *comp,
368                         oid_value *output_format, char **rec_bufp,
369                         int *rec_lenp, char **basenamep);
370
371 void extract_get_fname_tmp (ZebraHandle zh, char *fname, int no);
372 void zebra_index_merge (ZebraHandle zh);
373
374
375 int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
376                         const char *buf, size_t buf_size,
377                         const char *databaseName, int delete_flag,
378                         int test_mode, int *sysno,
379                         int store_keys, int store_data,
380                         const char *match_criteria);
381
382 void extract_flushWriteKeys (ZebraHandle zh);
383
384 struct zebra_fetch_control {
385     off_t offset_end;
386     off_t record_offset;
387     off_t record_int_pos;
388     const char *record_int_buf;
389     int record_int_len;
390     int fd;
391 };
392
393 int zebra_record_ext_read (void *fh, char *buf, size_t count);
394 off_t zebra_record_ext_seek (void *fh, off_t offset);
395 off_t zebra_record_ext_tell (void *fh);
396 off_t zebra_record_int_seek (void *fh, off_t offset);
397 off_t zebra_record_int_tell (void *fh);
398 int zebra_record_int_read (void *fh, char *buf, size_t count);
399 void zebra_record_int_end (void *fh, off_t offset);
400
401 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
402                               int cmd, struct recKeys *reckeys);
403 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
404                             int cmd, struct sortKey **skp);
405 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid);
406 void extract_token_add (RecWord *p);
407 int explain_extract (void *handle, Record rec, data1_node *n);
408
409 int fileExtract (ZebraHandle zh, SYSNO *sysno, const char *fname,
410                  const struct recordGroup *rGroup, int deleteFlag);
411
412 YAZ_END_CDECL
413
414 #endif