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