2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.64 1999-05-15 14:36:38 adam
8 * Updated dictionary. Implemented "compression" of dictionary.
10 * Revision 1.63 1999/03/09 16:27:49 adam
11 * More work on SDRKit integration.
13 * Revision 1.62 1999/02/18 15:01:24 adam
16 * Revision 1.61 1999/02/02 14:51:00 adam
17 * Updated WIN32 code specific sections. Changed header.
19 * Revision 1.60 1998/10/28 10:54:39 adam
22 * Revision 1.59 1998/10/16 08:14:32 adam
23 * Updated record control system.
25 * Revision 1.58 1998/06/08 14:43:13 adam
26 * Added suport for EXPLAIN Proxy servers - added settings databasePath
27 * and explainDatabase to facilitate this. Increased maximum number
28 * of databases and attributes in one register.
30 * Revision 1.57 1998/05/20 10:12:18 adam
31 * Implemented automatic EXPLAIN database maintenance.
32 * Modified Zebra to work with ASN.1 compiled version of YAZ.
34 * Revision 1.56 1998/03/05 08:45:12 adam
35 * New result set model and modular ranking system. Moved towards
36 * descent server API. System information stored as "SGML" records.
38 * Revision 1.55 1998/01/26 10:37:34 adam
41 * Revision 1.54 1998/01/12 15:04:08 adam
42 * The test option (-s) only uses read-lock (and not write lock).
44 * Revision 1.53 1997/11/18 10:05:08 adam
45 * Changed character map facility so that admin can specify character
46 * mapping files for each register type, w, p, etc.
48 * Revision 1.52 1997/10/31 12:34:04 adam
49 * Added missing nmem_init.
51 * Revision 1.51 1997/10/27 14:33:05 adam
52 * Moved towards generic character mapping depending on "structure"
53 * field in abstract syntax file. Fixed a few memory leaks. Fixed
54 * bug with negative integers when doing searches with relational
57 * Revision 1.50 1997/09/25 14:55:52 adam
60 * Revision 1.49 1997/09/17 12:19:15 adam
61 * Zebra version corresponds to YAZ version 1.4.
62 * Changed Zebra server so that it doesn't depend on global common_resource.
64 * Revision 1.48 1997/09/09 13:38:08 adam
65 * Partial port to WIN95/NT.
67 * Revision 1.47 1997/02/12 20:39:46 adam
68 * Implemented options -f <n> that limits the log to the first <n>
70 * Changed some log messages also.
72 * Revision 1.46 1997/02/10 10:20:13 adam
73 * Flag fileVerboseFlag set to 0 (default).
75 * Revision 1.45 1996/11/08 11:10:26 adam
76 * Buffers used during file match got bigger.
77 * Compressed ISAM support everywhere.
78 * Bug fixes regarding masking characters in queries.
79 * Redesigned Regexp-2 queries.
81 * Revision 1.44 1996/10/29 14:09:48 adam
82 * Use of cisam system - enabled if setting isamc is 1.
84 * Revision 1.43 1996/06/06 12:08:42 quinn
85 * Added showRecord function
87 * Revision 1.42 1996/05/31 09:07:01 quinn
88 * Work on character-set handling
90 * Revision 1.41 1996/05/14 14:04:34 adam
91 * In zebraidx, the 'stat' command is improved. Statistics about ISAM/DICT
94 * Revision 1.40 1996/04/26 10:00:23 adam
95 * Added option -V to zebraidx to display version information.
96 * Removed stupid warnings from file update.
98 * Revision 1.39 1996/04/09 10:05:21 adam
99 * Bug fix: prev_name buffer possibly too small; allocated in key_file_init.
101 * Revision 1.38 1996/03/26 16:01:14 adam
102 * New setting lockPath: directory of various lock files.
104 * Revision 1.37 1996/03/19 12:43:26 adam
105 * Bug fix: File update traversal didn't handle trailing slashes correctly.
106 * Bug fix: Update of sub directory groups wasn't handled correctly.
108 * Revision 1.36 1996/02/12 18:45:37 adam
109 * New fileVerboseFlag in record group control.
111 * Revision 1.35 1996/02/12 15:56:11 adam
112 * New code command: unread.
114 * Revision 1.34 1996/02/07 14:06:39 adam
115 * Better progress report during register merge.
116 * New command: clean - removes temporary shadow files.
118 * Revision 1.33 1996/02/06 17:11:18 adam
121 * Revision 1.32 1996/02/01 20:50:04 adam
122 * Bug fix: zebraIndexUnlock was always called even though zebraIndexLock
123 * was never called - happens when no commands are specified.
125 * Revision 1.31 1996/01/08 19:15:46 adam
126 * New input filter that works!
128 * Revision 1.30 1995/12/12 16:00:59 adam
129 * System call sync(2) used after update/commit.
130 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
133 * Revision 1.29 1995/12/11 11:43:30 adam
134 * Locking based on fcntl instead of flock.
135 * Setting commitEnable removed. Command line option -n can be used to
136 * prevent commit if commit setting is defined in the configuration file.
138 * Revision 1.28 1995/12/08 16:22:56 adam
139 * Work on update while servers are running. Three lock files introduced.
140 * The servers reload their registers when necessary, but they don't
141 * reestablish result sets yet.
143 * Revision 1.27 1995/12/07 17:38:47 adam
144 * Work locking mechanisms for concurrent updates/commit.
146 * Revision 1.26 1995/12/06 12:41:23 adam
147 * New command 'stat' for the index program.
148 * Filenames can be read from stdin by specifying '-'.
149 * Bug fix/enhancement of the transformation from terms to regular
150 * expressons in the search engine.
152 * Revision 1.25 1995/12/01 16:24:39 adam
153 * Commit files use separate meta file area.
155 * Revision 1.24 1995/11/30 17:01:38 adam
156 * New setting commitCache: points to commit directories/files.
157 * New command commit: commits at the end of a zebraidx run.
159 * Revision 1.23 1995/11/30 08:34:31 adam
160 * Started work on commit facility.
161 * Changed a few malloc/free to xmalloc/xfree.
163 * Revision 1.22 1995/11/28 09:09:42 adam
164 * Zebra config renamed.
165 * Use setting 'recordId' to identify record now.
166 * Bug fix in recindex.c: rec_release_blocks was invokeded even
167 * though the blocks were already released.
168 * File traversal properly deletes records when needed.
170 * Revision 1.21 1995/11/27 14:27:39 adam
171 * Renamed 'update' command to 'dir'.
173 * Revision 1.20 1995/11/27 13:58:53 adam
174 * New option -t. storeStore data implemented in server.
176 * Revision 1.19 1995/11/25 10:24:06 adam
177 * More record fields - they are enumerated now.
178 * New options: flagStoreData flagStoreKey.
180 * Revision 1.18 1995/11/22 17:19:17 adam
181 * Record management uses the bfile system.
183 * Revision 1.17 1995/11/21 15:01:16 adam
184 * New general match criteria implemented.
185 * New feature: document groups.
187 * Revision 1.16 1995/11/20 11:56:27 adam
188 * Work on new traversal.
190 * Revision 1.15 1995/11/01 16:25:51 quinn
191 * *** empty log message ***
193 * Revision 1.14 1995/10/17 18:02:09 adam
194 * New feature: databases. Implemented as prefix to words in dictionary.
196 * Revision 1.13 1995/10/10 12:24:39 adam
197 * Temporary sort files are compressed.
199 * Revision 1.12 1995/10/04 16:57:20 adam
200 * Key input and merge sort in one pass.
202 * Revision 1.11 1995/09/29 14:01:45 adam
205 * Revision 1.10 1995/09/28 14:22:57 adam
206 * Sort uses smaller temporary files.
208 * Revision 1.9 1995/09/14 07:48:24 adam
209 * Record control management.
211 * Revision 1.8 1995/09/06 16:11:18 adam
212 * Option: only one word key per file.
214 * Revision 1.7 1995/09/05 15:28:39 adam
215 * More work on search engine.
217 * Revision 1.6 1995/09/04 12:33:43 adam
218 * Various cleanup. YAZ util used instead.
220 * Revision 1.5 1995/09/04 09:10:39 adam
221 * More work on index add/del/update.
222 * Merge sort implemented.
223 * Initial work on z39 server.
225 * Revision 1.4 1995/09/01 14:06:36 adam
226 * Split of work into more files.
228 * Revision 1.3 1995/09/01 10:57:07 adam
231 * Revision 1.2 1995/09/01 10:30:24 adam
232 * More work on indexing. Not working yet.
234 * Revision 1.1 1995/08/31 14:50:24 adam
235 * New simple file index tool.
248 #include "recindex.h"
255 #include "zebrasdr.h"
260 Res common_resource = 0;
262 int main (int argc, char **argv)
267 char *configName = NULL;
269 int disableCommit = 0;
272 struct recordGroup rGroupDef;
278 rGroupDef.useSDR = 0;
280 rGroupDef.groupName = NULL;
281 rGroupDef.databaseName = NULL;
282 rGroupDef.path = NULL;
283 rGroupDef.recordId = NULL;
284 rGroupDef.recordType = NULL;
285 rGroupDef.flagStoreData = -1;
286 rGroupDef.flagStoreKeys = -1;
287 rGroupDef.flagRw = 1;
288 rGroupDef.databaseNamePath = 0;
289 rGroupDef.explainDatabase = 0;
290 rGroupDef.fileVerboseLimit = 100000;
291 rGroupDef.zebra_maps = NULL;
292 rGroupDef.dh = data1_create ();
293 rGroupDef.recTypes = recTypes_init (rGroupDef.dh);
294 recTypes_default_handlers (rGroupDef.recTypes);
299 fprintf (stderr, "zebraidx [options] command <dir> ...\n"
301 " update <dir> Update index with files below <dir>.\n"
302 " If <dir> is empty filenames are read from stdin.\n"
303 " delete <dir> Delete index with files below <dir>.\n"
304 " commit Commit changes\n"
305 " clean Clean shadow files\n"
307 " -t <type> Index files as <type> (grs or text).\n"
308 " -c <config> Read configuration file <config>.\n"
309 " -g <group> Index files according to group settings.\n"
310 " -d <database> Records belong to Z39.50 database <database>.\n"
311 " -m <mbytes> Use <mbytes> before flushing keys to disk.\n"
312 " -n Don't use shadow system.\n"
313 " -s Show analysis on stdout, but do no work.\n"
314 " -v <level> Set logging to <level>.\n"
315 " -l <file> Write log to <file>.\n"
316 " -f <n> Display information for the first <n> records.\n"
320 " -V Show version.\n"
324 while ((ret = options ("sVt:c:g:d:m:v:nf:l:"
328 , argv, argc, &arg)) != -2)
333 if(cmd == 0) /* command */
335 if (!common_resource)
337 logf (LOG_LOG, "zebra version %s %s",
338 ZEBRAVER, ZEBRADATE);
339 common_resource = res_open (configName ?
340 configName : FNAME_CONFIG);
341 if (!common_resource)
343 logf (LOG_FATAL, "cannot configuration file `%s'",
347 data1_set_tabpath (rGroupDef.dh, res_get (common_resource,
351 bfs_create (res_get (common_resource, "register"));
353 bf_lockDir (rGroupDef.bfs,
354 res_get (common_resource, "lockDir"));
355 rGroupDef.zebra_maps = zebra_maps_open (common_resource);
357 if (!strcmp (arg, "update"))
359 else if (!strcmp (arg, "update1"))
361 else if (!strcmp (arg, "update2"))
363 else if (!strcmp (arg, "dump"))
365 else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
367 else if (!strcmp (arg, "commit"))
369 rval = res_get (common_resource, "shadow");
370 zebraIndexLock (rGroupDef.bfs, 1, rval);
372 bf_cache (rGroupDef.bfs, rval);
375 logf (LOG_FATAL, "Cannot perform commit");
376 logf (LOG_FATAL, "No shadow area defined");
379 if (bf_commitExists (rGroupDef.bfs))
381 logf (LOG_LOG, "commit start");
382 zebraIndexLockMsg ("c");
384 logf (LOG_LOG, "commit execute");
385 bf_commitExec (rGroupDef.bfs);
389 zebraIndexLockMsg ("d");
391 logf (LOG_LOG, "commit clean");
392 bf_commitClean (rGroupDef.bfs, rval);
395 logf (LOG_LOG, "cothing to commit");
397 else if (!strcmp (arg, "clean"))
399 rval = res_get (common_resource, "shadow");
400 zebraIndexLock (rGroupDef.bfs, 1, rval);
401 if (bf_commitExists (rGroupDef.bfs))
403 zebraIndexLockMsg ("d");
405 logf (LOG_LOG, "commit clean");
406 bf_commitClean (rGroupDef.bfs, rval);
409 logf (LOG_LOG, "nothing to clean");
411 else if (!strcmp (arg, "stat") || !strcmp (arg, "status"))
414 rval = res_get (common_resource, "shadow");
415 zebraIndexLock (rGroupDef.bfs, 0, rval);
418 bf_cache (rGroupDef.bfs, rval);
419 zebraIndexLockMsg ("r");
421 records = rec_open (rGroupDef.bfs, 0);
422 rec_prstat (records);
423 rec_close (&records);
424 inv_prstat (rGroupDef.bfs);
426 else if (!strcmp (arg, "compact"))
428 rval = res_get (common_resource, "shadow");
429 zebraIndexLock (rGroupDef.bfs, 0, rval);
432 bf_cache (rGroupDef.bfs, rval);
433 zebraIndexLockMsg ("r");
435 inv_compact(rGroupDef.bfs);
439 logf (LOG_FATAL, "unknown command: %s", arg);
445 struct recordGroup rGroup;
447 rval = res_get (common_resource, "shadow");
448 zebraIndexLock (rGroupDef.bfs, 0, rval);
449 if (rGroupDef.flagRw)
451 if (rval && *rval && !disableCommit)
453 bf_cache (rGroupDef.bfs, rval);
454 zebraIndexLockMsg ("r");
458 bf_cache (rGroupDef.bfs, 0);
459 zebraIndexLockMsg ("w");
463 memcpy (&rGroup, &rGroupDef, sizeof(rGroup));
468 if (!key_open (&rGroup, mem_max))
470 logf (LOG_LOG, "updating %s", rGroup.path);
471 repositoryUpdate (&rGroup);
472 nsections = key_close (&rGroup);
476 if (!key_open (&rGroup, mem_max))
478 logf (LOG_LOG, "updating (pass 1) %s", rGroup.path);
479 repositoryUpdate (&rGroup);
485 if (!key_open (&rGroup,mem_max))
487 logf (LOG_LOG, "deleting %s", rGroup.path);
488 repositoryDelete (&rGroup);
489 nsections = key_close (&rGroup);
493 logf (LOG_LOG, "dumping %s", rGroup.path);
494 repositoryShow (&rGroup);
506 logf (LOG_LOG, "merging with index");
507 key_input (rGroup.bfs, nsections, 60);
512 log_event_end (NULL, NULL);
517 fprintf (stderr, "Zebra %s %s\n",
526 log_init_level (log_mask_str(arg));
530 mem_max = 1024*1024*atoi(arg);
532 rGroupDef.databaseName = arg;
534 rGroupDef.flagRw = 0;
536 rGroupDef.groupName = arg;
538 rGroupDef.fileVerboseLimit = atoi(arg);
542 rGroupDef.recordType = arg;
547 rGroupDef.useSDR = 1;
550 logf (LOG_WARN, "unknown option '-%s'", arg);
555 bfs_destroy (rGroupDef.bfs);
557 data1_destroy (rGroupDef.dh);