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