Beginnings of zebra_register_check
[idzebra-moved-to-github.git] / index / zebraidx.c
index 4fe0448..c411901 100644 (file)
@@ -1,8 +1,5 @@
-/* $Id: zebraidx.c,v 1.2 2006-12-05 09:26:37 adam Exp $
-   Copyright (C) 1995-2006
-   Index Data ApS
-
-This file is part of the Zebra server.
+/* This file is part of the Zebra server.
+   Copyright (C) 1994-2010 Index Data
 
 Zebra is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
@@ -30,23 +27,20 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #if HAVE_UNISTD_H
 #include <unistd.h>
 #endif
-#if HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
-#include <time.h>
-#if HAVE_SYS_TIMES_H
-#include <sys/times.h>
-#endif
 
+#if YAZ_HAVE_ICU
+#include <yaz/icu.h>
+#endif
 #include <yaz/log.h>
 #include <yaz/options.h>
+#include <idzebra/version.h>
 #include <idzebra/api.h>
 
 char *prog;
 
 static void filter_cb(void *cd, const char *name)
 {
-    puts (name);
+    puts(name);
 }
 
 static void show_filters(ZebraService zs)
@@ -54,7 +48,7 @@ static void show_filters(ZebraService zs)
     zebra_filter_info(zs, 0, filter_cb);
 }
 
-int main (int argc, char **argv)
+int main(int argc, char **argv)
 {
     int ret;
     int cmd = 0;
@@ -66,37 +60,22 @@ int main (int argc, char **argv)
     Res res = res_open(0, 0);
     Res default_res = res_open(0, 0);
     
-    int trans_started=0;
-#if HAVE_SYS_TIMES_H
-    struct tms tms1, tms2;
-    double usec;
-#endif
-#if HAVE_SYS_TIME_H
-    struct timeval start_time, end_time;
-#endif
+    int trans_started = 0;
 #ifndef WIN32
     char nbuf[100];
 #endif
     ZebraService zs = 0;
     ZebraHandle zh = 0;
 
-    nmem_init ();
-
 #ifdef WIN32
 #else
     sprintf(nbuf, "%.40s(%ld)", *argv, (long) getpid());
-    yaz_log_init_prefix (nbuf);
-#endif
-#if HAVE_SYS_TIMES_H
-    times(&tms1);
-#endif
-#if HAVE_SYS_TIME_H
-    gettimeofday(&start_time, 0);
+    yaz_log_init_prefix(nbuf);
 #endif
     prog = *argv;
     if (argc < 2)
     {
-        fprintf (stderr, "%s [options] command <dir> ...\n"
+        fprintf(stderr, "%s [options] command <dir> ...\n"
         "Commands:\n"
         " update <dir>  Update index with files below <dir>.\n"
        "               If <dir> is empty filenames are read from stdin.\n"
@@ -119,7 +98,7 @@ int main (int argc, char **argv)
         " -f <n>        Display information for the first <n> records.\n"
         " -V            Show version.\n", *argv
                  );
-        exit (1);
+        exit(1);
     }
     res_set(default_res, "profilePath", DEFAULT_PROFILE_PATH);
     res_set(default_res, "modulePath", DEFAULT_MODULE_PATH);
@@ -135,34 +114,36 @@ int main (int argc, char **argv)
                     zs = zebra_start_res(config, default_res, res);
                     if (!zs)
                     {
-                       yaz_log (YLOG_FATAL, "Cannot read config %s", config);
-                        exit (1);
+                       yaz_log(YLOG_FATAL, "Cannot read config %s", config);
+                        exit(1);
                    }   
-                    zh = zebra_open (zs, 0);
-                   zebra_shadow_enable (zh, enable_commit);
+                    zh = zebra_open(zs, 0);
+                   zebra_shadow_enable(zh, enable_commit);
                 }
 
                if (database &&
-                   zebra_select_database (zh, database) == ZEBRA_FAIL)
+                   zebra_select_database(zh, database) == ZEBRA_FAIL)
                {
                    yaz_log(YLOG_FATAL, "Could not select database %s "
                            "errCode=%d",
                            database, zebra_errCode(zh) );
-                   exit (1);
+                   exit(1);
                }
-                if (!strcmp (arg, "update"))
+                if (!strcmp(arg, "update"))
                     cmd = 'u';
-                else if (!strcmp (arg, "update1"))
+                else if (!strcmp(arg, "update1"))
                     cmd = 'U';
-                else if (!strcmp (arg, "update2"))
+                else if (!strcmp(arg, "update2"))
                     cmd = 'm';
-                else if (!strcmp (arg, "dump"))
+                else if (!strcmp(arg, "dump"))
                     cmd = 's';
-                else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
+                else if (!strcmp(arg, "del") || !strcmp(arg, "delete"))
                     cmd = 'd';
-               else if (!strcmp (arg, "init"))
+                else if (!strcmp(arg, "adelete"))
+                    cmd = 'a';
+               else if (!strcmp(arg, "init"))
                {
-                    zebra_init (zh);
+                    zebra_init(zh);
                }
                else if (!strcmp(arg, "drop"))
                {
@@ -172,34 +153,39 @@ int main (int argc, char **argv)
                {
                    cmd = 'C';
                }
-                else if (!strcmp (arg, "commit"))
+                else if (!strcmp(arg, "commit"))
                 {
-                    zebra_commit (zh);
+                    zebra_commit(zh);
                 }
-                else if (!strcmp (arg, "clean"))
+                else if (!strcmp(arg, "clean"))
                 {
-                    zebra_clean (zh);
+                    zebra_clean(zh);
                 }
-                else if (!strcmp (arg, "stat") || !strcmp (arg, "status"))
+                else if (!strcmp(arg, "stat") || !strcmp(arg, "status"))
                 {
-                    zebra_register_statistics (zh,0);
+                    zebra_register_statistics(zh, 0);
                 }
-                else if (!strcmp (arg, "dumpdict"))
+                else if (!strcmp(arg, "dumpdict"))
                 {
-                    zebra_register_statistics (zh,1);
+                    zebra_register_statistics(zh, 1);
                 }
-                else if (!strcmp (arg, "compact"))
+                else if (!strcmp(arg, "compact"))
                 {
-                    zebra_compact (zh);
+                    zebra_compact(zh);
                 }
-                else if (!strcmp (arg, "filters"))
+                else if (!strcmp(arg, "filters"))
                 {
                     show_filters(zs);
                 }
+               else if (!strcmp(arg, "check"))
+               {
+                    if (zebra_register_check(zh) != ZEBRA_OK)
+                        yaz_log(YLOG_WARN, "register check failed");
+               }
                 else
                 {
-                    yaz_log (YLOG_FATAL, "unknown command: %s", arg);
-                    exit (1);
+                    yaz_log(YLOG_FATAL, "unknown command: %s", arg);
+                    exit(1);
                 }
             }
            else
@@ -208,19 +194,22 @@ int main (int argc, char **argv)
                if (!trans_started)
                {
                    trans_started=1;
-                    if (zebra_begin_trans (zh, 1) != ZEBRA_OK)
+                    if (zebra_begin_trans(zh, 1) != ZEBRA_OK)
                         exit(1);
                }
                 switch (cmd)
                 {
                 case 'u':
-                    res = zebra_repository_update (zh, arg);
+                    res = zebra_repository_index(zh, arg, action_update);
                     break;
                 case 'd':
-                    res = zebra_repository_delete (zh, arg);
+                    res = zebra_repository_index(zh, arg, action_delete);
+                    break;
+                case 'a':
+                    res = zebra_repository_index(zh, arg, action_a_delete);
                     break;
                 case 's':
-                    res = zebra_repository_show (zh, arg);
+                    res = zebra_repository_show(zh, arg);
                     nsections = 0;
                     break;
                case 'C':
@@ -239,36 +228,38 @@ int main (int argc, char **argv)
                             zebra_errString(zh), add ? add : "");
                     
                     if (trans_started)
-                        if (zebra_end_trans (zh) != ZEBRA_OK)
-                            yaz_log (YLOG_WARN, "zebra_end_trans failed");
+                        if (zebra_end_trans(zh) != ZEBRA_OK)
+                            yaz_log(YLOG_WARN, "zebra_end_trans failed");
 
 
-                    zebra_close (zh);
-                    zebra_stop (zs);
+                    zebra_close(zh);
+                    zebra_stop(zs);
                    exit(1);
                }
-                log_event_end (NULL, NULL);
+                log_event_end(NULL, NULL);
             }
         }
         else if (ret == 'V')
         {
-            printf("Zebra %s %s\n", ZEBRAVER, ZEBRADATE);
-           printf(" (C) 1994-2006, Index Data ApS\n");
-#ifdef WIN32
-#ifdef _DEBUG
-            printf(" WIN32 Debug\n");
-#else
-            printf(" WIN32 Release\n");
-#endif
-#endif
-#if HAVE_BZLIB_H
-            printf("Using: libbzip2, (C) 1996-1999 Julian R Seward.  All rights reserved.\n");
+            char version_str[20];
+            char sys_str[80];
+            zebra_get_version(version_str, sys_str);
+
+            printf("Zebra %s\n", version_str);
+           printf("(C) 1994-2010, Index Data ApS\n");
+            printf("Zebra is free software, covered by the GNU General Public License, and you are\n");
+            printf("welcome to change it and/or distribute copies of it under certain conditions.\n");
+            printf("SHA1 ID: %s\n", sys_str);
+            if (strcmp(version_str, ZEBRAVER))
+                printf("zebraidx compiled version %s\n", ZEBRAVER);
+#if YAZ_HAVE_ICU
+            printf("Using ICU\n");
 #endif
         }
         else if (ret == 'v')
-            yaz_log_init_level (yaz_log_mask_str(arg));
+            yaz_log_init_level(yaz_log_mask_str(arg));
        else if (ret == 'l')
-           yaz_log_init_file (arg);
+           yaz_log_init_file(arg);
         else if (ret == 'm')
            res_set(res, "memMax", arg);
         else if (ret == 'd')
@@ -288,40 +279,26 @@ int main (int argc, char **argv)
        else if (ret == 'L')
            res_set(res, "followLinks", "0");
         else
-            yaz_log (YLOG_WARN, "unknown option '-%s'", arg);
+            yaz_log(YLOG_WARN, "unknown option '-%s'", arg);
     } /* while arg */
 
     if (trans_started)
-        if (zebra_end_trans (zh) != ZEBRA_OK)
-            yaz_log (YLOG_WARN, "zebra_end_trans failed");
+        if (zebra_end_trans(zh) != ZEBRA_OK)
+            yaz_log(YLOG_WARN, "zebra_end_trans failed");
+
+    zebra_close(zh);
+    zebra_stop(zs);
 
-    zebra_close (zh);
-    zebra_stop (zs);
-#if HAVE_SYS_TIMES_H
-#if HAVE_SYS_TIME_H
-    if (trans_started)
-    {
-        gettimeofday(&end_time, 0);
-        usec = (end_time.tv_sec - start_time.tv_sec) * 1000000.0 +
-           end_time.tv_usec - start_time.tv_usec;
-        times(&tms2);
-        yaz_log (YLOG_LOG, "zebraidx times: %5.2f %5.2f %5.2f",
-               usec / 1000000,
-               (double) (tms2.tms_utime - tms1.tms_utime)/100,
-               (double) (tms2.tms_stime - tms1.tms_stime)/100);
-    }
-#endif
-#endif
     res_close(res);
     res_close(default_res);
-    nmem_exit();
-    exit (0);
+    exit(0);
     return 0;
 }
 
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab