X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=index%2Fmain.c;h=60310e449c860a4360694a06a99906cfc0dd8553;hb=ac66787b71dd244e895a262a82b2a4944f8fea6b;hp=389585d28b4dc1621ce8d73bce80bbdeb3fe8011;hpb=a02c4cf957603c707fe9f4fde94a468f1d030dfb;p=idzebra-moved-to-github.git diff --git a/index/main.c b/index/main.c index 389585d..60310e4 100644 --- a/index/main.c +++ b/index/main.c @@ -1,111 +1,378 @@ /* - * Copyright (C) 1995, Index Data I/S + * Copyright (C) 1994-2001, Index Data * All rights reserved. - * Sebastian Hammer, Adam Dickmeiss - * - * $Log: main.c,v $ - * Revision 1.5 1995-09-04 09:10:39 adam - * More work on index add/del/update. - * Merge sort implemented. - * Initial work on z39 server. - * - * Revision 1.4 1995/09/01 14:06:36 adam - * Split of work into more files. - * - * Revision 1.3 1995/09/01 10:57:07 adam - * Minor changes. - * - * Revision 1.2 1995/09/01 10:30:24 adam - * More work on indexing. Not working yet. - * - * Revision 1.1 1995/08/31 14:50:24 adam - * New simple file index tool. * + * $Id: main.c,v 1.81 2001-11-19 23:29:09 adam Exp $ */ #include +#include #include +#ifdef WIN32 +#include +#else #include +#endif -#include +#include #include "index.h" +#include "recindex.h" + +#ifndef ZEBRASDR +#define ZEBRASDR 0 +#endif + +#if ZEBRASDR +#include "zebrasdr.h" +#endif char *prog; +Res common_resource = 0; + + int main (int argc, char **argv) { int ret; int cmd = 0; char *arg; - char *base_name = NULL; - char *base_path = NULL; + char *configName = FNAME_CONFIG; + int nsections = 0; + int disableCommit = 0; + size_t mem_max = 0; +#ifndef WIN32 + char nbuf[100]; +#endif + struct recordGroup rGroupDef; + + nmem_init (); + +#ifdef WIN32 +#else + sprintf(nbuf, "%.40s(%d)", *argv, getpid()); + yaz_log_init_prefix (nbuf); +#endif + +#if ZEBRASDR + zebraSdr_std (); + rGroupDef.useSDR = 0; +#endif + rGroupDef.groupName = NULL; + rGroupDef.databaseName = NULL; + rGroupDef.path = NULL; + rGroupDef.recordId = NULL; + rGroupDef.recordType = NULL; + rGroupDef.flagStoreData = -1; + rGroupDef.flagStoreKeys = -1; + rGroupDef.flagRw = 1; + rGroupDef.databaseNamePath = 0; + rGroupDef.explainDatabase = 0; + rGroupDef.fileVerboseLimit = 100000; + rGroupDef.zebra_maps = NULL; + rGroupDef.dh = data1_create (); + rGroupDef.recTypes = recTypes_init (rGroupDef.dh); + recTypes_default_handlers (rGroupDef.recTypes); prog = *argv; - while ((ret = options ("r:v:", argv, argc, &arg)) != -2) + if (argc < 2) + { + fprintf (stderr, "%s [options] command ...\n" + "Commands:\n" + " update Update index with files below .\n" + " If is empty filenames are read from stdin.\n" + " delete Delete index with files below .\n" + " commit Commit changes\n" + " clean Clean shadow files\n" + "Options:\n" + " -t Index files as (grs or text).\n" + " -c Read configuration file .\n" + " -g Index files according to group settings.\n" + " -d Records belong to Z39.50 database .\n" + " -m Use before flushing keys to disk.\n" + " -n Don't use shadow system.\n" + " -s Show analysis on stdout, but do no work.\n" + " -v Set logging to .\n" + " -l Write log to .\n" + " -f Display information for the first records.\n" +#if ZEBRASDR + " -S Use SDRKit\n" +#endif + " -V Show version.\n", *argv + ); + exit (1); + } + while ((ret = options ("sVt:c:g:d:m:v:nf:l:" +#if ZEBRASDR + "S" +#endif + , argv, argc, &arg)) != -2) { if (ret == 0) { - if (!base_name) + const char *rval; + if(cmd == 0) /* command */ { - base_name = arg; - - common_resource = res_open (base_name); if (!common_resource) { - log (LOG_FATAL, "Cannot open resource `%s'", base_name); - exit (1); +#if ZMBOL + logf (LOG_LOG, "zmbol version %s %s", + ZEBRAVER, ZEBRADATE); +#else + logf (LOG_LOG, "zebra version %s %s", + ZEBRAVER, ZEBRADATE); +#endif + common_resource = res_open (configName ? + configName : FNAME_CONFIG); + if (!common_resource) + { + logf (LOG_FATAL, "cannot read file `%s'", configName); + exit (1); + } + data1_set_tabpath (rGroupDef.dh, res_get (common_resource, + "profilePath")); + + rGroupDef.bfs = + bfs_create (res_get (common_resource, "register")); + if (!rGroupDef.bfs) + { + logf (LOG_FATAL, "Cannot access register"); + exit(1); + } + + bf_lockDir (rGroupDef.bfs, + res_get (common_resource, "lockDir")); + rGroupDef.zebra_maps = zebra_maps_open (common_resource); } - } - else if(cmd == 0) /* command */ - { - if (!strcmp (arg, "add")) + if (!strcmp (arg, "update")) + cmd = 'u'; + else if (!strcmp (arg, "update1")) + cmd = 'U'; + else if (!strcmp (arg, "update2")) + cmd = 'm'; + else if (!strcmp (arg, "dump")) + cmd = 's'; + else if (!strcmp (arg, "del") || !strcmp(arg, "delete")) + cmd = 'd'; + else if (!strcmp (arg, "init")) + { + zebraIndexUnlock(); + rval = res_get (common_resource, "shadow"); + zebraIndexLock (rGroupDef.bfs, 0, rval); + if (rval && *rval) + bf_cache (rGroupDef.bfs, rval); + zebraIndexLockMsg ("w"); + bf_reset (rGroupDef.bfs); + } + else if (!strcmp (arg, "commit")) { - cmd = 'a'; + rval = res_get (common_resource, "shadow"); + zebraIndexLock (rGroupDef.bfs, 1, rval); + if (rval && *rval) + bf_cache (rGroupDef.bfs, rval); + else + { + logf (LOG_FATAL, "Cannot perform commit"); + logf (LOG_FATAL, "No shadow area defined"); + exit (1); + } + if (bf_commitExists (rGroupDef.bfs)) + { + logf (LOG_LOG, "commit start"); + zebraIndexLockMsg ("c"); + zebraIndexWait (1); + logf (LOG_LOG, "commit execute"); + bf_commitExec (rGroupDef.bfs); +#ifndef WIN32 + sync (); +#endif + zebraIndexLockMsg ("d"); + zebraIndexWait (0); + logf (LOG_LOG, "commit clean"); + bf_commitClean (rGroupDef.bfs, rval); + } + else + logf (LOG_LOG, "nothing to commit"); } - else if (!strcmp (arg, "del")) + else if (!strcmp (arg, "clean")) { - cmd = 'd'; + rval = res_get (common_resource, "shadow"); + zebraIndexLock (rGroupDef.bfs, 1, rval); + if (bf_commitExists (rGroupDef.bfs)) + { + zebraIndexLockMsg ("d"); + zebraIndexWait (0); + logf (LOG_LOG, "commit clean"); + bf_commitClean (rGroupDef.bfs, rval); + } + else + logf (LOG_LOG, "nothing to clean"); + } + else if (!strcmp (arg, "stat") || !strcmp (arg, "status")) + { + Records records; + rval = res_get (common_resource, "shadow"); + zebraIndexLock (rGroupDef.bfs, 0, rval); + if (rval && *rval) + { + bf_cache (rGroupDef.bfs, rval); + zebraIndexLockMsg ("r"); + } + records = rec_open (rGroupDef.bfs, 0, 0); + rec_prstat (records); + rec_close (&records); + inv_prstat (rGroupDef.bfs); + } + else if (!strcmp (arg, "compact")) + { + rval = res_get (common_resource, "shadow"); + zebraIndexLock (rGroupDef.bfs, 0, rval); + if (rval && *rval) + { + bf_cache (rGroupDef.bfs, rval); + zebraIndexLockMsg ("r"); + } + inv_compact(rGroupDef.bfs); } else { - log (LOG_FATAL, "Unknown command: %s", arg); + logf (LOG_FATAL, "unknown command: %s", arg); exit (1); } } - else + else { - unlink ("keys.tmp"); - key_open ("keys.tmp"); - repository (cmd, arg, base_path); + struct recordGroup rGroup; +#if ZMBOL +#else + /* For zebra, delete lock file and reset register */ + if (rGroupDef.flagRw) + { + zebraIndexUnlock(); + bf_reset (rGroupDef.bfs); + } +#endif + rval = res_get (common_resource, "shadow"); + zebraIndexLock (rGroupDef.bfs, 0, rval); + if (rGroupDef.flagRw) + { + if (rval && *rval && !disableCommit) + { + bf_cache (rGroupDef.bfs, rval); + zebraIndexLockMsg ("r"); + } + else + { + bf_cache (rGroupDef.bfs, 0); + zebraIndexLockMsg ("w"); + } + zebraIndexWait (0); + } + memcpy (&rGroup, &rGroupDef, sizeof(rGroup)); + rGroup.path = arg; + switch (cmd) + { + case 'u': + if (!key_open (&rGroup, mem_max)) + { + logf (LOG_LOG, "updating %s", rGroup.path); + repositoryUpdate (&rGroup); + nsections = key_close (&rGroup); + } + break; + case 'U': + if (!key_open (&rGroup, mem_max)) + { + logf (LOG_LOG, "updating (pass 1) %s", rGroup.path); + repositoryUpdate (&rGroup); + key_close (&rGroup); + } + nsections = 0; + break; + case 'd': + if (!key_open (&rGroup,mem_max)) + { + logf (LOG_LOG, "deleting %s", rGroup.path); + repositoryDelete (&rGroup); + nsections = key_close (&rGroup); + } + break; + case 's': + logf (LOG_LOG, "dumping %s", rGroup.path); + repositoryShow (&rGroup); + nsections = 0; + break; + case 'm': + nsections = -1; + break; + default: + nsections = 0; + } cmd = 0; + if (nsections) + { + logf (LOG_LOG, "merging with index"); + key_input (rGroup.bfs, nsections, 60, common_resource); +#ifndef WIN32 + sync (); +#endif + } + log_event_end (NULL, NULL); } } - else if (ret == 'v') + else if (ret == 'V') { - log_init (log_mask_str(arg), prog, NULL); - } - else if (ret == 'r') - { - base_path = arg; +#if ZMBOL + fprintf (stderr, "Z'mbol %s %s\n", ZEBRAVER, ZEBRADATE); +#else + fprintf (stderr, "Zebra %s %s\n", ZEBRAVER, ZEBRADATE); +#endif + fprintf (stderr, " (C) 1994-2001, Index Data ApS\n"); +#ifdef WIN32 +#ifdef _DEBUG + fprintf (stderr, " WIN32 Debug\n"); +#else + fprintf (stderr, " WIN32 Release\n"); +#endif +#endif +#if HAVE_BZLIB_H + fprintf (stderr, "libbzip2\n" + " (C) 1996-1999 Julian R Seward. All rights reserved.\n"); +#endif } + else if (ret == 'v') + yaz_log_init_level (yaz_log_mask_str(arg)); + else if (ret == 'l') + yaz_log_init_file (arg); + else if (ret == 'm') + mem_max = 1024*1024*atoi(arg); + else if (ret == 'd') + rGroupDef.databaseName = arg; + else if (ret == 's') + rGroupDef.flagRw = 0; + else if (ret == 'g') + rGroupDef.groupName = arg; + else if (ret == 'f') + rGroupDef.fileVerboseLimit = atoi(arg); + else if (ret == 'c') + configName = arg; + else if (ret == 't') + rGroupDef.recordType = arg; + else if (ret == 'n') + disableCommit = 1; +#if ZEBRASDR + else if (ret == 'S') + rGroupDef.useSDR = 1; +#endif else - { - log (LOG_FATAL, "Unknown option '-%s'", arg); - exit (1); - } + logf (LOG_WARN, "unknown option '-%s'", arg); } - if (!base_name) + recTypes_destroy (rGroupDef.recTypes); + if (common_resource) { - fprintf (stderr, "index [-v log] [-r repository] " - "base cmd1 dir1 cmd2 dir2 ...\n"); - exit (1); + zebraIndexUnlock (); + bfs_destroy (rGroupDef.bfs); } - key_flush (); - if (!key_close ()) - exit (0); - log (LOG_DEBUG, "Sorting"); - if (!key_sort ("keys.tmp", 1000000)) - exit (0); - log (LOG_DEBUG, "Input"); - key_input ("dictinv", "isaminv", "keys.tmp", 50); + data1_destroy (rGroupDef.dh); exit (0); + return 0; } +