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