Caches NamePlusRecord BER; works well
[yaz-moved-to-github.git] / src / zoom-record-cache.c
index d9d3765..9e76d66 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2012 Index Data
+ * Copyright (C) Index Data
  * See the file LICENSE for details.
  */
 /**
@@ -49,16 +49,6 @@ struct ZOOM_record_cache_p {
     ZOOM_record_cache next;
 };
 
-
-static int strcmp_null(const char *v1, const char *v2)
-{
-    if (!v1 && !v2)
-        return 0;
-    if (!v1 || !v2)
-        return -1;
-    return strcmp(v1, v2);
-}
-
 static size_t record_hash(int pos)
 {
     if (pos < 0)
@@ -66,11 +56,13 @@ static size_t record_hash(int pos)
     return pos % RECORD_HASH_SIZE;
 }
 
-void ZOOM_record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
-                           int pos,
-                           const char *syntax, const char *elementSetName,
-                           const char *schema,
-                           Z_SRW_diagnostic *diag)
+static ZOOM_record record_cache_add(ZOOM_resultset r,
+                                    Z_NamePlusRecord *npr,
+                                    int pos,
+                                    const char *syntax,
+                                    const char *elementSetName,
+                                    const char *schema,
+                                    Z_SRW_diagnostic *diag)
 {
     ZOOM_record_cache rc = 0;
 
@@ -80,9 +72,9 @@ void ZOOM_record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
     for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
     {
         if (pos == rc->pos
-            && strcmp_null(r->schema, rc->schema) == 0
-            && strcmp_null(elementSetName,rc->elementSetName) == 0
-            && strcmp_null(syntax, rc->syntax) == 0)
+            && yaz_strcmp_null(schema, rc->schema) == 0
+            && yaz_strcmp_null(elementSetName,rc->elementSetName) == 0
+            && yaz_strcmp_null(syntax, rc->syntax) == 0)
             break;
     }
     if (!rc)
@@ -99,12 +91,14 @@ void ZOOM_record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
 
         rc->syntax = odr_strdup_null(r->odr, syntax);
 
-        rc->schema = odr_strdup_null(r->odr, r->schema);
+        rc->schema = odr_strdup_null(r->odr, schema);
 
         rc->pos = pos;
         rc->next = r->record_hash[record_hash(pos)];
         r->record_hash[record_hash(pos)] = rc;
+
     }
+
     rc->rec.npr = npr;
     rc->rec.schema = odr_strdup_null(r->odr, schema);
     rc->rec.diag_set = 0;
@@ -124,11 +118,55 @@ void ZOOM_record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
         rc->rec.diag_message = odr_strdup_null(r->odr, diag->message);
         rc->rec.diag_details = odr_strdup_null(r->odr, diag->details);
     }
+    return &rc->rec;
 }
 
+void ZOOM_record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
+                           int pos,
+                           const char *syntax, const char *elementSetName,
+                           const char *schema,
+                           Z_SRW_diagnostic *diag)
+{
+    record_cache_add(r, npr, pos, syntax, elementSetName, schema, diag);
+#if HAVE_LIBMEMCACHED_MEMCACHED_H
+    if (r->connection->mc_st &&
+        !diag && npr->which == Z_NamePlusRecord_databaseRecord)
+    {
+        WRBUF k = wrbuf_alloc();
+        uint32_t flags = 0;
+        memcached_return_t rc;
+        time_t expiration = 36000;
+        ODR odr = odr_createmem(ODR_ENCODE);
+        char *rec_buf;
+        int rec_len;
+
+        z_NamePlusRecord(odr, &npr, 0, 0);
+        rec_buf = odr_getbuf(odr, &rec_len, 0);
+
+        wrbuf_write(k, wrbuf_buf(r->mc_key), wrbuf_len(r->mc_key));
+        wrbuf_printf(k, ";%d;%s;%s;%s", pos,
+                     syntax ? syntax : "",
+                     elementSetName ? elementSetName : "",
+                     schema ? schema : "");
+        rc = memcached_set(r->connection->mc_st,
+                           wrbuf_buf(k),wrbuf_len(k),
+                           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,
+                memcached_last_error_message(r->connection->mc_st));
+        odr_destroy(odr);
+        wrbuf_destroy(k);
+    }
+#endif
+}
+
+
 ZOOM_record ZOOM_record_cache_lookup(ZOOM_resultset r, int pos,
                                      const char *syntax,
-                                     const char *elementSetName)
+                                     const char *elementSetName,
+                                     const char *schema)
 {
     ZOOM_record_cache rc;
 
@@ -136,15 +174,53 @@ ZOOM_record ZOOM_record_cache_lookup(ZOOM_resultset r, int pos,
     {
         if (pos == rc->pos)
         {
-            if (strcmp_null(r->schema, rc->schema))
+            if (yaz_strcmp_null(schema, rc->schema))
                 continue;
-            if (strcmp_null(elementSetName,rc->elementSetName))
+            if (yaz_strcmp_null(elementSetName,rc->elementSetName))
                 continue;
-            if (strcmp_null(syntax, rc->syntax))
+            if (yaz_strcmp_null(syntax, rc->syntax))
                 continue;
             return &rc->rec;
         }
     }
+#if HAVE_LIBMEMCACHED_MEMCACHED_H
+    if (r->connection && r->connection->mc_st)
+    {
+        WRBUF k = wrbuf_alloc();
+        size_t v_len;
+        char *v_buf;
+        uint32_t flags;
+        memcached_return_t rc;
+
+        wrbuf_write(k, wrbuf_buf(r->mc_key), wrbuf_len(r->mc_key));
+        wrbuf_printf(k, ";%d;%s;%s;%s", pos,
+                     syntax ? syntax : "",
+                     elementSetName ? elementSetName : "",
+                     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);
+        wrbuf_destroy(k);
+        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 record_cache_add(r, npr, pos, syntax, elementSetName,
+                                        schema, 0);
+            }
+            yaz_log(YLOG_WARN, "memcached_get npr failed v_len=%ld",
+                    (long) v_len);
+        }
+    }
+#endif
     return 0;
 }