Fix problem hit count caching in async mode
[yaz-moved-to-github.git] / src / zoom-c.c
index f2363b6..b1874ea 100644 (file)
@@ -96,8 +96,6 @@ int ZOOM_uri_to_code(const char *uri)
     return code;
 }
 
-
-
 void ZOOM_set_error(ZOOM_connection c, int error, const char *addinfo)
 {
     ZOOM_set_dset_error(c, error, "ZOOM", addinfo, 0);
@@ -136,9 +134,6 @@ void ZOOM_connection_show_task(ZOOM_task task)
     case ZOOM_TASK_SEARCH:
         yaz_log(YLOG_LOG, "search p=%p", task);
         break;
-    case ZOOM_TASK_RETRIEVE:
-        yaz_log(YLOG_LOG, "retrieve p=%p", task);
-        break;
     case ZOOM_TASK_CONNECT:
         yaz_log(YLOG_LOG, "connect p=%p", task);
         break;
@@ -201,12 +196,6 @@ void ZOOM_connection_remove_task(ZOOM_connection c)
             xfree(task->u.search.elementSetName);
             xfree(task->u.search.schema);
             break;
-        case ZOOM_TASK_RETRIEVE:
-            resultset_destroy(task->u.retrieve.resultset);
-            xfree(task->u.retrieve.syntax);
-            xfree(task->u.retrieve.elementSetName);
-            xfree(task->u.retrieve.schema);
-            break;
         case ZOOM_TASK_CONNECT:
             break;
         case ZOOM_TASK_SCAN:
@@ -374,6 +363,7 @@ ZOOM_API(void)
                             const char *host, int portnum)
 {
     const char *val;
+    const char *http_lead;
 
     initlog();
 
@@ -440,17 +430,25 @@ ZOOM_API(void)
     else
         c->lang = 0;
 
+    val = ZOOM_options_get(c->options, "sru");
+    if (val && *val && !strstr(host, "://"))
+        http_lead = "http://";
+    else
+        http_lead = "";
+    c->sru_mode = get_sru_mode_from_string(val);
+
     if (host)
     {
+        char hostn[128];
         xfree(c->host_port);
         if (portnum)
         {
-            char hostn[128];
             sprintf(hostn, "%.80s:%d", host, portnum);
-            c->host_port = xstrdup(hostn);
+            host = hostn;
         }
-        else
-            c->host_port = xstrdup(host);
+        c->host_port = xmalloc(strlen(host) + strlen(http_lead) + 1);
+        strcpy(c->host_port, http_lead);
+        strcat(c->host_port, host);
     }
 
     {
@@ -486,9 +484,6 @@ ZOOM_API(void)
         }
     }
 
-    val = ZOOM_options_get(c->options, "sru");
-    c->sru_mode = get_sru_mode_from_string(val);
-
     xfree(c->sru_version);
     val = ZOOM_options_get(c->options, "sru_version");
     c->sru_version = xstrdup(val ? val : "1.2");
@@ -703,7 +698,7 @@ ZOOM_resultset ZOOM_resultset_create(void)
     yaz_log(log_details0, "%p ZOOM_resultset_create", r);
     r->refcount = 1;
     r->size = 0;
-    r->odr = odr_createmem(ODR_ENCODE);
+    r->odr = odr_createmem(ODR_DECODE);
     r->piggyback = 1;
     r->setname = 0;
     r->step = 0;
@@ -727,6 +722,7 @@ ZOOM_resultset ZOOM_resultset_create(void)
 #endif
     resultset_use(1);
     r->mc_key = 0;
+    r->live_set = 0;
     return r;
 }
 
@@ -754,6 +750,7 @@ ZOOM_API(ZOOM_resultset)
     yaz_log(c->log_api, "%p ZOOM_connection_search set %p query %p", c, r, q);
     r->r_sort_spec = ZOOM_query_get_sortspec(q);
     r->query = q;
+    ZOOM_query_addref(q);
 
     r->options = ZOOM_options_create_with_parent(c->options);
 
@@ -779,26 +776,20 @@ ZOOM_API(ZOOM_resultset)
 
 #if HAVE_LIBMEMCACHED_MEMCACHED_H
     r->mc_key = wrbuf_alloc();
+    wrbuf_puts(r->mc_key, "0;");
     wrbuf_puts(r->mc_key, c->host_port);
     wrbuf_puts(r->mc_key, ";");
