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