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