Add timing for zebra_start/zebra_stop.
[idzebra-moved-to-github.git] / index / index.h
1 /* $Id: index.h,v 1.188 2006-12-22 12:14:25 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 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 <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 #include <it_key.h>
52 #include <su_codec.h>
53
54 YAZ_BEGIN_CDECL
55
56 #define ISAM_DEFAULT "b"
57
58 enum dirsKind { dirs_dir, dirs_file };
59
60 struct dir_entry {
61     enum dirsKind kind;
62     char *name;
63     time_t mtime;
64 };
65
66 struct dirs_entry {
67     enum dirsKind kind;
68     char path[256];
69     zint sysno;
70     time_t mtime;
71 };
72
73 void getFnameTmp(Res res, char *fname, int no);
74         
75 struct dirs_info *dirs_open(Dict dict, const char *rep, int rw);
76 struct dirs_info *dirs_fopen(Dict dict, const char *path, int rw);
77 struct dirs_entry *dirs_read(struct dirs_info *p);
78 struct dirs_entry *dirs_last(struct dirs_info *p);
79 void dirs_mkdir(struct dirs_info *p, const char *src, time_t mtime);
80 void dirs_rmdir(struct dirs_info *p, const char *src);
81 void dirs_add(struct dirs_info *p, const char *src, zint sysno, time_t mtime);
82 void dirs_del(struct dirs_info *p, const char *src);
83 void dirs_free(struct dirs_info **pp);
84
85 struct dir_entry *dir_open(const char *rep, const char *base,
86                            int follow_links);
87 void dir_sort(struct dir_entry *e);
88 void dir_free(struct dir_entry **e_p);
89
90 void repositoryUpdate(ZebraHandle zh, const char *path);
91 void repositoryAdd(ZebraHandle zh, const char *path);
92 void repositoryDelete(ZebraHandle zh, const char *path);
93 void repositoryShow(ZebraHandle zh, const char *path);
94
95 void inv_prstat(ZebraHandle zh);
96 void inv_compact(BFiles bfs);
97 void key_input(ZebraHandle zh, int nkeys, int cache, Res res);
98 ISAMS_M *key_isams_m(Res res, ISAMS_M *me);
99 ISAMC_M *key_isamc_m(Res res, ISAMC_M *me);
100
101 #define FNAME_DICT "dict"
102 #define FNAME_ISAM "isam"
103 #define FNAME_ISAMC "isamc"
104 #define FNAME_ISAMS "isams"
105 #define FNAME_CONFIG "zebra.cfg"
106
107 #define GMATCH_DICT "gmatch"
108 #define FMATCH_DICT "fmatch%d"
109
110 void zebra_lock_prefix(Res res, char *dst);
111
112 #define FNAME_MAIN_LOCK   "zebraidx.LCK"
113 #define FNAME_COMMIT_LOCK "zebracmt.LCK"
114 #define FNAME_ORG_LOCK    "zebraorg.LCK"
115 #define FNAME_TOUCH_TIME  "zebraidx.time"
116
117 typedef struct zebra_set *ZebraSet;
118
119 typedef struct zebra_rank_class {
120     struct rank_control *control;
121     int init_flag;
122     void *class_handle;
123     struct zebra_rank_class *next;
124 } *ZebraRankClass;
125
126 #include "reckeys.h"
127 #include "key_block.h"
128
129 struct zebra_register {
130     char *name;
131     
132     ISAMS isams;
133     ISAMC isamc;
134     ISAMB isamb;
135     Dict dict;
136     Dict matchDict;
137     zebra_sort_index_t sort_index;
138     int registerState; /* 0 (no commit pages), 1 (use commit pages) */
139     time_t registerChange;
140     BFiles bfs;
141     Records records;
142     ZebraExplainInfo zei;
143
144     char *server_path_prefix;
145     data1_handle dh;
146     ZebraMaps zebra_maps;
147     ZebraRankClass rank_classes;
148     RecTypes recTypes;
149     int seqno;
150     int last_val;
151     int stop_flag;
152
153     zebra_rec_keys_t keys;
154     zebra_rec_keys_t sortKeys;
155
156     zebra_key_block_t key_block;
157 };
158
159 struct zebra_service {
160     int stop_flag;
161     Res global_res;
162     struct zebra_session *sessions;
163     struct zebra_register *regs;
164     Zebra_mutex_cond session_lock;
165     Passwd_db passwd_db;
166     Res dbaccess;
167     const char *path_root;
168     RecTypeClass record_classes;
169     NMEM nmem;
170     zebra_timing_t timing;
171 };
172
173
174 struct zebra_session {
175     struct zebra_session *next;
176     struct zebra_service *service;
177     struct zebra_register *reg;
178
179     char *xadmin_databaseName;
180
181     char **basenames;
182     int num_basenames;
183     zint approx_limit;
184     char *reg_name;
185     char *path_reg;
186
187     ZebraLockHandle lock_normal;
188     ZebraLockHandle lock_shadow;
189
190     int trans_no;
191     int trans_w_no;
192
193     int destroyed;
194     ZebraSet sets;
195     Res res;
196     Res session_res;
197     char *user_perm;
198     char *dbaccesslist;
199     int errCode;
200     zint hits;
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 RSET resultSetRef(ZebraHandle zh, const char *resultSetId);
290 void resultSetDestroy(ZebraHandle zh, int num_names, char **names,
291                        int *statuses);
292 ZEBRA_RES resultSetSort(ZebraHandle zh, NMEM nmem,
293                          int num_input_setnames, const char **input_setnames,
294                          const char *output_setname,
295                          Z_SortKeySpecList *sort_sequence, int *sort_status);
296 ZEBRA_RES resultSetSortSingle(ZebraHandle zh, NMEM nmem,
297                                ZebraSet sset, RSET rset,
298                                Z_SortKeySpecList *sort_sequence,
299                                int *sort_status);
300 ZEBRA_RES resultSetRank(ZebraHandle zh, ZebraSet zebraSet, RSET rset,
301                          NMEM nmem);
302 void resultSetInvalidate(ZebraHandle zh);
303
304 int zebra_record_fetch(ZebraHandle zh, zint sysno, int score, 
305                         zebra_snippets *hit_snippet, ODR stream,
306                         oid_value input_format, Z_RecordComposition *comp,
307                         oid_value *output_format, char **rec_bufp,
308                         int *rec_lenp, char **basenamep,
309                         char **addinfo);
310
311 void extract_get_fname_tmp(ZebraHandle zh, char *fname, int no);
312
313 void zebra_index_merge(ZebraHandle zh);
314
315 ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh, 
316                                       const char *buf, size_t buf_size,
317                                       int delete_flag,
318                                       int test_mode, 
319                                       const char *recordType,
320                                       zint *sysno,
321                                       const char *match_criteria,
322                                       const char *fname,
323                                       int force_update,
324                                       int allow_update);
325
326 ZEBRA_RES zebra_extract_record_stream(ZebraHandle zh, 
327                                       struct ZebraRecStream *stream,
328                                       int delete_flag,
329                                       int test_mode, 
330                                       const char *recordType,
331                                       zint *sysno,
332                                       const char *match_criteria,
333                                       const char *fname,
334                                       int force_update,
335                                       int allow_update,
336                                       RecType recType,
337                                       void *recTypeClientData,
338                                       int *more);
339
340 YAZ_EXPORT void zebra_create_stream_mem(struct ZebraRecStream *stream,
341                                         const char *buf, size_t sz);
342 YAZ_EXPORT void zebra_create_stream_fd(struct ZebraRecStream *stream,
343                                        int fd, off_t start_offset);
344 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys);
345
346 ZEBRA_RES zebra_rec_keys_to_snippets(ZebraHandle zh, zebra_rec_keys_t reckeys,
347                                      zebra_snippets *snippets);
348 ZEBRA_RES zebra_snippets_hit_vector(ZebraHandle zh, const char *setname,
349                                     zint sysno, zebra_snippets *snippets);
350
351 ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n);
352
353 ZEBRA_RES zebra_extract_file(ZebraHandle zh, zint *sysno, const char *fname,
354                              int deleteFlag);
355
356 ZEBRA_RES zebra_begin_read(ZebraHandle zh);
357 ZEBRA_RES zebra_end_read(ZebraHandle zh);
358
359 int zebra_file_stat(const char *file_name, struct stat *buf,
360                      int follow_links);
361
362 Dict dict_open_res(BFiles bfs, const char *name, int cache, int rw,
363                    int compact_flag, Res res);
364
365 void zebra_setError(ZebraHandle zh, int code, const char *addinfo);
366 void zebra_setError_zint(ZebraHandle zh, int code, zint i);
367
368 void zebra_term_untrans_iconv(ZebraHandle zh, NMEM stream, int reg_type,
369                               char **dst, const char *src);
370
371 ZEBRA_RES zebra_get_hit_vector(ZebraHandle zh, const char *setname, zint sysno);
372
373 void zebra_term_untrans(ZebraHandle zh, int reg_type,
374                         char *dst, const char *src);
375
376 ZEBRA_RES zebra_apt_get_ord(ZebraHandle zh,
377                             Z_AttributesPlusTerm *zapt,
378                             int index_type,
379                             const char *xpath_use,
380                             oid_value curAttributeSet,
381                             int *ord);
382
383 ZEBRA_RES zebra_attr_list_get_ord(ZebraHandle zh,
384                                   Z_AttributeList *attr_list,
385                                   zinfo_index_category_t cat,
386                                   int index_type,
387                                   oid_value curAttributeSet,
388                                   int *ord);
389
390 ZEBRA_RES zebra_sort_get_ord(ZebraHandle zh,
391                              Z_SortAttributes *sortAttributes,
392                              int *ord,
393                              int *numerical);
394
395 ZEBRA_RES zebra_update_file_match(ZebraHandle zh, const char *path);
396 ZEBRA_RES zebra_update_from_path(ZebraHandle zh, const char *path);
397 ZEBRA_RES zebra_delete_from_path(ZebraHandle zh, const char *path);
398 ZEBRA_RES zebra_remove_file_match(ZebraHandle zh);
399
400 struct rpn_char_map_info
401 {
402     ZebraMaps zm;
403     int reg_type;
404 };
405
406 void rpn_char_map_prepare(struct zebra_register *reg, int reg_type,
407                           struct rpn_char_map_info *map_info);
408
409 ZEBRA_RES zapt_term_to_utf8(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
410                             char *termz);
411
412
413 int zebra_check_res(Res res);
414
415 #define FIRST_IN_FIELD_STR "\001^"
416 #define FIRST_IN_FIELD_CHAR 1
417 #define FIRST_IN_FIELD_LEN 2
418
419 YAZ_END_CDECL
420
421 #endif
422 /*
423  * Local variables:
424  * c-basic-offset: 4
425  * indent-tabs-mode: nil
426  * End:
427  * vim: shiftwidth=4 tabstop=8 expandtab
428  */
429