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