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