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