Towards GPL
[idzebra-moved-to-github.git] / index / zebraapi.c
index a84c4c1..9e48592 100644 (file)
@@ -1,9 +1,26 @@
-/*
- * Copyright (C) 1995-2002, Index Data
- * All rights reserved.
- *
- * $Id: zebraapi.c,v 1.51 2002-04-04 14:14:13 adam Exp $
- */
+/* $Id: zebraapi.c,v 1.65 2002-08-02 19:26:55 adam Exp $
+   Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
+   Index Data Aps
+
+This file is part of the Zebra server.
+
+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
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with Zebra; see the file LICENSE.zebra.  If not, write to the
+Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
+02111-1307, USA.
+*/
+
+
 
 #include <assert.h>
 #include <stdio.h>
@@ -35,16 +52,14 @@ static void zebra_chdir (ZebraService zh)
 #endif
 }
 
-
 static void zebra_flush_reg (ZebraHandle zh)
 {
-    zebraExplain_flush (zh->reg->zei, 1, zh);
+    zebraExplain_flush (zh->reg->zei, zh);
     
     extract_flushWriteKeys (zh);
     zebra_index_merge (zh);
 }
 
-
 static struct zebra_register *zebra_register_open (ZebraService zs, 
                                                    const char *name,
                                                    int rw, int useshadow,
@@ -52,14 +67,13 @@ static struct zebra_register *zebra_register_open (ZebraService zs,
                                                    const char *reg_path);
 static void zebra_register_close (ZebraService zs, struct zebra_register *reg);
 
-static int zebra_begin_read (ZebraHandle zh);
-static void zebra_end_read (ZebraHandle zh);
-
 ZebraHandle zebra_open (ZebraService zs)
 {
     ZebraHandle zh;
+    const char *default_encoding;
 
-    assert (zs);
+    if (!zs)
+        return 0;
 
     zh = (ZebraHandle) xmalloc (sizeof(*zh));
     yaz_log (LOG_LOG, "zebra_open zs=%p returns %p", zs, zh);
@@ -84,6 +98,23 @@ ZebraHandle zebra_open (ZebraService zs)
 
     zh->admin_databaseName = 0;
 
+    zh->shadow_enable = 1;
+
+    default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1");
+    zh->record_encoding = xstrdup (default_encoding);
+#if HAVE_ICONV_H
+    zh->iconv_to_utf8 =
+        iconv_open ("UTF-8", default_encoding);
+    if (zh->iconv_to_utf8 == (iconv_t)(-1))
+        yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported",
+           default_encoding);
+    zh->iconv_from_utf8 =
+        iconv_open (default_encoding, "UTF-8");
+    if (zh->iconv_to_utf8 == (iconv_t)(-1))
+        yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported",
+           default_encoding);
+#endif
+
     zebra_mutex_cond_lock (&zs->session_lock);
 
     zh->next = zs->sessions;
