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