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