2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.50 1997-09-25 14:55:52 adam
10 * Revision 1.49 1997/09/17 12:19:15 adam
11 * Zebra version corresponds to YAZ version 1.4.
12 * Changed Zebra server so that it doesn't depend on global common_resource.
14 * Revision 1.48 1997/09/09 13:38:08 adam
15 * Partial port to WIN95/NT.
17 * Revision 1.47 1997/02/12 20:39:46 adam
18 * Implemented options -f <n> that limits the log to the first <n>
20 * Changed some log messages also.
22 * Revision 1.46 1997/02/10 10:20:13 adam
23 * Flag fileVerboseFlag set to 0 (default).
25 * Revision 1.45 1996/11/08 11:10:26 adam
26 * Buffers used during file match got bigger.
27 * Compressed ISAM support everywhere.
28 * Bug fixes regarding masking characters in queries.
29 * Redesigned Regexp-2 queries.
31 * Revision 1.44 1996/10/29 14:09:48 adam
32 * Use of cisam system - enabled if setting isamc is 1.
34 * Revision 1.43 1996/06/06 12:08:42 quinn
35 * Added showRecord function
37 * Revision 1.42 1996/05/31 09:07:01 quinn
38 * Work on character-set handling
40 * Revision 1.41 1996/05/14 14:04:34 adam
41 * In zebraidx, the 'stat' command is improved. Statistics about ISAM/DICT
44 * Revision 1.40 1996/04/26 10:00:23 adam
45 * Added option -V to zebraidx to display version information.
46 * Removed stupid warnings from file update.
48 * Revision 1.39 1996/04/09 10:05:21 adam
49 * Bug fix: prev_name buffer possibly too small; allocated in key_file_init.
51 * Revision 1.38 1996/03/26 16:01:14 adam
52 * New setting lockPath: directory of various lock files.
54 * Revision 1.37 1996/03/19 12:43:26 adam
55 * Bug fix: File update traversal didn't handle trailing slashes correctly.
56 * Bug fix: Update of sub directory groups wasn't handled correctly.
58 * Revision 1.36 1996/02/12 18:45:37 adam
59 * New fileVerboseFlag in record group control.
61 * Revision 1.35 1996/02/12 15:56:11 adam
62 * New code command: unread.
64 * Revision 1.34 1996/02/07 14:06:39 adam
65 * Better progress report during register merge.
66 * New command: clean - removes temporary shadow files.
68 * Revision 1.33 1996/02/06 17:11:18 adam
71 * Revision 1.32 1996/02/01 20:50:04 adam
72 * Bug fix: zebraIndexUnlock was always called even though zebraIndexLock
73 * was never called - happens when no commands are specified.
75 * Revision 1.31 1996/01/08 19:15:46 adam
76 * New input filter that works!
78 * Revision 1.30 1995/12/12 16:00:59 adam
79 * System call sync(2) used after update/commit.
80 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
83 * Revision 1.29 1995/12/11 11:43:30 adam
84 * Locking based on fcntl instead of flock.
85 * Setting commitEnable removed. Command line option -n can be used to
86 * prevent commit if commit setting is defined in the configuration file.
88 * Revision 1.28 1995/12/08 16:22:56 adam
89 * Work on update while servers are running. Three lock files introduced.
90 * The servers reload their registers when necessary, but they don't
91 * reestablish result sets yet.
93 * Revision 1.27 1995/12/07 17:38:47 adam
94 * Work locking mechanisms for concurrent updates/commit.
96 * Revision 1.26 1995/12/06 12:41:23 adam
97 * New command 'stat' for the index program.
98 * Filenames can be read from stdin by specifying '-'.
99 * Bug fix/enhancement of the transformation from terms to regular
100 * expressons in the search engine.
102 * Revision 1.25 1995/12/01 16:24:39 adam
103 * Commit files use separate meta file area.
105 * Revision 1.24 1995/11/30 17:01:38 adam
106 * New setting commitCache: points to commit directories/files.
107 * New command commit: commits at the end of a zebraidx run.
109 * Revision 1.23 1995/11/30 08:34:31 adam
110 * Started work on commit facility.
111 * Changed a few malloc/free to xmalloc/xfree.
113 * Revision 1.22 1995/11/28 09:09:42 adam
114 * Zebra config renamed.
115 * Use setting 'recordId' to identify record now.
116 * Bug fix in recindex.c: rec_release_blocks was invokeded even
117 * though the blocks were already released.
118 * File traversal properly deletes records when needed.
120 * Revision 1.21 1995/11/27 14:27:39 adam
121 * Renamed 'update' command to 'dir'.
123 * Revision 1.20 1995/11/27 13:58:53 adam
124 * New option -t. storeStore data implemented in server.
126 * Revision 1.19 1995/11/25 10:24:06 adam
127 * More record fields - they are enumerated now.
128 * New options: flagStoreData flagStoreKey.
130 * Revision 1.18 1995/11/22 17:19:17 adam
131 * Record management uses the bfile system.
133 * Revision 1.17 1995/11/21 15:01:16 adam
134 * New general match criteria implemented.
135 * New feature: document groups.
137 * Revision 1.16 1995/11/20 11:56:27 adam
138 * Work on new traversal.
140 * Revision 1.15 1995/11/01 16:25:51 quinn
141 * *** empty log message ***
143 * Revision 1.14 1995/10/17 18:02:09 adam
144 * New feature: databases. Implemented as prefix to words in dictionary.
146 * Revision 1.13 1995/10/10 12:24:39 adam
147 * Temporary sort files are compressed.
149 * Revision 1.12 1995/10/04 16:57:20 adam
150 * Key input and merge sort in one pass.
152 * Revision 1.11 1995/09/29 14:01:45 adam
155 * Revision 1.10 1995/09/28 14:22:57 adam
156 * Sort uses smaller temporary files.
158 * Revision 1.9 1995/09/14 07:48:24 adam
159 * Record control management.
161 * Revision 1.8 1995/09/06 16:11:18 adam
162 * Option: only one word key per file.
164 * Revision 1.7 1995/09/05 15:28:39 adam
165 * More work on search engine.
167 * Revision 1.6 1995/09/04 12:33:43 adam
168 * Various cleanup. YAZ util used instead.
170 * Revision 1.5 1995/09/04 09:10:39 adam
171 * More work on index add/del/update.
172 * Merge sort implemented.
173 * Initial work on z39 server.
175 * Revision 1.4 1995/09/01 14:06:36 adam
176 * Split of work into more files.
178 * Revision 1.3 1995/09/01 10:57:07 adam
181 * Revision 1.2 1995/09/01 10:30:24 adam
182 * More work on indexing. Not working yet.
184 * Revision 1.1 1995/08/31 14:50:24 adam
185 * New simple file index tool.
202 Res common_resource = 0;
204 static void abort_func (int level, const char *msg, void *info)
206 if (level & LOG_FATAL)
210 int main (int argc, char **argv)
215 char *configName = NULL;
217 int disableCommit = 0;
219 struct recordGroup rGroupDef;
221 rGroupDef.groupName = NULL;
222 rGroupDef.databaseName = NULL;
223 rGroupDef.path = NULL;
224 rGroupDef.recordId = NULL;
225 rGroupDef.recordType = NULL;
226 rGroupDef.flagStoreData = -1;
227 rGroupDef.flagStoreKeys = -1;
228 rGroupDef.flagShowRecords = 0;
229 rGroupDef.fileVerboseLimit = 100000;
230 rGroupDef.dh = data1_create ();
235 fprintf (stderr, "zebraidx [options] command <dir> ...\n"
237 " update <dir> Update index with files below <dir>.\n"
238 " If <dir> is empty filenames are read from stdin.\n"
239 " delete <dir> Delete index with files below <dir>.\n"
240 " commit Commit changes\n"
241 " clean Clean shadow files\n"
243 " -t <type> Index files as <type> (grs or text).\n"
244 " -c <config> Read configuration file <config>.\n"
245 " -g <group> Index files according to group settings.\n"
246 " -d <database> Records belong to Z39.50 database <database>.\n"
247 " -m <mbytes> Use <mbytes> before flushing keys to disk.\n"
248 " -n Don't use shadow system.\n"
249 " -s Show analysis on stdout, but do no work.\n"
250 " -v <level> Set logging to <level>.\n"
251 " -f <n> Display information for the first <n> records.\n"
252 " -V Show version.\n"
256 while ((ret = options ("sVt:c:g:d:m:v:nf:", argv, argc, &arg)) != -2)
261 if(cmd == 0) /* command */
263 if (!common_resource)
265 logf (LOG_LOG, "zebra version %s %s",
266 ZEBRAVER, ZEBRADATE);
267 common_resource = res_open (configName ?
268 configName : FNAME_CONFIG);
269 if (!common_resource)
271 logf (LOG_FATAL, "cannot open resource `%s'",
275 data1_set_tabpath (rGroupDef.dh, res_get (common_resource,
279 bfs_create (res_get (common_resource, "register"));
281 bf_lockDir (rGroupDef.bfs,
282 res_get (common_resource, "lockDir"));
283 init_charmap(common_resource);
285 if (!strcmp (arg, "update"))
287 else if (!strcmp (arg, "update1"))
289 else if (!strcmp (arg, "update2"))
291 else if (!strcmp (arg, "dump"))
293 else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
295 else if (!strcmp (arg, "commit"))
297 rval = res_get (common_resource, "shadow");
298 zebraIndexLock (rGroupDef.bfs, 1, rval);
300 bf_cache (rGroupDef.bfs, rval);
303 logf (LOG_FATAL, "Cannot perform commit");
304 logf (LOG_FATAL, "No shadow area defined");
307 if (bf_commitExists (rGroupDef.bfs))
309 logf (LOG_LOG, "commit start");
310 zebraIndexLockMsg ("c");
312 logf (LOG_LOG, "commit execute");
313 bf_commitExec (rGroupDef.bfs);
317 zebraIndexLockMsg ("d");
319 logf (LOG_LOG, "commit clean");
320 bf_commitClean (rGroupDef.bfs, rval);
323 logf (LOG_LOG, "cothing to commit");
325 else if (!strcmp (arg, "clean"))
327 rval = res_get (common_resource, "shadow");
328 zebraIndexLock (rGroupDef.bfs, 1, rval);
329 if (bf_commitExists (rGroupDef.bfs))
331 zebraIndexLockMsg ("d");
333 logf (LOG_LOG, "commit clean");
334 bf_commitClean (rGroupDef.bfs, rval);
337 logf (LOG_LOG, "nothing to clean");
339 else if (!strcmp (arg, "stat") || !strcmp (arg, "status"))
341 rval = res_get (common_resource, "shadow");
342 zebraIndexLock (rGroupDef.bfs, 0, rval);
345 bf_cache (rGroupDef.bfs, rval);
346 zebraIndexLockMsg ("r");
349 inv_prstat (rGroupDef.bfs);
353 logf (LOG_FATAL, "unknown command: %s", arg);
359 struct recordGroup rGroup;
361 log_event_end (abort_func, NULL);
362 rval = res_get (common_resource, "shadow");
363 zebraIndexLock (rGroupDef.bfs, 0, rval);
364 if (rval && *rval && !disableCommit)
366 bf_cache (rGroupDef.bfs, rval);
367 zebraIndexLockMsg ("r");
371 bf_cache (rGroupDef.bfs, 0);
372 zebraIndexLockMsg ("w");
376 memcpy (&rGroup, &rGroupDef, sizeof(rGroup));
381 key_open (rGroup.bfs, mem_max);
382 logf (LOG_LOG, "updating %s", rGroup.path);
383 repositoryUpdate (&rGroup);
384 nsections = key_close ();
387 key_open (rGroup.bfs,mem_max);
388 logf (LOG_LOG, "updating (pass 1) %s", rGroup.path);
389 repositoryUpdate (&rGroup);
394 key_open (rGroup.bfs,mem_max);
395 logf (LOG_LOG, "deleting %s", rGroup.path);
396 repositoryDelete (&rGroup);
397 nsections = key_close ();
400 logf (LOG_LOG, "dumping %s", rGroup.path);
401 repositoryShow (&rGroup);
413 logf (LOG_LOG, "merging with index");
414 key_input (rGroup.bfs, nsections, 60);
419 log_event_end (NULL, NULL);
424 fprintf (stderr, "Zebra %s %s\n", ZEBRAVER, ZEBRADATE);
427 log_init (log_mask_str(arg), prog, NULL);
429 mem_max = 1024*1024*atoi(arg);
431 rGroupDef.databaseName = arg;
433 rGroupDef.flagShowRecords = 1;
435 rGroupDef.groupName = arg;
437 rGroupDef.fileVerboseLimit = atoi(arg);
441 rGroupDef.recordType = arg;
446 logf (LOG_FATAL, "unknown option '-%s'", arg);
453 bfs_destroy (rGroupDef.bfs);
455 data1_destroy (rGroupDef.dh);