@@ -96,36 +127,40 @@ ZebraHandle zebra_open (ZebraService zs)
 
 ZebraService zebra_start (const char *configName)
 {
-    ZebraService zh = xmalloc (sizeof(*zh));
+    Res res;
 
     yaz_log (LOG_LOG, "zebra_start %s", configName);
 
-    zh->configName = xstrdup(configName);
-    zh->sessions = 0;
-
-    if (!(zh->global_res = res_open (zh->configName, 0)))
-    {
-       logf (LOG_WARN, "Failed to read resources `%s'", zh->configName);
-    }
+    if (!(res = res_open (configName, 0)))
+       yaz_log (LOG_WARN, "Cannot read resources `%s'", configName);
     else
     {
-       logf (LOG_LOG, "Read resources `%s'", zh->configName);
-    }
-    zebra_chdir (zh);
+        ZebraService zh = xmalloc (sizeof(*zh));
 
-    zebra_mutex_cond_init (&zh->session_lock);
-    if (!res_get (zh->global_res, "passwd"))
-       zh->passwd_db = NULL;
-    else
-    {
-       zh->passwd_db = passwd_db_open ();
-       if (!zh->passwd_db)
-           logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
-       else
-           passwd_db_file (zh->passwd_db, res_get (zh->global_res, "passwd"));
+       yaz_log (LOG_LOG, "Read resources `%s'", configName);
+        
+        zh->global_res = res;
+        zh->configName = xstrdup(configName);
+        zh->sessions = 0;
+        
+        zebra_chdir (zh);
+        
+        zebra_mutex_cond_init (&zh->session_lock);
+        if (!res_get (zh->global_res, "passwd"))
+            zh->passwd_db = NULL;
+        else
+        {
+            zh->passwd_db = passwd_db_open ();
+            if (!zh->passwd_db)
+                logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
+            else
+                passwd_db_file (zh->passwd_db,
+                                res_get (zh->global_res, "passwd"));
+        }
+        zh->path_root = res_get (zh->global_res, "root");
+        return zh;
     }
-    zh->path_root = res_get (zh->global_res, "root");
-    return zh;
+    return 0;
 }
 
 static
@@ -150,7 +185,7 @@ struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
     yaz_log (LOG_LOG, "zebra_register_open rw = %d useshadow=%d p=%p",
              rw, useshadow, reg);
 
-    reg->dh = data1_create ();
+    reg->dh = data1_createx (DATA1_FLAG_XML);
     if (!reg->dh)
         return 0;
     reg->bfs = bfs_create (res_get (res, "register"), reg_path);
@@ -162,10 +197,11 @@ struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
     if (useshadow)
         bf_cache (reg->bfs, res_get (res, "shadow"));
     data1_set_tabpath (reg->dh, res_get(res, "profilePath"));
+    data1_set_tabroot (reg->dh, reg_path);
     reg->recTypes = recTypes_init (reg->dh);
     recTypes_default_handlers (reg->recTypes);
 
-    reg->zebra_maps = zebra_maps_open (res);
+    reg->zebra_maps = zebra_maps_open (res, reg_path);
     reg->rank_classes = NULL;
 
     reg->key_buf = 0;
@@ -178,11 +214,10 @@ struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
     reg->sortIdx = 0;
     reg->isams = 0;
     reg->matchDict = 0;
-#if ZMBOL
     reg->isam = 0;
     reg->isamc = 0;
     reg->isamd = 0;
-#endif
+    reg->isamb = 0;
     reg->zei = 0;
     reg->matchDict = 0;
     
@@ -223,8 +258,7 @@ struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
            return 0;
        }
     }
-#if ZMBOL
-    else if (res_get_match (res, "isam", "i", ISAM_DEFAULT))
+    if (res_get_match (res, "isam", "i", ISAM_DEFAULT))
     {
        if (!(reg->isam = is_open (reg->bfs, FNAME_ISAM, key_compare, rw,
                                  sizeof (struct it_key), res)))
@@ -233,7 +267,7 @@ struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
            return 0;
        }
     }
-    else if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
+    if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
     {
        struct ISAMC_M_s isamc_m;
        if (!(reg->isamc = isc_open (reg->bfs, FNAME_ISAMC,
@@ -243,7 +277,7 @@ struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
            return 0;
        }
     }
-    else if (res_get_match (res, "isam", "d", ISAM_DEFAULT))
+    if (res_get_match (res, "isam", "d", ISAM_DEFAULT))
     {
        struct ISAMD_M_s isamd_m;
        
@@ -254,7 +288,39 @@ struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
            return 0;
        }
     }
-#endif
+    if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
+    {
+       struct ISAMC_M_s isamc_m;
+       
+       if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
+                                       rw, key_isamc_m(res, &isamc_m), 0)))
+       {
+           logf (LOG_WARN, "isamb_open");
+           return 0;
+       }
+    }
+    if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
+    {
+       struct ISAMC_M_s isamc_m;
+       
+       if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
+                                       rw, key_isamc_m(res, &isamc_m), 1)))
+       {
+           logf (LOG_WARN, "isamb_open");
+           return 0;
+       }
+    }
+    if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
+    {
+       struct ISAMC_M_s isamc_m;
+       
+       if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
+                                       rw, key_isamc_m(res, &isamc_m), -1)))
+       {
+           logf (LOG_WARN, "isamb_open");
+           return 0;
+       }
+    }
     reg->zei = zebraExplain_open (reg->records, reg->dh,
                                   res, rw, reg,
                                   explain_extract);
