Definitions in zebramap.h are private. ZebraMaps no longer used in
[idzebra-moved-to-github.git] / index / index.h
1 /* $Id: index.h,v 1.172 2006-08-15 14:28:34 adam Exp $
2    Copyright (C) 1995-2006
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 #ifndef INDEX_H
24 #define INDEX_H
25
26 #include <time.h>
27 #include <stdlib.h>
28 #include <idzebra/version.h>
29 #include <idzebra/util.h>
30 #include <idzebra/flock.h>
31 #include <sortidx.h>
32
33 #if HAVE_SYS_TIMES_H
34 #include <sys/times.h>
35 #endif
36 #include <sys/stat.h>
37
38 #include <idzebra/dict.h>
39 #include <idzebra/isams.h>
40 #include <idzebra/isamc.h>
41 #include <idzebra/isamb.h>
42 #include <d1_absyn.h>
43 #include <idzebra/recgrs.h>
44 #include "recindex.h"
45 #include <idzebra/api.h>
46 #include "zinfo.h"
47 #include <passwddb.h>
48 #include <rset.h>
49 #include <zebramap.h>
50
51 YAZ_BEGIN_CDECL
52
53 #define ISAM_DEFAULT "b"
54
55 #define SU_SCHEME 1
56
57 #define IT_MAX_WORD 256
58
59 #define IT_KEY_LEVEL_MAX 5
60 struct it_key {
61     int  len;
62     zint mem[IT_KEY_LEVEL_MAX];
63 };
64
65 enum dirsKind { dirs_dir, dirs_file };
66
67 struct dir_entry {
68     enum dirsKind kind;
69     char *name;
70     time_t mtime;
71 };
72
73 struct dirs_entry {
74     enum dirsKind kind;
75     char path[256];
76     SYSNO sysno;
77     time_t mtime;
78 };
79
80 void getFnameTmp (Res res, char *fname, int no);
81         
82 struct dirs_info *dirs_open (Dict dict, const char *rep, int rw);
83 struct dirs_info *dirs_fopen (Dict dict, const char *path, int rw);
84 struct dirs_entry *dirs_read (struct dirs_info *p);
85 struct dirs_entry *dirs_last (struct dirs_info *p);
86 void dirs_mkdir (struct dirs_info *p, const char *src, time_t mtime);
87 void dirs_rmdir (struct dirs_info *p, const char *src);
88 void dirs_add (struct dirs_info *p, const char *src, SYSNO sysno, time_t mtime);
89 void dirs_del (struct dirs_info *p, const char *src);
90 void dirs_free (struct dirs_info **pp);
91
92 struct dir_entry *dir_open (const char *rep, const char *base,
93                             int follow_links);
94 void dir_sort (struct dir_entry *e);
95 void dir_free (struct dir_entry **e_p);
96
97 void repositoryUpdate (ZebraHandle zh, const char *path);
98 void repositoryAdd (ZebraHandle zh, const char *path);
99 void repositoryDelete (ZebraHandle zh, const char *path);
100 void repositoryShow (ZebraHandle zh, const char *path);
101
102 int key_open (ZebraHandle zh, int mem);
103 int key_close (ZebraHandle zh);
104 int key_compare (const void *p1, const void *p2);
105 void key_init(struct it_key *k);
106 char *key_print_it (const void *p, char *buf);
107 zint key_get_seq (const void *p);
108 zint key_get_segment (const void *p);
109 int key_compare_it (const void *p1, const void *p2);
110 int key_qsort_compare (const void *p1, const void *p2);
111 void key_logdump (int mask, const void *p);
112 void key_logdump_txt (int logmask, const void *p, const char *txt);
113 void inv_prstat (ZebraHandle zh);
114 void inv_compact (BFiles bfs);
115 void key_input (ZebraHandle zh, int nkeys, int cache, Res res);
116 ISAMS_M *key_isams_m (Res res, ISAMS_M *me);
117 ISAMC_M *key_isamc_m (Res res, ISAMC_M *me);
118 int merge_sort (char **buf, int from, int to);
119 int key_SU_code (int ch, char *out);
120
121 #define FNAME_DICT "dict"
122 #define FNAME_ISAM "isam"
123 #define FNAME_ISAMC "isamc"
124 #define FNAME_ISAMS "isams"
125 #define FNAME_ISAMH "isamh"
126 #define FNAME_ISAMD "isamd"
127 #define FNAME_CONFIG "zebra.cfg"
128
129 #define GMATCH_DICT "gmatch"
130 #define FMATCH_DICT "fmatch"
131
132 struct strtab *strtab_mk (void);
133 int strtab_src (struct strtab *t, const char *name, void ***infop);
134 void strtab_del (struct strtab *t,
135                  void (*func)(const char *name, void *info, void *data),
136                  void *data);
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 void zebra_lock_prefix (Res res, char *dst);
144
145 #define FNAME_MAIN_LOCK   "zebraidx.LCK"
146 #define FNAME_COMMIT_LOCK "zebracmt.LCK"
147 #define FNAME_ORG_LOCK    "zebraorg.LCK"
148 #define FNAME_TOUCH_TIME  "zebraidx.time"
149
150 int key_SU_decode (int *ch, const unsigned char *out);
151 int key_SU_encode (int ch, char *out);
152
153 typedef struct zebra_set *ZebraSet;
154
155 typedef struct zebra_rank_class {
156     struct rank_control *control;
157     int init_flag;
158     void *class_handle;
159     struct zebra_rank_class *next;
160 } *ZebraRankClass;
161
162 #include "reckeys.h"
163
164 struct zebra_register {
165     char *name;
166     
167     ISAMS isams;
168     ISAMC isamc;
169     ISAMB isamb;
170     Dict dict;
171     Dict matchDict;
172     SortIdx sortIdx;
173     int registerState; /* 0 (no commit pages), 1 (use commit pages) */
174     time_t registerChange;
175     BFiles bfs;
176     Records records;
177     ZebraExplainInfo zei;
178
179     char *server_path_prefix;
180     data1_handle dh;
181     ZebraMaps zebra_maps;
182     ZebraRankClass rank_classes;
183     RecTypes recTypes;
184     int seqno;
185     int last_val;
186     int stop_flag;
187
188     zebra_rec_keys_t keys;
189     zebra_rec_keys_t sortKeys;
190     char **key_buf;
191     size_t ptr_top;
192     size_t ptr_i;
193     size_t key_buf_used;
194     int key_file_no;
195 };
196
197 struct zebra_service {
198     int stop_flag;
199     Res global_res;
200     struct zebra_session *sessions;
201     struct zebra_register *regs;
202     Zebra_mutex_cond session_lock;
203     Passwd_db passwd_db;
204     Res dbaccess;
205     const char *path_root;
206     RecTypeClass record_classes;
207     NMEM nmem;
208 };
209
210
211 struct zebra_session {
212     struct zebra_session *next;
213     struct zebra_service *service;
214     struct zebra_register *reg;
215
216     char *xadmin_databaseName;
217
218     char **basenames;
219     int num_basenames;
220     zint approx_limit;
221     char *reg_name;
222     char *path_reg;
223
224     ZebraLockHandle lock_normal;
225     ZebraLockHandle lock_shadow;
226
227     int trans_no;
228     int trans_w_no;
229
230     int destroyed;
231     ZebraSet sets;
232     Res res;
233     Res session_res;
234     char *user_perm;
235     char *dbaccesslist;
236     int errCode;
237     zint hits;
238     char *errString;
239 #if HAVE_SYS_TIMES_H
240     struct tms tms1;
241     struct tms tms2;    
242 #endif
243     int  shadow_enable;
244
245     int m_staticrank;
246
247     zint records_inserted;
248     zint records_updated;
249     zint records_deleted;
250     zint records_processed;
251     char *record_encoding;
252
253     yaz_iconv_t iconv_to_utf8;
254     yaz_iconv_t iconv_from_utf8;
255
256     int m_follow_links;
257     const char *m_group;
258     const char *m_record_id;
259     const char *m_record_type;
260     int m_store_data;
261     int m_store_keys;
262     int m_explain_database;
263     int m_flag_rw;
264     int m_file_verbose_limit;
265
266     void *store_data_buf;
267     size_t store_data_size;
268     NMEM nmem_error;
269
270     struct zebra_limit *m_limit;
271 };
272
273
274 struct term_set_entry {
275     char *term;
276     struct term_set_entry *next;
277 };
278
279 struct term_set_list {
280     struct term_set_entry *first;
281     struct term_set_entry *last;
282 };
283
284
285 void zebra_limit_destroy(struct zebra_limit *zl);
286 struct zebra_limit *zebra_limit_create(int exclude_flag, zint *ids);
287 void zebra_limit_for_rset(struct zebra_limit *zl,
288                           int (**filter_func)(const void *buf, void *data),
289                           void (**filter_destroy)(void *data),
290                           void **filter_data);
291
292 struct rset_key_control *zebra_key_control_create(ZebraHandle zh);
293
294 ZEBRA_RES rpn_search_top(ZebraHandle zh, Z_RPNStructure *zs,
295                          oid_value attributeSet, 
296                          NMEM stream, NMEM rset_nmem,
297                          Z_SortKeySpecList *sort_sequence,
298                          int num_bases, char **basenames,
299                          RSET *result_set);
300
301 ZEBRA_RES rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
302                     oid_value attributeset,
303                     int num_bases, char **basenames,
304                     int *position, int *num_entries, ZebraScanEntry **list,
305                     int *is_partial, RSET limit_set, int return_zero);
306
307 RSET rset_trunc(ZebraHandle zh, ISAM_P *isam_p, int no,
308                 const char *term, int length_term, const char *flags,
309                 int preserve_position, int term_type, NMEM rset_nmem,
310                 struct rset_key_control *kctrl, int scope,
311                 struct ord_list *ol, int reg_type,
312                 zint hits_limit, const char *term_ref_id);
313
314 void resultSetAddTerm(ZebraHandle zh, ZebraSet s, int reg_type,
315                       const char *db, const char *index_name,
316                       const char *term);
317 ZebraSet resultSetAdd (ZebraHandle zh, const char *name, int ov);
318 ZebraSet resultSetGet (ZebraHandle zh, const char *name);
319 ZEBRA_RES resultSetAddRPN (ZebraHandle zh, NMEM m, Z_RPNQuery *rpn,
320                      int num_bases, char **basenames,
321                      const char *setname);
322 RSET resultSetRef (ZebraHandle zh, const char *resultSetId);
323 void resultSetDestroy (ZebraHandle zh, int num_names, char **names,
324                        int *statuses);
325 ZEBRA_RES resultSetSort (ZebraHandle zh, NMEM nmem,
326                          int num_input_setnames, const char **input_setnames,
327                          const char *output_setname,
328                          Z_SortKeySpecList *sort_sequence, int *sort_status);
329 ZEBRA_RES resultSetSortSingle (ZebraHandle zh, NMEM nmem,
330                                ZebraSet sset, RSET rset,
331                                Z_SortKeySpecList *sort_sequence,
332                                int *sort_status);
333 ZEBRA_RES resultSetRank (ZebraHandle zh, ZebraSet zebraSet, RSET rset,
334                          NMEM nmem);
335 void resultSetInvalidate (ZebraHandle zh);
336
337 int zebra_server_lock_init (ZebraService zh);
338 int zebra_server_lock_destroy (ZebraService zh);
339 int zebra_server_lock (ZebraService zh, int lockCommit);
340 void zebra_server_unlock (ZebraService zh, int commitPhase);
341 int zebra_server_lock_get_state (ZebraService zh, time_t *timep);
342
343 int zebra_record_fetch (ZebraHandle zh, SYSNO sysno, int score, 
344                         zebra_snippets *hit_snippet, ODR stream,
345                         oid_value input_format, Z_RecordComposition *comp,
346                         oid_value *output_format, char **rec_bufp,
347                         int *rec_lenp, char **basenamep,
348                         char **addinfo);
349
350 void extract_get_fname_tmp (ZebraHandle zh, char *fname, int no);
351
352 void zebra_index_merge (ZebraHandle zh);
353
354 ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh, 
355                                       const char *buf, size_t buf_size,
356                                       int delete_flag,
357                                       int test_mode, 
358                                       const char *recordType,
359                                       SYSNO *sysno,
360                                       const char *match_criteria,
361                                       const char *fname,
362                                       int force_update,
363                                       int allow_update);
364
365 #if 0
366 int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
367                         const char *buf, size_t buf_size,
368                         const char *databaseName, int delete_flag,
369                         int test_mode, int *sysno,
370                         int store_keys, int store_data,
371                         const char *match_criteria);
372 #endif
373 void extract_flushWriteKeys (ZebraHandle zh, int final);
374
375 struct zebra_fetch_control {
376     off_t offset_end;
377     off_t record_offset;
378     off_t record_int_pos;
379     const char *record_int_buf;
380     int record_int_len;
381     int fd;
382 };
383
384 int zebra_record_ext_read (void *fh, char *buf, size_t count);
385 off_t zebra_record_ext_seek (void *fh, off_t offset);
386 off_t zebra_record_ext_tell (void *fh);
387 off_t zebra_record_int_seek (void *fh, off_t offset);
388 off_t zebra_record_int_tell (void *fh);
389 int zebra_record_int_read (void *fh, char *buf, size_t count);
390 void zebra_record_int_end (void *fh, off_t offset);
391
392 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys);
393
394 ZEBRA_RES zebra_snippets_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys,
395                                   zebra_snippets *snippets);
396 ZEBRA_RES zebra_snippets_hit_vector(ZebraHandle zh, const char *setname,
397                                     zint sysno, zebra_snippets *snippets);
398
399 ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n);
400
401 ZEBRA_RES zebra_extract_file(ZebraHandle zh, SYSNO *sysno, const char *fname,
402                              int deleteFlag);
403
404 ZEBRA_RES zebra_begin_read (ZebraHandle zh);
405 ZEBRA_RES zebra_end_read (ZebraHandle zh);
406
407 int zebra_file_stat (const char *file_name, struct stat *buf,
408                      int follow_links);
409
410 void zebra_livcode_transform(ZebraHandle zh, Z_RPNQuery *query);
411
412 void *iscz1_start ();
413 void iscz1_reset (void *vp);
414 void iscz1_stop (void *p);
415 void iscz1_decode (void *vp, char **dst, const char **src);
416 void iscz1_encode (void *vp, char **dst, const char **src);
417
418 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
419                     int compact_flag, Res res);
420
421 void zebra_setError(ZebraHandle zh, int code, const char *addinfo);
422 void zebra_setError_zint(ZebraHandle zh, int code, zint i);
423
424 void zebra_term_untrans_iconv(ZebraHandle zh, NMEM stream, int reg_type,
425                               char **dst, const char *src);
426
427 ZEBRA_RES zebra_get_hit_vector(ZebraHandle zh, const char *setname, zint sysno);
428
429 void zebra_term_untrans(ZebraHandle zh, int reg_type,
430                         char *dst, const char *src);
431
432 ZEBRA_RES zebra_apt_get_ord(ZebraHandle zh,
433                             Z_AttributesPlusTerm *zapt,
434                             int index_type,
435                             const char *xpath_use,
436                             oid_value curAttributeSet,
437                             int *ord);
438
439 ZEBRA_RES zebra_attr_list_get_ord(ZebraHandle zh,
440                                   Z_AttributeList *attr_list,
441                                   zinfo_index_category_t cat,
442                                   int index_type,
443                                   oid_value curAttributeSet,
444                                   int *ord);
445
446 ZEBRA_RES zebra_sort_get_ord(ZebraHandle zh,
447                              Z_SortAttributes *sortAttributes,
448                              int *ord,
449                              int *numerical);
450
451 ZEBRA_RES zebra_update_file_match(ZebraHandle zh, const char *path);
452 ZEBRA_RES zebra_update_from_path(ZebraHandle zh, const char *path);
453 ZEBRA_RES zebra_delete_from_path(ZebraHandle zh, const char *path);
454
455 YAZ_END_CDECL
456
457 #endif
458 /*
459  * Local variables:
460  * c-basic-offset: 4
461  * indent-tabs-mode: nil
462  * End:
463  * vim: shiftwidth=4 tabstop=8 expandtab
464  */
465