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