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