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