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