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