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