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