Start work on record caching - does not work!
[yaz-moved-to-github.git] / src / zoom-c.c
index 7ac0304..41d6e1c 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2013 Index Data
+ * Copyright (C) Index Data
  * See the file LICENSE for details.
  */
 /**
@@ -136,9 +136,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;
@@ -199,11 +196,7 @@ void ZOOM_connection_remove_task(ZOOM_connection c)
             resultset_destroy(task->u.search.resultset);
             xfree(task->u.search.syntax);
             xfree(task->u.search.elementSetName);
-            break;
-        case ZOOM_TASK_RETRIEVE:
-            resultset_destroy(task->u.retrieve.resultset);
-            xfree(task->u.retrieve.syntax);
-            xfree(task->u.retrieve.elementSetName);
+            xfree(task->u.search.schema);
             break;
         case ZOOM_TASK_CONNECT:
             break;
@@ -285,6 +278,7 @@ ZOOM_API(ZOOM_connection)
     c->user = 0;
     c->group = 0;
     c->password = 0;
+    c->url_authentication = 0;
 
     c->maximum_record_size = 0;
     c->preferred_message_size = 0;
@@ -303,7 +297,12 @@ ZOOM_API(ZOOM_connection)
 
     c->sru_version = 0;
     c->no_redirects = 0;
+    c->cookies = 0;
     c->saveAPDU_wrbuf = 0;
+
+#if HAVE_LIBMEMCACHED_MEMCACHED_H
+    c->mc_st = 0;
+#endif
     return c;
 }
 
@@ -366,6 +365,7 @@ ZOOM_API(void)
                             const char *host, int portnum)
 {
     const char *val;
+    const char *http_lead;
 
     initlog();
 
@@ -432,17 +432,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);
     }
 
     {
@@ -478,9 +486,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");
@@ -523,10 +528,15 @@ ZOOM_API(void)
     val = ZOOM_options_get(c->options, "password");
     if (!val)
         val = ZOOM_options_get(c->options, "pass");
-
     if (val && *val)
         c->password = xstrdup(val);
 
+    val = ZOOM_options_get(c->options, "authenticationMode");
+    if (val && !strcmp(val, "url"))
+        c->url_authentication = 1;
+    else
+        c->url_authentication = 0;
+
     c->maximum_record_size =
         ZOOM_options_get_int(c->options, "maximumRecordSize", 64*1024*1024);
     c->preferred_message_size =
@@ -534,6 +544,32 @@ ZOOM_API(void)
 
     c->async = ZOOM_options_get_bool(c->options, "async", 0);
 
+    yaz_cookies_destroy(c->cookies);
+    c->cookies = yaz_cookies_create();
+
+#if HAVE_LIBMEMCACHED_MEMCACHED_H
+    if (c->mc_st)
+    {
+        memcached_free(c->mc_st);
+        c->mc_st = 0;
+    }
+#endif
+    val = ZOOM_options_get(c->options, "memcached");
+    if (val && *val)
+    {
+#if HAVE_LIBMEMCACHED_MEMCACHED_H
+        c->mc_st = memcached(val, strlen(val));
+        if (!c->mc_st)
+        {
+            ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED, val);
+            return;
+        }
+#else
+        ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED, "not enabled");
+        return;
+#endif
+    }
+
     if (c->sru_mode == zoom_sru_error)
     {
         ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, val);
@@ -553,8 +589,6 @@ ZOOM_API(void)
 
 ZOOM_API(void) ZOOM_resultset_release(ZOOM_resultset r)
 {
-#if ZOOM_RESULT_LISTS
-#else
     if (r->connection)
     {
         /* remove ourselves from the resultsets in connection */
@@ -571,36 +605,25 @@ ZOOM_API(void) ZOOM_resultset_release(ZOOM_resultset r)
         }
         r->connection = 0;
     }
-#endif
 }
 
 ZOOM_API(void)
     ZOOM_connection_destroy(ZOOM_connection c)
 {
-#if ZOOM_RESULT_LISTS
-    ZOOM_resultsets list;
-#else
     ZOOM_resultset r;
-#endif
     if (!c)
         return;
     yaz_log(c->log_api, "%p ZOOM_connection_destroy", c);
+
+#if HAVE_LIBMEMCACHED_MEMCACHED_H
+    if (c->mc_st)
+        memcached_free(c->mc_st);
+#endif
     if (c->cs)
         cs_close(c->cs);
 
-#if ZOOM_RESULT_LISTS
-    /* Remove the connection's usage of resultsets */
-    list = c->resultsets;
-    while (list) {
-        ZOOM_resultsets removed = list;
-        ZOOM_resultset_destroy(list->resultset);
-        list = list->next;
-        xfree(removed);
-    }
-#else
     for (r = c->resultsets; r; r = r->next)
         r->connection = 0;
-#endif
 
     xfree(c->buf_in);
     xfree(c->addinfo);
@@ -629,6 +652,7 @@ ZOOM_API(void)
     xfree(c->group);
     xfree(c->password);
     xfree(c->sru_version);
+    yaz_cookies_destroy(c->cookies);
     wrbuf_destroy(c->saveAPDU_wrbuf);
     xfree(c);
 }
