2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.68 1999-07-14 10:59:26 adam
8 * Changed functions isc_getmethod, isams_getmethod.
9 * Improved fatal error handling (such as missing EXPLAIN schema).
11 * Revision 1.67 1999/07/06 12:28:04 adam
12 * Updated record index structure. Format includes version ID. Compression
13 * algorithm ID is stored for each record block.
15 * Revision 1.66 1999/06/25 13:48:02 adam
16 * Updated MSVC project files.
17 * Added BZIP2 record compression (not very well tested).
19 * Revision 1.65 1999/05/20 12:57:18 adam
20 * Implemented TCL filter. Updated recctrl system.
22 * Revision 1.64 1999/05/15 14:36:38 adam
23 * Updated dictionary. Implemented "compression" of dictionary.
25 * Revision 1.63 1999/03/09 16:27:49 adam
26 * More work on SDRKit integration.
28 * Revision 1.62 1999/02/18 15:01:24 adam
31 * Revision 1.61 1999/02/02 14:51:00 adam
32 * Updated WIN32 code specific sections. Changed header.
34 * Revision 1.60 1998/10/28 10:54:39 adam
37 * Revision 1.59 1998/10/16 08:14:32 adam
38 * Updated record control system.
40 * Revision 1.58 1998/06/08 14:43:13 adam
41 * Added suport for EXPLAIN Proxy servers - added settings databasePath
42 * and explainDatabase to facilitate this. Increased maximum number
43 * of databases and attributes in one register.
45 * Revision 1.57 1998/05/20 10:12:18 adam
46 * Implemented automatic EXPLAIN database maintenance.
47 * Modified Zebra to work with ASN.1 compiled version of YAZ.
49 * Revision 1.56 1998/03/05 08:45:12 adam
50 * New result set model and modular ranking system. Moved towards
51 * descent server API. System information stored as "SGML" records.
53 * Revision 1.55 1998/01/26 10:37:34 adam
56 * Revision 1.54 1998/01/12 15:04:08 adam
57 * The test option (-s) only uses read-lock (and not write lock).
59 * Revision 1.53 1997/11/18 10:05:08 adam
60 * Changed character map facility so that admin can specify character
61 * mapping files for each register type, w, p, etc.
63 * Revision 1.52 1997/10/31 12:34:04 adam
64 * Added missing nmem_init.
66 * Revision 1.51 1997/10/27 14:33:05 adam
67 * Moved towards generic character mapping depending on "structure"
68 * field in abstract syntax file. Fixed a few memory leaks. Fixed
69 * bug with negative integers when doing searches with relational
72 * Revision 1.50 1997/09/25 14:55:52 adam
75 * Revision 1.49 1997/09/17 12:19:15 adam
76 * Zebra version corresponds to YAZ version 1.4.
77 * Changed Zebra server so that it doesn't depend on global common_resource.
79 * Revision 1.48 1997/09/09 13:38:08 adam
80 * Partial port to WIN95/NT.
82 * Revision 1.47 1997/02/12 20:39:46 adam
83 * Implemented options -f <n> that limits the log to the first <n>
85 * Changed some log messages also.
87 * Revision 1.46 1997/02/10 10:20:13 adam
88 * Flag fileVerboseFlag set to 0 (default).
90 * Revision 1.45 1996/11/08 11:10:26 adam
91 * Buffers used during file match got bigger.
92 * Compressed ISAM support everywhere.
93 * Bug fixes regarding masking characters in queries.
94 * Redesigned Regexp-2 queries.
96 * Revision 1.44 1996/10/29 14:09:48 adam
97 * Use of cisam system - enabled if setting isamc is 1.
99 * Revision 1.43 1996/06/06 12:08:42 quinn
100 * Added showRecord function
102 * Revision 1.42 1996/05/31 09:07:01 quinn
103 * Work on character-set handling
105 * Revision 1.41 1996/05/14 14:04:34 adam
106 * In zebraidx, the 'stat' command is improved. Statistics about ISAM/DICT
109 * Revision 1.40 1996/04/26 10:00:23 adam
110 * Added option -V to zebraidx to display version information.
111 * Removed stupid warnings from file update.
113 * Revision 1.39 1996/04/09 10:05:21 adam
114 * Bug fix: prev_name buffer possibly too small; allocated in key_file_init.
116 * Revision 1.38 1996/03/26 16:01:14 adam
117 * New setting lockPath: directory of various lock files.
119 * Revision 1.37 1996/03/19 12:43:26 adam
120 * Bug fix: File update traversal didn't handle trailing slashes correctly.
121 * Bug fix: Update of sub directory groups wasn't handled correctly.
123 * Revision 1.36 1996/02/12 18:45:37 adam
124 * New fileVerboseFlag in record group control.
126 * Revision 1.35 1996/02/12 15:56:11 adam
127 * New code command: unread.
129 * Revision 1.34 1996/02/07 14:06:39 adam
130 * Better progress report during register merge.
131 * New command: clean - removes temporary shadow files.
133 * Revision 1.33 1996/02/06 17:11:18 adam
136 * Revision 1.32 1996/02/01 20:50:04 adam
137 * Bug fix: zebraIndexUnlock was always called even though zebraIndexLock
138 * was never called - happens when no commands are specified.
140 * Revision 1.31 1996/01/08 19:15:46 adam
141 * New input filter that works!
143 * Revision 1.30 1995/12/12 16:00:59 adam
144 * System call sync(2) used after update/commit.
145 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
148 * Revision 1.29 1995/12/11 11:43:30 adam
149 * Locking based on fcntl instead of flock.
150 * Setting commitEnable removed. Command line option -n can be used to
151 * prevent commit if commit setting is defined in the configuration file.
153 * Revision 1.28 1995/12/08 16:22:56 adam
154 * Work on update while servers are running. Three lock files introduced.
155 * The servers reload their registers when necessary, but they don't
156 * reestablish result sets yet.
158 * Revision 1.27 1995/12/07 17:38:47 adam
159 * Work locking mechanisms for concurrent updates/commit.
161 * Revision 1.26 1995/12/06 12:41:23 adam
162 * New command 'stat' for the index program.
163 * Filenames can be read from stdin by specifying '-'.
164 * Bug fix/enhancement of the transformation from terms to regular
165 * expressons in the search engine.
167 * Revision 1.25 1995/12/01 16:24:39 adam
168 * Commit files use separate meta file area.
170 * Revision 1.24 1995/11/30 17:01:38 adam
171 * New setting commitCache: points to commit directories/files.
172 * New command commit: commits at the end of a zebraidx run.
174 * Revision 1.23 1995/11/30 08:34:31 adam
175 * Started work on commit facility.
176 * Changed a few malloc/free to xmalloc/xfree.
178 * Revision 1.22 1995/11/28 09:09:42 adam
179 * Zebra config renamed.
180 * Use setting 'recordId' to identify record now.
181 * Bug fix in recindex.c: rec_release_blocks was invokeded even
182 * though the blocks were already released.
183 * File traversal properly deletes records when needed.
185 * Revision 1.21 1995/11/27 14:27:39 adam
186 * Renamed 'update' command to 'dir'.
188 * Revision 1.20 1995/11/27 13:58:53 adam
189 * New option -t. storeStore data implemented in server.
191 * Revision 1.19 1995/11/25 10:24:06 adam
192 * More record fields - they are enumerated now.
193 * New options: flagStoreData flagStoreKey.
195 * Revision 1.18 1995/11/22 17:19:17 adam
196 * Record management uses the bfile system.
198 * Revision 1.17 1995/11/21 15:01:16 adam
199 * New general match criteria implemented.
200 * New feature: document groups.
202 * Revision 1.16 1995/11/20 11:56:27 adam
203 * Work on new traversal.
205 * Revision 1.15 1995/11/01 16:25:51 quinn
206 * *** empty log message ***
208 * Revision 1.14 1995/10/17 18:02:09 adam
209 * New feature: databases. Implemented as prefix to words in dictionary.
211 * Revision 1.13 1995/10/10 12:24:39 adam
212 * Temporary sort files are compressed.
214 * Revision 1.12 1995/10/04 16:57:20 adam
215 * Key input and merge sort in one pass.
217 * Revision 1.11 1995/09/29 14:01:45 adam
220 * Revision 1.10 1995/09/28 14:22:57 adam
221 * Sort uses smaller temporary files.
223 * Revision 1.9 1995/09/14 07:48:24 adam
224 * Record control management.
226 * Revision 1.8 1995/09/06 16:11:18 adam
227 * Option: only one word key per file.
229 * Revision 1.7 1995/09/05 15:28:39 adam
230 * More work on search engine.
232 * Revision 1.6 1995/09/04 12:33:43 adam
233 * Various cleanup. YAZ util used instead.
235 * Revision 1.5 1995/09/04 09:10:39 adam
236 * More work on index add/del/update.
237 * Merge sort implemented.
238 * Initial work on z39 server.
240 * Revision 1.4 1995/09/01 14:06:36 adam
241 * Split of work into more files.
243 * Revision 1.3 1995/09/01 10:57:07 adam
246 * Revision 1.2 1995/09/01 10:30:24 adam
247 * More work on indexing. Not working yet.
249 * Revision 1.1 1995/08/31 14:50:24 adam
250 * New simple file index tool.
263 #include "recindex.h"
270 #include "zebrasdr.h"
275 Res common_resource = 0;
277 int main (int argc, char **argv)
282 char *configName = NULL;
284 int disableCommit = 0;
287 struct recordGroup rGroupDef;
293 rGroupDef.useSDR = 0;
295 rGroupDef.groupName = NULL;
296 rGroupDef.databaseName = NULL;
297 rGroupDef.path = NULL;
298 rGroupDef.recordId = NULL;
299 rGroupDef.recordType = NULL;
300 rGroupDef.flagStoreData = -1;
301 rGroupDef.flagStoreKeys = -1;
302 rGroupDef.flagRw = 1;
303 rGroupDef.databaseNamePath = 0;
304 rGroupDef.explainDatabase = 0;
305 rGroupDef.fileVerboseLimit = 100000;
306 rGroupDef.zebra_maps = NULL;
307 rGroupDef.dh = data1_create ();
308 rGroupDef.recTypes = recTypes_init (rGroupDef.dh);
309 recTypes_default_handlers (rGroupDef.recTypes);
314 fprintf (stderr, "zebraidx [options] command <dir> ...\n"
316 " update <dir> Update index with files below <dir>.\n"
317 " If <dir> is empty filenames are read from stdin.\n"
318 " delete <dir> Delete index with files below <dir>.\n"
319 " commit Commit changes\n"
320 " clean Clean shadow files\n"
322 " -t <type> Index files as <type> (grs or text).\n"
323 " -c <config> Read configuration file <config>.\n"
324 " -g <group> Index files according to group settings.\n"
325 " -d <database> Records belong to Z39.50 database <database>.\n"
326 " -m <mbytes> Use <mbytes> before flushing keys to disk.\n"
327 " -n Don't use shadow system.\n"
328 " -s Show analysis on stdout, but do no work.\n"
329 " -v <level> Set logging to <level>.\n"
330 " -l <file> Write log to <file>.\n"
331 " -f <n> Display information for the first <n> records.\n"
335 " -V Show version.\n"
339 while ((ret = options ("sVt:c:g:d:m:v:nf:l:"
343 , argv, argc, &arg)) != -2)
348 if(cmd == 0) /* command */
350 if (!common_resource)
352 logf (LOG_LOG, "zebra version %s %s",
353 ZEBRAVER, ZEBRADATE);
354 common_resource = res_open (configName ?
355 configName : FNAME_CONFIG);
356 if (!common_resource)
358 logf (LOG_FATAL, "cannot configuration file `%s'",
362 data1_set_tabpath (rGroupDef.dh, res_get (common_resource,
366 bfs_create (res_get (common_resource, "register"));
368 bf_lockDir (rGroupDef.bfs,
369 res_get (common_resource, "lockDir"));
370 rGroupDef.zebra_maps = zebra_maps_open (common_resource);
372 if (!strcmp (arg, "update"))
374 else if (!strcmp (arg, "update1"))
376 else if (!strcmp (arg, "update2"))
378 else if (!strcmp (arg, "dump"))
380 else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
382 else if (!strcmp (arg, "commit"))
384 rval = res_get (common_resource, "shadow");
385 zebraIndexLock (rGroupDef.bfs, 1, rval);
387 bf_cache (rGroupDef.bfs, rval);
390 logf (LOG_FATAL, "Cannot perform commit");
391 logf (LOG_FATAL, "No shadow area defined");
394 if (bf_commitExists (rGroupDef.bfs))
396 logf (LOG_LOG, "commit start");
397 zebraIndexLockMsg ("c");
399 logf (LOG_LOG, "commit execute");
400 bf_commitExec (rGroupDef.bfs);
404 zebraIndexLockMsg ("d");
406 logf (LOG_LOG, "commit clean");
407 bf_commitClean (rGroupDef.bfs, rval);
410 logf (LOG_LOG, "cothing to commit");
412 else if (!strcmp (arg, "clean"))
414 rval = res_get (common_resource, "shadow");
415 zebraIndexLock (rGroupDef.bfs, 1, rval);
416 if (bf_commitExists (rGroupDef.bfs))
418 zebraIndexLockMsg ("d");
420 logf (LOG_LOG, "commit clean");
421 bf_commitClean (rGroupDef.bfs, rval);
424 logf (LOG_LOG, "nothing to clean");
426 else if (!strcmp (arg, "stat") || !strcmp (arg, "status"))
429 rval = res_get (common_resource, "shadow");
430 zebraIndexLock (rGroupDef.bfs, 0, rval);
433 bf_cache (rGroupDef.bfs, rval);
434 zebraIndexLockMsg ("r");
436 records = rec_open (rGroupDef.bfs, 0, 0);
437 rec_prstat (records);
438 rec_close (&records);
439 inv_prstat (rGroupDef.bfs);
441 else if (!strcmp (arg, "compact"))
443 rval = res_get (common_resource, "shadow");
444 zebraIndexLock (rGroupDef.bfs, 0, rval);
447 bf_cache (rGroupDef.bfs, rval);
448 zebraIndexLockMsg ("r");
450 inv_compact(rGroupDef.bfs);
454 logf (LOG_FATAL, "unknown command: %s", arg);
460 struct recordGroup rGroup;
462 rval = res_get (common_resource, "shadow");
463 zebraIndexLock (rGroupDef.bfs, 0, rval);
464 if (rGroupDef.flagRw)
466 if (rval && *rval && !disableCommit)
468 bf_cache (rGroupDef.bfs, rval);
469 zebraIndexLockMsg ("r");
473 bf_cache (rGroupDef.bfs, 0);
474 zebraIndexLockMsg ("w");
478 memcpy (&rGroup, &rGroupDef, sizeof(rGroup));
483 if (!key_open (&rGroup, mem_max))
485 logf (LOG_LOG, "updating %s", rGroup.path);
486 repositoryUpdate (&rGroup);
487 nsections = key_close (&rGroup);
491 if (!key_open (&rGroup, mem_max))
493 logf (LOG_LOG, "updating (pass 1) %s", rGroup.path);
494 repositoryUpdate (&rGroup);
500 if (!key_open (&rGroup,mem_max))
502 logf (LOG_LOG, "deleting %s", rGroup.path);
503 repositoryDelete (&rGroup);
504 nsections = key_close (&rGroup);
508 logf (LOG_LOG, "dumping %s", rGroup.path);
509 repositoryShow (&rGroup);
521 logf (LOG_LOG, "merging with index");
522 key_input (rGroup.bfs, nsections, 60);
527 log_event_end (NULL, NULL);
532 fprintf (stderr, "Zebra %s %s\n", ZEBRAVER, ZEBRADATE);
533 fprintf (stderr, " (C) 1994-1999, Index Data ApS\n");
535 fprintf (stderr, "libbzip2\n"
536 " (C) 1996-1998 Julian R Seward. All rights reserved.\n");
540 log_init_level (log_mask_str(arg));
544 mem_max = 1024*1024*atoi(arg);
546 rGroupDef.databaseName = arg;
548 rGroupDef.flagRw = 0;
550 rGroupDef.groupName = arg;
552 rGroupDef.fileVerboseLimit = atoi(arg);
556 rGroupDef.recordType = arg;
561 rGroupDef.useSDR = 1;
564 logf (LOG_WARN, "unknown option '-%s'", arg);
566 recTypes_destroy (rGroupDef.recTypes);
570 bfs_destroy (rGroupDef.bfs);
572 data1_destroy (rGroupDef.dh);