Make codec routines public again. Add key_get_seq
[idzebra-moved-to-github.git] / index / index.h
1 /* $Id: index.h,v 1.108 2004-06-09 13:11:49 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
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
24
25 #ifndef INDEX_H
26 #define INDEX_H
27
28 #include <time.h>
29 #include <zebraver.h>
30 #include <zebrautl.h>
31 #include <zebramap.h>
32 #include <sortidx.h>
33
34 #if HAVE_SYS_TIMES_H
35 #include <sys/times.h>
36 #endif
37 #include <sys/stat.h>
38
39 #include <dict.h>
40 #include <isams.h>
41 #include <isam.h>
42 #include <isamc.h>
43 #include <isamd.h>
44 #include <isamb.h>
45 #define ISAM_DEFAULT "c"
46 #include <data1.h>
47 #include <recctrl.h>
48 #include "recindex.h"
49 #include "zebraapi.h"
50 #include "zinfo.h"
51 #include <passwddb.h>
52 #include <rset.h>
53
54 YAZ_BEGIN_CDECL
55
56 #define SU_SCHEME 1
57
58 #define IT_MAX_WORD 256
59 #define IT_KEY_HAVE_SEQNO 1
60 #define IT_KEY_HAVE_FIELD 0
61
62 typedef int SYSNO;
63
64 struct it_key {
65     int  sysno;
66     int  seqno;
67 };
68
69 enum dirsKind { dirs_dir, dirs_file };
70
71 struct dir_entry {
72     enum dirsKind kind;
73     char *name;
74     time_t mtime;
75 };
76
77 struct dirs_entry {
78     enum dirsKind kind;
79     char path[256];
80     SYSNO sysno;
81     time_t mtime;
82 };
83
84 void getFnameTmp (Res res, char *fname, int no);
85         
86 struct dirs_info *dirs_open (Dict dict, const char *rep, int rw);
87 struct dirs_info *dirs_fopen (Dict dict, const char *path);
88 struct dirs_entry *dirs_read (struct dirs_info *p);
89 struct dirs_entry *dirs_last (struct dirs_info *p);
90 void dirs_mkdir (struct dirs_info *p, const char *src, time_t mtime);
91 void dirs_rmdir (struct dirs_info *p, const char *src);
92 void dirs_add (struct dirs_info *p, const char *src, int sysno, time_t mtime);
93 void dirs_del (struct dirs_info *p, const char *src);
94 void dirs_free (struct dirs_info **pp);
95
96 struct dir_entry *dir_open (const char *rep, const char *base,
97                             int follow_links);
98 void dir_sort (struct dir_entry *e);
99 void dir_free (struct dir_entry **e_p);
100
101 void repositoryUpdate (ZebraHandle zh, const char *path);
102 void repositoryAdd (ZebraHandle zh, const char *path);
103 void repositoryDelete (ZebraHandle zh, const char *path);
104 void repositoryShow (ZebraHandle zh, const char *path);
105
106 int key_open (ZebraHandle zh, int mem);
107 int key_close (ZebraHandle zh);
108 int key_compare (const void *p1, const void *p2);
109 char *key_print_it (const void *p, char *buf);
110 int key_get_seq (const void *p);
111 int key_compare_it (const void *p1, const void *p2);
112 int key_qsort_compare (const void *p1, const void *p2);
113 void key_logdump (int mask, const void *p);
114 void key_logdump_txt (int logmask, const void *p, const char *txt);
115 void inv_prstat (ZebraHandle zh);
116 void inv_compact (BFiles bfs);
117 void key_input (ZebraHandle zh, int nkeys, int cache, Res res);
118 ISAMS_M *key_isams_m (Res res, ISAMS_M *me);
119 ISAMC_M *key_isamc_m (Res res, ISAMC_M *me);
120 ISAMD_M *key_isamd_m (Res res, ISAMD_M *me);
121 int merge_sort (char **buf, int from, int to);
122 int key_SU_code (int ch, char *out);
123
124 #define FNAME_DICT "dict"
125 #define FNAME_ISAM "isam"
126 #define FNAME_ISAMC "isamc"
127 #define FNAME_ISAMS "isams"
128 #define FNAME_ISAMH "isamh"
129 #define FNAME_ISAMD "isamd"
130 #define FNAME_CONFIG "zebra.cfg"
131
132 #define GMATCH_DICT "gmatch"
133 #define FMATCH_DICT "fmatch"
134
135 struct strtab *strtab_mk (void);
136 int strtab_src (struct strtab *t, const char *name, void ***infop);
137 void strtab_del (struct strtab *t,
138                  void (*func)(const char *name, void *info, void *data),
139                  void *data);
140 int index_char_cvt (int c);
141 int index_word_prefix (char *string, int attset_ordinal,
142                        int local_attribute, const char *databaseName);
143
144
145 void zebraIndexLockMsg (ZebraHandle zh, const char *str);
146 void zebraIndexUnlock (ZebraHandle zh);
147 int zebraIndexLock (BFiles bfs, ZebraHandle zh, int commitNow, const char *rval);
148 int zebraIndexWait (ZebraHandle zh, int commitPhase);
149
150 #define FNAME_MAIN_LOCK   "zebraidx.LCK"
151 #define FNAME_COMMIT_LOCK "zebracmt.LCK"
152 #define FNAME_ORG_LOCK    "zebraorg.LCK"
153 #define FNAME_TOUCH_TIME  "zebraidx.time"
154
155 typedef struct zebra_lock_info *ZebraLockHandle;
156 ZebraLockHandle zebra_lock_create(const char *dir,
157                                   const char *file, int excl_flag);
158 void zebra_lock_destroy (ZebraLockHandle h);
159 int zebra_lock (ZebraLockHandle h);
160 int zebra_lock_nb (ZebraLockHandle h);
161 int zebra_unlock (ZebraLockHandle h);
162 int zebra_lock_fd (ZebraLockHandle h);
163 void zebra_lock_prefix (Res res, char *dst);
164 char *zebra_mk_fname (const char *dir, const char *name);
165
166 int zebra_lock_w (ZebraLockHandle h);
167 int zebra_lock_r (ZebraLockHandle h);
168
169 void zebra_load_atts (data1_handle dh, Res res);
170
171 int key_SU_decode (int *ch, const unsigned char *out);
172 int key_SU_encode (int ch, char *out);
173
174 #define ENCODE_BUFLEN 768
175 struct encode_info {
176     int  sysno;  /* previously written values for delta-compress */
177     int  seqno;
178     int  cmd;
179     int prevsys; /* buffer for skipping insert/delete pairs */
180     int prevseq;
181     int prevcmd;
182     int keylen; /* tells if we have an unwritten key in buf, and how long*/
183     char buf[ENCODE_BUFLEN];
184 };
185
186 void encode_key_init (struct encode_info *i);
187 char *encode_key_int (int d, char *bp);
188 void encode_key_write (char *k, struct encode_info *i, FILE *outf);
189 void encode_key_flush (struct encode_info *i, FILE *outf);
190
191 typedef struct {
192     char *term;
193     char *db;
194     int sysno;
195     int score;
196 } *ZebraPosSet;
197
198 typedef struct zebra_set *ZebraSet;
199
200 typedef struct zebra_rank_class {
201     struct rank_control *control;
202     int init_flag;
203     void *class_handle;
204     struct zebra_rank_class *next;
205 } *ZebraRankClass;
206
207 struct recKeys {
208     int buf_used;
209     int buf_max;
210     char *buf;
211     char prevAttrSet;
212     short prevAttrUse;
213     int prevSeqNo;
214 };
215
216 #if 1
217 struct sortKeys {
218     int buf_used;
219     int buf_max;
220     char *buf;
221 };
222 #else
223 struct sortKey {
224     char *string;
225     int length;
226     int attrSet;
227     int attrUse;
228     struct sortKey *next;
229 };
230 #endif
231
232 struct zebra_register {
233     char *name;
234     
235     ISAMS isams;
236     ISAM isam;
237     ISAMC isamc;
238     ISAMD isamd;
239     ISAMB isamb;
240     Dict dict;
241     Dict matchDict;
242     SortIdx sortIdx;
243     int registerState; /* 0 (no commit pages), 1 (use commit pages) */
244     time_t registerChange;
245     BFiles bfs;
246     Records records;
247     ZebraExplainInfo zei;
248
249     char *server_path_prefix;
250     data1_handle dh;
251     ZebraMaps zebra_maps;
252     ZebraRankClass rank_classes;
253     RecTypes recTypes;
254     int seqno;
255     int last_val;
256     int stop_flag;
257     int active; /* 0=shutdown, 1=enabled and inactive, 2=activated */
258
259     struct recKeys keys;
260 #if 1
261     struct sortKeys sortKeys;
262 #else
263     struct sortKey *sortKeys;
264 #endif
265     char **key_buf;
266     size_t ptr_top;
267     size_t ptr_i;
268     size_t key_buf_used;
269     int key_file_no;
270 };
271
272 struct zebra_service {
273     int stop_flag;
274     Res global_res;
275     char *configName;
276     struct zebra_session *sessions;
277     struct zebra_register *regs;
278     Zebra_mutex_cond session_lock;
279     Passwd_db passwd_db;
280     const char *path_root;
281 };
282
283
284 struct zebra_session {
285     struct zebra_session *next;
286     struct zebra_service *service;
287     struct zebra_register *reg;
288
289     char *xadmin_databaseName;
290
291     char **basenames;
292     int num_basenames;
293     char *reg_name;
294     char *path_reg;
295
296     ZebraLockHandle lock_normal;
297     ZebraLockHandle lock_shadow;
298
299     int trans_no;
300     int trans_w_no;
301
302     int destroyed;
303     ZebraSet sets;
304     Res res;
305     char *user_perm;
306     int errCode;
307     int hits;
308     char *errString;
309 #if HAVE_SYS_TIMES_H
310     struct tms tms1;
311     struct tms tms2;    
312 #endif
313     int  shadow_enable;
314
315     int records_inserted;
316     int records_updated;
317     int records_deleted;
318     int records_processed;
319     char *record_encoding;
320
321     yaz_iconv_t iconv_to_utf8;
322     yaz_iconv_t iconv_from_utf8;
323
324     int m_follow_links;
325     const char *m_group;
326     const char *m_record_id;
327     const char *m_record_type;
328     int m_store_data;
329     int m_store_keys;
330     int m_explain_database;
331     int m_flag_rw;
332     int m_file_verbose_limit;
333 };
334
335 struct rank_control {
336     char *name;
337     void *(*create)(ZebraHandle zh);
338     void (*destroy)(struct zebra_register *reg, void *class_handle);
339     void *(*begin)(struct zebra_register *reg, void *class_handle, RSET rset);
340     void (*end)(struct zebra_register *reg, void *set_handle);
341     int (*calc)(void *set_handle, int sysno);
342     void (*add)(void *set_handle, int seqno, int term_index);
343 };
344
345 struct term_set_entry {
346     char *term;
347     struct term_set_entry *next;
348 };
349
350 struct term_set_list {
351     struct term_set_entry *first;
352     struct term_set_entry *last;
353 };
354
355 RSET rpn_search (ZebraHandle zh, NMEM mem,
356                  Z_RPNQuery *rpn, int num_bases, char **basenames, 
357                  const char *setname, ZebraSet sset);
358
359
360 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
361                oid_value attributeset,
362                int num_bases, char **basenames,
363                int *position, int *num_entries, ZebraScanEntry **list,
364                int *is_partial, RSET limit_set, int return_zero);
365
366 RSET rset_trunc (ZebraHandle zh, ISAMS_P *isam_p, int no,
367                  const char *term, int length_term, const char *flags,
368                  int preserve_position, int term_type);
369
370 void resultSetAddTerm (ZebraHandle zh, ZebraSet s, int reg_type,
371                        const char *db, int set,
372                        int use, const char *term);
373 ZebraSet resultSetAdd (ZebraHandle zh, const char *name, int ov);
374 ZebraSet resultSetGet (ZebraHandle zh, const char *name);
375 ZebraSet resultSetAddRPN (ZebraHandle zh, NMEM m, Z_RPNQuery *rpn,
376                           int num_bases, char **basenames,
377                           const char *setname);
378 RSET resultSetRef (ZebraHandle zh, const char *resultSetId);
379 void resultSetDestroy (ZebraHandle zh, int num_names, char **names,
380                        int *statuses);
381
382
383 ZebraPosSet zebraPosSetCreate (ZebraHandle zh, const char *name,
384                                int num, int *positions);
385 void zebraPosSetDestroy (ZebraHandle zh, ZebraPosSet records, int num);
386
387 void resultSetSort (ZebraHandle zh, NMEM nmem,
388                     int num_input_setnames, const char **input_setnames,
389                     const char *output_setname,
390                     Z_SortKeySpecList *sort_sequence, int *sort_status);
391 void resultSetSortSingle (ZebraHandle zh, NMEM nmem,
392                           ZebraSet sset, RSET rset,
393                           Z_SortKeySpecList *sort_sequence, int *sort_status);
394 void resultSetRank (ZebraHandle zh, ZebraSet zebraSet, RSET rset);
395 void resultSetInvalidate (ZebraHandle zh);
396
397 int zebra_server_lock_init (ZebraService zh);
398 int zebra_server_lock_destroy (ZebraService zh);
399 int zebra_server_lock (ZebraService zh, int lockCommit);
400 void zebra_server_unlock (ZebraService zh, int commitPhase);
401 int zebra_server_lock_get_state (ZebraService zh, time_t *timep);
402
403 typedef struct attent
404 {
405     int attset_ordinal;
406     data1_local_attribute *local_attributes;
407 } attent;
408
409 void zebraRankInstall (struct zebra_register *reg, struct rank_control *ctrl);
410 ZebraRankClass zebraRankLookup (ZebraHandle zh, const char *name);
411 void zebraRankDestroy (struct zebra_register *reg);
412
413 int att_getentbyatt(ZebraHandle zh, attent *res, oid_value set, int att,
414                 const char *sattr);
415
416 extern struct rank_control *rank1_class;
417 extern struct rank_control *rankzv_class;
418 extern struct rank_control *rankliv_class;
419
420 int zebra_record_fetch (ZebraHandle zh, int sysno, int score, ODR stream,
421                         oid_value input_format, Z_RecordComposition *comp,
422                         oid_value *output_format, char **rec_bufp,
423                         int *rec_lenp, char **basenamep);
424
425 void extract_get_fname_tmp (ZebraHandle zh, char *fname, int no);
426
427 void zebra_index_merge (ZebraHandle zh);
428
429 int buffer_extract_record (ZebraHandle zh, 
430                            const char *buf, size_t buf_size,
431                            int delete_flag,
432                            int test_mode, 
433                            const char *recordType,
434                            int *sysno,
435                            const char *match_criteria,
436                            const char *fname,
437                            int force_update,
438                            int allow_update);
439
440 #if 0
441 int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
442                         const char *buf, size_t buf_size,
443                         const char *databaseName, int delete_flag,
444                         int test_mode, int *sysno,
445                         int store_keys, int store_data,
446                         const char *match_criteria);
447 #endif
448 void extract_flushWriteKeys (ZebraHandle zh, int final);
449
450 struct zebra_fetch_control {
451     off_t offset_end;
452     off_t record_offset;
453     off_t record_int_pos;
454     const char *record_int_buf;
455     int record_int_len;
456     int fd;
457 };
458
459 int zebra_record_ext_read (void *fh, char *buf, size_t count);
460 off_t zebra_record_ext_seek (void *fh, off_t offset);
461 off_t zebra_record_ext_tell (void *fh);
462 off_t zebra_record_int_seek (void *fh, off_t offset);
463 off_t zebra_record_int_tell (void *fh);
464 int zebra_record_int_read (void *fh, char *buf, size_t count);
465 void zebra_record_int_end (void *fh, off_t offset);
466
467 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
468                               int cmd, struct recKeys *reckeys);
469 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
470                             int cmd, struct sortKeys *skp);
471 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid);
472 void extract_token_add (RecWord *p);
473 int explain_extract (void *handle, Record rec, data1_node *n);
474
475 int fileExtract (ZebraHandle zh, SYSNO *sysno, const char *fname,
476                  int deleteFlag);
477
478 int zebra_begin_read (ZebraHandle zh);
479 int zebra_end_read (ZebraHandle zh);
480
481 int zebra_file_stat (const char *file_name, struct stat *buf,
482                      int follow_links);
483
484 void zebra_livcode_transform(ZebraHandle zh, Z_RPNQuery *query);
485
486 void *iscz1_code_start (int mode);
487 void iscz1_code_reset (void *vp);
488 void iscz1_code_stop (int mode, void *p);
489 void iscz1_code_item (int mode, void *vp, char **dst, char **src);
490
491
492
493 YAZ_END_CDECL
494
495 #endif