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