2 * Copyright (C) 1994-2001, Index Data
5 * $Id: main.c,v 1.81 2001-11-19 23:29:09 adam Exp $
16 #include <yaz/data1.h>
30 Res common_resource = 0;
33 int main (int argc, char **argv)
38 char *configName = FNAME_CONFIG;
40 int disableCommit = 0;
45 struct recordGroup rGroupDef;
51 sprintf(nbuf, "%.40s(%d)", *argv, getpid());
52 yaz_log_init_prefix (nbuf);
59 rGroupDef.groupName = NULL;
60 rGroupDef.databaseName = NULL;
61 rGroupDef.path = NULL;
62 rGroupDef.recordId = NULL;
63 rGroupDef.recordType = NULL;
64 rGroupDef.flagStoreData = -1;
65 rGroupDef.flagStoreKeys = -1;
67 rGroupDef.databaseNamePath = 0;
68 rGroupDef.explainDatabase = 0;
69 rGroupDef.fileVerboseLimit = 100000;
70 rGroupDef.zebra_maps = NULL;
71 rGroupDef.dh = data1_create ();
72 rGroupDef.recTypes = recTypes_init (rGroupDef.dh);
73 recTypes_default_handlers (rGroupDef.recTypes);
78 fprintf (stderr, "%s [options] command <dir> ...\n"
80 " update <dir> Update index with files below <dir>.\n"
81 " If <dir> is empty filenames are read from stdin.\n"
82 " delete <dir> Delete index with files below <dir>.\n"
83 " commit Commit changes\n"
84 " clean Clean shadow files\n"
86 " -t <type> Index files as <type> (grs or text).\n"
87 " -c <config> Read configuration file <config>.\n"
88 " -g <group> Index files according to group settings.\n"
89 " -d <database> Records belong to Z39.50 database <database>.\n"
90 " -m <mbytes> Use <mbytes> before flushing keys to disk.\n"
91 " -n Don't use shadow system.\n"
92 " -s Show analysis on stdout, but do no work.\n"
93 " -v <level> Set logging to <level>.\n"
94 " -l <file> Write log to <file>.\n"
95 " -f <n> Display information for the first <n> records.\n"
99 " -V Show version.\n", *argv
103 while ((ret = options ("sVt:c:g:d:m:v:nf:l:"
107 , argv, argc, &arg)) != -2)
112 if(cmd == 0) /* command */
114 if (!common_resource)
117 logf (LOG_LOG, "zmbol version %s %s",
118 ZEBRAVER, ZEBRADATE);
120 logf (LOG_LOG, "zebra version %s %s",
121 ZEBRAVER, ZEBRADATE);
123 common_resource = res_open (configName ?
124 configName : FNAME_CONFIG);
125 if (!common_resource)
127 logf (LOG_FATAL, "cannot read file `%s'", configName);
130 data1_set_tabpath (rGroupDef.dh, res_get (common_resource,
134 bfs_create (res_get (common_resource, "register"));
137 logf (LOG_FATAL, "Cannot access register");
141 bf_lockDir (rGroupDef.bfs,
142 res_get (common_resource, "lockDir"));
143 rGroupDef.zebra_maps = zebra_maps_open (common_resource);
145 if (!strcmp (arg, "update"))
147 else if (!strcmp (arg, "update1"))
149 else if (!strcmp (arg, "update2"))
151 else if (!strcmp (arg, "dump"))
153 else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
155 else if (!strcmp (arg, "init"))
158 rval = res_get (common_resource, "shadow");
159 zebraIndexLock (rGroupDef.bfs, 0, rval);
161 bf_cache (rGroupDef.bfs, rval);
162 zebraIndexLockMsg ("w");
163 bf_reset (rGroupDef.bfs);
165 else if (!strcmp (arg, "commit"))
167 rval = res_get (common_resource, "shadow");
168 zebraIndexLock (rGroupDef.bfs, 1, rval);
170 bf_cache (rGroupDef.bfs, rval);
173 logf (LOG_FATAL, "Cannot perform commit");
174 logf (LOG_FATAL, "No shadow area defined");
177 if (bf_commitExists (rGroupDef.bfs))
179 logf (LOG_LOG, "commit start");
180 zebraIndexLockMsg ("c");
182 logf (LOG_LOG, "commit execute");
183 bf_commitExec (rGroupDef.bfs);
187 zebraIndexLockMsg ("d");
189 logf (LOG_LOG, "commit clean");
190 bf_commitClean (rGroupDef.bfs, rval);
193 logf (LOG_LOG, "nothing to commit");
195 else if (!strcmp (arg, "clean"))
197 rval = res_get (common_resource, "shadow");
198 zebraIndexLock (rGroupDef.bfs, 1, rval);
199 if (bf_commitExists (rGroupDef.bfs))
201 zebraIndexLockMsg ("d");
203 logf (LOG_LOG, "commit clean");
204 bf_commitClean (rGroupDef.bfs, rval);
207 logf (LOG_LOG, "nothing to clean");
209 else if (!strcmp (arg, "stat") || !strcmp (arg, "status"))
212 rval = res_get (common_resource, "shadow");
213 zebraIndexLock (rGroupDef.bfs, 0, rval);
216 bf_cache (rGroupDef.bfs, rval);
217 zebraIndexLockMsg ("r");
219 records = rec_open (rGroupDef.bfs, 0, 0);
220 rec_prstat (records);
221 rec_close (&records);
222 inv_prstat (rGroupDef.bfs);
224 else if (!strcmp (arg, "compact"))
226 rval = res_get (common_resource, "shadow");
227 zebraIndexLock (rGroupDef.bfs, 0, rval);
230 bf_cache (rGroupDef.bfs, rval);
231 zebraIndexLockMsg ("r");
233 inv_compact(rGroupDef.bfs);
237 logf (LOG_FATAL, "unknown command: %s", arg);
243 struct recordGroup rGroup;
246 /* For zebra, delete lock file and reset register */
247 if (rGroupDef.flagRw)
250 bf_reset (rGroupDef.bfs);
253 rval = res_get (common_resource, "shadow");
254 zebraIndexLock (rGroupDef.bfs, 0, rval);
255 if (rGroupDef.flagRw)
257 if (rval && *rval && !disableCommit)
259 bf_cache (rGroupDef.bfs, rval);
260 zebraIndexLockMsg ("r");
264 bf_cache (rGroupDef.bfs, 0);
265 zebraIndexLockMsg ("w");
269 memcpy (&rGroup, &rGroupDef, sizeof(rGroup));
274 if (!key_open (&rGroup, mem_max))
276 logf (LOG_LOG, "updating %s", rGroup.path);
277 repositoryUpdate (&rGroup);
278 nsections = key_close (&rGroup);
282 if (!key_open (&rGroup, mem_max))
284 logf (LOG_LOG, "updating (pass 1) %s", rGroup.path);
285 repositoryUpdate (&rGroup);
291 if (!key_open (&rGroup,mem_max))
293 logf (LOG_LOG, "deleting %s", rGroup.path);
294 repositoryDelete (&rGroup);
295 nsections = key_close (&rGroup);
299 logf (LOG_LOG, "dumping %s", rGroup.path);
300 repositoryShow (&rGroup);
312 logf (LOG_LOG, "merging with index");
313 key_input (rGroup.bfs, nsections, 60, common_resource);
318 log_event_end (NULL, NULL);
324 fprintf (stderr, "Z'mbol %s %s\n", ZEBRAVER, ZEBRADATE);
326 fprintf (stderr, "Zebra %s %s\n", ZEBRAVER, ZEBRADATE);
328 fprintf (stderr, " (C) 1994-2001, Index Data ApS\n");
331 fprintf (stderr, " WIN32 Debug\n");
333 fprintf (stderr, " WIN32 Release\n");
337 fprintf (stderr, "libbzip2\n"
338 " (C) 1996-1999 Julian R Seward. All rights reserved.\n");
342 yaz_log_init_level (yaz_log_mask_str(arg));
344 yaz_log_init_file (arg);
346 mem_max = 1024*1024*atoi(arg);
348 rGroupDef.databaseName = arg;
350 rGroupDef.flagRw = 0;
352 rGroupDef.groupName = arg;
354 rGroupDef.fileVerboseLimit = atoi(arg);
358 rGroupDef.recordType = arg;
363 rGroupDef.useSDR = 1;
366 logf (LOG_WARN, "unknown option '-%s'", arg);
368 recTypes_destroy (rGroupDef.recTypes);
372 bfs_destroy (rGroupDef.bfs);
374 data1_destroy (rGroupDef.dh);