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