memcached: generate SHA1 for records and store blob once
[yaz-moved-to-github.git] / src / zoom-memcached.c
index f35e7e6..d92758e 100644 (file)
@@ -55,6 +55,7 @@ int ZOOM_memcached_configure(ZOOM_connection c)
             ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED, val);
             return -1;
         }
+        memcached_behavior_set(c->mc_st, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
 #else
         ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED, "not enabled");
         return -1;
@@ -95,7 +96,6 @@ void ZOOM_memcached_resultset(ZOOM_resultset r, ZOOM_query q)
 void ZOOM_memcached_search(ZOOM_connection c, ZOOM_resultset resultset)
 {
 #if HAVE_LIBMEMCACHED_MEMCACHED_H
-    /* TODO: add sorting */
     if (c->mc_st && resultset->live_set == 0)
     {
         size_t v_len;
@@ -104,19 +104,32 @@ void ZOOM_memcached_search(ZOOM_connection c, ZOOM_resultset resultset)
         char *v = memcached_get(c->mc_st, wrbuf_buf(resultset->mc_key),
                                 wrbuf_len(resultset->mc_key),
                                 &v_len, &flags, &rc);
+        /* count;precision (ASCII) + '\0' + BER buffer for otherInformation */
         if (v)
         {
             ZOOM_Event event;
-            WRBUF w = wrbuf_alloc();
+            size_t lead_len = strlen(v) + 1;
 
-            wrbuf_write(w, v, v_len);
-            free(v);
-            resultset->size = odr_atoi(wrbuf_cstr(w));
-
-            yaz_log(YLOG_LOG, "For key %s got value %s",
-                    wrbuf_cstr(resultset->mc_key), wrbuf_cstr(w));
+            resultset->size = odr_atoi(v);
 
-            wrbuf_destroy(w);
+            yaz_log(YLOG_LOG, "For key %s got value %s lead_len=%d len=%d",
+                    wrbuf_cstr(resultset->mc_key), v, (int) lead_len,
+                    (int) v_len);
+            if (v_len > lead_len)
+            {
+                Z_OtherInformation *oi = 0;
+                int oi_len = v_len - lead_len;
+                odr_setbuf(resultset->odr, v + lead_len, oi_len, 0);
+                if (!z_OtherInformation(resultset->odr, &oi, 0, 0))
+                {
+                    yaz_log(YLOG_WARN, "oi decoding failed");
+                    free(v);
+                    return;
+                }
+                ZOOM_handle_search_result(c, resultset, oi);
+                ZOOM_handle_facet_result(c, resultset, oi);
+            }
+            free(v);
             event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
             ZOOM_connection_put_event(c, event);
             resultset->live_set = 1;
@@ -125,7 +138,8 @@ void ZOOM_memcached_search(ZOOM_connection c, ZOOM_resultset resultset)
 #endif
 }
 
-void ZOOM_memcached_hitcount(ZOOM_connection c, ZOOM_resultset resultset)
+void ZOOM_memcached_hitcount(ZOOM_connection c, ZOOM_resultset resultset,
+                             Z_OtherInformation *oi, const char *precision)
 {
 #if HAVE_LIBMEMCACHED_MEMCACHED_H
     if (c->mc_st && resultset->live_set == 0)
@@ -133,15 +147,33 @@ void ZOOM_memcached_hitcount(ZOOM_connection c, ZOOM_resultset resultset)
         uint32_t flags = 0;
         memcached_return_t rc;
         time_t expiration = 36000;
-        char str[40];
+        char *str;
+        ODR odr = odr_createmem(ODR_ENCODE);
+        char *oi_buf = 0;
+        int oi_len = 0;
+        char *key;
+
+        str = odr_malloc(odr, 20 + strlen(precision));
+        /* count;precision (ASCII) + '\0' + BER buffer for otherInformation */
+        sprintf(str, ODR_INT_PRINTF ";%s", resultset->size, precision);
+        if (oi)
+        {
+            z_OtherInformation(odr, &oi, 0, 0);
+            oi_buf = odr_getbuf(odr, &oi_len, 0);
+        }
+        key = odr_malloc(odr, strlen(str) + 1 + oi_len);
+        strcpy(key, str);
+        if (oi_len)
+            memcpy(key + strlen(str) + 1, oi_buf, oi_len);
 
-        sprintf(str, ODR_INT_PRINTF, resultset->size);
         rc = memcached_set(c->mc_st,
-                           wrbuf_buf(resultset->mc_key),wrbuf_len(resultset->mc_key),
-                           str, strlen(str), expiration, flags);
-        yaz_log(YLOG_LOG, "Store hit count key=%s value=%s rc=%u %s",
-                wrbuf_cstr(resultset->mc_key), str, (unsigned) rc,
+                           wrbuf_buf(resultset->mc_key),
+                           wrbuf_len(resultset->mc_key),
+                           key, strlen(str) + 1 + oi_len, expiration, flags);
+        yaz_log(YLOG_LOG, "Store hit count key=%s value=%s oi_len=%d rc=%u %s",
+                wrbuf_cstr(resultset->mc_key), str, oi_len, (unsigned) rc,
                 memcached_last_error_message(c->mc_st));
+        odr_destroy(odr);
     }
 #endif
 }
@@ -157,6 +189,7 @@ void ZOOM_memcached_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
         !diag && npr->which == Z_NamePlusRecord_databaseRecord)
     {
         WRBUF k = wrbuf_alloc();
+        WRBUF rec_sha1 = wrbuf_alloc();
         uint32_t flags = 0;
         memcached_return_t rc;
         time_t expiration = 36000;
@@ -172,16 +205,30 @@ void ZOOM_memcached_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
                      syntax ? syntax : "",
                      elementSetName ? elementSetName : "",
                      schema ? schema : "");
