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