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