2 * Copyright (C) 1994-2001, Index Data
5 * $Id: main.c,v 1.80 2001-11-19 23:05:22 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;
43 struct recordGroup rGroupDef;
49 sprintf(nbuf, "%.40s(%d)", *argv, getpid());
50 yaz_log_init_prefix (nbuf);
57 rGroupDef.groupName = NULL;
58 rGroupDef.databaseName = NULL;
59 rGroupDef.path = NULL;
60 rGroupDef.recordId = NULL;
61 rGroupDef.recordType = NULL;
62 rGroupDef.flagStoreData = -1;
63 rGroupDef.flagStoreKeys = -1;
65 rGroupDef.databaseNamePath = 0;
66 rGroupDef.explainDatabase = 0;
67 rGroupDef.fileVerboseLimit = 100000;
68 rGroupDef.zebra_maps = NULL;
69 rGroupDef.dh = data1_create ();
70 rGroupDef.recTypes = recTypes_init (rGroupDef.dh);
71 recTypes_default_handlers (rGroupDef.recTypes);
76 fprintf (stderr, "%s [options] command <dir> ...\n"
78 " update <dir> Update index with files below <dir>.\n"
79 " If <dir> is empty filenames are read from stdin.\n"
80 " delete <dir> Delete index with files below <dir>.\n"
81 " commit Commit changes\n"
82 " clean Clean shadow files\n"
84 " -t <type> Index files as <type> (grs or text).\n"
85 " -c <config> Read configuration file <config>.\n"
86 " -g <group> Index files according to group settings.\n"
87 " -d <database> Records belong to Z39.50 database <database>.\n"
88 " -m <mbytes> Use <mbytes> before flushing keys to disk.\n"
89 " -n Don't use shadow system.\n"
90 " -s Show analysis on stdout, but do no work.\n"
91 " -v <level> Set logging to <level>.\n"
92 " -l <file> Write log to <file>.\n"
93 " -f <n> Display information for the first <n> records.\n"
97 " -V Show version.\n", *argv
101 while ((ret = options ("sVt:c:g:d:m:v:nf:l:"
105 , argv, argc, &arg)) != -2)
110 if(cmd == 0) /* command */
112 if (!common_resource)
115 logf (LOG_LOG, "zmbol version %s %s",
116 ZEBRAVER, ZEBRADATE);
118 logf (LOG_LOG, "zebra version %s %s",
119 ZEBRAVER, ZEBRADATE);
121 common_resource = res_open (configName ?
122 configName : FNAME_CONFIG);
123 if (!common_resource)
125 logf (LOG_FATAL, "cannot read file `%s'", configName);
128 data1_set_tabpath (rGroupDef.dh, res_get (common_resource,
132 bfs_create (res_get (common_resource, "register"));
135 logf (LOG_FATAL, "Cannot access register");
139 bf_lockDir (rGroupDef.bfs,
140 res_get (common_resource, "lockDir"));
141 rGroupDef.zebra_maps = zebra_maps_open (common_resource);
143 if (!strcmp (arg, "update"))
145 else if (!strcmp (arg, "update1"))
147 else if (!strcmp (arg, "update2"))
149 else if (!strcmp (arg, "dump"))
151 else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
153 else if (!strcmp (arg, "init"))
156 rval = res_get (common_resource, "shadow");
157 zebraIndexLock (rGroupDef.bfs, 0, rval);
159 bf_cache (rGroupDef.bfs, rval);
160 zebraIndexLockMsg ("w");
161 bf_reset (rGroupDef.bfs);
163 else if (!strcmp (arg, "commit"))
165 rval = res_get (common_resource, "shadow");
166 zebraIndexLock (rGroupDef.bfs, 1, rval);
168 bf_cache (rGroupDef.bfs, rval);
171 logf (LOG_FATAL, "Cannot perform commit");
172 logf (LOG_FATAL, "No shadow area defined");
175 if (bf_commitExists (rGroupDef.bfs))
177 logf (LOG_LOG, "commit start");
178 zebraIndexLockMsg ("c");
180 logf (LOG_LOG, "commit execute");
181 bf_commitExec (rGroupDef.bfs);
185 zebraIndexLockMsg ("d");
187 logf (LOG_LOG, "commit clean");
188 bf_commitClean (rGroupDef.bfs, rval);
191 logf (LOG_LOG, "nothing to commit");
193 else if (!strcmp (arg, "clean"))
195 rval = res_get (common_resource, "shadow");
196 zebraIndexLock (rGroupDef.bfs, 1, rval);
197 if (bf_commitExists (rGroupDef.bfs))
199 zebraIndexLockMsg ("d");
201 logf (LOG_LOG, "commit clean");
202 bf_commitClean (rGroupDef.bfs, rval);
205 logf (LOG_LOG, "nothing to clean");
207 else if (!strcmp (arg, "stat") || !strcmp (arg, "status"))
210 rval = res_get (common_resource, "shadow");
211 zebraIndexLock (rGroupDef.bfs, 0, rval);
214 bf_cache (rGroupDef.bfs, rval);
215 zebraIndexLockMsg ("r");
217 records = rec_open (rGroupDef.bfs, 0, 0);
218 rec_prstat (records);
219 rec_close (&records);
220 inv_prstat (rGroupDef.bfs);
222 else if (!strcmp (arg, "compact"))
224 rval = res_get (common_resource, "shadow");
225 zebraIndexLock (rGroupDef.bfs, 0, rval);
228 bf_cache (rGroupDef.bfs, rval);
229 zebraIndexLockMsg ("r");
231 inv_compact(rGroupDef.bfs);
235 logf (LOG_FATAL, "unknown command: %s", arg);
241 struct recordGroup rGroup;
244 /* For zebra, delete lock file and reset register */
245 if (rGroupDef.flagRw)
248 bf_reset (rGroupDef.bfs);
251 rval = res_get (common_resource, "shadow");
252 zebraIndexLock (rGroupDef.bfs, 0, rval);
253 if (rGroupDef.flagRw)
255 if (rval && *rval && !disableCommit)
257 bf_cache (rGroupDef.bfs, rval);
258 zebraIndexLockMsg ("r");
262 bf_cache (rGroupDef.bfs, 0);
263 zebraIndexLockMsg ("w");
267 memcpy (&rGroup, &rGroupDef, sizeof(rGroup));
272 if (!key_open (&rGroup, mem_max))
274 logf (LOG_LOG, "updating %s", rGroup.path);
275 repositoryUpdate (&rGroup);
276 nsections = key_close (&rGroup);
280 if (!key_open (&rGroup, mem_max))
282 logf (LOG_LOG, "updating (pass 1) %s", rGroup.path);
283 repositoryUpdate (&rGroup);
289 if (!key_open (&rGroup,mem_max))
291 logf (LOG_LOG, "deleting %s", rGroup.path);
292 repositoryDelete (&rGroup);
293 nsections = key_close (&rGroup);
297 logf (LOG_LOG, "dumping %s", rGroup.path);
298 repositoryShow (&rGroup);
310 logf (LOG_LOG, "merging with index");
311 key_input (rGroup.bfs, nsections, 60, common_resource);
316 log_event_end (NULL, NULL);
322 fprintf (stderr, "Z'mbol %s %s\n", ZEBRAVER, ZEBRADATE);
324 fprintf (stderr, "Zebra %s %s\n", ZEBRAVER, ZEBRADATE);
326 fprintf (stderr, " (C) 1994-2001, Index Data ApS\n");
329 fprintf (stderr, " WIN32 Debug\n");
331 fprintf (stderr, " WIN32 Release\n");
335 fprintf (stderr, "libbzip2\n"
336 " (C) 1996-1999 Julian R Seward. All rights reserved.\n");
340 yaz_log_init_level (yaz_log_mask_str(arg));
342 yaz_log_init_file (arg);
344 mem_max = 1024*1024*atoi(arg);
346 rGroupDef.databaseName = arg;
348 rGroupDef.flagRw = 0;
350 rGroupDef.groupName = arg;
352 rGroupDef.fileVerboseLimit = atoi(arg);
356 rGroupDef.recordType = arg;
361 rGroupDef.useSDR = 1;
364 logf (LOG_WARN, "unknown option '-%s'", arg);
366 recTypes_destroy (rGroupDef.recTypes);
370 bfs_destroy (rGroupDef.bfs);
372 data1_destroy (rGroupDef.dh);