For estimated hit counts, Zebra returns resultSetStatus=estimtate as
[idzebra-moved-to-github.git] / index / index.h
1 /* $Id: index.h,v 1.191 2007-01-16 15:01:15 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 #if HAVE_SYS_TIMES_H
203     struct tms tms1;
204     struct tms tms2;    
205 #endif
206     int  shadow_enable;
207
208     int m_staticrank;
209     int m_segment_indexing;
210
211     zint records_inserted;
212     zint records_updated;
213     zint records_deleted;
214     zint records_processed;
215     char *record_encoding;
216
217     yaz_iconv_t iconv_to_utf8;
218     yaz_iconv_t iconv_from_utf8;
219
220     int m_follow_links;
221     const char *m_group;
222     const char *m_record_id;
223     const char *m_record_type;
224     int m_store_data;
225     int m_store_keys;
226     int m_explain_database;
227     int m_flag_rw;
228     int m_file_verbose_limit;
229
230     void *store_data_buf;
231     size_t store_data_size;
232     NMEM nmem_error;
233
234     struct zebra_limit *m_limit;
235 };
236
237
238 struct term_set_entry {
239     char *term;
240     struct term_set_entry *next;
241 };
242
243 struct term_set_list {
244     struct term_set_entry *first;
245     struct term_set_entry *last;
246 };
247
248
249 void zebra_limit_destroy(struct zebra_limit *zl);
250 struct zebra_limit *zebra_limit_create(int exclude_flag, zint *ids);
251 void zebra_limit_for_rset(struct zebra_limit *zl,
252                           int (**filter_func)(const void *buf, void *data),
253                           void (**filter_destroy)(void *data),
254                           void **filter_data);
255
256 struct rset_key_control *zebra_key_control_create(ZebraHandle zh);
257
258 ZEBRA_RES rpn_search_top(ZebraHandle zh, Z_RPNStructure *zs,
259                          oid_value attributeSet, 
260                          NMEM stream, NMEM rset_nmem,
261                          Z_SortKeySpecList *sort_sequence,
262                          int num_bases, char **basenames,
263                          RSET *result_set);
264
265 ZEBRA_RES rpn_get_top_approx_limit(ZebraHandle zh, Z_RPNStructure *zs,
266                                    zint *approx_limit);
267
268 ZEBRA_RES rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
269                    oid_value attributeset,
270                    int num_bases, char **basenames,
271                    int *position, int *num_entries, ZebraScanEntry **list,
272                    int *is_partial, RSET limit_set);
273
274 RSET rset_trunc(ZebraHandle zh, ISAM_P *isam_p, int no,
275                 const char *term, int length_term, const char *flags,
276                 int preserve_position, int term_type, NMEM rset_nmem,
277                 struct rset_key_control *kctrl, int scope,
278                 struct ord_list *ol, int reg_type,
279                 zint hits_limit, const char *term_ref_id);
280
281 void resultSetAddTerm(ZebraHandle zh, ZebraSet s, int reg_type,
282                       const char *db, const char *index_name,
283                       const char *term);
284 ZebraSet resultSetAdd(ZebraHandle zh, const char *name, int ov);
285 ZebraSet resultSetGet(ZebraHandle zh, const char *name);
286 ZEBRA_RES resultSetAddRPN(ZebraHandle zh, NMEM m, Z_RPNQuery *rpn,
287                           int num_bases, char **basenames,
288                           const char *setname,
289                           zint *hits, int *estimated_hit_count,
290                           int *partial_resultset);
291 RSET resultSetRef(ZebraHandle zh, const char *resultSetId);
292 void resultSetDestroy(ZebraHandle zh, int num_names, char **names,
293                        int *statuses);
294 ZEBRA_RES resultSetSort(ZebraHandle zh, NMEM nmem,
295                          int num_input_setnames, const char **input_setnames,
296                          const char *output_setname,
297                          Z_SortKeySpecList *sort_sequence, int *sort_status);
298 ZEBRA_RES resultSetSortSingle(ZebraHandle zh, NMEM nmem,
299                                ZebraSet sset, RSET rset,
300                                Z_SortKeySpecList *sort_sequence,
301                                int *sort_status);
302 ZEBRA_RES resultSetRank(ZebraHandle zh, ZebraSet zebraSet, RSET rset,
303                          NMEM nmem);
304 void resultSetInvalidate(ZebraHandle zh);
305
306 int zebra_record_fetch(ZebraHandle zh, zint sysno, int score, 
307                         zebra_snippets *hit_snippet, ODR stream,
308                         oid_value input_format, Z_RecordComposition *comp,
309                         oid_value *output_format, char **rec_bufp,
310                         int *rec_lenp, char **basenamep,
311                         char **addinfo);
312
313 void extract_get_fname_tmp(ZebraHandle zh, char *fname, int no);
314
315 void zebra_index_merge(ZebraHandle zh);
316
317 ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh, 
318                                       const char *buf, size_t buf_size,
319                                       int delete_flag,
320                                       int test_mode, 
321                                       const char *recordType,
322                                       zint *sysno,
323                                       const char *match_criteria,
324                                       const char *fname,
325                                       int force_update,
326                                       int allow_update);
327
328 ZEBRA_RES zebra_extract_record_stream(ZebraHandle zh, 
329                                       struct ZebraRecStream *stream,
330                                       int delete_flag,
331                                       int test_mode, 
332                                       const char *recordType,
333                                       zint *sysno,
334                                       const char *match_criteria,
335                                       const char *fname,
336                                       int force_update,
337                                       int allow_update,
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 int zebra_check_res(Res res);
416
417 #define FIRST_IN_FIELD_STR "\001^"
418 #define FIRST_IN_FIELD_CHAR 1
419 #define FIRST_IN_FIELD_LEN 2
420
421 YAZ_END_CDECL
422
423 #endif
424 /*
425  * Local variables:
426  * c-basic-offset: 4
427  * indent-tabs-mode: nil
428  * End:
429  * vim: shiftwidth=4 tabstop=8 expandtab
430  */
431