First work on threaded version.
[idzebra-moved-to-github.git] / index / zebraapi.c
index 3400ea9..1bb461b 100644 (file)
@@ -4,7 +4,19 @@
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: zebraapi.c,v $
- * Revision 1.24  1999-10-14 14:33:50  adam
+ * Revision 1.28  2000-03-15 15:00:30  adam
+ * First work on threaded version.
+ *
+ * Revision 1.27  2000/02/24 12:31:17  adam
+ * Added zebra_string_norm.
+ *
+ * Revision 1.26  1999/11/30 13:48:03  adam
+ * Improved installation. Updated for inclusion of YAZ header files.
+ *
+ * Revision 1.25  1999/11/04 15:00:45  adam
+ * Implemented delete result set(s).
+ *
+ * Revision 1.24  1999/10/14 14:33:50  adam
  * Added truncation 5=106.
  *
  * Revision 1.23  1999/09/07 11:36:32  adam
 #include <unistd.h>
 #endif
 
-#include <diagbib1.h>
+#include <yaz/diagbib1.h>
 #include "zserver.h"
 
-static void zebra_chdir (ZebraHandle zh)
+static void zebra_chdir (ZebraService zh)
 {
     const char *dir = res_get (zh->res, "chdir");
     if (!dir)
@@ -112,15 +124,15 @@ static void zebra_chdir (ZebraHandle zh)
 #endif
 }
 
-static void zebra_register_unlock (ZebraHandle zh);
+static void zebra_register_unlock (ZebraService zh);
 
-static int zebra_register_lock (ZebraHandle zh)
+static int zebra_register_lock (ZebraService zh)
 {
     time_t lastChange;
     int state;
+    int errCode = 0;
 
-    zh->errCode = 0;
-    zh->errString = 0;
+    errCode = 0;
 
     zebra_chdir (zh);
 
@@ -158,20 +170,24 @@ static int zebra_register_lock (ZebraHandle zh)
            dict_close (zh->dict);
        if (zh->sortIdx)
            sortIdx_close (zh->sortIdx);
+        if (zh->isams)
+            isams_close (zh->isams);
+#if ZMBOL
         if (zh->isam)
             is_close (zh->isam);
         if (zh->isamc)
             isc_close (zh->isamc);
-        if (zh->isams)
-            isams_close (zh->isams);
+#endif
         rec_close (&zh->records);
     }
     bf_cache (zh->bfs, state ? res_get (zh->res, "shadow") : NULL);
     zh->registerState = state;
 
+    zh->isams = NULL;
+#if ZMBOL
     zh->isam = NULL;
     zh->isamc = NULL;
-    zh->isams = NULL;
+#endif
     zh->dict = NULL;
     zh->sortIdx = NULL;
     zh->zei = NULL;
@@ -179,66 +195,69 @@ static int zebra_register_lock (ZebraHandle zh)
     if (!(zh->records = rec_open (zh->bfs, 0, 0)))
     {
        logf (LOG_WARN, "rec_open");
-       zh->errCode = 2;
+       errCode = 2;
     }
     else
     {
        if (!(zh->dict = dict_open (zh->bfs, FNAME_DICT, 40, 0, 0)))
        {
            logf (LOG_WARN, "dict_open");
-           zh->errCode = 2;
+           errCode = 2;
        }
        if (!(zh->sortIdx = sortIdx_open (zh->bfs, 0)))
        {
            logf (LOG_WARN, "sortIdx_open");
-           zh->errCode = 2;
+           errCode = 2;
        }
-       if (res_get_match (zh->res, "isam", "i", NULL))
-       {
-           if (!(zh->isam = is_open (zh->bfs, FNAME_ISAM, key_compare, 0,
-                                     sizeof (struct it_key), zh->res)))
-           {
-               logf (LOG_WARN, "is_open");
-               zh->errCode = 2;
-           }
-       }
-       else if (res_get_match (zh->res, "isam", "s", NULL))
+       if (res_get_match (zh->res, "isam", "s", ISAM_DEFAULT))
        {
            struct ISAMS_M_s isams_m;
            if (!(zh->isams = isams_open (zh->bfs, FNAME_ISAMS, 0,
                                          key_isams_m(zh->res, &isams_m))))
            {
                logf (LOG_WARN, "isams_open");
-               zh->errCode = 2;
+               errCode = 2;
            }
        }
-       else
+#if ZMBOL
+       else if (res_get_match (zh->res, "isam", "i", ISAM_DEFAULT))
+       {
+           if (!(zh->isam = is_open (zh->bfs, FNAME_ISAM, key_compare, 0,
+                                     sizeof (struct it_key), zh->res)))
+           {
+               logf (LOG_WARN, "is_open");
+               errCode = 2;
+           }
+       }
+       else if (res_get_match (zh->res, "isam", "c", ISAM_DEFAULT))
        {
            struct ISAMC_M_s isamc_m;
            if (!(zh->isamc = isc_open (zh->bfs, FNAME_ISAMC,
                                        0, key_isamc_m(zh->res, &isamc_m))))
            {
                logf (LOG_WARN, "isc_open");
-               zh->errCode = 2;
+               errCode = 2;
            }
        }
-       zh->zei = zebraExplain_open (zh->records, zh->dh, zh->res, 0, 0, 0);
+#endif
+       zh->zei = zebraExplain_open (zh->records, zh->dh,
+                                    zh->res, 0, 0, 0);
        if (!zh->zei)
        {
            logf (LOG_WARN, "Cannot obtain EXPLAIN information");
-           zh->errCode = 2;
+           errCode = 2;
        }
     }
