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