@@ -679,7 +703,6 @@ ZOOM_resultset ZOOM_resultset_create(void)
     r->odr = odr_createmem(ODR_ENCODE);
     r->piggyback = 1;
     r->setname = 0;
-    r->schema = 0;
     r->step = 0;
     for (i = 0; i<RECORD_HASH_SIZE; i++)
         r->record_hash[i] = 0;
@@ -700,6 +723,8 @@ ZOOM_resultset ZOOM_resultset_create(void)
     }
 #endif
     resultset_use(1);
+    r->mc_key = 0;
+    r->live_set = 0;
     return r;
 }
 
@@ -720,17 +745,14 @@ ZOOM_API(ZOOM_resultset)
     ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
 {
     ZOOM_resultset r = ZOOM_resultset_create();
-    ZOOM_task task;
     const char *cp;
+    ZOOM_task task;
     int start, count;
-    const char *syntax, *elementSetName;
-#if ZOOM_RESULT_LISTS
-    ZOOM_resultsets set;
-#endif
-
+    const char *syntax, *elementSetName, *schema;
     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);
 
@@ -746,26 +768,62 @@ ZOOM_API(ZOOM_resultset)
     cp = ZOOM_options_get(r->options, "setname");
     if (cp)
         r->setname = xstrdup(cp);
-    cp = ZOOM_options_get(r->options, "schema");
-    if (cp)
-        r->schema = xstrdup(cp);
 
     r->databaseNames = ZOOM_connection_get_databases(c, c->options, &r->num_databaseNames,
                                          r->odr);
 
     r->connection = c;
-
-#if ZOOM_RESULT_LISTS
-    yaz_log(log_details, "%p ZOOM_connection_search: Adding new resultset (%p) to resultsets (%p) ", c, r, c->resultsets);
-    set = xmalloc(sizeof(*set));
-    ZOOM_resultset_addref(r);
-    set->resultset = r;
-    set->next = c->resultsets;
-    c->resultsets = set;
-#else
     r->next = c->resultsets;
     c->resultsets = r;
+
+#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, ";");
+    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, ";");
+    /* TODO: add sorting */
+    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)
+        {
+            ZOOM_Event event;
+            WRBUF w = wrbuf_alloc();
+
+            wrbuf_write(w, v, v_len);
+            free(v);
+            r->size = odr_atoi(wrbuf_cstr(w));
+
+            yaz_log(YLOG_LOG, "For key %s got value %s",
+                    wrbuf_cstr(r->mc_key), wrbuf_cstr(w));
+
+            wrbuf_destroy(w);
+            event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
+            ZOOM_connection_put_event(c, event);
+            return r;
+        }
+        else
+        {
+            yaz_log(YLOG_LOG, "For key %s got NO value", wrbuf_cstr(r->mc_key));
+        }
+    }
 #endif
+
     if (c->host_port && c->proto == PROTO_HTTP)
     {
         if (!c->cs)
@@ -789,13 +847,13 @@ ZOOM_API(ZOOM_resultset)
     syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
     task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
     elementSetName = ZOOM_options_get(r->options, "elementSetName");
-    task->u.search.elementSetName = elementSetName
-        ? xstrdup(elementSetName) : 0;
+    task->u.search.elementSetName = elementSetName ?
+        xstrdup(elementSetName) : 0;
+    schema = ZOOM_options_get(r->options, "schema");
+    task->u.search.schema = schema ? xstrdup(schema) : 0;
 
     ZOOM_resultset_addref(r);
 
-    ZOOM_query_addref(q);
-
     if (!c->async)
     {
         while (ZOOM_event(1, &c))
@@ -884,11 +942,11 @@ static void resultset_destroy(ZOOM_resultset r)
         ZOOM_options_destroy(r->options);
         odr_destroy(r->odr);
         xfree(r->setname);
-        xfree(r->schema);
         yaz_mutex_destroy(&r->mutex);
 #if SHPTR
         YAZ_SHPTR_DEC(r->record_wrbuf, wrbuf_destroy);
 #endif
+        wrbuf_destroy(r->mc_key);
         resultset_use(-1);
         xfree(r);
     }
@@ -949,26 +1007,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");
-    if (cp)
-    {
-        if (!r->schema || strcmp(r->schema, cp))
-        {
-            xfree(r->schema);
-            r->schema = xstrdup(cp);
-        }
-    }
+    task->u.search.schema = cp ? xstrdup(cp) : 0;
 
     ZOOM_resultset_addref(r);
 
@@ -1146,8 +1197,10 @@ ZOOM_API(ZOOM_record)
         ZOOM_options_get(s->options, "preferredRecordSyntax");
     const char *elementSetName =
         ZOOM_options_get(s->options, "elementSetName");
+    const char *schema =
+        ZOOM_options_get(s->options, "schema");
 
-    return ZOOM_record_cache_lookup(s, pos, syntax, elementSetName);
+    return ZOOM_record_cache_lookup(s, pos, syntax, elementSetName, schema);
 }
 
 ZOOM_API(ZOOM_record)
