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