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