+
+        wrbuf_sha1_write(rec_sha1, rec_buf, rec_len, 1);
+
         rc = memcached_set(r->connection->mc_st,
-                           wrbuf_buf(k),wrbuf_len(k),
+                           wrbuf_buf(k), wrbuf_len(k),
+                           wrbuf_buf(rec_sha1), wrbuf_len(rec_sha1),
+                           expiration, flags);
+
+        yaz_log(YLOG_LOG, "Store record key=%s val=%s rc=%u %s",
+                wrbuf_cstr(k), wrbuf_cstr(rec_sha1), (unsigned) rc,
+                memcached_last_error_message(r->connection->mc_st));
+
+        rc = memcached_add(r->connection->mc_st,
+                           wrbuf_buf(rec_sha1), wrbuf_len(rec_sha1),
                            rec_buf, rec_len,
                            expiration, flags);
 
-        yaz_log(YLOG_LOG, "Store record lkey=%s len=%d rc=%u %s",
-                wrbuf_cstr(k), rec_len, (unsigned) rc,
+        yaz_log(YLOG_LOG, "Add record key=%s rec_len=%d rc=%u %s",
+                wrbuf_cstr(rec_sha1), rec_len, (unsigned) rc,
                 memcached_last_error_message(r->connection->mc_st));
+
         odr_destroy(odr);
         wrbuf_destroy(k);
+        wrbuf_destroy(rec_sha1);
     }
 #endif
 }
@@ -195,8 +242,8 @@ Z_NamePlusRecord *ZOOM_memcached_lookup(ZOOM_resultset r, int pos,
     if (r->connection && r->connection->mc_st)
     {
         WRBUF k = wrbuf_alloc();
-        size_t v_len;
-        char *v_buf;
+        char *sha1_buf;
+        size_t sha1_len;
         uint32_t flags;
         memcached_return_t rc;
 
@@ -207,19 +254,31 @@ Z_NamePlusRecord *ZOOM_memcached_lookup(ZOOM_resultset r, int pos,
                      schema ? schema : "");
 
         yaz_log(YLOG_LOG, "Lookup record %s", wrbuf_cstr(k));
-        v_buf = memcached_get(r->connection->mc_st, wrbuf_buf(k), wrbuf_len(k),
-                              &v_len, &flags, &rc);
+        sha1_buf = memcached_get(r->connection->mc_st,
+                                 wrbuf_buf(k), wrbuf_len(k),
+                                 &sha1_len, &flags, &rc);
+
         wrbuf_destroy(k);
-        if (v_buf)
+        if (sha1_buf)
         {
-            Z_NamePlusRecord *npr = 0;
-
-            odr_setbuf(r->odr, v_buf, v_len, 0);
-            z_NamePlusRecord(r->odr, &npr, 0, 0);
-            free(v_buf);
-            if (npr)
-                yaz_log(YLOG_LOG, "returned memcached copy");
-            return npr;
+            size_t v_len;
+            char *v_buf;
+
+            yaz_log(YLOG_LOG, "Lookup record %.*s", (int) sha1_len, sha1_buf);
+            v_buf = memcached_get(r->connection->mc_st, sha1_buf, sha1_len,
+                                  &v_len, &flags, &rc);
+            free(sha1_buf);
+            if (v_buf)
+            {
+                Z_NamePlusRecord *npr = 0;
+
+                odr_setbuf(r->odr, v_buf, v_len, 0);
+                z_NamePlusRecord(r->odr, &npr, 0, 0);
+                free(v_buf);
+                if (npr)
+                    yaz_log(YLOG_LOG, "returned memcached copy");
+                return npr;
+            }
         }
     }
 #endif