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