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