Implemented automatic EXPLAIN database maintenance.
[idzebra-moved-to-github.git] / index / main.c
1 /*
2  * Copyright (C) 1994-1998, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: main.c,v $
7  * Revision 1.57  1998-05-20 10:12:18  adam
8  * Implemented automatic EXPLAIN database maintenance.
9  * Modified Zebra to work with ASN.1 compiled version of YAZ.
10  *
11  * Revision 1.56  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.55  1998/01/26 10:37:34  adam
16  * Minor changes.
17  *
18  * Revision 1.54  1998/01/12 15:04:08  adam
19  * The test option (-s) only uses read-lock (and not write lock).
20  *
21  * Revision 1.53  1997/11/18 10:05:08  adam
22  * Changed character map facility so that admin can specify character
23  * mapping files for each register type, w, p, etc.
24  *
25  * Revision 1.52  1997/10/31 12:34:04  adam
26  * Added missing nmem_init.
27  *
28  * Revision 1.51  1997/10/27 14:33:05  adam
29  * Moved towards generic character mapping depending on "structure"
30  * field in abstract syntax file. Fixed a few memory leaks. Fixed
31  * bug with negative integers when doing searches with relational
32  * operators.
33  *
34  * Revision 1.50  1997/09/25 14:55:52  adam
35  * Minor changes.
36  *
37  * Revision 1.49  1997/09/17 12:19:15  adam
38  * Zebra version corresponds to YAZ version 1.4.
39  * Changed Zebra server so that it doesn't depend on global common_resource.
40  *
41  * Revision 1.48  1997/09/09 13:38:08  adam
42  * Partial port to WIN95/NT.
43  *
44  * Revision 1.47  1997/02/12 20:39:46  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.46  1997/02/10 10:20:13  adam
50  * Flag fileVerboseFlag set to 0 (default).
51  *
52  * Revision 1.45  1996/11/08 11:10:26  adam
53  * Buffers used during file match got bigger.
54  * Compressed ISAM support everywhere.
55  * Bug fixes regarding masking characters in queries.
56  * Redesigned Regexp-2 queries.
57  *
58  * Revision 1.44  1996/10/29 14:09:48  adam
59  * Use of cisam system - enabled if setting isamc is 1.
60  *
61  * Revision 1.43  1996/06/06 12:08:42  quinn
62  * Added showRecord function
63  *
64  * Revision 1.42  1996/05/31  09:07:01  quinn
65  * Work on character-set handling
66  *
67  * Revision 1.41  1996/05/14  14:04:34  adam
68  * In zebraidx, the 'stat' command is improved. Statistics about ISAM/DICT
69  * is collected.
70  *
71  * Revision 1.40  1996/04/26  10:00:23  adam
72  * Added option -V to zebraidx to display version information.
73  * Removed stupid warnings from file update.
74  *
75  * Revision 1.39  1996/04/09  10:05:21  adam
76  * Bug fix: prev_name buffer possibly too small; allocated in key_file_init.
77  *
78  * Revision 1.38  1996/03/26  16:01:14  adam
79  * New setting lockPath: directory of various lock files.
80  *
81  * Revision 1.37  1996/03/19  12:43:26  adam
82  * Bug fix: File update traversal didn't handle trailing slashes correctly.
83  * Bug fix: Update of sub directory groups wasn't handled correctly.
84  *
85  * Revision 1.36  1996/02/12  18:45:37  adam
86  * New fileVerboseFlag in record group control.
87  *
88  * Revision 1.35  1996/02/12  15:56:11  adam
89  * New code command: unread.
90  *
91  * Revision 1.34  1996/02/07  14:06:39  adam
92  * Better progress report during register merge.
93  * New command: clean - removes temporary shadow files.
94  *
95  * Revision 1.33  1996/02/06  17:11:18  adam
96  * Minor changes.
97  *
98  * Revision 1.32  1996/02/01  20:50:04  adam
99  * Bug fix: zebraIndexUnlock was always called even though zebraIndexLock
100  * was never called - happens when no commands are specified.
101  *
102  * Revision 1.31  1996/01/08  19:15:46  adam
103  * New input filter that works!
104  *
105  * Revision 1.30  1995/12/12  16:00:59  adam
106  * System call sync(2) used after update/commit.
107  * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
108  * and F_RDLCK.
109  *
110  * Revision 1.29  1995/12/11  11:43:30  adam
111  * Locking based on fcntl instead of flock.
112  * Setting commitEnable removed. Command line option -n can be used to
113  * prevent commit if commit setting is defined in the configuration file.
114  *
115  * Revision 1.28  1995/12/08  16:22:56  adam
116  * Work on update while servers are running. Three lock files introduced.
117  * The servers reload their registers when necessary, but they don't
118  * reestablish result sets yet.
119  *
120  * Revision 1.27  1995/12/07  17:38:47  adam
121  * Work locking mechanisms for concurrent updates/commit.
122  *
123  * Revision 1.26  1995/12/06  12:41:23  adam
124  * New command 'stat' for the index program.
125  * Filenames can be read from stdin by specifying '-'.
126  * Bug fix/enhancement of the transformation from terms to regular
127  * expressons in the search engine.
128  *
129  * Revision 1.25  1995/12/01  16:24:39  adam
130  * Commit files use separate meta file area.
131  *
132  * Revision 1.24  1995/11/30  17:01:38  adam
133  * New setting commitCache: points to commit directories/files.
134  * New command commit: commits at the end of a zebraidx run.
135  *
136  * Revision 1.23  1995/11/30  08:34:31  adam
137  * Started work on commit facility.
138  * Changed a few malloc/free to xmalloc/xfree.
139  *
140  * Revision 1.22  1995/11/28  09:09:42  adam
141  * Zebra config renamed.
142  * Use setting 'recordId' to identify record now.
143  * Bug fix in recindex.c: rec_release_blocks was invokeded even
144  * though the blocks were already released.
145  * File traversal properly deletes records when needed.
146  *
147  * Revision 1.21  1995/11/27  14:27:39  adam
148  * Renamed 'update' command to 'dir'.
149  *
150  * Revision 1.20  1995/11/27  13:58:53  adam
151  * New option -t. storeStore data implemented in server.
152  *
153  * Revision 1.19  1995/11/25  10:24:06  adam
154  * More record fields - they are enumerated now.
155  * New options: flagStoreData flagStoreKey.
156  *
157  * Revision 1.18  1995/11/22  17:19:17  adam
158  * Record management uses the bfile system.
159  *
160  * Revision 1.17  1995/11/21  15:01:16  adam
161  * New general match criteria implemented.
162  * New feature: document groups.
163  *
164  * Revision 1.16  1995/11/20  11:56:27  adam
165  * Work on new traversal.
166  *
167  * Revision 1.15  1995/11/01  16:25:51  quinn
168  * *** empty log message ***
169  *
170  * Revision 1.14  1995/10/17  18:02:09  adam
171  * New feature: databases. Implemented as prefix to words in dictionary.
172  *
173  * Revision 1.13  1995/10/10  12:24:39  adam
174  * Temporary sort files are compressed.
175  *
176  * Revision 1.12  1995/10/04  16:57:20  adam
177  * Key input and merge sort in one pass.
178  *
179  * Revision 1.11  1995/09/29  14:01:45  adam
180  * Bug fixes.
181  *
182  * Revision 1.10  1995/09/28  14:22:57  adam
183  * Sort uses smaller temporary files.
184  *
185  * Revision 1.9  1995/09/14  07:48:24  adam
186  * Record control management.
187  *
188  * Revision 1.8  1995/09/06  16:11:18  adam
189  * Option: only one word key per file.
190  *
191  * Revision 1.7  1995/09/05  15:28:39  adam
192  * More work on search engine.
193  *
194  * Revision 1.6  1995/09/04  12:33:43  adam
195  * Various cleanup. YAZ util used instead.
196  *
197  * Revision 1.5  1995/09/04  09:10:39  adam
198  * More work on index add/del/update.
199  * Merge sort implemented.
200  * Initial work on z39 server.
201  *
202  * Revision 1.4  1995/09/01  14:06:36  adam
203  * Split of work into more files.
204  *
205  * Revision 1.3  1995/09/01  10:57:07  adam
206  * Minor changes.
207  *
208  * Revision 1.2  1995/09/01  10:30:24  adam
209  * More work on indexing. Not working yet.
210  *
211  * Revision 1.1  1995/08/31  14:50:24  adam
212  * New simple file index tool.
213  *
214  */
215 #include <stdio.h>
216 #include <assert.h>
217 #ifdef WINDOWS
218 #include <io.h>
219 #else
220 #include <unistd.h>
221 #endif
222
223 #include <data1.h>
224 #include "index.h"
225 #include "recindex.h"
226
227 char *prog;
228
229 Res common_resource = 0;
230
231 int main (int argc, char **argv)
232 {
233     int ret;
234     int cmd = 0;
235     char *arg;
236     char *configName = NULL;
237     int nsections;
238     int disableCommit = 0;
239     size_t mem_max = 0;
240
241     struct recordGroup rGroupDef;
242
243     nmem_init ();
244
245     rGroupDef.groupName = NULL;
246     rGroupDef.databaseName = NULL;
247     rGroupDef.path = NULL;
248     rGroupDef.recordId = NULL;
249     rGroupDef.recordType = NULL;
250     rGroupDef.flagStoreData = -1;
251     rGroupDef.flagStoreKeys = -1;
252     rGroupDef.flagRw = 1;
253     rGroupDef.fileVerboseLimit = 100000;
254     rGroupDef.zebra_maps = NULL;
255     rGroupDef.dh = data1_create ();
256
257     prog = *argv;
258     if (argc < 2)
259     {
260         fprintf (stderr, "zebraidx [options] command <dir> ...\n"
261         "Commands:\n"
262         " update <dir>  Update index with files below <dir>.\n"
263         "               If <dir> is empty filenames are read from stdin.\n"
264         " delete <dir>  Delete index with files below <dir>.\n"
265         " commit        Commit changes\n"
266         " clean         Clean shadow files\n"
267         "Options:\n"
268         " -t <type>     Index files as <type> (grs or text).\n"
269         " -c <config>   Read configuration file <config>.\n"
270         " -g <group>    Index files according to group settings.\n"
271         " -d <database> Records belong to Z39.50 database <database>.\n"
272         " -m <mbytes>   Use <mbytes> before flushing keys to disk.\n"
273         " -n            Don't use shadow system.\n"
274         " -s            Show analysis on stdout, but do no work.\n"
275         " -v <level>    Set logging to <level>.\n"
276         " -f <n>        Display information for the first <n> records.\n"
277         " -V            Show version.\n"
278                  );
279         exit (1);
280     }
281     while ((ret = options ("sVt:c:g:d:m:v:nf:", argv, argc, &arg)) != -2)
282     {
283         if (ret == 0)
284         {
285             const char *rval;
286             if(cmd == 0) /* command */
287             {
288                 if (!common_resource)
289                 {
290                     logf (LOG_LOG, "zebra version %s %s",
291                           ZEBRAVER, ZEBRADATE);
292                     common_resource = res_open (configName ?
293                                                 configName : FNAME_CONFIG);
294                     if (!common_resource)
295                     {
296                         logf (LOG_FATAL, "cannot configuration file `%s'",
297                               configName);
298                         exit (1);
299                     }
300                     data1_set_tabpath (rGroupDef.dh, res_get (common_resource,
301                                                               "profilePath"));
302
303                     rGroupDef.bfs =
304                         bfs_create (res_get (common_resource, "register"));
305
306                     bf_lockDir (rGroupDef.bfs,
307                                 res_get (common_resource, "lockDir"));
308                     rGroupDef.zebra_maps = zebra_maps_open (common_resource);
309                 }
310                 if (!strcmp (arg, "update"))
311                     cmd = 'u';
312                 else if (!strcmp (arg, "update1"))
313                     cmd = 'U';
314                 else if (!strcmp (arg, "update2"))
315                     cmd = 'm';
316                 else if (!strcmp (arg, "dump"))
317                     cmd = 's';
318                 else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
319                     cmd = 'd';
320                 else if (!strcmp (arg, "commit"))
321                 {
322                     rval = res_get (common_resource, "shadow");
323                     zebraIndexLock (rGroupDef.bfs, 1, rval);
324                     if (rval && *rval)
325                         bf_cache (rGroupDef.bfs, rval);
326                     else
327                     {
328                         logf (LOG_FATAL, "Cannot perform commit");
329                         logf (LOG_FATAL, "No shadow area defined");
330                         exit (1);
331                     }
332                     if (bf_commitExists (rGroupDef.bfs))
333                     {
334                         logf (LOG_LOG, "commit start");
335                         zebraIndexLockMsg ("c");
336                         zebraIndexWait (1);
337                         logf (LOG_LOG, "commit execute");
338                         bf_commitExec (rGroupDef.bfs);
339 #ifndef WINDOWS
340                         sync ();
341 #endif
342                         zebraIndexLockMsg ("d");
343                         zebraIndexWait (0);
344                         logf (LOG_LOG, "commit clean");
345                         bf_commitClean (rGroupDef.bfs, rval);
346                     }
347                     else
348                         logf (LOG_LOG, "cothing to commit");
349                 }
350                 else if (!strcmp (arg, "clean"))
351                 {
352                     rval = res_get (common_resource, "shadow");
353                     zebraIndexLock (rGroupDef.bfs, 1, rval);
354                     if (bf_commitExists (rGroupDef.bfs))
355                     {
356                         zebraIndexLockMsg ("d");
357                         zebraIndexWait (0);
358                         logf (LOG_LOG, "commit clean");
359                         bf_commitClean (rGroupDef.bfs, rval);
360                     }
361                     else
362                         logf (LOG_LOG, "nothing to clean");
363                 }
364                 else if (!strcmp (arg, "stat") || !strcmp (arg, "status"))
365                 {
366                     Records records;
367                     rval = res_get (common_resource, "shadow");
368                     zebraIndexLock (rGroupDef.bfs, 0, rval);
369                     if (rval && *rval)
370                     {
371                         bf_cache (rGroupDef.bfs, rval);
372                         zebraIndexLockMsg ("r");
373                     }
374                     records = rec_open (rGroupDef.bfs, 0);
375                     rec_prstat (records);
376                     rec_close (&records);
377                     inv_prstat (rGroupDef.bfs);
378                 }
379                 else
380                 {
381                     logf (LOG_FATAL, "unknown command: %s", arg);
382                     exit (1);
383                 }
384             }
385             else
386             {
387                 struct recordGroup rGroup;
388
389                 rval = res_get (common_resource, "shadow");
390                 zebraIndexLock (rGroupDef.bfs, 0, rval);
391                 if (rGroupDef.flagRw)
392                 {
393                     if (rval && *rval && !disableCommit)
394                     {
395                         bf_cache (rGroupDef.bfs, rval);
396                         zebraIndexLockMsg ("r");
397                     }
398                     else
399                     {
400                         bf_cache (rGroupDef.bfs, 0);
401                         zebraIndexLockMsg ("w");
402                     }
403                     zebraIndexWait (0);
404                 }
405                 memcpy (&rGroup, &rGroupDef, sizeof(rGroup));
406                 rGroup.path = arg;
407                 switch (cmd)
408                 {
409                 case 'u':
410                     if (!key_open (&rGroup, mem_max))
411                     {
412                         logf (LOG_LOG, "updating %s", rGroup.path);
413                         repositoryUpdate (&rGroup);
414                         nsections = key_close (&rGroup);
415                     }
416                     break;
417                 case 'U':
418                     if (!key_open (&rGroup, mem_max))
419                     {
420                         logf (LOG_LOG, "updating (pass 1) %s", rGroup.path);
421                         repositoryUpdate (&rGroup);
422                         key_close (&rGroup);
423                     }
424                     nsections = 0;
425                     break;
426                 case 'd':
427                     if (!key_open (&rGroup,mem_max))
428                     {
429                         logf (LOG_LOG, "deleting %s", rGroup.path);
430                         repositoryDelete (&rGroup);
431                         nsections = key_close (&rGroup);
432                     }
433                     break;
434                 case 's':
435                     logf (LOG_LOG, "dumping %s", rGroup.path);
436                     repositoryShow (&rGroup);
437                     nsections = 0;
438                     break;
439                 case 'm':
440                     nsections = -1;
441                     break;
442                 default:
443                     nsections = 0;
444                 }
445                 cmd = 0;
446                 if (nsections)
447                 {
448                     logf (LOG_LOG, "merging with index");
449                     key_input (rGroup.bfs, nsections, 60);
450 #ifndef WINDOWS
451                     sync ();
452 #endif
453                 }
454                 log_event_end (NULL, NULL);
455             }
456         }
457         else if (ret == 'V')
458         {
459             fprintf (stderr, "Zebra %s %s\n", ZEBRAVER, ZEBRADATE);
460         }
461         else if (ret == 'v')
462             log_init (log_mask_str(arg), prog, NULL);
463         else if (ret == 'm')
464             mem_max = 1024*1024*atoi(arg);
465         else if (ret == 'd')
466             rGroupDef.databaseName = arg;
467         else if (ret == 's')
468             rGroupDef.flagRw = 0;
469         else if (ret == 'g')
470             rGroupDef.groupName = arg;
471         else if (ret == 'f')
472             rGroupDef.fileVerboseLimit = atoi(arg);
473         else if (ret == 'c')
474             configName = arg;
475         else if (ret == 't')
476             rGroupDef.recordType = arg;
477         else if (ret == 'n')
478             disableCommit = 1;
479         else
480             logf (LOG_WARN, "unknown option '-%s'", arg);
481     }
482     if (common_resource)
483     {
484         zebraIndexUnlock ();
485         bfs_destroy (rGroupDef.bfs);
486     }
487     data1_destroy (rGroupDef.dh);
488     exit (0);
489     return 0;
490 }
491