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