Added suport for EXPLAIN Proxy servers - added settings databasePath
[idzebra-moved-to-github.git] / index / index.h
1 /*
2  * Copyright (C) 1995-1998, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: index.h,v $
7  * Revision 1.59  1998-06-08 14:43:11  adam
8  * Added suport for EXPLAIN Proxy servers - added settings databasePath
9  * and explainDatabase to facilitate this. Increased maximum number
10  * of databases and attributes in one register.
11  *
12  * Revision 1.58  1998/05/20 10:12:16  adam
13  * Implemented automatic EXPLAIN database maintenance.
14  * Modified Zebra to work with ASN.1 compiled version of YAZ.
15  *
16  * Revision 1.57  1998/03/05 08:45:12  adam
17  * New result set model and modular ranking system. Moved towards
18  * descent server API. System information stored as "SGML" records.
19  *
20  * Revision 1.56  1998/01/12 15:04:08  adam
21  * The test option (-s) only uses read-lock (and not write lock).
22  *
23  * Revision 1.55  1997/10/27 14:33:04  adam
24  * Moved towards generic character mapping depending on "structure"
25  * field in abstract syntax file. Fixed a few memory leaks. Fixed
26  * bug with negative integers when doing searches with relational
27  * operators.
28  *
29  * Revision 1.54  1997/09/29 09:08:36  adam
30  * Revised locking system to be thread safe for the server.
31  *
32  * Revision 1.53  1997/09/25 14:54:43  adam
33  * WIN32 files lock support.
34  *
35  * Revision 1.52  1997/09/22 12:39:06  adam
36  * Added get_pos method for the ranked result sets.
37  *
38  * Revision 1.51  1997/09/18 08:59:19  adam
39  * Extra generic handle for the character mapping routines.
40  *
41  * Revision 1.50  1997/09/17 12:19:13  adam
42  * Zebra version corresponds to YAZ version 1.4.
43  * Changed Zebra server so that it doesn't depend on global common_resource.
44  *
45  * Revision 1.49  1997/09/05 15:30:08  adam
46  * Changed prototype for chr_map_input - added const.
47  * Added support for C++, headers uses extern "C" for public definitions.
48  *
49  * Revision 1.48  1997/02/12 20:39:45  adam
50  * Implemented options -f <n> that limits the log to the first <n>
51  * records.
52  * Changed some log messages also.
53  *
54  * Revision 1.47  1996/12/23 15:30:44  adam
55  * Work on truncation.
56  * Bug fix: result sets weren't deleted after server shut down.
57  *
58  * Revision 1.46  1996/11/08 11:10:19  adam
59  * Buffers used during file match got bigger.
60  * Compressed ISAM support everywhere.
61  * Bug fixes regarding masking characters in queries.
62  * Redesigned Regexp-2 queries.
63  *
64  * Revision 1.45  1996/10/29 14:09:42  adam
65  * Use of cisam system - enabled if setting isamc is 1.
66  *
67  * Revision 1.44  1996/06/06 12:08:40  quinn
68  * Added showRecord function
69  *
70  * Revision 1.43  1996/06/04  10:18:12  adam
71  * Search/scan uses character mapping module.
72  *
73  * Revision 1.42  1996/06/04  08:20:16  quinn
74  * Smallish
75  *
76  * Revision 1.41  1996/06/04  07:54:55  quinn
77  * Added output-map.
78  *
79  * Revision 1.40  1996/05/31  09:06:58  quinn
80  * Work on character-set handling
81  *
82  * Revision 1.39  1996/05/14  14:04:33  adam
83  * In zebraidx, the 'stat' command is improved. Statistics about ISAM/DICT
84  * is collected.
85  *
86  * Revision 1.38  1996/04/12  07:02:23  adam
87  * File update of single files.
88  *
89  * Revision 1.37  1996/03/26 16:01:13  adam
90  * New setting lockPath: directory of various lock files.
91  *
92  * Revision 1.36  1996/03/21  14:50:09  adam
93  * File update uses modify-time instead of change-time.
94  *
95  * Revision 1.35  1996/02/12  18:45:36  adam
96  * New fileVerboseFlag in record group control.
97  *
98  * Revision 1.34  1995/12/11  11:43:29  adam
99  * Locking based on fcntl instead of flock.
100  * Setting commitEnable removed. Command line option -n can be used to
101  * prevent commit if commit setting is defined in the configuration file.
102  *
103  * Revision 1.33  1995/12/08  16:22:53  adam
104  * Work on update while servers are running. Three lock files introduced.
105  * The servers reload their registers when necessary, but they don't
106  * reestablish result sets yet.
107  *
108  * Revision 1.32  1995/12/07  17:38:46  adam
109  * Work locking mechanisms for concurrent updates/commit.
110  *
111  * Revision 1.31  1995/12/06  12:41:22  adam
112  * New command 'stat' for the index program.
113  * Filenames can be read from stdin by specifying '-'.
114  * Bug fix/enhancement of the transformation from terms to regular
115  * expressons in the search engine.
116  *
117  * Revision 1.30  1995/12/05  11:25:02  adam
118  * Include of zebraver.h.
119  *
120  * Revision 1.29  1995/11/28  09:09:40  adam
121  * Zebra config renamed.
122  * Use setting 'recordId' to identify record now.
123  * Bug fix in recindex.c: rec_release_blocks was invokeded even
124  * though the blocks were already released.
125  * File traversal properly deletes records when needed.
126  *
127  * Revision 1.28  1995/11/27  13:58:53  adam
128  * New option -t. storeStore data implemented in server.
129  *
130  * Revision 1.27  1995/11/25  10:24:06  adam
131  * More record fields - they are enumerated now.
132  * New options: flagStoreData flagStoreKey.
133  *
134  * Revision 1.26  1995/11/22  17:19:17  adam
135  * Record management uses the bfile system.
136  *
137  * Revision 1.25  1995/11/21  15:29:12  adam
138  * Config file 'base' read by default by both indexer and server.
139  *
140  * Revision 1.24  1995/11/21  15:01:15  adam
141  * New general match criteria implemented.
142  * New feature: document groups.
143  *
144  * Revision 1.23  1995/11/20  16:59:45  adam
145  * New update method: the 'old' keys are saved for each records.
146  *
147  * Revision 1.22  1995/11/20  11:56:26  adam
148  * Work on new traversal.
149  *
150  * Revision 1.21  1995/11/16  15:34:55  adam
151  * Uses new record management system in both indexer and server.
152  *
153  * Revision 1.20  1995/11/15  14:46:18  adam
154  * Started work on better record management system.
155  *
156  * Revision 1.19  1995/10/27  14:00:11  adam
157  * Implemented detection of database availability.
158  *
159  * Revision 1.18  1995/10/17  18:02:08  adam
160  * New feature: databases. Implemented as prefix to words in dictionary.
161  *
162  * Revision 1.17  1995/10/13  16:01:49  adam
163  * Work on relations.
164  *
165  * Revision 1.16  1995/10/10  12:24:38  adam
166  * Temporary sort files are compressed.
167  *
168  * Revision 1.15  1995/10/04  16:57:19  adam
169  * Key input and merge sort in one pass.
170  *
171  * Revision 1.14  1995/09/29  14:01:40  adam
172  * Bug fixes.
173  *
174  * Revision 1.13  1995/09/28  14:22:56  adam
175  * Sort uses smaller temporary files.
176  *
177  * Revision 1.12  1995/09/28  12:10:32  adam
178  * Bug fixes. Field prefix used in queries.
179  *
180  * Revision 1.11  1995/09/27  12:22:28  adam
181  * More work on extract in record control.
182  * Field name is not in isam keys but in prefix in dictionary words.
183  *
184  * Revision 1.10  1995/09/14  07:48:23  adam
185  * Record control management.
186  *
187  * Revision 1.9  1995/09/11  13:09:33  adam
188  * More work on relevance feedback.
189  *
190  * Revision 1.8  1995/09/08  14:52:27  adam
191  * Minor changes. Dictionary is lower case now.
192  *
193  * Revision 1.7  1995/09/06  16:11:16  adam
194  * Option: only one word key per file.
195  *
196  * Revision 1.6  1995/09/05  15:28:39  adam
197  * More work on search engine.
198  *
199  * Revision 1.5  1995/09/04  12:33:42  adam
200  * Various cleanup. YAZ util used instead.
201  *
202  * Revision 1.4  1995/09/04  09:10:35  adam
203  * More work on index add/del/update.
204  * Merge sort implemented.
205  * Initial work on z39 server.
206  *
207  * Revision 1.3  1995/09/01  14:06:35  adam
208  * Split of work into more files.
209  *
210  * Revision 1.2  1995/09/01  10:30:24  adam
211  * More work on indexing. Not working yet.
212  *
213  * Revision 1.1  1995/08/31  14:50:24  adam
214  * New simple file index tool.
215  *
216  */
217 #include <time.h>
218 #include <zebraver.h>
219 #include <zebrautl.h>
220 #include <zebramap.h>
221
222 #include <dict.h>
223 #include <isam.h>
224 #include <isamc.h>
225 #include <data1.h>
226
227 #define IT_MAX_WORD 256
228 #define IT_KEY_HAVE_SEQNO 1
229 #define IT_KEY_HAVE_FIELD 0
230
231 typedef int SYSNO;
232
233 struct it_key {
234     int  sysno;
235     int  seqno;
236 };
237
238 enum dirsKind { dirs_dir, dirs_file };
239
240 struct dir_entry {
241     enum dirsKind kind;
242     char *name;
243     time_t mtime;
244 };
245
246 struct dirs_entry {
247     enum dirsKind kind;
248     char path[256];
249     SYSNO sysno;
250     time_t mtime;
251 };
252
253
254 struct recordGroup {
255     char         *groupName;
256     char         *databaseName;
257     char         *path;
258     char         *recordId;
259     char         *recordType;
260     int          flagStoreData;
261     int          flagStoreKeys;
262     int          flagRw;
263     int          fileVerboseLimit;
264     int          databaseNamePath;
265     int          explainDatabase;
266     data1_handle dh;
267     BFiles       bfs;
268     ZebraMaps    zebra_maps;
269 };
270
271 void getFnameTmp (char *fname, int no);
272         
273 struct dirs_info *dirs_open (Dict dict, const char *rep, int rw);
274 struct dirs_info *dirs_fopen (Dict dict, const char *path);
275 struct dirs_entry *dirs_read (struct dirs_info *p);
276 struct dirs_entry *dirs_last (struct dirs_info *p);
277 void dirs_mkdir (struct dirs_info *p, const char *src, time_t mtime);
278 void dirs_rmdir (struct dirs_info *p, const char *src);
279 void dirs_add (struct dirs_info *p, const char *src, int sysno, time_t mtime);
280 void dirs_del (struct dirs_info *p, const char *src);
281 void dirs_free (struct dirs_info **pp);
282
283 struct dir_entry *dir_open (const char *rep);
284 void dir_sort (struct dir_entry *e);
285 void dir_free (struct dir_entry **e_p);
286
287 void repositoryUpdate (struct recordGroup *rGroup);
288 void repositoryAdd (struct recordGroup *rGroup);
289 void repositoryDelete (struct recordGroup *rGroup);
290 void repositoryShow (struct recordGroup *rGroup);
291
292 int key_open (struct recordGroup *rGroup, int mem);
293 int key_close (struct recordGroup *group);
294 int key_compare (const void *p1, const void *p2);
295 int key_get_pos (const void *p);
296 int key_compare_it (const void *p1, const void *p2);
297 int key_qsort_compare (const void *p1, const void *p2);
298 void key_logdump (int mask, const void *p);
299 void inv_prstat (BFiles bfs);
300 void key_input (BFiles bfs, int nkeys, int cache);
301 ISAMC_M key_isamc_m (Res res);
302 int merge_sort (char **buf, int from, int to);
303 int key_SU_code (int ch, char *out);
304
305 #define FNAME_DICT "dict"
306 #define FNAME_ISAM "isam"
307 #define FNAME_ISAMC "isamc"
308 #define FNAME_CONFIG "zebra.cfg"
309
310 #define GMATCH_DICT "gmatch"
311 #define FMATCH_DICT "fmatch"
312
313 struct strtab *strtab_mk (void);
314 int strtab_src (struct strtab *t, const char *name, void ***infop);
315 void strtab_del (struct strtab *t,
316                  void (*func)(const char *name, void *info, void *data),
317                  void *data);
318 int index_char_cvt (int c);
319 int index_word_prefix (char *string, int attset_ordinal,
320                        int local_attribute, const char *databaseName);
321
322 int fileExtract (SYSNO *sysno, const char *fname,
323                  const struct recordGroup *rGroup, int deleteFlag);
324
325 void zebraIndexLockMsg (const char *str);
326 void zebraIndexUnlock (void);
327 void zebraIndexLock (BFiles bfs, int commitNow, const char *rval);
328 int zebraIndexWait (int commitPhase);
329
330 #define FNAME_MAIN_LOCK   "zebraidx.LCK"
331 #define FNAME_COMMIT_LOCK "zebracmt.LCK"
332 #define FNAME_ORG_LOCK    "zebraorg.LCK"
333 #define FNAME_TOUCH_TIME  "zebraidx.time"
334
335 typedef struct zebra_lock_info *ZebraLockHandle;
336 ZebraLockHandle zebra_lock_create(const char *file, int excl_flag);
337 void zebra_lock_destroy (ZebraLockHandle h);
338 int zebra_lock (ZebraLockHandle h);
339 int zebra_lock_nb (ZebraLockHandle h);
340 int zebra_unlock (ZebraLockHandle h);
341 int zebra_lock_fd (ZebraLockHandle h);
342 void zebra_lock_prefix (Res res, char *dst);
343
344 void zebra_load_atts (data1_handle dh, Res res);
345
346 extern Res common_resource;