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