32ded3e7eae320a20ad111f40f3794a007721200
[idzebra-moved-to-github.git] / index / index.h
1 /* $Id: index.h,v 1.196 2007-03-14 11:48:32 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     char *errString;
202     int partial_result;
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     zint records_skipped;
217     char *record_encoding;
218
219     yaz_iconv_t iconv_to_utf8;
220     yaz_iconv_t iconv_from_utf8;
221
222     int m_follow_links;
223     const char *m_group;
224     const char *m_record_id;
225     const char *m_record_type;
226     int m_store_data;
227     int m_store_keys;
228     int m_explain_database;
229     int m_flag_rw;
230     int m_file_verbose_limit;
231
232     void *store_data_buf;
233     size_t store_data_size;
234     NMEM nmem_error;
235
236     struct zebra_limit *m_limit;
237
238     int (*break_handler_func)(void *client_data);
239     void *break_handler_data;
240 };
241
242
243 struct term_set_entry {
244     char *term;
245     struct term_set_entry *next;
246 };
247
248 struct term_set_list {
249     struct term_set_entry *first;
250     struct term_set_entry *last;
251 };
252
253
254 void zebra_limit_destroy(struct zebra_limit *zl);
255 struct zebra_limit *zebra_limit_create(int exclude_flag, zint *ids);
256 void zebra_limit_for_rset(struct zebra_limit *zl,
257                           int (**filter_func)(const void *buf, void *data),
258                           void (**filter_destroy)(void *data),
259                           void **filter_data);
260
261 struct rset_key_control *zebra_key_control_create(ZebraHandle zh);
262
263 ZEBRA_RES rpn_search_top(ZebraHandle zh, Z_RPNStructure *zs,
264                          oid_value attributeSet, 
265                          NMEM stream, NMEM rset_nmem,
266                          Z_SortKeySpecList *sort_sequence,
267                          int num_bases, char **basenames,
268                          RSET *result_set);
269
270 ZEBRA_RES rpn_get_top_approx_limit(ZebraHandle zh, Z_RPNStructure *zs,
271                                    zint *approx_limit);
272
273 ZEBRA_RES rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
274                    oid_value attributeset,
275                    int num_bases, char **basenames,
276                    int *position, int *num_entries, ZebraScanEntry **list,
277                    int *is_partial, RSET limit_set);
278
279 RSET rset_trunc(ZebraHandle zh, ISAM_P *isam_p, int no,
280                 const char *term, int length_term, const char *flags,
281                 int preserve_position, int term_type, NMEM rset_nmem,
282                 struct rset_key_control *kctrl, int scope,
283                 struct ord_list *ol, int reg_type,
284                 zint hits_limit, const char *term_ref_id);
285
286 void resultSetAddTerm(ZebraHandle zh, ZebraSet s, int reg_type,
287                       const char *db, const char *index_name,
288                       const char *term);
289 ZebraSet resultSetAdd(ZebraHandle zh, const char *name, int ov);
290 ZebraSet resultSetGet(ZebraHandle zh, const char *name);
291 ZEBRA_RES resultSetAddRPN(ZebraHandle zh, NMEM m, Z_RPNQuery *rpn,
292                           int num_bases, char **basenames,
293                           const char *setname,
294                           zint *hits, int *estimated_hit_count);
295 RSET resultSetRef(ZebraHandle zh, const char *resultSetId);
296 void resultSetDestroy(ZebraHandle zh, int num_names, char **names,
297                        int *statuses);
298 ZEBRA_RES resultSetSort(ZebraHandle zh, NMEM nmem,
299                          int num_input_setnames, const char **input_setnames,
300                          const char *output_setname,
301                          Z_SortKeySpecList *sort_sequence, int *sort_status);
302 ZEBRA_RES resultSetSortSingle(ZebraHandle zh, NMEM nmem,
303                                ZebraSet sset, RSET rset,
304                                Z_SortKeySpecList *sort_sequence,
305                                int *sort_status);
306 ZEBRA_RES resultSetRank(ZebraHandle zh, ZebraSet zebraSet, RSET rset,
307                          NMEM nmem);
308 void resultSetInvalidate(ZebraHandle zh);
309
310 int zebra_record_fetch(ZebraHandle zh, zint sysno, int score, 
311                         zebra_snippets *hit_snippet, ODR stream,
312                         oid_value input_format, Z_RecordComposition *comp,
313                         oid_value *output_format, char **rec_bufp,
314                         int *rec_lenp, char **basenamep,
315                         char **addinfo);
316
317 void extract_get_fname_tmp(ZebraHandle zh, char *fname, int no);
318
319 void zebra_index_merge(ZebraHandle zh);
320
321 ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh, 
322                                       const char *buf, size_t buf_size,
323                                       enum zebra_recctrl_action_t action,
324                                       int test_mode, 
325                                       const char *recordType,
326                                       zint *sysno,
327                                       const char *match_criteria,
328                                       const char *fname);
329
330 ZEBRA_RES zebra_extract_record_stream(ZebraHandle zh, 
331                                       struct ZebraRecStream *stream,
332                                       enum zebra_recctrl_action_t action,
333                                       int test_mode, 
334                                       const char *recordType,
335                                       zint *sysno,
336                                       const char *match_criteria,
337                                       const char *fname,
338                                       RecType recType,
339                                       void *recTypeClientData,
340                                       int *more);
341
342 YAZ_EXPORT void zebra_create_stream_mem(struct ZebraRecStream *stream,
343                                         const char *buf, size_t sz);
344 YAZ_EXPORT void zebra_create_stream_fd(struct ZebraRecStream *stream,
345                                        int fd, off_t start_offset);
346 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys);
347
348 ZEBRA_RES zebra_rec_keys_to_snippets(ZebraHandle zh, zebra_rec_keys_t reckeys,
349                                      zebra_snippets *snippets);
350 ZEBRA_RES zebra_snippets_hit_vector(ZebraHandle zh, const char *setname,
351                                     zint sysno, zebra_snippets *snippets);
352
353 ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n);
354
355 ZEBRA_RES zebra_extract_file(ZebraHandle zh, zint *sysno, const char *fname,
356                              int deleteFlag);
357
358 ZEBRA_RES zebra_begin_read(ZebraHandle zh);
359 ZEBRA_RES zebra_end_read(ZebraHandle zh);
360
361 int zebra_file_stat(const char *file_name, struct stat *buf,
362                      int follow_links);
363
364 Dict dict_open_res(BFiles bfs, const char *name, int cache, int rw,
365                    int compact_flag, Res res);
366
367 void zebra_setError(ZebraHandle zh, int code, const char *addinfo);
368 void zebra_setError_zint(ZebraHandle zh, int code, zint i);
369
370 void zebra_term_untrans_iconv(ZebraHandle zh, NMEM stream, int reg_type,
371                               char **dst, const char *src);
372
373 ZEBRA_RES zebra_get_hit_vector(ZebraHandle zh, const char *setname, zint sysno);
374
375 void zebra_term_untrans(ZebraHandle zh, int reg_type,
376                         char *dst, const char *src);
377
378 ZEBRA_RES zebra_apt_get_ord(ZebraHandle zh,
379                             Z_AttributesPlusTerm *zapt,
380                             int index_type,
381                             const char *xpath_use,
382                             oid_value curAttributeSet,
383                             int *ord);
384
385 ZEBRA_RES zebra_attr_list_get_ord(ZebraHandle zh,
386                                   Z_AttributeList *attr_list,
387                                   zinfo_index_category_t cat,
388                                   int index_type,
389                                   oid_value curAttributeSet,
390                                   int *ord);
391
392 ZEBRA_RES zebra_sort_get_ord(ZebraHandle zh,
393                              Z_SortAttributes *sortAttributes,
394                              int *ord,
395                              int *numerical);
396
397 ZEBRA_RES zebra_update_file_match(ZebraHandle zh, const char *path);
398 ZEBRA_RES zebra_update_from_path(ZebraHandle zh, const char *path);
399 ZEBRA_RES zebra_delete_from_path(ZebraHandle zh, const char *path);
400 ZEBRA_RES zebra_remove_file_match(ZebraHandle zh);
401
402 struct rpn_char_map_info
403 {
404     ZebraMaps zm;
405     int reg_type;
406 };
407
408 void rpn_char_map_prepare(struct zebra_register *reg, int reg_type,
409                           struct rpn_char_map_info *map_info);
410
411 ZEBRA_RES zapt_term_to_utf8(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
412                             char *termz);
413
414
415 void zebra_set_partial_result(ZebraHandle zh);
416
417 int zebra_check_res(Res res);
418
419 #define FIRST_IN_FIELD_STR "\001^"
420 #define FIRST_IN_FIELD_CHAR 1
421 #define FIRST_IN_FIELD_LEN 2
422
423 YAZ_END_CDECL
424
425 #endif
426 /*
427  * Local variables:
428  * c-basic-offset: 4
429  * indent-tabs-mode: nil
430  * End:
431  * vim: shiftwidth=4 tabstop=8 expandtab
432  */
433