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