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