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