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