-    if (zh->errCode)
+    if (errCode)
     {
        zebra_register_unlock (zh);
        zh->registerState = -1;
        return -1;
     }
-    return 0;
+    return errCode;
 }
 
-static void zebra_register_unlock (ZebraHandle zh)
+static void zebra_register_unlock (ZebraService zh)
 {
     static int waitSec = -1;
 
@@ -265,11 +284,24 @@ static void zebra_register_unlock (ZebraHandle zh)
         zebra_server_unlock (zh, zh->registerState);
 }
 
-ZebraHandle zebra_open (const char *configName)
+ZebraHandle zebra_open (ZebraService service)
 {
     ZebraHandle zh;
 
     zh = (ZebraHandle) xmalloc (sizeof(*zh));
+
+    zh->service = service;
+    zh->sets = 0;
+
+    return zh;
+}
+
+ZebraService zebra_start (const char *configName)
+{
+    ZebraService zh = xmalloc (sizeof(*zh));
+    zh->configName = xstrdup(configName);
+    zh->sessions = 0;
+    yaz_log (LOG_LOG, "zebra_start %s", configName);
     if (!(zh->res = res_open (configName)))
     {
        logf (LOG_WARN, "Failed to read resources `%s'", configName);
@@ -294,7 +326,6 @@ ZebraHandle zebra_open (const char *configName)
     }
     bf_lockDir (zh->bfs, res_get (zh->res, "lockDir"));
     data1_set_tabpath (zh->dh, res_get(zh->res, "profilePath"));
-    zh->sets = NULL;
     zh->registerState = -1;  /* trigger open of registers! */
     zh->registerChange = 0;
     zh->recTypes = recTypes_init (zh->dh);
@@ -303,8 +334,6 @@ ZebraHandle zebra_open (const char *configName)
     zh->records = NULL;
     zh->zebra_maps = zebra_maps_open (zh->res);
     zh->rank_classes = NULL;
-    zh->errCode = 0;
-    zh->errString = 0;
     
     zebraRankInstall (zh, rank1_class);
 
@@ -321,23 +350,28 @@ ZebraHandle zebra_open (const char *configName)
     return zh;
 }
 
-void zebra_close (ZebraHandle zh)
+void zebra_stop(ZebraService zh)
 {
     if (!zh)
-       return;
+       return ;
+    yaz_log (LOG_LOG, "zebra_stop");
+
+    /* wait for sessions to die ...... */
+
     zebra_chdir (zh);
     if (zh->records)
     {
-        resultSetDestroy (zh);
         zebraExplain_close (zh->zei, 0, 0);
         dict_close (zh->dict);
        sortIdx_close (zh->sortIdx);
+       if (zh->isams)
+           isams_close (zh->isams);
+#if ZMBOL
         if (zh->isam)
             is_close (zh->isam);
         if (zh->isamc)
             isc_close (zh->isamc);
-       if (zh->isams)
-           isams_close (zh->isams);
+#endif
         rec_close (&zh->records);
         zebra_register_unlock (zh);
     }
@@ -351,6 +385,17 @@ void zebra_close (ZebraHandle zh)
     if (zh->passwd_db)
        passwd_db_close (zh->passwd_db);
     res_close (zh->res);
+
+    xfree (zh->configName);
+    xfree (zh);
+}
+
+void zebra_close (ZebraHandle zh)
+{
+    if (!zh)
+       return ;
+    resultSetDestroy (zh, -1, 0, 0);
+
     xfree (zh);
 }
 
