2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.66 1999-06-25 13:48:02 adam
8 * Updated MSVC project files.
9 * Added BZIP2 record compression (not very well tested).
11 * Revision 1.65 1999/05/20 12:57:18 adam
12 * Implemented TCL filter. Updated recctrl system.
14 * Revision 1.64 1999/05/15 14:36:38 adam
15 * Updated dictionary. Implemented "compression" of dictionary.
17 * Revision 1.63 1999/03/09 16:27:49 adam
18 * More work on SDRKit integration.
20 * Revision 1.62 1999/02/18 15:01:24 adam
23 * Revision 1.61 1999/02/02 14:51:00 adam
24 * Updated WIN32 code specific sections. Changed header.
26 * Revision 1.60 1998/10/28 10:54:39 adam
29 * Revision 1.59 1998/10/16 08:14:32 adam
30 * Updated record control system.
32 * Revision 1.58 1998/06/08 14:43:13 adam
33 * Added suport for EXPLAIN Proxy servers - added settings databasePath
34 * and explainDatabase to facilitate this. Increased maximum number
35 * of databases and attributes in one register.
37 * Revision 1.57 1998/05/20 10:12:18 adam
38 * Implemented automatic EXPLAIN database maintenance.
39 * Modified Zebra to work with ASN.1 compiled version of YAZ.
41 * Revision 1.56 1998/03/05 08:45:12 adam
42 * New result set model and modular ranking system. Moved towards
43 * descent server API. System information stored as "SGML" records.
45 * Revision 1.55 1998/01/26 10:37:34 adam
48 * Revision 1.54 1998/01/12 15:04:08 adam
49 * The test option (-s) only uses read-lock (and not write lock).
51 * Revision 1.53 1997/11/18 10:05:08 adam
52 * Changed character map facility so that admin can specify character
53 * mapping files for each register type, w, p, etc.
55 * Revision 1.52 1997/10/31 12:34:04 adam
56 * Added missing nmem_init.
58 * Revision 1.51 1997/10/27 14:33:05 adam
59 * Moved towards generic character mapping depending on "structure"
60 * field in abstract syntax file. Fixed a few memory leaks. Fixed
61 * bug with negative integers when doing searches with relational
64 * Revision 1.50 1997/09/25 14:55:52 adam
67 * Revision 1.49 1997/09/17 12:19:15 adam
68 * Zebra version corresponds to YAZ version 1.4.
69 * Changed Zebra server so that it doesn't depend on global common_resource.
71 * Revision 1.48 1997/09/09 13:38:08 adam
72 * Partial port to WIN95/NT.
74 * Revision 1.47 1997/02/12 20:39:46 adam
75 * Implemented options -f <n> that limits the log to the first <n>
77 * Changed some log messages also.
79 * Revision 1.46 1997/02/10 10:20:13 adam
80 * Flag fileVerboseFlag set to 0 (default).
82 * Revision 1.45 1996/11/08 11:10:26 adam
83 * Buffers used during file match got bigger.
84 * Compressed ISAM support everywhere.
85 * Bug fixes regarding masking characters in queries.
86 * Redesigned Regexp-2 queries.
88 * Revision 1.44 1996/10/29 14:09:48 adam
89 * Use of cisam system - enabled if setting isamc is 1.
91 * Revision 1.43 1996/06/06 12:08:42 quinn
92 * Added showRecord function
94 * Revision 1.42 1996/05/31 09:07:01 quinn
95 * Work on character-set handling
97 * Revision 1.41 1996/05/14 14:04:34 adam
98 * In zebraidx, the 'stat' command is improved. Statistics about ISAM/DICT
101 * Revision 1.40 1996/04/26 10:00:23 adam
102 * Added option -V to zebraidx to display version information.
103 * Removed stupid warnings from file update.
105 * Revision 1.39 1996/04/09 10:05:21 adam
106 * Bug fix: prev_name buffer possibly too small; allocated in key_file_init.
108 * Revision 1.38 1996/03/26 16:01:14 adam
109 * New setting lockPath: directory of various lock files.
111 * Revision 1.37 1996/03/19 12:43:26 adam
112 * Bug fix: File update traversal didn't handle trailing slashes correctly.
113 * Bug fix: Update of sub directory groups wasn't handled correctly.
115 * Revision 1.36 1996/02/12 18:45:37 adam
116 * New fileVerboseFlag in record group control.
118 * Revision 1.35 1996/02/12 15:56:11 adam
119 * New code command: unread.
121 * Revision 1.34 1996/02/07 14:06:39 adam
122 * Better progress report during register merge.
123 * New command: clean - removes temporary shadow files.
125 * Revision 1.33 1996/02/06 17:11:18 adam
128 * Revision 1.32 1996/02/01 20:50:04 adam
129 * Bug fix: zebraIndexUnlock was always called even though zebraIndexLock
130 * was never called - happens when no commands are specified.
132 * Revision 1.31 1996/01/08 19:15:46 adam
133 * New input filter that works!
135 * Revision 1.30 1995/12/12 16:00:59 adam
136 * System call sync(2) used after update/commit.
137 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
140 * Revision 1.29 1995/12/11 11:43:30 adam
141 * Locking based on fcntl instead of flock.
142 * Setting commitEnable removed. Command line option -n can be used to
143 * prevent commit if commit setting is defined in the configuration file.
145 * Revision 1.28 1995/12/08 16:22:56 adam
146 * Work on update while servers are running. Three lock files introduced.
147 * The servers reload their registers when necessary, but they don't
148 * reestablish result sets yet.
150 * Revision 1.27 1995/12/07 17:38:47 adam
151 * Work locking mechanisms for concurrent updates/commit.
153 * Revision 1.26 1995/12/06 12:41:23 adam
154 * New command 'stat' for the index program.
155 * Filenames can be read from stdin by specifying '-'.
156 * Bug fix/enhancement of the transformation from terms to regular
157 * expressons in the search engine.
159 * Revision 1.25 1995/12/01 16:24:39 adam
160 * Commit files use separate meta file area.
162 * Revision 1.24 1995/11/30 17:01:38 adam
163 * New setting commitCache: points to commit directories/files.
164 * New command commit: commits at the end of a zebraidx run.
166 * Revision 1.23 1995/11/30 08:34:31 adam
167 * Started work on commit facility.
168 * Changed a few malloc/free to xmalloc/xfree.
170 * Revision 1.22 1995/11/28 09:09:42 adam
171 * Zebra config renamed.
172 * Use setting 'recordId' to identify record now.
173 * Bug fix in recindex.c: rec_release_blocks was invokeded even
174 * though the blocks were already released.
175 * File traversal properly deletes records when needed.
177 * Revision 1.21 1995/11/27 14:27:39 adam
178 * Renamed 'update' command to 'dir'.
180 * Revision 1.20 1995/11/27 13:58:53 adam
181 * New option -t. storeStore data implemented in server.
183 * Revision 1.19 1995/11/25 10:24:06 adam
184 * More record fields - they are enumerated now.
185 * New options: flagStoreData flagStoreKey.
187 * Revision 1.18 1995/11/22 17:19:17 adam
188 * Record management uses the bfile system.
190 * Revision 1.17 1995/11/21 15:01:16 adam
191 * New general match criteria implemented.
192 * New feature: document groups.
194 * Revision 1.16 1995/11/20 11:56:27 adam
195 * Work on new traversal.
197 * Revision 1.15 1995/11/01 16:25:51 quinn
198 * *** empty log message ***
200 * Revision 1.14 1995/10/17 18:02:09 adam
201 * New feature: databases. Implemented as prefix to words in dictionary.
203 * Revision 1.13 1995/10/10 12:24:39 adam
204 * Temporary sort files are compressed.
206 * Revision 1.12 1995/10/04 16:57:20 adam
207 * Key input and merge sort in one pass.
209 * Revision 1.11 1995/09/29 14:01:45 adam
212 * Revision 1.10 1995/09/28 14:22:57 adam
213 * Sort uses smaller temporary files.
215 * Revision 1.9 1995/09/14 07:48:24 adam
216 * Record control management.
218 * Revision 1.8 1995/09/06 16:11:18 adam
219 * Option: only one word key per file.
221 * Revision 1.7 1995/09/05 15:28:39 adam
222 * More work on search engine.
224 * Revision 1.6 1995/09/04 12:33:43 adam
225 * Various cleanup. YAZ util used instead.
227 * Revision 1.5 1995/09/04 09:10:39 adam
228 * More work on index add/del/update.
229 * Merge sort implemented.
230 * Initial work on z39 server.
232 * Revision 1.4 1995/09/01 14:06:36 adam
233 * Split of work into more files.
235 * Revision 1.3 1995/09/01 10:57:07 adam
238 * Revision 1.2 1995/09/01 10:30:24 adam
239 * More work on indexing. Not working yet.
241 * Revision 1.1 1995/08/31 14:50:24 adam
242 * New simple file index tool.
255 #include "recindex.h"
262 #include "zebrasdr.h"
267 Res common_resource = 0;
269 int main (int argc, char **argv)
274 char *configName = NULL;
276 int disableCommit = 0;
279 struct recordGroup rGroupDef;
285 rGroupDef.useSDR = 0;
287 rGroupDef.groupName = NULL;
288 rGroupDef.databaseName = NULL;
289 rGroupDef.path = NULL;
290 rGroupDef.recordId = NULL;
291 rGroupDef.recordType = NULL;
292 rGroupDef.flagStoreData = -1;
293 rGroupDef.flagStoreKeys = -1;
294 rGroupDef.flagRw = 1;
295 rGroupDef.databaseNamePath = 0;
296 rGroupDef.explainDatabase = 0;
297 rGroupDef.fileVerboseLimit = 100000;
298 rGroupDef.zebra_maps = NULL;
299 rGroupDef.dh = data1_create ();
300 rGroupDef.recTypes = recTypes_init (rGroupDef.dh);
301 recTypes_default_handlers (rGroupDef.recTypes);
306 fprintf (stderr, "zebraidx [options] command <dir> ...\n"
308 " update <dir> Update index with files below <dir>.\n"
309 " If <dir> is empty filenames are read from stdin.\n"
310 " delete <dir> Delete index with files below <dir>.\n"
311 " commit Commit changes\n"
312 " clean Clean shadow files\n"
314 " -t <type> Index files as <type> (grs or text).\n"
315 " -c <config> Read configuration file <config>.\n"
316 " -g <group> Index files according to group settings.\n"
317 " -d <database> Records belong to Z39.50 database <database>.\n"
318 " -m <mbytes> Use <mbytes> before flushing keys to disk.\n"
319 " -n Don't use shadow system.\n"
320 " -s Show analysis on stdout, but do no work.\n"
321 " -v <level> Set logging to <level>.\n"
322 " -l <file> Write log to <file>.\n"
323 " -f <n> Display information for the first <n> records.\n"
327 " -V Show version.\n"
331 while ((ret = options ("sVt:c:g:d:m:v:nf:l:"
335 , argv, argc, &arg)) != -2)
340 if(cmd == 0) /* command */
342 if (!common_resource)
344 logf (LOG_LOG, "zebra version %s %s",
345 ZEBRAVER, ZEBRADATE);
346 common_resource = res_open (configName ?
347 configName : FNAME_CONFIG);
348 if (!common_resource)
350 logf (LOG_FATAL, "cannot configuration file `%s'",
354 data1_set_tabpath (rGroupDef.dh, res_get (common_resource,
358 bfs_create (res_get (common_resource, "register"));
360 bf_lockDir (rGroupDef.bfs,
361 res_get (common_resource, "lockDir"));
362 rGroupDef.zebra_maps = zebra_maps_open (common_resource);
364 if (!strcmp (arg, "update"))
366 else if (!strcmp (arg, "update1"))
368 else if (!strcmp (arg, "update2"))
370 else if (!strcmp (arg, "dump"))
372 else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
374 else if (!strcmp (arg, "commit"))
376 rval = res_get (common_resource, "shadow");
377 zebraIndexLock (rGroupDef.bfs, 1, rval);
379 bf_cache (rGroupDef.bfs, rval);
382 logf (LOG_FATAL, "Cannot perform commit");
383 logf (LOG_FATAL, "No shadow area defined");
386 if (bf_commitExists (rGroupDef.bfs))
388 logf (LOG_LOG, "commit start");
389 zebraIndexLockMsg ("c");
391 logf (LOG_LOG, "commit execute");
392 bf_commitExec (rGroupDef.bfs);
396 zebraIndexLockMsg ("d");
398 logf (LOG_LOG, "commit clean");
399 bf_commitClean (rGroupDef.bfs, rval);
402 logf (LOG_LOG, "cothing to commit");
404 else if (!strcmp (arg, "clean"))
406 rval = res_get (common_resource, "shadow");
407 zebraIndexLock (rGroupDef.bfs, 1, rval);
408 if (bf_commitExists (rGroupDef.bfs))
410 zebraIndexLockMsg ("d");
412 logf (LOG_LOG, "commit clean");
413 bf_commitClean (rGroupDef.bfs, rval);
416 logf (LOG_LOG, "nothing to clean");
418 else if (!strcmp (arg, "stat") || !strcmp (arg, "status"))
421 rval = res_get (common_resource, "shadow");
422 zebraIndexLock (rGroupDef.bfs, 0, rval);
425 bf_cache (rGroupDef.bfs, rval);
426 zebraIndexLockMsg ("r");
428 records = rec_open (rGroupDef.bfs, 0);
429 rec_prstat (records);
430 rec_close (&records);
431 inv_prstat (rGroupDef.bfs);
433 else if (!strcmp (arg, "compact"))
435 rval = res_get (common_resource, "shadow");
436 zebraIndexLock (rGroupDef.bfs, 0, rval);
439 bf_cache (rGroupDef.bfs, rval);
440 zebraIndexLockMsg ("r");
442 inv_compact(rGroupDef.bfs);
446 logf (LOG_FATAL, "unknown command: %s", arg);
452 struct recordGroup rGroup;
454 rval = res_get (common_resource, "shadow");
455 zebraIndexLock (rGroupDef.bfs, 0, rval);
456 if (rGroupDef.flagRw)
458 if (rval && *rval && !disableCommit)
460 bf_cache (rGroupDef.bfs, rval);
461 zebraIndexLockMsg ("r");
465 bf_cache (rGroupDef.bfs, 0);
466 zebraIndexLockMsg ("w");
470 memcpy (&rGroup, &rGroupDef, sizeof(rGroup));
475 if (!key_open (&rGroup, mem_max))
477 logf (LOG_LOG, "updating %s", rGroup.path);
478 repositoryUpdate (&rGroup);
479 nsections = key_close (&rGroup);
483 if (!key_open (&rGroup, mem_max))
485 logf (LOG_LOG, "updating (pass 1) %s", rGroup.path);
486 repositoryUpdate (&rGroup);
492 if (!key_open (&rGroup,mem_max))
494 logf (LOG_LOG, "deleting %s", rGroup.path);
495 repositoryDelete (&rGroup);
496 nsections = key_close (&rGroup);
500 logf (LOG_LOG, "dumping %s", rGroup.path);
501 repositoryShow (&rGroup);
513 logf (LOG_LOG, "merging with index");
514 key_input (rGroup.bfs, nsections, 60);
519 log_event_end (NULL, NULL);
524 fprintf (stderr, "Zebra %s %s\n", ZEBRAVER, ZEBRADATE);
525 fprintf (stderr, " (C) 1994-1999, Index Data ApS\n");
527 fprintf (stderr, "libbzip2\n"
528 " (C) 1996-1998 Julian R Seward. All rights reserved.\n");
532 log_init_level (log_mask_str(arg));
536 mem_max = 1024*1024*atoi(arg);
538 rGroupDef.databaseName = arg;
540 rGroupDef.flagRw = 0;
542 rGroupDef.groupName = arg;
544 rGroupDef.fileVerboseLimit = atoi(arg);
548 rGroupDef.recordType = arg;
553 rGroupDef.useSDR = 1;
556 logf (LOG_WARN, "unknown option '-%s'", arg);
558 recTypes_destroy (rGroupDef.recTypes);
562 bfs_destroy (rGroupDef.bfs);
564 data1_destroy (rGroupDef.dh);