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