Added remote record import using Z39.50 extended services and Segment
[idzebra-moved-to-github.git] / index / main.c
1 /*
2  * Copyright (C) 1994-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: main.c,v $
7  * Revision 1.76  2000-03-20 19:08:36  adam
8  * Added remote record import using Z39.50 extended services and Segment
9  * Requests.
10  *
11  * Revision 1.75  1999/12/08 22:44:45  adam
12  * Zebra/Z'mbol dependencies added.
13  *
14  * Revision 1.74  1999/12/08 15:03:11  adam
15  * Implemented bf_reset.
16  *
17  *
18  * Revision 1.73  1999/11/30 13:48:03  adam
19  * Improved installation. Updated for inclusion of YAZ header files.
20  *
21  * Revision 1.72  1999/10/14 14:33:50  adam
22  * Added truncation 5=106.
23  *
24  * Revision 1.71  1999/09/08 12:12:06  adam
25  * Fixed bad message.
26  *
27  * Revision 1.70  1999/09/07 07:19:21  adam
28  * Work on character mapping. Implemented replace rules.
29  *
30  * Revision 1.69  1999/07/21 08:31:33  adam
31  * More version info on WIN32.
32  *
33  * Revision 1.68  1999/07/14 10:59:26  adam
34  * Changed functions isc_getmethod, isams_getmethod.
35  * Improved fatal error handling (such as missing EXPLAIN schema).
36  *
37  * Revision 1.67  1999/07/06 12:28:04  adam
38  * Updated record index structure. Format includes version ID. Compression
39  * algorithm ID is stored for each record block.
40  *
41  * Revision 1.66  1999/06/25 13:48:02  adam
42  * Updated MSVC project files.
43  * Added BZIP2 record compression (not very well tested).
44  *
45  * Revision 1.65  1999/05/20 12:57:18  adam
46  * Implemented TCL filter. Updated recctrl system.
47  *
48  * Revision 1.64  1999/05/15 14:36:38  adam
49  * Updated dictionary. Implemented "compression" of dictionary.
50  *
51  * Revision 1.63  1999/03/09 16:27:49  adam
52  * More work on SDRKit integration.
53  *
54  * Revision 1.62  1999/02/18 15:01:24  adam
55  * Minor changes.
56  *
57  * Revision 1.61  1999/02/02 14:51:00  adam
58  * Updated WIN32 code specific sections. Changed header.
59  *
60  * Revision 1.60  1998/10/28 10:54:39  adam
61  * SDRKit integration.
62  *
63  * Revision 1.59  1998/10/16 08:14:32  adam
64  * Updated record control system.
65  *
66  * Revision 1.58  1998/06/08 14:43:13  adam
67  * Added suport for EXPLAIN Proxy servers - added settings databasePath
68  * and explainDatabase to facilitate this. Increased maximum number
69  * of databases and attributes in one register.
70  *
71  * Revision 1.57  1998/05/20 10:12:18  adam
72  * Implemented automatic EXPLAIN database maintenance.
73  * Modified Zebra to work with ASN.1 compiled version of YAZ.
74  *
75  * Revision 1.56  1998/03/05 08:45:12  adam
76  * New result set model and modular ranking system. Moved towards
77  * descent server API. System information stored as "SGML" records.
78  *
79  * Revision 1.55  1998/01/26 10:37:34  adam
80  * Minor changes.
81  *
82  * Revision 1.54  1998/01/12 15:04:08  adam
83  * The test option (-s) only uses read-lock (and not write lock).
84  *
85  * Revision 1.53  1997/11/18 10:05:08  adam
86  * Changed character map facility so that admin can specify character
87  * mapping files for each register type, w, p, etc.
88  *
89  * Revision 1.52  1997/10/31 12:34:04  adam
90  * Added missing nmem_init.
91  *
92  * Revision 1.51  1997/10/27 14:33:05  adam
93  * Moved towards generic character mapping depending on "structure"
94  * field in abstract syntax file. Fixed a few memory leaks. Fixed
95  * bug with negative integers when doing searches with relational
96  * operators.
97  *
98  * Revision 1.50  1997/09/25 14:55:52  adam
99  * Minor changes.
100  *
101  * Revision 1.49  1997/09/17 12:19:15  adam
102  * Zebra version corresponds to YAZ version 1.4.
103  * Changed Zebra server so that it doesn't depend on global common_resource.
104  *
105  * Revision 1.48  1997/09/09 13:38:08  adam
106  * Partial port to WIN95/NT.
107  *
108  * Revision 1.47  1997/02/12 20:39:46  adam
109  * Implemented options -f <n> that limits the log to the first <n>
110  * records.
111  * Changed some log messages also.
112  *
113  * Revision 1.46  1997/02/10 10:20:13  adam
114  * Flag fileVerboseFlag set to 0 (default).
115  *
116  * Revision 1.45  1996/11/08 11:10:26  adam
117  * Buffers used during file match got bigger.
118  * Compressed ISAM support everywhere.
119  * Bug fixes regarding masking characters in queries.
120  * Redesigned Regexp-2 queries.
121  *
122  * Revision 1.44  1996/10/29 14:09:48  adam
123  * Use of cisam system - enabled if setting isamc is 1.
124  *
125  * Revision 1.43  1996/06/06 12:08:42  quinn
126  * Added showRecord function
127  *
128  * Revision 1.42  1996/05/31  09:07:01  quinn
129  * Work on character-set handling
130  *
131  * Revision 1.41  1996/05/14  14:04:34  adam
132  * In zebraidx, the 'stat' command is improved. Statistics about ISAM/DICT
133  * is collected.
134  *
135  * Revision 1.40  1996/04/26  10:00:23  adam
136  * Added option -V to zebraidx to display version information.
137  * Removed stupid warnings from file update.
138  *
139  * Revision 1.39  1996/04/09  10:05:21  adam
140  * Bug fix: prev_name buffer possibly too small; allocated in key_file_init.
141  *
142  * Revision 1.38  1996/03/26  16:01:14  adam
143  * New setting lockPath: directory of various lock files.
144  *
145  * Revision 1.37  1996/03/19  12:43:26  adam
146  * Bug fix: File update traversal didn't handle trailing slashes correctly.
147  * Bug fix: Update of sub directory groups wasn't handled correctly.
148  *
149  * Revision 1.36  1996/02/12  18:45:37  adam
150  * New fileVerboseFlag in record group control.
151  *
152  * Revision 1.35  1996/02/12  15:56:11  adam
153  * New code command: unread.
154  *
155  * Revision 1.34  1996/02/07  14:06:39  adam
156  * Better progress report during register merge.
157  * New command: clean - removes temporary shadow files.
158  *
159  * Revision 1.33  1996/02/06  17:11:18  adam
160  * Minor changes.
161  *
162  * Revision 1.32  1996/02/01  20:50:04  adam
163  * Bug fix: zebraIndexUnlock was always called even though zebraIndexLock
164  * was never called - happens when no commands are specified.
165  *
166  * Revision 1.31  1996/01/08  19:15:46  adam
167  * New input filter that works!
168  *
169  * Revision 1.30  1995/12/12  16:00:59  adam
170  * System call sync(2) used after update/commit.
171  * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
172  * and F_RDLCK.
173  *
174  * Revision 1.29  1995/12/11  11:43:30  adam
175  * Locking based on fcntl instead of flock.
176  * Setting commitEnable removed. Command line option -n can be used to
177  * prevent commit if commit setting is defined in the configuration file.
178  *
179  * Revision 1.28  1995/12/08  16:22:56  adam
180  * Work on update while servers are running. Three lock files introduced.
181  * The servers reload their registers when necessary, but they don't
182  * reestablish result sets yet.
183  *
184  * Revision 1.27  1995/12/07  17:38:47  adam
185  * Work locking mechanisms for concurrent updates/commit.
186  *
187  * Revision 1.26  1995/12/06  12:41:23  adam
188  * New command 'stat' for the index program.
189  * Filenames can be read from stdin by specifying '-'.
190  * Bug fix/enhancement of the transformation from terms to regular
191  * expressons in the search engine.
192  *
193  * Revision 1.25  1995/12/01  16:24:39  adam
194  * Commit files use separate meta file area.
195  *
196  * Revision 1.24  1995/11/30  17:01:38  adam
197  * New setting commitCache: points to commit directories/files.
198  * New command commit: commits at the end of a zebraidx run.
199  *
200  * Revision 1.23  1995/11/30  08:34:31  adam
201  * Started work on commit facility.
202  * Changed a few malloc/free to xmalloc/xfree.
203  *
204  * Revision 1.22  1995/11/28  09:09:42  adam
205  * Zebra config renamed.
206  * Use setting 'recordId' to identify record now.
207  * Bug fix in recindex.c: rec_release_blocks was invokeded even
208  * though the blocks were already released.
209  * File traversal properly deletes records when needed.
210  *
211  * Revision 1.21  1995/11/27  14:27:39  adam
212  * Renamed 'update' command to 'dir'.
213  *
214  * Revision 1.20  1995/11/27  13:58:53  adam
215  * New option -t. storeStore data implemented in server.
216  *
217  * Revision 1.19  1995/11/25  10:24:06  adam
218  * More record fields - they are enumerated now.
219  * New options: flagStoreData flagStoreKey.
220  *
221  * Revision 1.18  1995/11/22  17:19:17  adam
222  * Record management uses the bfile system.
223  *
224  * Revision 1.17  1995/11/21  15:01:16  adam
225  * New general match criteria implemented.
226  * New feature: document groups.
227  *
228  * Revision 1.16  1995/11/20  11:56:27  adam
229  * Work on new traversal.
230  *
231  * Revision 1.15  1995/11/01  16:25:51  quinn
232  * *** empty log message ***
233  *
234  * Revision 1.14  1995/10/17  18:02:09  adam
235  * New feature: databases. Implemented as prefix to words in dictionary.
236  *
237  * Revision 1.13  1995/10/10  12:24:39  adam
238  * Temporary sort files are compressed.
239  *
240  * Revision 1.12  1995/10/04  16:57:20  adam
241  * Key input and merge sort in one pass.
242  *
243  * Revision 1.11  1995/09/29  14:01:45  adam
244  * Bug fixes.
245  *
246  * Revision 1.10  1995/09/28  14:22:57  adam
247  * Sort uses smaller temporary files.
248  *
249  * Revision 1.9  1995/09/14  07:48:24  adam
250  * Record control management.
251  *
252  * Revision 1.8  1995/09/06  16:11:18  adam
253  * Option: only one word key per file.
254  *
255  * Revision 1.7  1995/09/05  15:28:39  adam
256  * More work on search engine.
257  *
258  * Revision 1.6  1995/09/04  12:33:43  adam
259  * Various cleanup. YAZ util used instead.
260  *
261  * Revision 1.5  1995/09/04  09:10:39  adam
262  * More work on index add/del/update.
263  * Merge sort implemented.
264  * Initial work on z39 server.
265  *
266  * Revision 1.4  1995/09/01  14:06:36  adam
267  * Split of work into more files.
268  *
269  * Revision 1.3  1995/09/01  10:57:07  adam
270  * Minor changes.
271  *
272  * Revision 1.2  1995/09/01  10:30:24  adam
273  * More work on indexing. Not working yet.
274  *
275  * Revision 1.1  1995/08/31  14:50:24  adam
276  * New simple file index tool.
277  *
278  */
279 #include <stdio.h>
280 #include <assert.h>
281 #ifdef WIN32
282 #include <io.h>
283 #else
284 #include <unistd.h>
285 #endif
286
287 #include <yaz/data1.h>
288 #include "index.h"
289 #include "recindex.h"
290
291 #ifndef ZEBRASDR
292 #define ZEBRASDR 0
293 #endif
294
295 #if ZEBRASDR
296 #include "zebrasdr.h"
297 #endif
298
299 char *prog;
300
301 Res common_resource = 0;
302
303
304 int main (int argc, char **argv)
305 {
306     int ret;
307     int cmd = 0;
308     char *arg;
309     char *configName = FNAME_CONFIG;
310     int nsections = 0;
311     int disableCommit = 0;
312     size_t mem_max = 0;
313
314     struct recordGroup rGroupDef;
315
316     nmem_init ();
317
318 #if ZEBRASDR
319     zebraSdr_std ();
320     rGroupDef.useSDR = 0;
321 #endif
322     rGroupDef.groupName = NULL;
323     rGroupDef.databaseName = NULL;
324     rGroupDef.path = NULL;
325     rGroupDef.recordId = NULL;
326     rGroupDef.recordType = NULL;
327     rGroupDef.flagStoreData = -1;
328     rGroupDef.flagStoreKeys = -1;
329     rGroupDef.flagRw = 1;
330     rGroupDef.databaseNamePath = 0;
331     rGroupDef.explainDatabase = 0;
332     rGroupDef.fileVerboseLimit = 100000;
333     rGroupDef.zebra_maps = NULL;
334     rGroupDef.dh = data1_create ();
335     rGroupDef.recTypes = recTypes_init (rGroupDef.dh);
336     recTypes_default_handlers (rGroupDef.recTypes);
337
338     prog = *argv;
339     if (argc < 2)
340     {
341         fprintf (stderr, "%s [options] command <dir> ...\n"
342         "Commands:\n"
343         " update <dir>  Update index with files below <dir>.\n"
344         "               If <dir> is empty filenames are read from stdin.\n"
345         " delete <dir>  Delete index with files below <dir>.\n"
346         " commit        Commit changes\n"
347         " clean         Clean shadow files\n"
348         "Options:\n"
349         " -t <type>     Index files as <type> (grs or text).\n"
350         " -c <config>   Read configuration file <config>.\n"
351         " -g <group>    Index files according to group settings.\n"
352         " -d <database> Records belong to Z39.50 database <database>.\n"
353         " -m <mbytes>   Use <mbytes> before flushing keys to disk.\n"
354         " -n            Don't use shadow system.\n"
355         " -s            Show analysis on stdout, but do no work.\n"
356         " -v <level>    Set logging to <level>.\n"
357         " -l <file>     Write log to <file>.\n"
358         " -f <n>        Display information for the first <n> records.\n"
359 #if ZEBRASDR
360         " -S            Use SDRKit\n"
361 #endif
362         " -V            Show version.\n", *argv
363                  );
364         exit (1);
365     }
366     while ((ret = options ("sVt:c:g:d:m:v:nf:l:"
367 #if ZEBRASDR
368                            "S"
369 #endif
370                            , argv, argc, &arg)) != -2)
371     {
372         if (ret == 0)
373         {
374             const char *rval;
375             if(cmd == 0) /* command */
376             {
377                 if (!common_resource)
378                 {
379 #if ZMBOL
380                     logf (LOG_LOG, "zmbol version %s %s",
381                           ZEBRAVER, ZEBRADATE);
382 #else
383                     logf (LOG_LOG, "zebra version %s %s",
384                           ZEBRAVER, ZEBRADATE);
385 #endif
386                     common_resource = res_open (configName ?
387                                                 configName : FNAME_CONFIG);
388                     if (!common_resource)
389                     {
390                         logf (LOG_FATAL, "cannot read file `%s'", configName);
391                         exit (1);
392                     }
393                     data1_set_tabpath (rGroupDef.dh, res_get (common_resource,
394                                                               "profilePath"));
395
396                     rGroupDef.bfs =
397                         bfs_create (res_get (common_resource, "register"));
398                     if (!rGroupDef.bfs)
399                     {
400                         logf (LOG_FATAL, "Cannot access register");
401                         exit(1);
402                     }
403
404                     bf_lockDir (rGroupDef.bfs,
405                                 res_get (common_resource, "lockDir"));
406                     rGroupDef.zebra_maps = zebra_maps_open (common_resource);
407                 }
408                 if (!strcmp (arg, "update"))
409                     cmd = 'u';
410                 else if (!strcmp (arg, "update1"))
411                     cmd = 'U';
412                 else if (!strcmp (arg, "update2"))
413                     cmd = 'm';
414                 else if (!strcmp (arg, "dump"))
415                     cmd = 's';
416                 else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
417                     cmd = 'd';
418                 else if (!strcmp (arg, "init"))
419                 {
420                     zebraIndexUnlock(); 
421                     rval = res_get (common_resource, "shadow");
422                     zebraIndexLock (rGroupDef.bfs, 0, rval);
423                     zebraIndexLockMsg ("w");
424                     bf_reset (rGroupDef.bfs);
425                 }
426                 else if (!strcmp (arg, "commit"))
427                 {
428                     rval = res_get (common_resource, "shadow");
429                     zebraIndexLock (rGroupDef.bfs, 1, rval);
430                     if (rval && *rval)
431                         bf_cache (rGroupDef.bfs, rval);
432                     else
433                     {
434                         logf (LOG_FATAL, "Cannot perform commit");
435                         logf (LOG_FATAL, "No shadow area defined");
436                         exit (1);
437                     }
438                     if (bf_commitExists (rGroupDef.bfs))
439                     {
440                         logf (LOG_LOG, "commit start");
441                         zebraIndexLockMsg ("c");
442                         zebraIndexWait (1);
443                         logf (LOG_LOG, "commit execute");
444                         bf_commitExec (rGroupDef.bfs);
445 #ifndef WIN32
446                         sync ();
447 #endif
448                         zebraIndexLockMsg ("d");
449                         zebraIndexWait (0);
450                         logf (LOG_LOG, "commit clean");
451                         bf_commitClean (rGroupDef.bfs, rval);
452                     }
453                     else
454                         logf (LOG_LOG, "cothing to commit");
455                 }
456                 else if (!strcmp (arg, "clean"))
457                 {
458                     rval = res_get (common_resource, "shadow");
459                     zebraIndexLock (rGroupDef.bfs, 1, rval);
460                     if (bf_commitExists (rGroupDef.bfs))
461                     {
462                         zebraIndexLockMsg ("d");
463                         zebraIndexWait (0);
464                         logf (LOG_LOG, "commit clean");
465                         bf_commitClean (rGroupDef.bfs, rval);
466                     }
467                     else
468                         logf (LOG_LOG, "nothing to clean");
469                 }
470                 else if (!strcmp (arg, "stat") || !strcmp (arg, "status"))
471                 {
472                     Records records;
473                     rval = res_get (common_resource, "shadow");
474                     zebraIndexLock (rGroupDef.bfs, 0, rval);
475                     if (rval && *rval)
476                     {
477                         bf_cache (rGroupDef.bfs, rval);
478                         zebraIndexLockMsg ("r");
479                     }
480                     records = rec_open (rGroupDef.bfs, 0, 0);
481                     rec_prstat (records);
482                     rec_close (&records);
483                     inv_prstat (rGroupDef.bfs);
484                 }
485                 else if (!strcmp (arg, "compact"))
486                 {
487                     rval = res_get (common_resource, "shadow");
488                     zebraIndexLock (rGroupDef.bfs, 0, rval);
489                     if (rval && *rval)
490                     {
491                         bf_cache (rGroupDef.bfs, rval);
492                         zebraIndexLockMsg ("r");
493                     }
494                     inv_compact(rGroupDef.bfs);
495                 }
496                 else
497                 {
498                     logf (LOG_FATAL, "unknown command: %s", arg);
499                     exit (1);
500                 }
501             }
502             else
503             {
504                 struct recordGroup rGroup;
505 #if ZMBOL
506 #else
507                 /* For zebra, delete lock file and reset register */
508                 if (rGroupDef.flagRw)
509                 {
510                     zebraIndexUnlock();
511                     bf_reset (rGroupDef.bfs);
512                 }
513 #endif
514                 rval = res_get (common_resource, "shadow");
515                 zebraIndexLock (rGroupDef.bfs, 0, rval);
516                 if (rGroupDef.flagRw)
517                 {
518                     if (rval && *rval && !disableCommit)
519                     {
520                         bf_cache (rGroupDef.bfs, rval);
521                         zebraIndexLockMsg ("r");
522                     }
523                     else
524                     {
525                         bf_cache (rGroupDef.bfs, 0);
526                         zebraIndexLockMsg ("w");
527                     }
528                     zebraIndexWait (0);
529                 }
530                 memcpy (&rGroup, &rGroupDef, sizeof(rGroup));
531                 rGroup.path = arg;
532                 switch (cmd)
533                 {
534                 case 'u':
535                     if (!key_open (&rGroup, mem_max))
536                     {
537                         logf (LOG_LOG, "updating %s", rGroup.path);
538                         repositoryUpdate (&rGroup);
539                         nsections = key_close (&rGroup);
540                     }
541                     break;
542                 case 'U':
543                     if (!key_open (&rGroup, mem_max))
544                     {
545                         logf (LOG_LOG, "updating (pass 1) %s", rGroup.path);
546                         repositoryUpdate (&rGroup);
547                         key_close (&rGroup);
548                     }
549                     nsections = 0;
550                     break;
551                 case 'd':
552                     if (!key_open (&rGroup,mem_max))
553                     {
554                         logf (LOG_LOG, "deleting %s", rGroup.path);
555                         repositoryDelete (&rGroup);
556                         nsections = key_close (&rGroup);
557                     }
558                     break;
559                 case 's':
560                     logf (LOG_LOG, "dumping %s", rGroup.path);
561                     repositoryShow (&rGroup);
562                     nsections = 0;
563                     break;
564                 case 'm':
565                     nsections = -1;
566                     break;
567                 default:
568                     nsections = 0;
569                 }
570                 cmd = 0;
571                 if (nsections)
572                 {
573                     logf (LOG_LOG, "merging with index");
574                     key_input (rGroup.bfs, nsections, 60, common_resource);
575 #ifndef WIN32
576                     sync ();
577 #endif
578                 }
579                 log_event_end (NULL, NULL);
580             }
581         }
582         else if (ret == 'V')
583         {
584 #if ZMBOL
585             fprintf (stderr, "Z'mbol %s %s\n", ZEBRAVER, ZEBRADATE);
586 #else
587             fprintf (stderr, "Zebra %s %s\n", ZEBRAVER, ZEBRADATE);
588 #endif
589             fprintf (stderr, " (C) 1994-1999, Index Data ApS\n");
590 #ifdef WIN32
591 #ifdef _DEBUG
592             fprintf (stderr, " WIN32 Debug\n");
593 #else
594             fprintf (stderr, " WIN32 Release\n");
595 #endif
596 #endif
597 #if HAVE_BZLIB_H
598             fprintf (stderr, "libbzip2\n"
599                      " (C) 1996-1999 Julian R Seward.  All rights reserved.\n");
600 #endif
601         }
602         else if (ret == 'v')
603             log_init_level (log_mask_str(arg));
604         else if (ret == 'l')
605             log_init_file (arg);
606         else if (ret == 'm')
607             mem_max = 1024*1024*atoi(arg);
608         else if (ret == 'd')
609             rGroupDef.databaseName = arg;
610         else if (ret == 's')
611             rGroupDef.flagRw = 0;
612         else if (ret == 'g')
613             rGroupDef.groupName = arg;
614         else if (ret == 'f')
615             rGroupDef.fileVerboseLimit = atoi(arg);
616         else if (ret == 'c')
617             configName = arg;
618         else if (ret == 't')
619             rGroupDef.recordType = arg;
620         else if (ret == 'n')
621             disableCommit = 1;
622 #if ZEBRASDR
623         else if (ret == 'S')
624             rGroupDef.useSDR = 1;
625 #endif
626         else
627             logf (LOG_WARN, "unknown option '-%s'", arg);
628     }
629     recTypes_destroy (rGroupDef.recTypes);
630     if (common_resource)
631     {
632         zebraIndexUnlock ();
633         bfs_destroy (rGroupDef.bfs);
634     }
635     data1_destroy (rGroupDef.dh);
636     exit (0);
637     return 0;
638 }
639