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