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