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