-    wrbuf_puts(r->mc_key, ZOOM_query_get_query_string(q));
-    if (c->mc_st)
-    {
-        size_t v_len;
-        uint32_t flags;
-        memcached_return_t rc;
-        char *v = memcached_get(c->mc_st, wrbuf_buf(r->mc_key),
-                                wrbuf_len(r->mc_key), &v_len, &flags, &rc);
-        if (v)
-        {
-            yaz_log(YLOG_LOG, "For key %s got value %.*s",
-                    wrbuf_cstr(r->mc_key), (int) v_len, v);
-        }
-        else
-        {
-            yaz_log(YLOG_LOG, "For key %s got NO value", wrbuf_cstr(r->mc_key));
-        }
-    }
+    if (c->user)
+        wrbuf_puts(r->mc_key, c->user);
+    wrbuf_puts(r->mc_key, ";");
+    if (c->group)
+        wrbuf_puts(r->mc_key, c->group);
+    wrbuf_puts(r->mc_key, ";");
+    if (c->password)
+        wrbuf_sha1_puts(r->mc_key, c->password, 1);
+    wrbuf_puts(r->mc_key, ";");
+    wrbuf_sha1_puts(r->mc_key, ZOOM_query_get_query_string(q), 1);
+    wrbuf_puts(r->mc_key, ";");
 #endif
 
     if (c->host_port && c->proto == PROTO_HTTP)
@@ -819,7 +810,6 @@ ZOOM_API(ZOOM_resultset)
     task->u.search.resultset = r;
     task->u.search.start = start;
     task->u.search.count = count;
-    task->u.search.recv_search_fired = 0;
 
     syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
     task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
@@ -831,8 +821,6 @@ ZOOM_API(ZOOM_resultset)
 
     ZOOM_resultset_addref(r);
 
-    ZOOM_query_addref(q);
-
     if (!c->async)
     {
         while (ZOOM_event(1, &c))
@@ -986,19 +974,19 @@ static void ZOOM_resultset_retrieve(ZOOM_resultset r,
             c->reconnect_ok = 1;
         }
     }
-    task = ZOOM_connection_add_task(c, ZOOM_TASK_RETRIEVE);
-    task->u.retrieve.resultset = r;
-    task->u.retrieve.start = start;
-    task->u.retrieve.count = count;
+    task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
+    task->u.search.resultset = r;
+    task->u.search.start = start;
+    task->u.search.count = count;
 
     syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
-    task->u.retrieve.syntax = syntax ? xstrdup(syntax) : 0;
+    task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
     elementSetName = ZOOM_options_get(r->options, "elementSetName");
-    task->u.retrieve.elementSetName = elementSetName
+    task->u.search.elementSetName = elementSetName
         ? xstrdup(elementSetName) : 0;
 
     cp = ZOOM_options_get(r->options, "schema");
-    task->u.retrieve.schema = cp ? xstrdup(cp) : 0;
+    task->u.search.schema = cp ? xstrdup(cp) : 0;
 
     ZOOM_resultset_addref(r);
 
@@ -1029,30 +1017,30 @@ ZOOM_API(void)
 }
 
 ZOOM_API(size_t)
-    ZOOM_resultset_facets_size(ZOOM_resultset r) {
+    ZOOM_resultset_facets_size(ZOOM_resultset r)
+{
     return r->num_facets;
 }
 
 ZOOM_API(ZOOM_facet_field)
-    ZOOM_resultset_get_facet_field(ZOOM_resultset r, const char *name) {
+    ZOOM_resultset_get_facet_field(ZOOM_resultset r, const char *name)
+{
     int num = r->num_facets;
     ZOOM_facet_field *facets = r->facets;
-    int index;
-    for (index = 0; index < num; index++) {
-        if (!strcmp(facets[index]->facet_name, name)) {
-            return facets[index];
-        }
-    }
+    int i;
+    for (i = 0; i < num; i++)
+        if (!strcmp(facets[i]->facet_name, name))
+            return facets[i];
     return 0;
 }
 
 ZOOM_API(ZOOM_facet_field)
-    ZOOM_resultset_get_facet_field_by_index(ZOOM_resultset r, int index) {
+    ZOOM_resultset_get_facet_field_by_index(ZOOM_resultset r, int idx)
+{
     int num = r->num_facets;
     ZOOM_facet_field *facets = r->facets;
-    if (index >= 0 && index < num) {
-        return facets[index];
-    }
+    if (idx >= 0 && idx < num)
+        return facets[idx];
     return 0;
 }
 
@@ -1081,7 +1069,8 @@ ZOOM_API(size_t)
 }
 
 ZOOM_API(const char*)
-    ZOOM_facet_field_get_term(ZOOM_facet_field field, size_t idx, int *freq) {
+    ZOOM_facet_field_get_term(ZOOM_facet_field field, size_t idx, int *freq)
+{
     *freq = field->facet_terms[idx].frequency;
     return field->facet_terms[idx].term;
 }
@@ -1494,13 +1483,7 @@ ZOOM_API(int)
             if (c->proto == PROTO_HTTP)
                 ret = ZOOM_connection_srw_send_search(c);
             else
-                ret = ZOOM_connection_Z3950_send_search(c);
-            break;
-        case ZOOM_TASK_RETRIEVE:
-            if (c->proto == PROTO_HTTP)
-                ret = ZOOM_connection_srw_send_search(c);
-            else
-                ret = send_Z3950_present(c);
+                ret = ZOOM_connection_Z3950_search(c);
             break;
         case ZOOM_TASK_CONNECT:
             ret = do_connect(c);