Start work on record caching - does not work!
authorAdam Dickmeiss <adam@indexdata.dk>
Fri, 24 Jan 2014 13:27:17 +0000 (14:27 +0100)
committerAdam Dickmeiss <adam@indexdata.dk>
Fri, 24 Jan 2014 13:27:17 +0000 (14:27 +0100)
Instead of caching the octet aligned record, we must cache
Z_NamePlusRecord ..

src/zoom-c.c
src/zoom-record-cache.c
src/zoom-z3950.c

index 6e0a883..41d6e1c 100644 (file)
@@ -778,9 +778,21 @@ 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->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;
index 84f4dd2..17c4ac5 100644 (file)
@@ -56,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;
 
@@ -94,7 +96,9 @@ void ZOOM_record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
         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;
@@ -114,8 +118,46 @@ 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 &&
+        npr->u.databaseRecord->which == Z_External_octet)
+    {
+        WRBUF k = wrbuf_alloc();
+        uint32_t flags = 0;
+        memcached_return_t rc;
+        time_t expiration = 36000;
+
+        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),
+                           npr->u.databaseRecord->u.octet_aligned->buf,
+                           npr->u.databaseRecord->u.octet_aligned->len,
+                           expiration, flags);
+
+        yaz_log(YLOG_LOG, "Store record key=%s rc=%u %s",
+                wrbuf_cstr(k), (unsigned) rc,
+                memcached_last_error_message(r->connection->mc_st));
+        wrbuf_destroy(k);
+    }
+#endif
 }
 
+
 ZOOM_record ZOOM_record_cache_lookup(ZOOM_resultset r, int pos,
                                      const char *syntax,
                                      const char *elementSetName,
@@ -136,6 +178,51 @@ ZOOM_record ZOOM_record_cache_lookup(ZOOM_resultset r, int pos,
             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;
+        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 : "");
+
+        v = memcached_get(r->connection->mc_st, wrbuf_buf(k), wrbuf_len(k),
+                          &v_len, &flags, &rc);
+        wrbuf_destroy(k);
+        if (v)
+        {
+            yaz_log(YLOG_LOG, "Building record from memcached!! syntax=%s",
+                syntax);
+            Z_NamePlusRecord *npr = (Z_NamePlusRecord *)
+                odr_malloc(r->odr, sizeof(Z_NamePlusRecord));
+            npr->databaseName = 0;
+            npr->which = Z_NamePlusRecord_databaseRecord;
+            npr->u.databaseRecord = (Z_External *)
+                odr_malloc(r->odr, sizeof(Z_External));
+            npr->u.databaseRecord->descriptor = 0;
+            npr->u.databaseRecord->direct_reference =
+                syntax ?
+                yaz_string_to_oid_odr(yaz_oid_std(), CLASS_RECSYN,
+                                      syntax, r->odr) : 0;
+            npr->u.databaseRecord->indirect_reference = 0;
+            npr->u.databaseRecord->which = Z_External_octet;
+            npr->u.databaseRecord->u.octet_aligned =
+                odr_create_Odr_oct(r->odr, v, v_len);
+            free(v);
+
+            if (v)
+                return record_cache_add(r, npr, pos, syntax, elementSetName,
+                                        schema, 0);
+        }
+    }
+#endif
     return 0;
 }
 
index df31381..4ad090d 100644 (file)
@@ -1292,7 +1292,7 @@ static void handle_Z3950_search_response(ZOOM_connection c,
         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, "Key=%s value=%s rc=%u %s",
+        yaz_log(YLOG_LOG, "Store hit count key=%s value=%s rc=%u %s",
                 wrbuf_cstr(resultset->mc_key), str, (unsigned) rc,
                 memcached_last_error_message(c->mc_st));
     }