Renamed fileExtract to zebra_extract_file.. The new function returns
[idzebra-moved-to-github.git] / index / index.h
1 /* $Id: index.h,v 1.158 2006-04-05 02:11:44 adam Exp $
2    Copyright (C) 1995-2005
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 Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
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
50 YAZ_BEGIN_CDECL
51
52 #define ISAM_DEFAULT "b"
53
54 #define SU_SCHEME 1
55
56 #define IT_MAX_WORD 256
57
58 #define IT_KEY_LEVEL_MAX 5
59 struct it_key {
60     int  len;
61     zint mem[IT_KEY_LEVEL_MAX];
62 };
63
64 enum dirsKind { dirs_dir, dirs_file };
65
66 struct dir_entry {
67     enum dirsKind kind;
68     char *name;
69     time_t mtime;
70 };
71
72 struct dirs_entry {
73     enum dirsKind kind;
74     char path[256];
75     SYSNO sysno;
76     time_t mtime;
77 };
78
79 void getFnameTmp (Res res, char *fname, int no);
80         
81 struct dirs_info *dirs_open (Dict dict, const char *rep, int rw);
82 struct dirs_info *dirs_fopen (Dict dict, const char *path, int rw);
83 struct dirs_entry *dirs_read (struct dirs_info *p);
84 struct dirs_entry *dirs_last (struct dirs_info *p);
85 void dirs_mkdir (struct dirs_info *p, const char *src, time_t mtime);
86 void dirs_rmdir (struct dirs_info *p, const char *src);
87 void dirs_add (struct dirs_info *p, const char *src, SYSNO sysno, time_t mtime);
88 void dirs_del (struct dirs_info *p, const char *src);
89 void dirs_free (struct dirs_info **pp);
90
91 struct dir_entry *dir_open (const char *rep, const char *base,
92                             int follow_links);
93 void dir_sort (struct dir_entry *e);
94 void dir_free (struct dir_entry **e_p);
95
96 void repositoryUpdate (ZebraHandle zh, const char *path);
97 void repositoryAdd (ZebraHandle zh, const char *path);
98 void repositoryDelete (ZebraHandle zh, const char *path);
99 void repositoryShow (ZebraHandle zh, const char *path);
100
101 int key_open (ZebraHandle zh, int mem);
102 int key_close (ZebraHandle zh);
103 int key_compare (const void *p1, const void *p2);
104 void key_init(struct it_key *k);
105 char *key_print_it (const void *p, char *buf);
106 zint key_get_seq (const void *p);
107 int key_compare_it (const void *p1, const void *p2);
108 int key_qsort_compare (const void *p1, const void *p2);
109 void key_logdump (int mask, const void *p);
110 void key_logdump_txt (int logmask, const void *p, const char *txt);
111 void inv_prstat (ZebraHandle zh);
112 void inv_compact (BFiles bfs);
113 void key_input (ZebraHandle zh, int nkeys, int cache, Res res);
114 ISAMS_M *key_isams_m (Res res, ISAMS_M *me);
115 ISAMC_M *key_isamc_m (Res res, ISAMC_M *me);
116 int merge_sort (char **buf, int from, int to);
117 int key_SU_code (int ch, char *out);
118
119 #define FNAME_DICT "dict"
120 #define FNAME_ISAM "isam"
121 #define FNAME_ISAMC "isamc"
122 #define FNAME_ISAMS "isams"
123 #define FNAME_ISAMH "isamh"
124 #define FNAME_ISAMD "isamd"
125 #define FNAME_CONFIG "zebra.cfg"
126
127 #define GMATCH_DICT "gmatch"
128 #define FMATCH_DICT "fmatch"
129
130 struct strtab *strtab_mk (void);
131 int strtab_src (struct strtab *t, const char *name, void ***infop);
132 void strtab_del (struct strtab *t,
133                  void (*func)(const char *name, void *info, void *data),
134                  void *data);
135 int index_char_cvt (int c);
136 int index_word_prefix (char *string, int attset_ordinal,
137                        int local_attribute, const char *databaseName);
138
139
140 void zebraIndexLockMsg (ZebraHandle zh, const char *str);
141 void zebraIndexUnlock (ZebraHandle zh);
142 int zebraIndexLock (BFiles bfs, ZebraHandle zh, int commitNow, const char *rval);
143 int zebraIndexWait (ZebraHandle zh, int commitPhase);
144
145 void zebra_lock_prefix (Res res, char *dst);
146
147 #define FNAME_MAIN_LOCK   "zebraidx.LCK"
148 #define FNAME_COMMIT_LOCK "zebracmt.LCK"
149 #define FNAME_ORG_LOCK    "zebraorg.LCK"
150 #define FNAME_TOUCH_TIME  "zebraidx.time"
151
152 void zebra_load_atts (data1_handle dh, Res res);
153
154 int key_SU_decode (int *ch, const unsigned char *out);
155 int key_SU_encode (int ch, char *out);
156
157 #define ENCODE_BUFLEN 768
158 struct encode_info {
159     int  sysno;  /* previously written values for delta-compress */
160     int  seqno;
161     int  cmd;
162     int prevsys; /* buffer for skipping insert/delete pairs */
163     int prevseq;
164     int prevcmd;
165     int keylen; /* tells if we have an unwritten key in buf, and how long*/
166     void *encode_handle;
167     void *decode_handle;
168     char buf[ENCODE_BUFLEN];
169 };
170
171 void encode_key_init (struct encode_info *i);
172 char *encode_key_int (int d, char *bp);
173 void encode_key_write (char *k, struct encode_info *i, FILE *outf);
174 void encode_key_flush (struct encode_info *i, FILE *outf);
175
176 typedef struct zebra_set *ZebraSet;
177
178 typedef struct zebra_rank_class {
179     struct rank_control *control;
180     int init_flag;
181     void *class_handle;
182     struct zebra_rank_class *next;
183 } *ZebraRankClass;
184
185 #include "reckeys.h"
186
187 #if NATTR
188
189 #else
190 struct sortKeys {
191     int buf_used;
192     int buf_max;
193     char *buf;
194 };
195 #endif
196
197 struct zebra_register {
198     char *name;
199     
200     ISAMS isams;
201     ISAMC isamc;
202     ISAMB isamb;
203     Dict dict;
204     Dict matchDict;
205     SortIdx sortIdx;
206     int registerState; /* 0 (no commit pages), 1 (use commit pages) */
207     time_t registerChange;
208     BFiles bfs;
209     Records records;
210     ZebraExplainInfo zei;
211
212     char *server_path_prefix;
213     data1_handle dh;
214     ZebraMaps zebra_maps;
215     ZebraRankClass rank_classes;
216     RecTypes recTypes;
217     int seqno;
218     int last_val;
219     int stop_flag;
220
221     zebra_rec_keys_t keys;
222
223 #if NATTR
224     zebra_rec_keys_t sortKeys;
225 #else
226     struct sortKeys sortKeys;
227 #endif
228     char **key_buf;
229     size_t ptr_top;
230     size_t ptr_i;
231     size_t key_buf_used;
232     int key_file_no;
233 };
234
235 struct zebra_service {
236     int stop_flag;
237     Res global_res;
238     struct zebra_session *sessions;
239     struct zebra_register *regs;
240     Zebra_mutex_cond session_lock;
241     Passwd_db passwd_db;
242     Res dbaccess;
243     const char *path_root;
244     RecTypeClass record_classes;
245     NMEM nmem;
246 };
247
248
249 struct zebra_session {
250     struct zebra_session *next;
251     struct zebra_service *service;
252     struct zebra_register *reg;
253
254     char *xadmin_databaseName;
255
256     char **basenames;
257     int num_basenames;
258     zint approx_limit;
259     char *reg_name;
260     char *path_reg;
261
262     ZebraLockHandle lock_normal;
263     ZebraLockHandle lock_shadow;
264
265     int trans_no;
266     int trans_w_no;
267
268     int destroyed;
269     ZebraSet sets;
270     Res res;
271     Res session_res;
272     char *user_perm;
273     char *dbaccesslist;
274     int errCode;
275     zint hits;
276     char *errString;
277 #if HAVE_SYS_TIMES_H
278     struct tms tms1;
279     struct tms tms2;    
280 #endif
281     int  shadow_enable;
282
283     int m_staticrank;
284
285     zint records_inserted;
286     zint records_updated;
287     zint records_deleted;
288     zint records_processed;
289     char *record_encoding;
290
291     yaz_iconv_t iconv_to_utf8;
292     yaz_iconv_t iconv_from_utf8;
293
294     int m_follow_links;
295     const char *m_group;
296     const char *m_record_id;
297     const char *m_record_type;
298     int m_store_data;
299     int m_store_keys;
300     int m_explain_database;
301     int m_flag_rw;
302     int m_file_verbose_limit;
303
304     void *store_data_buf;
305     size_t store_data_size;
306     NMEM nmem_error;
307
308     struct zebra_limit *m_limit;
309 };
310
311 struct rank_control {
312     char *name;
313     void *(*create)(ZebraHandle zh);
314     void (*destroy)(struct zebra_register *reg, void *class_handle);
315     void *(*begin)(struct zebra_register *reg, 
316                    void *class_handle, RSET rset, NMEM nmem,
317                    TERMID *terms, int numterms);
318     /* ### Could add parameters to begin:
319      *  char *index;    // author, title, etc.
320      *  int dbsize;     // number of records in database
321      *  int rssize;     // number of records in result set (estimate?)
322      */
323     void (*end)(struct zebra_register *reg, void *set_handle);
324     int (*calc)(void *set_handle, zint sysno, zint staticrank,
325                 int *stop_flag);
326     void (*add)(void *set_handle, int seqno, TERMID term);
327 };
328
329 struct term_set_entry {
330     char *term;
331     struct term_set_entry *next;
332 };
333
334 struct term_set_list {
335     struct term_set_entry *first;
336     struct term_set_entry *last;
337 };
338
339
340 void zebra_limit_destroy(struct zebra_limit *zl);
341 struct zebra_limit *zebra_limit_create(int exclude_flag, zint *ids);
342 void zebra_limit_for_rset(struct zebra_limit *zl,
343                           int (**filter_func)(const void *buf, void *data),
344                           void (**filter_destroy)(void *data),
345                           void **filter_data);
346
347 struct rset_key_control *zebra_key_control_create(ZebraHandle zh);
348
349 ZEBRA_RES rpn_search_top(ZebraHandle zh, Z_RPNStructure *zs,
350                          oid_value attributeSet, 
351                          NMEM stream, NMEM rset_nmem,
352                          Z_SortKeySpecList *sort_sequence,
353                          int num_bases, char **basenames,
354                          RSET *result_set);
355
356 ZEBRA_RES rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
357                     oid_value attributeset,
358                     int num_bases, char **basenames,
359                     int *position, int *num_entries, ZebraScanEntry **list,
360                     int *is_partial, RSET limit_set, int return_zero);
361
362 RSET rset_trunc(ZebraHandle zh, ISAM_P *isam_p, int no,
363                 const char *term, int length_term, const char *flags,
364                 int preserve_position, int term_type, NMEM rset_nmem,
365                 struct rset_key_control *kctrl, int scope,
366                 struct ord_list *ol, int reg_type,
367                 zint hits_limit, const char *term_ref_id);
368
369 void resultSetAddTerm (ZebraHandle zh, ZebraSet s, int reg_type,
370                        const char *db, int set,
371                        int use, const char *term);
372 ZebraSet resultSetAdd (ZebraHandle zh, const char *name, int ov);
373 ZebraSet resultSetGet (ZebraHandle zh, const char *name);
374 ZEBRA_RES resultSetAddRPN (ZebraHandle zh, NMEM m, Z_RPNQuery *rpn,
375                      int num_bases, char **basenames,
376                      const char *setname);
377 RSET resultSetRef (ZebraHandle zh, const char *resultSetId);
378 void resultSetDestroy (ZebraHandle zh, int num_names, char **names,
379                        int *statuses);
380 ZEBRA_RES resultSetSort (ZebraHandle zh, NMEM nmem,
381                          int num_input_setnames, const char **input_setnames,
382                          const char *output_setname,
383                          Z_SortKeySpecList *sort_sequence, int *sort_status);
384 ZEBRA_RES resultSetSortSingle (ZebraHandle zh, NMEM nmem,
385                                ZebraSet sset, RSET rset,
386                                Z_SortKeySpecList *sort_sequence,
387                                int *sort_status);
388 ZEBRA_RES resultSetRank (ZebraHandle zh, ZebraSet zebraSet, RSET rset,
389                          NMEM nmem);
390 void resultSetInvalidate (ZebraHandle zh);
391
392 int zebra_server_lock_init (ZebraService zh);
393 int zebra_server_lock_destroy (ZebraService zh);
394 int zebra_server_lock (ZebraService zh, int lockCommit);
395 void zebra_server_unlock (ZebraService zh, int commitPhase);
396 int zebra_server_lock_get_state (ZebraService zh, time_t *timep);
397
398 typedef struct attent
399 {
400     int attset_ordinal;
401     data1_local_attribute *local_attributes;
402 } attent;
403
404 void zebraRankInstall (struct zebra_register *reg, struct rank_control *ctrl);
405 ZebraRankClass zebraRankLookup (ZebraHandle zh, const char *name);
406 void zebraRankDestroy (struct zebra_register *reg);
407
408 int att_getentbyatt(ZebraHandle zh, attent *res, oid_value set, int att,
409                 const char *sattr);
410
411 extern struct rank_control *rank_1_class;
412 extern struct rank_control *rank_zv_class;
413 extern struct rank_control *rank_static_class;
414
415 int zebra_record_fetch (ZebraHandle zh, SYSNO sysno, int score, 
416                         zebra_snippets *hit_snippet, ODR stream,
417                         oid_value input_format, Z_RecordComposition *comp,
418                         oid_value *output_format, char **rec_bufp,
419                         int *rec_lenp, char **basenamep,
420                         char **addinfo);
421
422 void extract_get_fname_tmp (ZebraHandle zh, char *fname, int no);
423
424 void zebra_index_merge (ZebraHandle zh);
425
426 ZEBRA_RES buffer_extract_record (ZebraHandle zh, 
427                                  const char *buf, size_t buf_size,
428                                  int delete_flag,
429                                  int test_mode, 
430                                  const char *recordType,
431                                  SYSNO *sysno,
432                                  const char *match_criteria,
433                                  const char *fname,
434                                  int force_update,
435                                  int allow_update);
436
437 #if 0
438 int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
439                         const char *buf, size_t buf_size,
440                         const char *databaseName, int delete_flag,
441                         int test_mode, int *sysno,
442                         int store_keys, int store_data,
443                         const char *match_criteria);
444 #endif
445 void extract_flushWriteKeys (ZebraHandle zh, int final);
446
447 struct zebra_fetch_control {
448     off_t offset_end;
449     off_t record_offset;
450     off_t record_int_pos;
451     const char *record_int_buf;
452     int record_int_len;
453     int fd;
454 };
455
456 int zebra_record_ext_read (void *fh, char *buf, size_t count);
457 off_t zebra_record_ext_seek (void *fh, off_t offset);
458 off_t zebra_record_ext_tell (void *fh);
459 off_t zebra_record_int_seek (void *fh, off_t offset);
460 off_t zebra_record_int_tell (void *fh);
461 int zebra_record_int_read (void *fh, char *buf, size_t count);
462 void zebra_record_int_end (void *fh, off_t offset);
463
464 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys);
465
466 ZEBRA_RES zebra_snippets_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys,
467                                   zebra_snippets *snippets);
468 ZEBRA_RES zebra_snippets_hit_vector(ZebraHandle zh, const char *setname,
469                                     zint sysno, zebra_snippets *snippets);
470
471 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
472                               int cmd, zebra_rec_keys_t reckeys,
473                               zint staticrank);
474 #if NATTR
475 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
476                             int cmd, zebra_rec_keys_t skp);
477 #else
478 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
479                             int cmd, struct sortKeys *skp);
480 #endif
481 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid);
482 void extract_token_add (RecWord *p);
483 int explain_extract (void *handle, Record rec, data1_node *n);
484
485 ZEBRA_RES zebra_extract_file(ZebraHandle zh, SYSNO *sysno, const char *fname,
486                              int deleteFlag);
487
488 ZEBRA_RES zebra_begin_read (ZebraHandle zh);
489 ZEBRA_RES zebra_end_read (ZebraHandle zh);
490
491 int zebra_file_stat (const char *file_name, struct stat *buf,
492                      int follow_links);
493
494 void zebra_livcode_transform(ZebraHandle zh, Z_RPNQuery *query);
495
496 void *iscz1_start ();
497 void iscz1_reset (void *vp);
498 void iscz1_stop (void *p);
499 void iscz1_decode (void *vp, char **dst, const char **src);
500 void iscz1_encode (void *vp, char **dst, const char **src);
501
502 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
503                     int compact_flag, Res res);
504
505 void zebra_setError(ZebraHandle zh, int code, const char *addinfo);
506 void zebra_setError_zint(ZebraHandle zh, int code, zint i);
507
508 void zebra_term_untrans_iconv(ZebraHandle zh, NMEM stream, int reg_type,
509                               char **dst, const char *src);
510
511 ZEBRA_RES zebra_get_hit_vector(ZebraHandle zh, const char *setname, zint sysno);
512
513 void zebra_term_untrans(ZebraHandle zh, int reg_type,
514                         char *dst, const char *src);
515
516 YAZ_END_CDECL
517
518 #endif