2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.67 1999-07-06 12:28:04 adam
8 * Updated record index structure. Format includes version ID. Compression
9 * algorithm ID is stored for each record block.
11 * Revision 1.66 1999/06/25 13:48:02 adam
12 * Updated MSVC project files.
13 * Added BZIP2 record compression (not very well tested).
15 * Revision 1.65 1999/05/20 12:57:18 adam
16 * Implemented TCL filter. Updated recctrl system.
18 * Revision 1.64 1999/05/15 14:36:38 adam
19 * Updated dictionary. Implemented "compression" of dictionary.
21 * Revision 1.63 1999/03/09 16:27:49 adam
22 * More work on SDRKit integration.
24 * Revision 1.62 1999/02/18 15:01:24 adam
27 * Revision 1.61 1999/02/02 14:51:00 adam
28 * Updated WIN32 code specific sections. Changed header.
30 * Revision 1.60 1998/10/28 10:54:39 adam
33 * Revision 1.59 1998/10/16 08:14:32 adam
34 * Updated record control system.
36 * Revision 1.58 1998/06/08 14:43:13 adam
37 * Added suport for EXPLAIN Proxy servers - added settings databasePath
38 * and explainDatabase to facilitate this. Increased maximum number
39 * of databases and attributes in one register.
41 * Revision 1.57 1998/05/20 10:12:18 adam
42 * Implemented automatic EXPLAIN database maintenance.
43 * Modified Zebra to work with ASN.1 compiled version of YAZ.
45 * Revision 1.56 1998/03/05 08:45:12 adam
46 * New result set model and modular ranking system. Moved towards
47 * descent server API. System information stored as "SGML" records.
49 * Revision 1.55 1998/01/26 10:37:34 adam
52 * Revision 1.54 1998/01/12 15:04:08 adam
53 * The test option (-s) only uses read-lock (and not write lock).
55 * Revision 1.53 1997/11/18 10:05:08 adam
56 * Changed character map facility so that admin can specify character
57 * mapping files for each register type, w, p, etc.
59 * Revision 1.52 1997/10/31 12:34:04 adam
60 * Added missing nmem_init.
62 * Revision 1.51 1997/10/27 14:33:05 adam
63 * Moved towards generic character mapping depending on "structure"
64 * field in abstract syntax file. Fixed a few memory leaks. Fixed
65 * bug with negative integers when doing searches with relational
68 * Revision 1.50 1997/09/25 14:55:52 adam
71 * Revision 1.49 1997/09/17 12:19:15 adam
72 * Zebra version corresponds to YAZ version 1.4.
73 * Changed Zebra server so that it doesn't depend on global common_resource.
75 * Revision 1.48 1997/09/09 13:38:08 adam
76 * Partial port to WIN95/NT.
78 * Revision 1.47 1997/02/12 20:39:46 adam
79 * Implemented options -f <n> that limits the log to the first <n>
81 * Changed some log messages also.
83 * Revision 1.46 1997/02/10 10:20:13 adam
84 * Flag fileVerboseFlag set to 0 (default).
86 * Revision 1.45 1996/11/08 11:10:26 adam
87 * Buffers used during file match got bigger.
88 * Compressed ISAM support everywhere.
89 * Bug fixes regarding masking characters in queries.
90 * Redesigned Regexp-2 queries.
92 * Revision 1.44 1996/10/29 14:09:48 adam
93 * Use of cisam system - enabled if setting isamc is 1.
95 * Revision 1.43 1996/06/06 12:08:42 quinn
96 * Added showRecord function
98 * Revision 1.42 1996/05/31 09:07:01 quinn
99 * Work on character-set handling
101 * Revision 1.41 1996/05/14 14:04:34 adam
102 * In zebraidx, the 'stat' command is improved. Statistics about ISAM/DICT
105 * Revision 1.40 1996/04/26 10:00:23 adam
106 * Added option -V to zebraidx to display version information.
107 * Removed stupid warnings from file update.
109 * Revision 1.39 1996/04/09 10:05:21 adam
110 * Bug fix: prev_name buffer possibly too small; allocated in key_file_init.
112 * Revision 1.38 1996/03/26 16:01:14 adam
113 * New setting lockPath: directory of various lock files.
115 * Revision 1.37 1996/03/19 12:43:26 adam
116 * Bug fix: File update traversal didn't handle trailing slashes correctly.
117 * Bug fix: Update of sub directory groups wasn't handled correctly.
119 * Revision 1.36 1996/02/12 18:45:37 adam
120 * New fileVerboseFlag in record group control.
122 * Revision 1.35 1996/02/12 15:56:11 adam
123 * New code command: unread.
125 * Revision 1.34 1996/02/07 14:06:39 adam
126 * Better progress report during register merge.
127 * New command: clean - removes temporary shadow files.
129 * Revision 1.33 1996/02/06 17:11:18 adam
132 * Revision 1.32 1996/02/01 20:50:04 adam
133 * Bug fix: zebraIndexUnlock was always called even though zebraIndexLock
134 * was never called - happens when no commands are specified.
136 * Revision 1.31 1996/01/08 19:15:46 adam
137 * New input filter that works!
139 * Revision 1.30 1995/12/12 16:00:59 adam
140 * System call sync(2) used after update/commit.
141 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
144 * Revision 1.29 1995/12/11 11:43:30 adam
145 * Locking based on fcntl instead of flock.
146 * Setting commitEnable removed. Command line option -n can be used to
147 * prevent commit if commit setting is defined in the configuration file.
149 * Revision 1.28 1995/12/08 16:22:56 adam
150 * Work on update while servers are running. Three lock files introduced.
151 * The servers reload their registers when necessary, but they don't
152 * reestablish result sets yet.
154 * Revision 1.27 1995/12/07 17:38:47 adam
155 * Work locking mechanisms for concurrent updates/commit.
157 * Revision 1.26 1995/12/06 12:41:23 adam
158 * New command 'stat' for the index program.
159 * Filenames can be read from stdin by specifying '-'.
160 * Bug fix/enhancement of the transformation from terms to regular
161 * expressons in the search engine.
163 * Revision 1.25 1995/12/01 16:24:39 adam
164 * Commit files use separate meta file area.
166 * Revision 1.24 1995/11/30 17:01:38 adam
167 * New setting commitCache: points to commit directories/files.
168 * New command commit: commits at the end of a zebraidx run.
170 * Revision 1.23 1995/11/30 08:34:31 adam
171 * Started work on commit facility.
172 * Changed a few malloc/free to xmalloc/xfree.
174 * Revision 1.22 1995/11/28 09:09:42 adam
175 * Zebra config renamed.
176 * Use setting 'recordId' to identify record now.
177 * Bug fix in recindex.c: rec_release_blocks was invokeded even
178 * though the blocks were already released.
179 * File traversal properly deletes records when needed.
181 * Revision 1.21 1995/11/27 14:27:39 adam
182 * Renamed 'update' command to 'dir'.
184 * Revision 1.20 1995/11/27 13:58:53 adam
185 * New option -t. storeStore data implemented in server.
187 * Revision 1.19 1995/11/25 10:24:06 adam
188 * More record fields - they are enumerated now.
189 * New options: flagStoreData flagStoreKey.
191 * Revision 1.18 1995/11/22 17:19:17 adam
192 * Record management uses the bfile system.
194 * Revision 1.17 1995/11/21 15:01:16 adam
195 * New general match criteria implemented.
196 * New feature: document groups.
198 * Revision 1.16 1995/11/20 11:56:27 adam
199 * Work on new traversal.
201 * Revision 1.15 1995/11/01 16:25:51 quinn
202 * *** empty log message ***
204 * Revision 1.14 1995/10/17 18:02:09 adam
205 * New feature: databases. Implemented as prefix to words in dictionary.
207 * Revision 1.13 1995/10/10 12:24:39 adam
208 * Temporary sort files are compressed.
210 * Revision 1.12 1995/10/04 16:57:20 adam
211 * Key input and merge sort in one pass.
213 * Revision 1.11 1995/09/29 14:01:45 adam
216 * Revision 1.10 1995/09/28 14:22:57 adam
217 * Sort uses smaller temporary files.
219 * Revision 1.9 1995/09/14 07:48:24 adam
220 * Record control management.
222 * Revision 1.8 1995/09/06 16:11:18 adam
223 * Option: only one word key per file.
225 * Revision 1.7 1995/09/05 15:28:39 adam
226 * More work on search engine.
228 * Revision 1.6 1995/09/04 12:33:43 adam
229 * Various cleanup. YAZ util used instead.
231 * Revision 1.5 1995/09/04 09:10:39 adam
232 * More work on index add/del/update.
233 * Merge sort implemented.
234 * Initial work on z39 server.
236 * Revision 1.4 1995/09/01 14:06:36 adam
237 * Split of work into more files.
239 * Revision 1.3 1995/09/01 10:57:07 adam
242 * Revision 1.2 1995/09/01 10:30:24 adam
243 * More work on indexing. Not working yet.
245 * Revision 1.1 1995/08/31 14:50:24 adam
246 * New simple file index tool.
259 #include "recindex.h"
266 #include "zebrasdr.h"
271 Res common_resource = 0;
273 int main (int argc, char **argv)
278 char *configName = NULL;
280 int disableCommit = 0;
283 struct recordGroup rGroupDef;
289 rGroupDef.useSDR = 0;
291 rGroupDef.groupName = NULL;
292 rGroupDef.databaseName = NULL;
293 rGroupDef.path = NULL;
294 rGroupDef.recordId = NULL;
295 rGroupDef.recordType = NULL;
296 rGroupDef.flagStoreData = -1;
297 rGroupDef.flagStoreKeys = -1;
298 rGroupDef.flagRw = 1;
299 rGroupDef.databaseNamePath = 0;
300 rGroupDef.explainDatabase = 0;
301 rGroupDef.fileVerboseLimit = 100000;
302 rGroupDef.zebra_maps = NULL;
303 rGroupDef.dh = data1_create ();
304 rGroupDef.recTypes = recTypes_init (rGroupDef.dh);
305 recTypes_default_handlers (rGroupDef.recTypes);
310 fprintf (stderr, "zebraidx [options] command <dir> ...\n"
312 " update <dir> Update index with files below <dir>.\n"
313 " If <dir> is empty filenames are read from stdin.\n"
314 " delete <dir> Delete index with files below <dir>.\n"
315 " commit Commit changes\n"
316 " clean Clean shadow files\n"
318 " -t <type> Index files as <type> (grs or text).\n"
319 " -c <config> Read configuration file <config>.\n"
320 " -g <group> Index files according to group settings.\n"
321 " -d <database> Records belong to Z39.50 database <database>.\n"
322 " -m <mbytes> Use <mbytes> before flushing keys to disk.\n"
323 " -n Don't use shadow system.\n"
324 " -s Show analysis on stdout, but do no work.\n"
325 " -v <level> Set logging to <level>.\n"
326 " -l <file> Write log to <file>.\n"
327 " -f <n> Display information for the first <n> records.\n"
331 " -V Show version.\n"
335 while ((ret = options ("sVt:c:g:d:m:v:nf:l:"
339 , argv, argc, &arg)) != -2)
344 if(cmd == 0) /* command */
346 if (!common_resource)
348 logf (LOG_LOG, "zebra version %s %s",
349 ZEBRAVER, ZEBRADATE);
350 common_resource = res_open (configName ?
351 configName : FNAME_CONFIG);
352 if (!common_resource)
354 logf (LOG_FATAL, "cannot configuration file `%s'",
358 data1_set_tabpath (rGroupDef.dh, res_get (common_resource,
362 bfs_create (res_get (common_resource, "register"));
364 bf_lockDir (rGroupDef.bfs,
365 res_get (common_resource, "lockDir"));
366 rGroupDef.zebra_maps = zebra_maps_open (common_resource);
368 if (!strcmp (arg, "update"))
370 else if (!strcmp (arg, "update1"))
372 else if (!strcmp (arg, "update2"))
374 else if (!strcmp (arg, "dump"))
376 else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
378 else if (!strcmp (arg, "commit"))
380 rval = res_get (common_resource, "shadow");
381 zebraIndexLock (rGroupDef.bfs, 1, rval);
383 bf_cache (rGroupDef.bfs, rval);
386 logf (LOG_FATAL, "Cannot perform commit");
387 logf (LOG_FATAL, "No shadow area defined");
390 if (bf_commitExists (rGroupDef.bfs))
392 logf (LOG_LOG, "commit start");
393 zebraIndexLockMsg ("c");
395 logf (LOG_LOG, "commit execute");
396 bf_commitExec (rGroupDef.bfs);
400 zebraIndexLockMsg ("d");
402 logf (LOG_LOG, "commit clean");
403 bf_commitClean (rGroupDef.bfs, rval);
406 logf (LOG_LOG, "cothing to commit");
408 else if (!strcmp (arg, "clean"))
410 rval = res_get (common_resource, "shadow");
411 zebraIndexLock (rGroupDef.bfs, 1, rval);
412 if (bf_commitExists (rGroupDef.bfs))
414 zebraIndexLockMsg ("d");
416 logf (LOG_LOG, "commit clean");
417 bf_commitClean (rGroupDef.bfs, rval);
420 logf (LOG_LOG, "nothing to clean");
422 else if (!strcmp (arg, "stat") || !strcmp (arg, "status"))
425 rval = res_get (common_resource, "shadow");
426 zebraIndexLock (rGroupDef.bfs, 0, rval);
429 bf_cache (rGroupDef.bfs, rval);
430 zebraIndexLockMsg ("r");
432 records = rec_open (rGroupDef.bfs, 0, 0);
433 rec_prstat (records);
434 rec_close (&records);
435 inv_prstat (rGroupDef.bfs);
437 else if (!strcmp (arg, "compact"))
439 rval = res_get (common_resource, "shadow");
440 zebraIndexLock (rGroupDef.bfs, 0, rval);
443 bf_cache (rGroupDef.bfs, rval);
444 zebraIndexLockMsg ("r");
446 inv_compact(rGroupDef.bfs);
450 logf (LOG_FATAL, "unknown command: %s", arg);
456 struct recordGroup rGroup;
458 rval = res_get (common_resource, "shadow");
459 zebraIndexLock (rGroupDef.bfs, 0, rval);
460 if (rGroupDef.flagRw)
462 if (rval && *rval && !disableCommit)
464 bf_cache (rGroupDef.bfs, rval);
465 zebraIndexLockMsg ("r");
469 bf_cache (rGroupDef.bfs, 0);
470 zebraIndexLockMsg ("w");
474 memcpy (&rGroup, &rGroupDef, sizeof(rGroup));
479 if (!key_open (&rGroup, mem_max))
481 logf (LOG_LOG, "updating %s", rGroup.path);
482 repositoryUpdate (&rGroup);
483 nsections = key_close (&rGroup);
487 if (!key_open (&rGroup, mem_max))
489 logf (LOG_LOG, "updating (pass 1) %s", rGroup.path);
490 repositoryUpdate (&rGroup);
496 if (!key_open (&rGroup,mem_max))
498 logf (LOG_LOG, "deleting %s", rGroup.path);
499 repositoryDelete (&rGroup);
500 nsections = key_close (&rGroup);
504 logf (LOG_LOG, "dumping %s", rGroup.path);
505 repositoryShow (&rGroup);
517 logf (LOG_LOG, "merging with index");
518 key_input (rGroup.bfs, nsections, 60);
523 log_event_end (NULL, NULL);
528 fprintf (stderr, "Zebra %s %s\n", ZEBRAVER, ZEBRADATE);
529 fprintf (stderr, " (C) 1994-1999, Index Data ApS\n");
531 fprintf (stderr, "libbzip2\n"
532 " (C) 1996-1998 Julian R Seward. All rights reserved.\n");
536 log_init_level (log_mask_str(arg));
540 mem_max = 1024*1024*atoi(arg);
542 rGroupDef.databaseName = arg;
544 rGroupDef.flagRw = 0;
546 rGroupDef.groupName = arg;
548 rGroupDef.fileVerboseLimit = atoi(arg);
552 rGroupDef.recordType = arg;
557 rGroupDef.useSDR = 1;
560 logf (LOG_WARN, "unknown option '-%s'", arg);
562 recTypes_destroy (rGroupDef.recTypes);
566 bfs_destroy (rGroupDef.bfs);
568 data1_destroy (rGroupDef.dh);