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