@@ -408,7 +453,7 @@ void map_basenames (ZebraHandle zh, ODR stream,
        odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
     info.mem = stream->mem;
 
-    res_trav (zh->res, "mapdb", &info, map_basenames_func);
+    res_trav (zh->service->res, "mapdb", &info, map_basenames_func);
     
     for (i = 0; i<p->num_bases; i++)
        if (p->basenames[i] && p->new_num_bases < p->new_num_max)
@@ -427,12 +472,12 @@ void zebra_search_rpn (ZebraHandle zh, ODR stream, ODR decode,
                       const char *setname)
 {
     zh->hits = 0;
-    if (zebra_register_lock (zh))
+    if (zebra_register_lock (zh->service))
        return;
     map_basenames (zh, stream, &num_bases, &basenames);
     resultSetAddRPN (zh, stream, decode, query, num_bases, basenames, setname);
 
-    zebra_register_unlock (zh);
+    zebra_register_unlock (zh->service);
 }
 
 void zebra_records_retrieve (ZebraHandle zh, ODR stream,
@@ -443,7 +488,7 @@ void zebra_records_retrieve (ZebraHandle zh, ODR stream,
     ZebraPosSet poset;
     int i, *pos_array;
 
-    if (zebra_register_lock (zh))
+    if (zebra_register_lock (zh->service))
        return;
     pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
     for (i = 0; i<num_recs; i++)
@@ -481,7 +526,7 @@ void zebra_records_retrieve (ZebraHandle zh, ODR stream,
        }
        zebraPosSetDestroy (zh, poset, num_recs);
     }
-    zebra_register_unlock (zh);
+    zebra_register_unlock (zh->service);
     xfree (pos_array);
 }
 
@@ -491,7 +536,7 @@ void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
                 int *position, int *num_entries, ZebraScanEntry **entries,
                 int *is_partial)
 {
-    if (zebra_register_lock (zh))
+    if (zebra_register_lock (zh->service))
     {
        *entries = 0;
        *num_entries = 0;
@@ -501,7 +546,7 @@ void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
     rpn_scan (zh, stream, zapt, attributeset,
              num_bases, basenames, position,
              num_entries, entries, is_partial);
-    zebra_register_unlock (zh);
+    zebra_register_unlock (zh->service);
 }
 
 void zebra_sort (ZebraHandle zh, ODR stream,
@@ -509,11 +554,35 @@ void zebra_sort (ZebraHandle zh, ODR stream,
                 const char *output_setname, Z_SortKeySpecList *sort_sequence,
                 int *sort_status)
 {
-    if (zebra_register_lock (zh))
+    if (zebra_register_lock (zh->service))
        return;
     resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
                   output_setname, sort_sequence, sort_status);
-    zebra_register_unlock (zh);
+    zebra_register_unlock (zh->service);
+}
+
+int zebra_deleleResultSet(ZebraHandle zh, int function,
+                         int num_setnames, char **setnames,
+                         int *statuses)
+{
+    int i, status;
+    if (zebra_register_lock (zh->service))
+       return Z_DeleteStatus_systemProblemAtTarget;
+    switch (function)
+    {
+    case Z_DeleteRequest_list:
+       resultSetDestroy (zh, num_setnames, setnames, statuses);
+       break;
+    case Z_DeleteRequest_all:
+       resultSetDestroy (zh, -1, 0, statuses);
+       break;
+    }
+    zebra_register_unlock (zh->service);
+    status = Z_DeleteStatus_success;
+    for (i = 0; i<num_setnames; i++)
+       if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
+           status = statuses[i];
+    return status;
 }
 
 int zebra_errCode (ZebraHandle zh)
@@ -536,7 +605,7 @@ int zebra_hits (ZebraHandle zh)
     return zh->hits;
 }
 
-int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
+int zebra_auth (ZebraService zh, const char *user, const char *pass)
 {
     if (!zh->passwd_db || !passwd_db_auth (zh->passwd_db, user, pass))
        return 0;
@@ -558,7 +627,26 @@ void zebra_setGroup (ZebraHandle zh, const char *group)
 
 }
 
-void zebra_admin (ZebraHandle zh, const char *command)
+void zebra_admin_create (ZebraHandle zh, const char *database)
 {
+    
+}
 
+int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
+                      const char *input_str, int input_len,
+                      char *output_str, int output_len)
+{
+    WRBUF wrbuf;
+    if (!zh->service->zebra_maps)
+       return -1;
+    wrbuf = zebra_replace(zh->service->zebra_maps, reg_id, "",
+                         input_str, input_len);
+    if (!wrbuf)
+       return -2;
+    if (wrbuf_len(wrbuf) >= output_len)
+       return -3;
+    if (wrbuf_len(wrbuf))
+       memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
+    output_str[wrbuf_len(wrbuf)] = '\0';
+    return wrbuf_len(wrbuf);
 }