Do not use sync(2) after commit. The fsync on individual files suffices.
[idzebra-moved-to-github.git] / index / index.h
1 /* $Id: index.h,v 1.109.2.3 2006-08-14 10:38:57 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
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 #include <zebra-flock.h>
54
55 YAZ_BEGIN_CDECL
56
57 #define SU_SCHEME 1
58
59 #define IT_MAX_WORD 256
60 #define IT_KEY_HAVE_SEQNO 1
61 #define IT_KEY_HAVE_FIELD 0
62
63 typedef int SYSNO;
64
65 struct it_key {
66     int  sysno;
67     int  seqno;
68 };
69
70 enum dirsKind { dirs_dir, dirs_file };
71
72 struct dir_entry {
73     enum dirsKind kind;
74     char *name;
75     time_t mtime;
76 };
77
78 struct dirs_entry {
79     enum dirsKind kind;
80     char path[256];
81     SYSNO sysno;
82     time_t mtime;
83 };
84
85 void getFnameTmp (Res res, char *fname, int no);
86         
87 struct dirs_info *dirs_open (Dict dict, const char *rep, int rw);
88 struct dirs_info *dirs_fopen (Dict dict, const char *path, int rw);
89 struct dirs_entry *dirs_read (struct dirs_info *p);
90 struct dirs_entry *dirs_last (struct dirs_info *p);
91 void dirs_mkdir (struct dirs_info *p, const char *src, time_t mtime);
92 void dirs_rmdir (struct dirs_info *p, const char *src);
93 void dirs_add (struct dirs_info *p, const char *src, int sysno, time_t mtime);
94 void dirs_del (struct dirs_info *p, const char *src);
95 void dirs_free (struct dirs_info **pp);
96
97 struct dir_entry *dir_open (const char *rep, const char *base,
98                             int follow_links);
99 void dir_sort (struct dir_entry *e);
100 void dir_free (struct dir_entry **e_p);
101
102 void repositoryUpdate (ZebraHandle zh, const char *path);
103 void repositoryAdd (ZebraHandle zh, const char *path);
104 void repositoryDelete (ZebraHandle zh, const char *path);
105 void repositoryShow (ZebraHandle zh, const char *path);
106
107 int key_open (ZebraHandle zh, int mem);
108 int key_close (ZebraHandle zh);
109 int key_compare (const void *p1, const void *p2);
110 char *key_print_it (const void *p, char *buf);
111 int key_get_seq (const void *p);
112 int key_compare_it (const void *p1, const void *p2);
113 int key_qsort_compare (const void *p1, const void *p2);
114 void key_logdump (int mask, const void *p);
115 void key_logdump_txt (int logmask, const void *p, const char *txt);
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 void zebra_lock_prefix (Res res, char *dst);
157
158 void zebra_load_atts (data1_handle dh, Res res);
159
160 int key_SU_decode (int *ch, const unsigned char *out);
161 int key_SU_encode (int ch, char *out);
162
163 #define ENCODE_BUFLEN 768
164 struct encode_info {
165     int  sysno;  /* previously written values for delta-compress */
166     int  seqno;
167     int  cmd;
168     int prevsys; /* buffer for skipping insert/delete pairs */
169     int prevseq;
170     int prevcmd;
171     int keylen; /* tells if we have an unwritten key in buf, and how long*/
172     char buf[ENCODE_BUFLEN];
173 };
174
175 void encode_key_init (struct encode_info *i);
176 char *encode_key_int (int d, char *bp);
177 void encode_key_write (char *k, struct encode_info *i, FILE *outf);
178 void encode_key_flush (struct encode_info *i, FILE *outf);
179
180 typedef struct {
181     char *term;
182     char *db;
183     int sysno;
184     int score;
185 } *ZebraPosSet;
186
187 typedef struct zebra_set *ZebraSet;
188
189 typedef struct zebra_rank_class {
190     struct rank_control *control;
191     int init_flag;
192     void *class_handle;
193     struct zebra_rank_class *next;
194 } *ZebraRankClass;
195
196 struct recKeys {
197     int buf_used;
198     int buf_max;
199     char *buf;
200     char prevAttrSet;
201     short prevAttrUse;
202     int prevSeqNo;
203 };
204
205 #if 1
206 struct sortKeys {
207     int buf_used;
208     int buf_max;
209     char *buf;
210 };
211 #else
212 struct sortKey {
213     char *string;
214     int length;
215     int attrSet;
216     int attrUse;
217     struct sortKey *next;
218 };
219 #endif
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     struct recKeys keys;
249 #if 1
250     struct sortKeys sortKeys;
251 #else
252     struct sortKey *sortKeys;
253 #endif
254     char **key_buf;
255     size_t ptr_top;
256     size_t ptr_i;
257     size_t key_buf_used;
258     int key_file_no;
259 };
260
261 struct zebra_service {
262     int stop_flag;
263     Res global_res;
264     char *configName;
265     struct zebra_session *sessions;
266     struct zebra_register *regs;
267     Zebra_mutex_cond session_lock;
268     Passwd_db passwd_db;
269     const char *path_root;
270 };
271
272
273 struct zebra_session {
274     struct zebra_session *next;
275     struct zebra_service *service;
276     struct zebra_register *reg;
277
278     char *xadmin_databaseName;
279
280     char **basenames;
281     int num_basenames;
282     char *reg_name;
283     char *path_reg;
284
285     ZebraLockHandle lock_normal;
286     ZebraLockHandle lock_shadow;
287
288     int trans_no;
289     int trans_w_no;
290
291     int destroyed;
292     ZebraSet sets;
293     Res res;
294     char *user_perm;
295     int errCode;
296     int hits;
297     char *errString;
298 #if HAVE_SYS_TIMES_H
299     struct tms tms1;
300     struct tms tms2;    
301 #endif
302     int  shadow_enable;
303
304     int records_inserted;
305     int records_updated;
306     int records_deleted;
307     int records_processed;
308     char *record_encoding;
309
310     yaz_iconv_t iconv_to_utf8;
311     yaz_iconv_t iconv_from_utf8;
312
313     int m_follow_links;
314     const char *m_group;
315     const char *m_record_id;
316     const char *m_record_type;
317     int m_store_data;
318     int m_store_keys;
319     int m_explain_database;
320     int m_flag_rw;
321     int m_file_verbose_limit;
322 };
323
324 struct rank_control {
325     char *name;
326     void *(*create)(ZebraHandle zh);
327     void (*destroy)(struct zebra_register *reg, void *class_handle);
328     void *(*begin)(struct zebra_register *reg, void *class_handle, RSET rset);
329     /* ### Could add parameters to begin:
330      *  char *index;    // author, title, etc.
331      *  int dbsize;     // number of records in database
332      *  int rssize;     // number of records in result set (estimate?)
333      */
334     void (*end)(struct zebra_register *reg, void *set_handle);
335     int (*calc)(void *set_handle, int sysno);
336     void (*add)(void *set_handle, int seqno, int term_index);
337 };
338
339 struct term_set_entry {
340     char *term;
341     struct term_set_entry *next;
342 };
343
344 struct term_set_list {
345     struct term_set_entry *first;
346     struct term_set_entry *last;
347 };
348
349 RSET rpn_search (ZebraHandle zh, NMEM mem,
350                  Z_RPNQuery *rpn, int num_bases, char **basenames, 
351                  const char *setname, ZebraSet sset);
352
353
354 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
355                oid_value attributeset,
356                int num_bases, char **basenames,
357                int *position, int *num_entries, ZebraScanEntry **list,
358                int *is_partial, RSET limit_set, int return_zero);
359
360 RSET rset_trunc (ZebraHandle zh, ISAMS_P *isam_p, int no,
361                  const char *term, int length_term, const char *flags,
362                  int preserve_position, int term_type);
363
364 void resultSetAddTerm (ZebraHandle zh, ZebraSet s, int reg_type,
365                        const char *db, int set,
366                        int use, const char *term);
367 ZebraSet resultSetAdd (ZebraHandle zh, const char *name, int ov);
368 ZebraSet resultSetGet (ZebraHandle zh, const char *name);
369 ZebraSet resultSetAddRPN (ZebraHandle zh, NMEM m, Z_RPNQuery *rpn,
370                           int num_bases, char **basenames,
371                           const char *setname);
372 RSET resultSetRef (ZebraHandle zh, const char *resultSetId);
373 void resultSetDestroy (ZebraHandle zh, int num_names, char **names,
374                        int *statuses);
375
376
377 ZebraPosSet zebraPosSetCreate (ZebraHandle zh, const char *name,
378                                int num, int *positions);
379 void zebraPosSetDestroy (ZebraHandle zh, ZebraPosSet records, int num);
380
381 void resultSetSort (ZebraHandle zh, NMEM nmem,
382                     int num_input_setnames, const char **input_setnames,
383                     const char *output_setname,
384                     Z_SortKeySpecList *sort_sequence, int *sort_status);
385 void resultSetSortSingle (ZebraHandle zh, NMEM nmem,
386                           ZebraSet sset, RSET rset,
387                           Z_SortKeySpecList *sort_sequence, int *sort_status);
388 void resultSetRank (ZebraHandle zh, ZebraSet zebraSet, RSET rset);
389 void resultSetInvalidate (ZebraHandle zh);
390
391 int zebra_server_lock_init (ZebraService zh);
392 int zebra_server_lock_destroy (ZebraService zh);
393 int zebra_server_lock (ZebraService zh, int lockCommit);
394 void zebra_server_unlock (ZebraService zh, int commitPhase);
395 int zebra_server_lock_get_state (ZebraService zh, time_t *timep);
396
397 typedef struct attent
398 {
399     int attset_ordinal;
400     data1_local_attribute *local_attributes;
401 } attent;
402
403 void zebraRankInstall (struct zebra_register *reg, struct rank_control *ctrl);
404 ZebraRankClass zebraRankLookup (ZebraHandle zh, const char *name);
405 void zebraRankDestroy (struct zebra_register *reg);
406
407 int att_getentbyatt(ZebraHandle zh, attent *res, oid_value set, int att,
408                 const char *sattr);
409
410 extern struct rank_control *rank1_class;
411 extern struct rank_control *rankzv_class;
412 extern struct rank_control *rankliv_class;
413
414 int zebra_record_fetch (ZebraHandle zh, int sysno, int score, ODR stream,
415                         oid_value input_format, Z_RecordComposition *comp,
416                         oid_value *output_format, char **rec_bufp,
417                         int *rec_lenp, char **basenamep);
418
419 void extract_get_fname_tmp (ZebraHandle zh, char *fname, int no);
420
421 void zebra_index_merge (ZebraHandle zh);
422
423 int buffer_extract_record (ZebraHandle zh, 
424                            const char *buf, size_t buf_size,
425                            int delete_flag,
426                            int test_mode, 
427                            const char *recordType,
428                            int *sysno,
429                            const char *match_criteria,
430                            const char *fname,
431                            int force_update,
432                            int allow_update);
433
434 #if 0
435 int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
436                         const char *buf, size_t buf_size,
437                         const char *databaseName, int delete_flag,
438                         int test_mode, int *sysno,
439                         int store_keys, int store_data,
440                         const char *match_criteria);
441 #endif
442 void extract_flushWriteKeys (ZebraHandle zh, int final);
443
444 struct zebra_fetch_control {
445     off_t offset_end;
446     off_t record_offset;
447     off_t record_int_pos;
448     const char *record_int_buf;
449     int record_int_len;
450     int fd;
451 };
452
453 int zebra_record_ext_read (void *fh, char *buf, size_t count);
454 off_t zebra_record_ext_seek (void *fh, off_t offset);
455 off_t zebra_record_ext_tell (void *fh);
456 off_t zebra_record_int_seek (void *fh, off_t offset);
457 off_t zebra_record_int_tell (void *fh);
458 int zebra_record_int_read (void *fh, char *buf, size_t count);
459 void zebra_record_int_end (void *fh, off_t offset);
460
461 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
462                               int cmd, struct recKeys *reckeys);
463 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
464                             int cmd, struct sortKeys *skp);
465 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid);
466 void extract_token_add (RecWord *p);
467 int explain_extract (void *handle, Record rec, data1_node *n);
468
469 int fileExtract (ZebraHandle zh, SYSNO *sysno, const char *fname,
470                  int deleteFlag);
471
472 int zebra_begin_read (ZebraHandle zh);
473 int zebra_end_read (ZebraHandle zh);
474
475 int zebra_file_stat (const char *file_name, struct stat *buf,
476                      int follow_links);
477
478 void zebra_livcode_transform(ZebraHandle zh, Z_RPNQuery *query);
479
480 void *iscz1_code_start (int mode);
481 void iscz1_code_reset (void *vp);
482 void iscz1_code_stop (int mode, void *p);
483 void iscz1_code_item (int mode, void *vp, char **dst, char **src);
484
485
486
487 YAZ_END_CDECL
488
489 #endif