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