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