@@ -290,21 +356,21 @@ static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
     zebra_chdir (zs);
     if (reg->records)
     {
-        zebraExplain_close (reg->zei, 0);
+        zebraExplain_close (reg->zei);
         dict_close (reg->dict);
         if (reg->matchDict)
             dict_close (reg->matchDict);
        sortIdx_close (reg->sortIdx);
        if (reg->isams)
            isams_close (reg->isams);
-#if ZMBOL
         if (reg->isam)
             is_close (reg->isam);
         if (reg->isamc)
             isc_close (reg->isamc);
         if (reg->isamd)
             isamd_close (reg->isamd);
-#endif
+        if (reg->isamb)
+            isamb_close (reg->isamb);
         rec_close (&reg->records);
     }
 
@@ -364,6 +430,14 @@ void zebra_close (ZebraHandle zh)
         zebra_register_close (zh->service, zh->reg);
     zebra_close_res (zh);
 
+    xfree (zh->record_encoding);
+#if HAVE_ICONV_H
+    if (zh->iconv_to_utf8 != (iconv_t) (-1))
+        iconv_close (zh->iconv_to_utf8);
+    if (zh->iconv_from_utf8 != (iconv_t) (-1))
+        iconv_close (zh->iconv_from_utf8);
+#endif
+
     xfree (zh->admin_databaseName);
     zebra_mutex_cond_lock (&zs->session_lock);
     zebra_lock_destroy (zh->lock_normal);
@@ -384,7 +458,7 @@ void zebra_close (ZebraHandle zh)
     zebra_mutex_cond_unlock (&zs->session_lock);
     xfree (zh->reg_name);
     xfree (zh);
