2007.
[idzebra-moved-to-github.git] / index / index.h
1 /* $Id: index.h,v 1.190 2007-01-15 20:08:25 adam Exp $
2    Copyright (C) 1995-2007
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 ZEBRA_INDEX_H
24 #define ZEBRA_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 <yaz/timing.h>
39 #include <idzebra/dict.h>
40 #include <idzebra/isams.h>
41 #include <idzebra/isamc.h>
42 #include <idzebra/isamb.h>
43 #include <d1_absyn.h>
44 #include <idzebra/recgrs.h>
45 #include "recindex.h"
46 #include <idzebra/api.h>
47 #include "zinfo.h"
48 #include <passwddb.h>
49 #include <rset.h>
50 #include <zebramap.h>
51
52 #include <it_key.h>
53 #include <su_codec.h>
54
55 YAZ_BEGIN_CDECL
56
57 #define ISAM_DEFAULT "b"
58
59 enum dirsKind { dirs_dir, dirs_file };
60
61 struct dir_entry {
62     enum dirsKind kind;
63     char *name;
64     time_t mtime;
65 };
66
67 struct dirs_entry {
68     enum dirsKind kind;
69     char path[256];
70     zint sysno;
71     time_t mtime;
72 };
73
74 void getFnameTmp(Res res, char *fname, int no);
75         
76 struct dirs_info *dirs_open(Dict dict, const char *rep, int rw);
77 struct dirs_info *dirs_fopen(Dict dict, const char *path, int rw);
78 struct dirs_entry *dirs_read(struct dirs_info *p);
79 struct dirs_entry *dirs_last(struct dirs_info *p);
80 void dirs_mkdir(struct dirs_info *p, const char *src, time_t mtime);
81 void dirs_rmdir(struct dirs_info *p, const char *src);
82 void dirs_add(struct dirs_info *p, const char *src, zint sysno, time_t mtime);
83 void dirs_del(struct dirs_info *p, const char *src);
84 void dirs_free(struct dirs_info **pp);
85
86 struct dir_entry *dir_open(const char *rep, const char *base,
87                            int follow_links);
88 void dir_sort(struct dir_entry *e);
89 void dir_free(struct dir_entry **e_p);
90
91 void repositoryUpdate(ZebraHandle zh, const char *path);
92 void repositoryAdd(ZebraHandle zh, const char *path);
93 void repositoryDelete(ZebraHandle zh, const char *path);
94 void repositoryShow(ZebraHandle zh, const char *path);
95
96 void inv_prstat(ZebraHandle zh);
97 void inv_compact(BFiles bfs);
98 void key_input(ZebraHandle zh, int nkeys, int cache, Res res);
99 ISAMS_M *key_isams_m(Res res, ISAMS_M *me);
100 ISAMC_M *key_isamc_m(Res res, ISAMC_M *me);
101
102 #define FNAME_DICT "dict"
103 #define FNAME_ISAM "isam"
104 #define FNAME_ISAMC "isamc"
105 #define FNAME_ISAMS "isams"
106 #define FNAME_CONFIG "zebra.cfg"
107
108 #define GMATCH_DICT "gmatch"
109 #define FMATCH_DICT "fmatch%d"
110
111 void zebra_lock_prefix(Res res, char *dst);
112
113 #define FNAME_MAIN_LOCK   "zebraidx.LCK"
114 #define FNAME_COMMIT_LOCK "zebracmt.LCK"
115 #define FNAME_ORG_LOCK    "zebraorg.LCK"
116 #define FNAME_TOUCH_TIME  "zebraidx.time"
117
118 typedef struct zebra_set *ZebraSet;
119
120 typedef struct zebra_rank_class {
121     struct rank_control *control;
122     int init_flag;
123     void *class_handle;
124     struct zebra_rank_class *next;
125 } *ZebraRankClass;
126
127 #include "reckeys.h"
128 #include "key_block.h"
129
130 struct zebra_register {
131     char *name;
132     
133     ISAMS isams;
134     ISAMC isamc;
135     ISAMB isamb;
136     Dict dict;
137     Dict matchDict;
138     zebra_sort_index_t sort_index;
139     int registerState; /* 0 (no commit pages), 1 (use commit pages) */
140     time_t registerChange;
141     BFiles bfs;
142     Records records;
143     ZebraExplainInfo zei;
144
145     char *server_path_prefix;
146     data1_handle dh;
147     ZebraMaps zebra_maps;
148     ZebraRankClass rank_classes;
149     RecTypes recTypes;
150     int seqno;
151     int last_val;
152     int stop_flag;
153
154     zebra_rec_keys_t keys;
155     zebra_rec_keys_t sortKeys;
156
157     zebra_key_block_t key_block;
158 };
159
160 struct zebra_service {
161     int stop_flag;
162     Res global_res;
163     struct zebra_session *sessions;
164     struct zebra_register *regs;
165     Zebra_mutex_cond session_lock;
166     Passwd_db passwd_db;
167     Res dbaccess;
168     const char *path_root;
169     RecTypeClass record_classes;
170     NMEM nmem;
171     yaz_timing_t timing;
172 };
173
174
175 struct zebra_session {
176     struct zebra_session *next;
177     struct zebra_service *service;
178     struct zebra_register *reg;
179
180     char *xadmin_databaseName;
181
182     char **basenames;
183     int num_basenames;
184     zint approx_limit;
185     char *reg_name;
186     char *path_reg;
187
188     ZebraLockHandle lock_normal;
189     ZebraLockHandle lock_shadow;
190
191     int trans_no;
192     int trans_w_no;
193
194     int destroyed;
195     ZebraSet sets;
196     Res res;
197     Res session_res;
198     char *user_perm;
199     char *dbaccesslist;
200     int errCode;
201     zint hits;
202     char *errString;
203 #if HAVE_SYS_TIMES_H
204     struct tms tms1;
205     struct tms tms2;    
206 #endif
207     int  shadow_enable;
208
209     int m_staticrank;
210     int m_segment_indexing;
211
212     zint records_inserted;
213     zint records_updated;
214     zint records_deleted;
215     zint records_processed;
216     char *record_encoding;
217
218     yaz_iconv_t iconv_to_utf8;
219     yaz_iconv_t iconv_from_utf8;
220
221     int m_follow_links;
222     const char *m_group;
223     const char *m_record_id;
224     const char *m_record_type;
225     int m_store_data;
226     int m_store_keys;
227     int m_explain_database;
228     int m_flag_rw;
229     int m_file_verbose_limit;
230
231     void *store_data_buf;
232     size_t store_data_size;
233     NMEM nmem_error;
234
235     struct zebra_limit *m_limit;
236 };
237
238
239 struct term_set_entry {
240     char *term;
241     struct term_set_entry *next;
242 };
243
244 struct term_set_list {
245     struct term_set_entry *first;
246     struct term_set_entry *last;
247 };
248
249
250 void zebra_limit_destroy(struct zebra_limit *zl);
251 struct zebra_limit *zebra_limit_create(int exclude_flag, zint *ids);
252 void zebra_limit_for_rset(struct zebra_limit *zl,
253                           int (**filter_func)(const void *buf, void *data),
254                           void (**filter_destroy)(void *data),
255                           void **filter_data);
256
257 struct rset_key_control *zebra_key_control_create(ZebraHandle zh);
258
259 ZEBRA_RES rpn_search_top(ZebraHandle zh, Z_RPNStructure *zs,
260                          oid_value attributeSet, 
261                          NMEM stream, NMEM rset_nmem,
262                          Z_SortKeySpecList *sort_sequence,
263                          int num_bases, char **basenames,
264                          RSET *result_set);
265
266 ZEBRA_RES rpn_get_top_approx_limit(ZebraHandle zh, Z_RPNStructure *zs,
267                                    zint *approx_limit);
268
269 ZEBRA_RES rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
270                    oid_value attributeset,
271                    int num_bases, char **basenames,
272                    int *position, int *num_entries, ZebraScanEntry **list,
273                    int *is_partial, RSET limit_set);
274
275 RSET rset_trunc(ZebraHandle zh, ISAM_P *isam_p, int no,
276                 const char *term, int length_term, const char *flags,
277                 int preserve_position, int term_type, NMEM rset_nmem,
278                 struct rset_key_control *kctrl, int scope,
279                 struct ord_list *ol, int reg_type,
280                 zint hits_limit, const char *term_ref_id);
281
282 void resultSetAddTerm(ZebraHandle zh, ZebraSet s, int reg_type,
283                       const char *db, const char *index_name,
284                       const char *term);
285 ZebraSet resultSetAdd(ZebraHandle zh, const char *name, int ov);
286 ZebraSet resultSetGet(ZebraHandle zh, const char *name);
287 ZEBRA_RES resultSetAddRPN(ZebraHandle zh, NMEM m, Z_RPNQuery *rpn,
288                      int num_bases, char **basenames,
289                      const char *setname);
290 RSET resultSetRef(ZebraHandle zh, const char *resultSetId);
291 void resultSetDestroy(ZebraHandle zh, int num_names, char **names,
292                        int *statuses);
293 ZEBRA_RES resultSetSort(ZebraHandle zh, NMEM nmem,
294                          int num_input_setnames, const char **input_setnames,
295                          const char *output_setname,
296                          Z_SortKeySpecList *sort_sequence, int *sort_status);
297 ZEBRA_RES resultSetSortSingle(ZebraHandle zh, NMEM nmem,
298                                ZebraSet sset, RSET rset,
299                                Z_SortKeySpecList *sort_sequence,
300                                int *sort_status);
301 ZEBRA_RES resultSetRank(ZebraHandle zh, ZebraSet zebraSet, RSET rset,
302                          NMEM nmem);
303 void resultSetInvalidate(ZebraHandle zh);
304
305 int zebra_record_fetch(ZebraHandle zh, zint sysno, int score, 
306                         zebra_snippets *hit_snippet, ODR stream,
307                         oid_value input_format, Z_RecordComposition *comp,
308                         oid_value *output_format, char **rec_bufp,
309                         int *rec_lenp, char **basenamep,
310                         char **addinfo);
311
312 void extract_get_fname_tmp(ZebraHandle zh, char *fname, int no);
313
314 void zebra_index_merge(ZebraHandle zh);
315
316 ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh, 
317                                       const char *buf, size_t buf_size,
318                                       int delete_flag,
319                                       int test_mode, 
320                                       const char *recordType,
321                                       zint *sysno,
322                                       const char *match_criteria,
323                                       const char *fname,
324                                       int force_update,
325                                       int allow_update);
326
327 ZEBRA_RES zebra_extract_record_stream(ZebraHandle zh, 
328                                       struct ZebraRecStream *stream,
329                                       int delete_flag,
330                                       int test_mode, 
331                                       const char *recordType,
332                                       zint *sysno,
333                                       const char *match_criteria,
334                                       const char *fname,
335                                       int force_update,
336                                       int allow_update,
337                                       RecType recType,
338                                       void *recTypeClientData,
339                                       int *more);
340
341 YAZ_EXPORT void zebra_create_stream_mem(struct ZebraRecStream *stream,
342                                         const char *buf, size_t sz);
343 YAZ_EXPORT void zebra_create_stream_fd(struct ZebraRecStream *stream,
344                                        int fd, off_t start_offset);
345 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys);
346
347 ZEBRA_RES zebra_rec_keys_to_snippets(ZebraHandle zh, zebra_rec_keys_t reckeys,
348                                      zebra_snippets *snippets);
349 ZEBRA_RES zebra_snippets_hit_vector(ZebraHandle zh, const char *setname,
350                                     zint sysno, zebra_snippets *snippets);
351
352 ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n);
353
354 ZEBRA_RES zebra_extract_file(ZebraHandle zh, zint *sysno, const char *fname,
355                              int deleteFlag);
356
357 ZEBRA_RES zebra_begin_read(ZebraHandle zh);
358 ZEBRA_RES zebra_end_read(ZebraHandle zh);
359
360 int zebra_file_stat(const char *file_name, struct stat *buf,
361                      int follow_links);
362
363 Dict dict_open_res(BFiles bfs, const char *name, int cache, int rw,
364                    int compact_flag, Res res);
365
366 void zebra_setError(ZebraHandle zh, int code, const char *addinfo);
367 void zebra_setError_zint(ZebraHandle zh, int code, zint i);
368
369 void zebra_term_untrans_iconv(ZebraHandle zh, NMEM stream, int reg_type,
370                               char **dst, const char *src);
371
372 ZEBRA_RES zebra_get_hit_vector(ZebraHandle zh, const char *setname, zint sysno);
373
374 void zebra_term_untrans(ZebraHandle zh, int reg_type,
375                         char *dst, const char *src);
376
377 ZEBRA_RES zebra_apt_get_ord(ZebraHandle zh,
378                             Z_AttributesPlusTerm *zapt,
379                             int index_type,
380                             const char *xpath_use,
381                             oid_value curAttributeSet,
382                             int *ord);
383
384 ZEBRA_RES zebra_attr_list_get_ord(ZebraHandle zh,
385                                   Z_AttributeList *attr_list,
386                                   zinfo_index_category_t cat,
387                                   int index_type,
388                                   oid_value curAttributeSet,
389                                   int *ord);
390
391 ZEBRA_RES zebra_sort_get_ord(ZebraHandle zh,
392                              Z_SortAttributes *sortAttributes,
393                              int *ord,
394                              int *numerical);
395
396 ZEBRA_RES zebra_update_file_match(ZebraHandle zh, const char *path);
397 ZEBRA_RES zebra_update_from_path(ZebraHandle zh, const char *path);
398 ZEBRA_RES zebra_delete_from_path(ZebraHandle zh, const char *path);
399 ZEBRA_RES zebra_remove_file_match(ZebraHandle zh);
400
401 struct rpn_char_map_info
402 {
403     ZebraMaps zm;
404     int reg_type;
405 };
406
407 void rpn_char_map_prepare(struct zebra_register *reg, int reg_type,
408                           struct rpn_char_map_info *map_info);
409
410 ZEBRA_RES zapt_term_to_utf8(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
411                             char *termz);
412
413
414 int zebra_check_res(Res res);
415
416 #define FIRST_IN_FIELD_STR "\001^"
417 #define FIRST_IN_FIELD_CHAR 1
418 #define FIRST_IN_FIELD_LEN 2
419
420 YAZ_END_CDECL
421
422 #endif
423 /*
424  * Local variables:
425  * c-basic-offset: 4
426  * indent-tabs-mode: nil
427  * End:
428  * vim: shiftwidth=4 tabstop=8 expandtab
429  */
430