@@ -1464,12 +1517,6 @@ ZOOM_API(int)
             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);
-            break;
         case ZOOM_TASK_CONNECT:
             ret = do_connect(c);
             break;
@@ -1509,43 +1556,14 @@ ZOOM_API(int)
 
 #if YAZ_HAVE_XML2
 
-static zoom_ret send_HTTP_redirect(ZOOM_connection c, const char *uri,
-                                  Z_HTTP_Response *cookie_hres)
+static zoom_ret send_HTTP_redirect(ZOOM_connection c, const char *uri)
 {
-    struct Z_HTTP_Header *h;
-    char *combined_cookies = 0;
-    int combined_cookies_len = 0;
     Z_GDU *gdu = z_get_HTTP_Request_uri(c->odr_out, uri, 0, c->proxy ? 1 : 0);
 
     gdu->u.HTTP_Request->method = odr_strdup(c->odr_out, "GET");
     z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers, "Accept",
                       "text/xml");
-
-    for (h = cookie_hres->headers; h; h = h->next)
-    {
-        if (!strcmp(h->name, "Set-Cookie"))
-        {
-            char *cp;
-
-            if (!(cp = strchr(h->value, ';')))
-                cp = h->value + strlen(h->value);
-            if (cp - h->value >= 1) {
-                combined_cookies = xrealloc(combined_cookies, combined_cookies_len + cp - h->value + 3);
-                memcpy(combined_cookies+combined_cookies_len, h->value, cp - h->value);
-                combined_cookies[combined_cookies_len + cp - h->value] = '\0';
-                strcat(combined_cookies,"; ");
-                combined_cookies_len = strlen(combined_cookies);
-            }
-        }
-    }
-
-    if (combined_cookies_len)
-    {
-        z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers,
-                          "Cookie", combined_cookies);
-        xfree(combined_cookies);
-    }
-
+    yaz_cookies_request(c->cookies, c->odr_out, gdu->u.HTTP_Request);
     if (c->user && c->password)
     {
         z_HTTP_header_add_basic_auth(c->odr_out, &gdu->u.HTTP_Request->headers,
@@ -1595,6 +1613,7 @@ static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
     ZOOM_connection_set_mask(c, 0);
     yaz_log(c->log_details, "%p handle_http", c);
 
+    yaz_cookies_response(c->cookies, hres);
     if ((hres->code == 301 || hres->code == 302) && c->sru_mode == zoom_sru_get
         && (location = z_HTTP_header_lookup(hres->headers, "Location")))
     {
@@ -1609,9 +1628,13 @@ static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
         {
             /* since redirect may change host we just reconnect. A smarter
                implementation might check whether it's the same server */
-            do_connect_host(c, location);
-            send_HTTP_redirect(c, location, hres);
-            /* we're OK for now. Operation is not really complete */
+
+            int host_change = 0;
+            location = yaz_check_location(c->odr_in, c->host_port,
+                                          location, &host_change);
+            if (do_connect_host(c, location) == zoom_complete)
+                return;  /* connect failed.. */
+            send_HTTP_redirect(c, location);
             return;
         }
     }
@@ -1942,6 +1965,8 @@ ZOOM_API(const char *)
         return "Extended Service. invalid version";
     case ZOOM_ERROR_ES_INVALID_SYNTAX:
         return "Extended Service. invalid syntax";
+    case ZOOM_ERROR_MEMCACHED:
+        return "Memcached";
     default:
         return diagbib1_str(error);
     }