-    yaz_log (LOG_LOG, "zebra_close zh=%p end", zh);}
+}
 
 struct map_baseinfo {
     ZebraHandle zh;
@@ -400,21 +474,22 @@ static Res zebra_open_res (ZebraHandle zh)
 {
     Res res = 0;
     char fname[512];
-    if (*zh->reg_name == 0)
-    {
-        res = zh->service->global_res;
-        yaz_log (LOG_LOG, "local res = global res");
-    }
-    else if (zh->path_reg)
+    if (zh->path_reg)
     {
         sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
-        yaz_log (LOG_LOG, "res_open(%s)", fname);
         res = res_open (fname, zh->service->global_res);
         if (!res)
-            return 0;
+            res = zh->service->global_res;
+    }
+    else if (*zh->reg_name == 0)
+    {
+        res = zh->service->global_res;
     }
     else
+    {
+        yaz_log (LOG_WARN, "no register root specified");
         return 0;  /* no path for register - fail! */
+    }
     return res;
 }
 
@@ -557,7 +632,7 @@ int zebra_select_databases (ZebraHandle zh, int num_bases,
 {
     int i;
     const char *cp;
-    size_t len = 0;
+    int len = 0;
     char *new_reg = 0;
     
     if (num_bases < 1)
@@ -596,7 +671,7 @@ int zebra_select_databases (ZebraHandle zh, int num_bases,
                 zh->errCode = 23;
                 return -1;
             }
-            if (len != cp - basenames[i] ||
+            if (len != cp1 - basenames[i] ||
                 memcmp (basenames[i], new_reg, len))
             {
                 zh->errCode = 23;
@@ -635,7 +710,6 @@ void zebra_search_rpn (ZebraHandle zh, ODR decode, ODR stream,
 
     zebra_end_read (zh);
 
-    logf(LOG_APP,"SEARCH:%d:",zh->hits);
     *hits = zh->hits;
 }
 
@@ -653,6 +727,8 @@ void zebra_records_retrieve (ZebraHandle zh, ODR stream,
         zh->errString = odr_strdup (stream, setname);
         return;
     }
+    
+    zh->errCode = 0;
 
     if (zebra_begin_read (zh))
        return;
@@ -908,7 +984,7 @@ void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
     xfree (fname);
 }
 
-static int zebra_begin_read (ZebraHandle zh)
+int zebra_begin_read (ZebraHandle zh)
 {
     int dirty = 0;
     char val;
@@ -923,6 +999,9 @@ static int zebra_begin_read (ZebraHandle zh)
         zebra_flush_reg (zh);
         return 0;
     }
+#if HAVE_SYS_TIMES_H
+    times (&zh->tms1);
+#endif
     if (!zh->res)
     {
         (zh->trans_no)--;
@@ -971,13 +1050,21 @@ static int zebra_begin_read (ZebraHandle zh)
     return 0;
 }
 
-static void zebra_end_read (ZebraHandle zh)
+void zebra_end_read (ZebraHandle zh)
 {
     (zh->trans_no)--;
 
     if (zh->trans_no != 0)
         return;
 
+#if HAVE_SYS_TIMES_H
+    times (&zh->tms2);
+    logf (LOG_LOG, "user/system: %ld/%ld",
+                    (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
+                    (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
+
+#endif
+
     zebra_unlock (zh->lock_normal);
     zebra_unlock (zh->lock_shadow);
 }
@@ -987,7 +1074,7 @@ void zebra_begin_trans (ZebraHandle zh)
     int pass;
     int seqno = 0;
     char val = '?';
-    const char *rval;
+    const char *rval = 0;
 
     assert (zh->res);
 
@@ -998,12 +1085,19 @@ void zebra_begin_trans (ZebraHandle zh)
     }
     
     yaz_log (LOG_LOG, "zebra_begin_trans");
+
+    zh->records_inserted = 0;
+    zh->records_updated = 0;
+    zh->records_deleted = 0;
+    zh->records_processed = 0;
+
 #if HAVE_SYS_TIMES_H
     times (&zh->tms1);
 #endif
     
     /* lock */
-    rval = res_get (zh->res, "shadow");
+    if (zh->shadow_enable)
+        rval = res_get (zh->res, "shadow");
 
     for (pass = 0; pass < 2; pass++)
     {
@@ -1072,11 +1166,18 @@ void zebra_end_trans (ZebraHandle zh)
     yaz_log (LOG_LOG, "zebra_end_trans");
     rval = res_get (zh->res, "shadow");
 
+    zebraExplain_runNumberIncrement (zh->reg->zei, 1);
+
     zebra_flush_reg (zh);
 
     zebra_register_close (zh->service, zh->reg);
     zh->reg = 0;
 
+    
+    yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d", 
+             zh->records_processed, zh->records_inserted,
+             zh->records_updated, zh->records_deleted);
+
     zebra_get_state (zh, &val, &seqno);
     if (val != 'd')
     {
@@ -1154,7 +1255,6 @@ int zebra_commit (ZebraHandle zh)
         zebra_set_state (zh, 'c', seqno);
 
         logf (LOG_LOG, "commit start");
-        sleep (2);
         bf_commitExec (bfs);
 #ifndef WIN32
         sync ();
@@ -1240,3 +1340,14 @@ void zebra_result (ZebraHandle zh, int *code, char **addinfo)
     *addinfo = zh->errString;
 }
 
+void zebra_shadow_enable (ZebraHandle zh, int value)
+{
+    zh->shadow_enable = value;
+}
+
+int zebra_record_encoding (ZebraHandle zh, const char *encoding)
+{
+    xfree (zh->record_encoding);
+    zh->record_encoding = xstrdup (encoding);
+    return 0;
+}