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