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