zebra_end_trans is just calling zebra_end_transaction now: this second one is re
[idzebra-moved-to-github.git] / index / zebraapi.c
index fabd5e4..abc9124 100644 (file)
@@ -1,9 +1,26 @@
-/*
- * Copyright (C) 1995-2002, Index Data
- * All rights reserved.
- *
- * $Id: zebraapi.c,v 1.63 2002-07-25 13:06:43 adam Exp $
- */
+/* $Id: zebraapi.c,v 1.86 2003-02-27 23:08:10 pop 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>
 #include "index.h"
 #include <charmap.h>
 
+/* simple asserts to validate the most essential input args */
+#define ASSERTZH assert(zh && zh->service)
+#define ASSERTZHRES assert(zh && zh->service && zh->res)
+#define ASSERTZS assert(zs)
+
 static Res zebra_open_res (ZebraHandle zh);
 static void zebra_close_res (ZebraHandle zh);
 
-static void zebra_chdir (ZebraService zh)
+
+static void zebra_chdir (ZebraService zs)
 {
-    const char *dir = res_get (zh->global_res, "chdir");
+    const char *dir ;
+    ASSERTZS;
+    dir = res_get (zs->global_res, "chdir");
     if (!dir)
        return;
     logf (LOG_DEBUG, "chdir %s", dir);
@@ -37,6 +62,8 @@ static void zebra_chdir (ZebraService zh)
 
 static void zebra_flush_reg (ZebraHandle zh)
 {
+    ASSERTZH;
+    zh->errCode=0;
     zebraExplain_flush (zh->reg->zei, zh);
     
     extract_flushWriteKeys (zh);
@@ -54,12 +81,13 @@ ZebraHandle zebra_open (ZebraService zs)
 {
     ZebraHandle zh;
     const char *default_encoding;
+    ASSERTZS;
 
     if (!zs)
         return 0;
 
     zh = (ZebraHandle) xmalloc (sizeof(*zh));
-    yaz_log (LOG_LOG, "zebra_open zs=%p returns %p", zs, zh);
+    yaz_log (LOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
 
     zh->service = zs;
     zh->reg = 0;          /* no register attached yet */
@@ -67,7 +95,7 @@ ZebraHandle zebra_open (ZebraService zs)
     zh->destroyed = 0;
     zh->errCode = 0;
     zh->errString = 0;
-    zh->res = 0;
+    zh->res = 0; 
 
     zh->reg_name = xstrdup ("");
     zh->path_reg = 0;
@@ -85,18 +113,17 @@ ZebraHandle zebra_open (ZebraService zs)
 
     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_iconv_open ("UTF-8", default_encoding);
+    if (zh->iconv_to_utf8 == 0)
         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_iconv_open (default_encoding, "UTF-8");
+    if (zh->iconv_to_utf8 == 0)
         yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported",
            default_encoding);
-#endif
 
     zebra_mutex_cond_lock (&zs->session_lock);
 
@@ -114,13 +141,11 @@ ZebraService zebra_start (const char *configName)
 
     yaz_log (LOG_LOG, "zebra_start %s", configName);
 
-    if (!(res = res_open (configName, 0)))
-       yaz_log (LOG_WARN, "Cannot read resources `%s'", configName);
-    else
+    if ((res = res_open (configName, 0)))
     {
         ZebraService zh = xmalloc (sizeof(*zh));
 
-       yaz_log (LOG_LOG, "Read resources `%s'", configName);
+       yaz_log (LOG_DEBUG, "Read resources `%s'", configName);
         
         zh->global_res = res;
         zh->configName = xstrdup(configName);
@@ -155,6 +180,8 @@ struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
     int record_compression = REC_COMPRESS_NONE;
     char *recordCompression = 0;
 
+    ASSERTZS;
+    
     reg = xmalloc (sizeof(*reg));
 
     assert (name);
@@ -165,10 +192,10 @@ struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
 
     assert (res);
 
-    yaz_log (LOG_LOG, "zebra_register_open rw = %d useshadow=%d p=%p",
+    yaz_log (LOG_DEBUG, "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);
@@ -179,7 +206,8 @@ 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_tabpath (reg->dh, res_get_def(res, "profilePath",
+                                            DEFAULT_PROFILE_PATH));
     data1_set_tabroot (reg->dh, reg_path);
     reg->recTypes = recTypes_init (reg->dh);
     recTypes_default_handlers (reg->recTypes);
@@ -191,6 +219,8 @@ struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
 
     reg->keys.buf_max = 0;
     reg->keys.buf = 0;
+    reg->sortKeys.buf = 0;
+    reg->sortKeys.buf_max = 0;
 
     reg->records = 0;
     reg->dict = 0;
@@ -203,8 +233,10 @@ struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
     reg->isamb = 0;
     reg->zei = 0;
     reg->matchDict = 0;
+    reg->key_file_no = 0;
     
     zebraRankInstall (reg, rank1_class);
+    zebraRankInstall (reg, rankzv_class);
 
     recordCompression = res_get_def (res, "recordCompression", "none");
     if (!strcmp (recordCompression, "none"))
@@ -313,12 +345,15 @@ struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
        return 0;
     }
     reg->active = 2;
-    yaz_log (LOG_LOG, "zebra_register_open ok p=%p", reg);
+    yaz_log (LOG_DEBUG, "zebra_register_open ok p=%p", reg);
     return reg;
 }
 
 void zebra_admin_shutdown (ZebraHandle zh)
 {
+    ASSERTZH;
+    zh->errCode=0;
+
     zebra_mutex_cond_lock (&zh->service->session_lock);
     zh->service->stop_flag = 1;
     zebra_mutex_cond_unlock (&zh->service->session_lock);
@@ -326,15 +361,18 @@ void zebra_admin_shutdown (ZebraHandle zh)
 
 void zebra_admin_start (ZebraHandle zh)
 {
-    ZebraService zs = zh->service;
-    zh->errCode = 0;
+    ZebraService zs;
+    ASSERTZH;
+    zh->errCode=0;
+    zs = zh->service;
     zebra_mutex_cond_lock (&zs->session_lock);
     zebra_mutex_cond_unlock (&zs->session_lock);
 }
 
 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
 {
-    yaz_log(LOG_LOG, "zebra_register_close p=%p", reg);
+    ASSERTZS;
+    yaz_log(LOG_DEBUG, "zebra_register_close p=%p", reg);
     reg->stop_flag = 0;
     zebra_chdir (zs);
     if (reg->records)
@@ -363,26 +401,25 @@ static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
     bfs_destroy (reg->bfs);
     data1_destroy (reg->dh);
 
+    xfree (reg->sortKeys.buf);
+    xfree (reg->keys.buf);
+
     xfree (reg->key_buf);
     xfree (reg->name);
     xfree (reg);
-    yaz_log (LOG_LOG, "zebra_register_close 2");
 }
 
 void zebra_stop(ZebraService zs)
 {
     if (!zs)
-       return ;
+        return ;
     yaz_log (LOG_LOG, "zebra_stop");
 
-    zebra_mutex_cond_lock (&zs->session_lock);
     while (zs->sessions)
     {
         zebra_close (zs->sessions);
     }
         
-    zebra_mutex_cond_unlock (&zs->session_lock);
-
     zebra_mutex_cond_destroy (&zs->session_lock);
 
     if (zs->passwd_db)
@@ -398,28 +435,33 @@ void zebra_close (ZebraHandle zh)
 {
     ZebraService zs;
     struct zebra_session **sp;
+    int i;
 
     if (!zh)
         return;
-
+    ASSERTZH;
+    zh->errCode=0;
+    
     zs = zh->service;
-    yaz_log (LOG_LOG, "zebra_close zh=%p", zh);
+    yaz_log (LOG_DEBUG, "zebra_close zh=%p", zh);
     if (!zh)
-       return ;
+        return ;
     resultSetDestroy (zh, -1, 0, 0);
 
-
     if (zh->reg)
         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
+
+    for (i = 0; i < zh->num_basenames; i++)
+        xfree (zh->basenames[i]);
+    xfree (zh->basenames);
+
+    if (zh->iconv_to_utf8 != 0)
+        yaz_iconv_close (zh->iconv_to_utf8);
+    if (zh->iconv_from_utf8 != 0)
+        yaz_iconv_close (zh->iconv_from_utf8);
 
     xfree (zh->admin_databaseName);
     zebra_mutex_cond_lock (&zs->session_lock);
@@ -436,10 +478,9 @@ void zebra_close (ZebraHandle zh)
        }
        sp = &(*sp)->next;
     }
-//    if (!zs->sessions && zs->stop_flag)
-//     zebra_register_deactivate(zs);
     zebra_mutex_cond_unlock (&zs->session_lock);
     xfree (zh->reg_name);
+    zh->service=0; /* more likely to trigger an assert */
     xfree (zh);
 }
 
@@ -457,6 +498,9 @@ static Res zebra_open_res (ZebraHandle zh)
 {
     Res res = 0;
     char fname[512];
+    ASSERTZH;
+    zh->errCode=0;
+
     if (zh->path_reg)
     {
         sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
@@ -478,6 +522,8 @@ static Res zebra_open_res (ZebraHandle zh)
 
 static void zebra_close_res (ZebraHandle zh)
 {
+    ASSERTZH;
+    zh->errCode=0;
     if (zh->res != zh->service->global_res)
         res_close (zh->res);
     zh->res = 0;
@@ -485,6 +531,8 @@ static void zebra_close_res (ZebraHandle zh)
 
 static int zebra_select_register (ZebraHandle zh, const char *new_reg)
 {
+    ASSERTZH;
+    zh->errCode=0;
     if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
         return 0;
     if (!zh->res)
@@ -561,7 +609,7 @@ void map_basenames_func (void *vp, const char *name, const char *value)
        return ;
     no--;
     for (i = 0; i<p->num_bases; i++)
-       if (p->basenames[i] && !strcmp (p->basenames[i], fromdb))
+       if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
        {
            p->basenames[i] = 0;
            for (i = 0; i < no; i++)
@@ -581,6 +629,8 @@ void map_basenames (ZebraHandle zh, ODR stream,
     struct map_baseinfo info;
     struct map_baseinfo *p = &info;
     int i;
+    ASSERTZH;
+    zh->errCode=0;
 
     info.zh = zh;
     info.num_bases = *num_bases;
@@ -607,6 +657,8 @@ void map_basenames (ZebraHandle zh, ODR stream,
 
 int zebra_select_database (ZebraHandle zh, const char *basename)
 {
+    ASSERTZH;
+    zh->errCode=0;
     return zebra_select_databases (zh, 1, &basename);
 }
 
@@ -617,6 +669,8 @@ int zebra_select_databases (ZebraHandle zh, int num_bases,
     const char *cp;
     int len = 0;
     char *new_reg = 0;
+    ASSERTZH;
+    zh->errCode=0;
     
     if (num_bases < 1)
     {
@@ -677,12 +731,19 @@ int zebra_select_databases (ZebraHandle zh, int num_bases,
         zh->errCode = 109;
         return -1;
     }
+    if (!zh->lock_normal || !zh->lock_shadow)
+    {
+        zh->errCode = 2;
+       return -1;
+    }
     return 0;
 }
 
 void zebra_search_rpn (ZebraHandle zh, ODR decode, ODR stream,
                       Z_RPNQuery *query, const char *setname, int *hits)
 {
+    ASSERTZH;
+    zh->errCode=0;
     zh->hits = 0;
     *hits = 0;
 
@@ -703,6 +764,8 @@ void zebra_records_retrieve (ZebraHandle zh, ODR stream,
 {
     ZebraPosSet poset;
     int i, *pos_array;
+    ASSERTZH;
+    zh->errCode=0;
 
     if (!zh->res)
     {
@@ -769,6 +832,8 @@ void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
                 int *position, int *num_entries, ZebraScanEntry **entries,
                 int *is_partial)
 {
+    ASSERTZH;
+    zh->errCode=0;
     if (zebra_begin_read (zh))
     {
        *entries = 0;
@@ -786,6 +851,8 @@ void zebra_sort (ZebraHandle zh, ODR stream,
                 const char *output_setname, Z_SortKeySpecList *sort_sequence,
                 int *sort_status)
 {
+    ASSERTZH;
+    zh->errCode=0;
     if (zebra_begin_read (zh))
        return;
     resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
@@ -798,6 +865,8 @@ int zebra_deleleResultSet(ZebraHandle zh, int function,
                          int *statuses)
 {
     int i, status;
+    ASSERTZH;
+    zh->errCode=0;
     if (zebra_begin_read(zh))
        return Z_DeleteStatus_systemProblemAtTarget;
     switch (function)
@@ -819,22 +888,31 @@ int zebra_deleleResultSet(ZebraHandle zh, int function,
 
 int zebra_errCode (ZebraHandle zh)
 {
-    return zh->errCode;
+    if (zh)
+        return zh->errCode;
+    return 0; 
 }
 
 const char *zebra_errString (ZebraHandle zh)
 {
-    return diagbib1_str (zh->errCode);
+    if (zh)
+        return diagbib1_str (zh->errCode);
+    return "";
 }
 
 char *zebra_errAdd (ZebraHandle zh)
 {
-    return zh->errString;
+    if (zh)
+        return zh->errString;
+    return "";
 }
 
 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
 {
-    ZebraService zs = zh->service;
+    ZebraService zs;
+    ASSERTZH;
+    zh->errCode=0;
+    zs= zh->service;
     if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
     {
         logf(LOG_APP,"AUTHOK:%s", user?user:"ANONYMOUS");
@@ -845,8 +923,13 @@ int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
     return 1;
 }
 
-void zebra_admin_import_begin (ZebraHandle zh, const char *database)
+void zebra_admin_import_begin (ZebraHandle zh, const char *database,
+                               const char *record_type)
 {
+    ASSERTZH;
+    zh->errCode=0;
+    if (zebra_select_database(zh, database))
+        return;
     zebra_begin_trans (zh);
     xfree (zh->admin_databaseName);
     zh->admin_databaseName = xstrdup(database);
@@ -854,6 +937,8 @@ void zebra_admin_import_begin (ZebraHandle zh, const char *database)
 
 void zebra_admin_import_end (ZebraHandle zh)
 {
+    ASSERTZH;
+    zh->errCode=0;
     zebra_end_trans (zh);
 }
 
@@ -861,6 +946,8 @@ void zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
 {
     int sysno;
     int i;
+    ASSERTZH;
+    zh->errCode=0;
     for (i = 0; i<segment->num_segmentRecords; i++)
     {
        Z_NamePlusRecord *npr = segment->segmentRecords[i];
@@ -893,10 +980,58 @@ void zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
     }
 }
 
+int zebra_admin_exchange_record (ZebraHandle zh,
+                                 const char *database,
+                                 const char *rec_buf,
+                                 size_t rec_len,
+                                 const char *recid_buf, size_t recid_len,
+                                 int action)
+{
+    int sysno = 0;
+    char *rinfo = 0;
+    char recid_z[256];
+    ASSERTZH;
+    zh->errCode=0;
+
+    if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
+        return -1;
+    memcpy (recid_z, recid_buf, recid_len);
+    recid_z[recid_len] = 0;
+
+    rinfo = dict_lookup (zh->reg->matchDict, recid_z);
+    if (rinfo)
+    {
+        if (action == 1)  /* fail if insert */
+            return -1;
+        memcpy (&sysno, rinfo+1, sizeof(sysno));
+    }
+    else
+    {
+        if (action == 2 || action == 3) /* fail if delete or update */
+            return -1;
+    }
+    extract_rec_in_mem (zh, "grs.sgml", rec_buf, rec_len, database,
+                        action == 3 ? 1 : 0 /* delete flag */,
+                        0, &sysno, 1, 1, 0);
+    if (action == 1)
+    {
+        dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
+    }
+    else if (action == 3)
+    {
+        dict_delete (zh->reg->matchDict, recid_z);
+    }
+    return 0;
+}
+
 void zebra_admin_create (ZebraHandle zh, const char *database)
 {
     ZebraService zs;
+    ASSERTZH;
+    zh->errCode=0;
 
+    if (zebra_select_database (zh, database))
+        return;
     zebra_begin_trans (zh);
 
     zs = zh->service;
@@ -905,7 +1040,7 @@ void zebra_admin_create (ZebraHandle zh, const char *database)
                                   /* explainDatabase */))
     {
        zh->errCode = 224;
-       zh->errString = "Database already exist";
+       zh->errString = "database already exist";
     }
     zebra_end_trans (zh);
 }
@@ -915,6 +1050,8 @@ int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
                       char *output_str, int output_len)
 {
     WRBUF wrbuf;
+    ASSERTZH;
+    zh->errCode=0;
     if (!zh->reg->zebra_maps)
        return -1;
     wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
@@ -936,12 +1073,14 @@ void zebra_set_state (ZebraHandle zh, int val, int seqno)
     char *fname;
     long p = getpid();
     FILE *f;
+    ASSERTZH;
+    zh->errCode=0;
 
     sprintf (state_fname, "state.%s.LCK", zh->reg_name);
     fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
     f = fopen (fname, "w");
 
-    yaz_log (LOG_LOG, "%c %d %ld", val, seqno, p);
+    yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
     fprintf (f, "%c %d %ld\n", val, seqno, p);
     fclose (f);
     xfree (fname);
@@ -953,6 +1092,8 @@ void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
     char *fname;
     FILE *f;
 
+    ASSERTZH;
+    zh->errCode=0;
     sprintf (state_fname, "state.%s.LCK", zh->reg_name);
     fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
     f = fopen (fname, "r");
@@ -972,6 +1113,7 @@ int zebra_begin_read (ZebraHandle zh)
     int dirty = 0;
     char val;
     int seqno;
+    ASSERTZH;
 
     assert (zh->res);
 
@@ -982,6 +1124,7 @@ int zebra_begin_read (ZebraHandle zh)
         zebra_flush_reg (zh);
         return 0;
     }
+    zh->errCode=0;
 #if HAVE_SYS_TIMES_H
     times (&zh->tms1);
 #endif
@@ -991,6 +1134,12 @@ int zebra_begin_read (ZebraHandle zh)
         zh->errCode = 109;
         return -1;
     }
+    if (!zh->lock_normal || !zh->lock_shadow)
+    {
+        (zh->trans_no)--;
+        zh->errCode = 2;
+       return -1;
+    }
     zebra_get_state (zh, &val, &seqno);
     if (val == 'd')
         val = 'o';
@@ -1035,11 +1184,11 @@ int zebra_begin_read (ZebraHandle zh)
 
 void zebra_end_read (ZebraHandle zh)
 {
+    ASSERTZH;
     (zh->trans_no)--;
 
     if (zh->trans_no != 0)
         return;
-
 #if HAVE_SYS_TIMES_H
     times (&zh->tms2);
     logf (LOG_LOG, "user/system: %ld/%ld",
@@ -1058,6 +1207,7 @@ void zebra_begin_trans (ZebraHandle zh)
     int seqno = 0;
     char val = '?';
     const char *rval = 0;
+    ASSERTZHRES;
 
     assert (zh->res);
 
@@ -1066,6 +1216,7 @@ void zebra_begin_trans (ZebraHandle zh)
     {
         return;
     }
+    zh->errCode=0;
     
     yaz_log (LOG_LOG, "zebra_begin_trans");
 
@@ -1136,16 +1287,29 @@ void zebra_begin_trans (ZebraHandle zh)
     zh->reg->seqno = seqno;
 }
 
-void zebra_end_trans (ZebraHandle zh)
+void zebra_end_trans (ZebraHandle zh) {
+  ZebraTransactionStatus dummy;
+  zebra_end_transaction(zh, &dummy);
+}
+
+void zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
 {
     char val;
     int seqno;
     const char *rval;
 
+    ASSERTZH;
+
+    status->processed = 0;
+    status->inserted  = 0;
+    status->updated   = 0;
+    status->deleted   = 0;
+    status->utime     = 0;
+    status->stime     = 0;
+
     zh->trans_no--;
     if (zh->trans_no != 0)
         return;
-
     yaz_log (LOG_LOG, "zebra_end_trans");
     rval = res_get (zh->res, "shadow");
 
@@ -1156,11 +1320,15 @@ void zebra_end_trans (ZebraHandle 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);
 
+    status->processed = zh->records_processed;
+    status->inserted = zh->records_inserted;
+    status->updated = zh->records_updated;
+    status->deleted = zh->records_deleted;
+
     zebra_get_state (zh, &val, &seqno);
     if (val != 'd')
     {
@@ -1182,25 +1350,33 @@ void zebra_end_trans (ZebraHandle zh)
                     (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
                     (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
 
+    status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
+    status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
 #endif
+
+    return;
 }
 
 void zebra_repository_update (ZebraHandle zh)
 {
-    zebra_begin_trans (zh);
+    ASSERTZH;
+    zh->errCode=0;
     logf (LOG_LOG, "updating %s", zh->rGroup.path);
     repositoryUpdate (zh);    
-    zebra_end_trans (zh);
 }
 
 void zebra_repository_delete (ZebraHandle zh)
 {
+    ASSERTZH;
+    zh->errCode=0;
     logf (LOG_LOG, "deleting %s", zh->rGroup.path);
     repositoryDelete (zh);
 }
 
 void zebra_repository_show (ZebraHandle zh)
 {
+    ASSERTZH;
+    zh->errCode=0;
     repositoryShow (zh);
 }
 
@@ -1210,6 +1386,8 @@ int zebra_commit (ZebraHandle zh)
     char val;
     const char *rval;
     BFiles bfs;
+    ASSERTZH;
+    zh->errCode=0;
 
     if (!zh->res)
     {
@@ -1262,6 +1440,8 @@ int zebra_init (ZebraHandle zh)
 {
     const char *rval;
     BFiles bfs = 0;
+    ASSERTZH;
+    zh->errCode=0;
 
     if (!zh->res)
     {
@@ -1284,6 +1464,8 @@ int zebra_init (ZebraHandle zh)
 int zebra_compact (ZebraHandle zh)
 {
     BFiles bfs;
+    ASSERTZH;
+    zh->errCode=0;
     if (!zh->res)
     {
         zh->errCode = 109;
@@ -1298,7 +1480,12 @@ int zebra_compact (ZebraHandle zh)
 int zebra_record_insert (ZebraHandle zh, const char *buf, int len)
 {
     int sysno = 0;
+    int olderr;
+    ASSERTZH;
+    zh->errCode=0;
     zebra_begin_trans (zh);
+    if (zh->errCode)
+      return 0; /* bad sysno */
     extract_rec_in_mem (zh, "grs.sgml",
                         buf, len,
                         "Default",  /* database */
@@ -1308,29 +1495,76 @@ int zebra_record_insert (ZebraHandle zh, const char *buf, int len)
                         1 /* store_keys */,
                         1 /* store_data */,
                         0 /* match criteria */);
+    olderr=zh->errCode;
     zebra_end_trans (zh);
+    if (olderr)
+      zh->errCode=olderr; 
     return sysno;
 }
 
 void zebra_set_group (ZebraHandle zh, struct recordGroup *rg)
 {
+    ASSERTZH;
+    zh->errCode=0;
     memcpy (&zh->rGroup, rg, sizeof(*rg));
 }
 
 void zebra_result (ZebraHandle zh, int *code, char **addinfo)
 {
+    ASSERTZH;
     *code = zh->errCode;
     *addinfo = zh->errString;
 }
 
 void zebra_shadow_enable (ZebraHandle zh, int value)
 {
+    ASSERTZH;
+    zh->errCode=0;
     zh->shadow_enable = value;
 }
 
 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
 {
+    ASSERTZH;
+    zh->errCode=0;
     xfree (zh->record_encoding);
+
+    /*
+     * Fixme!
+     */
+
+    if (zh->iconv_to_utf8 != 0)
+        yaz_iconv_close(zh->iconv_to_utf8);
+    if (zh->iconv_from_utf8 != 0)
+        yaz_iconv_close(zh->iconv_from_utf8);
+    
     zh->record_encoding = xstrdup (encoding);
+    
+    logf(LOG_DEBUG, "Reset record encoding: %s", encoding);
+    
+    zh->iconv_to_utf8 =
+        yaz_iconv_open ("UTF-8", encoding);
+    if (zh->iconv_to_utf8 == 0)
+        yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
+    zh->iconv_from_utf8 =
+        yaz_iconv_open (encoding, "UTF-8");
+    if (zh->iconv_to_utf8 == 0)
+        yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
+
     return 0;
 }
+
+void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
+{
+    ASSERTZH;
+    zh->errCode=0;
+    res_put(zh->res, name, value);
+}
+
+const char *zebra_get_resource(ZebraHandle zh,
+                               const char *name, const char *defaultvalue)
+{
+    ASSERTZH;
+    zh->errCode=0;
+    return res_get_def( zh->res, name, (char *)defaultvalue);
+}