Deal with facets for caching
authorAdam Dickmeiss <adam@indexdata.dk>
Tue, 4 Feb 2014 10:25:29 +0000 (11:25 +0100)
committerAdam Dickmeiss <adam@indexdata.dk>
Tue, 4 Feb 2014 10:25:29 +0000 (11:25 +0100)
Facets is part of search key. And corresponding facets in response
are cached with the key.

src/pquery.c
src/zoom-memcached.c
src/zoom-p.h
src/zoom-sru.c
src/zoom-z3950.c

index a9a6b60..002d9eb 100644 (file)
@@ -832,7 +832,7 @@ static Z_FacetField* parse_facet(ODR odr, const char *facet)
 {
     YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
     struct yaz_pqf_parser *li = pqf_parser;
-    Odr_oid *attributeSetId;
+    Odr_oid *attributeSetId = 0;
     Z_FacetField *facet_field = 0;
     Z_AttributeList *attribute_list;
 
index 25c4ea3..e2cd76e 100644 (file)
@@ -96,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;
@@ -105,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;
@@ -126,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)
@@ -134,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
 }
index 9de5509..4372ece 100644 (file)
@@ -277,7 +277,8 @@ int ZOOM_memcached_configure(ZOOM_connection c);
 void ZOOM_memcached_destroy(ZOOM_connection c);
 void ZOOM_memcached_resultset(ZOOM_resultset r, ZOOM_query q);
 void ZOOM_memcached_search(ZOOM_connection c, ZOOM_resultset r);
-void ZOOM_memcached_hitcount(ZOOM_connection c, ZOOM_resultset result);
+void ZOOM_memcached_hitcount(ZOOM_connection c, ZOOM_resultset result,
+                             Z_OtherInformation *oi, const char *precision);
 void ZOOM_memcached_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
                         int pos,
                         const char *syntax, const char *elementSetName,
@@ -288,6 +289,11 @@ Z_NamePlusRecord *ZOOM_memcached_lookup(ZOOM_resultset r, int pos,
                                         const char *elementSetName,
                                         const char *schema);
 
+void ZOOM_handle_facet_result(ZOOM_connection c, ZOOM_resultset r,
+                              Z_OtherInformation *o);
+void ZOOM_handle_search_result(ZOOM_connection c, ZOOM_resultset resultset,
+                               Z_OtherInformation *o);
+
 /*
  * Local variables:
  * c-basic-offset: 4
index fd0c2bc..0185e68 100644 (file)
@@ -294,8 +294,33 @@ static zoom_ret handle_srw_response(ZOOM_connection c,
     {
         if (res->numberOfRecords)
         {
+            Z_OtherInformation *oi = 0;
+            if (res->facetList)
+            {
+                ODR o = c->odr_in;
+                Z_External *ext = (Z_External *)
+                    odr_malloc(o, sizeof(*ext));
+
+                ext->which = Z_External_userFacets;
+                ext->u.facetList = res->facetList;
+                ext->direct_reference =
+                    odr_oiddup(o, yaz_oid_userinfo_facet_1);
+                ext->indirect_reference = 0;
+                ext->descriptor = 0;
+                oi = (Z_OtherInformation *) odr_malloc(o, sizeof(*oi));
+                oi->num_elements = 1;
+                oi->list = (Z_OtherInformationUnit **)
+                    odr_malloc(o, sizeof(*oi->list));
+                oi->list[0] = (Z_OtherInformationUnit *)
+                    odr_malloc(o, sizeof(**oi->list));
+                oi->list[0]->category = 0;
+                oi->list[0]->which = Z_OtherInfo_externallyDefinedInfo;
+                oi->list[0]->information.externallyDefinedInfo = ext;
+            }
             resultset->size = *res->numberOfRecords;
-            ZOOM_memcached_hitcount(c, resultset);
+            ZOOM_memcached_hitcount(c, resultset, oi,
+                                    res->resultCountPrecision ?
+                                    res->resultCountPrecision : "exact");
         }
         resultset->live_set = 2;
         if (res->suggestions)
index b082108..5726d7d 100644 (file)
@@ -1140,8 +1140,8 @@ void ZOOM_handle_facet_list(ZOOM_resultset r, Z_FacetList *fl)
     }
 }
 
-static void handle_facet_result(ZOOM_connection c, ZOOM_resultset r,
-                                Z_OtherInformation *o)
+void ZOOM_handle_facet_result(ZOOM_connection c, ZOOM_resultset r,
+                              Z_OtherInformation *o)
 {
     int i;
     for (i = 0; o && i < o->num_elements; i++)
@@ -1195,8 +1195,8 @@ static void handle_queryExpression(ZOOM_options opt, const char *name,
 }
 
 
-static void handle_search_result(ZOOM_connection c, ZOOM_resultset resultset,
-                                Z_OtherInformation *o)
+void ZOOM_handle_search_result(ZOOM_connection c, ZOOM_resultset resultset,
+                               Z_OtherInformation *o)
 {
     int i;
     for (i = 0; o && i < o->num_elements; i++)
@@ -1268,6 +1268,7 @@ static void handle_Z3950_search_response(ZOOM_connection c,
 {
     ZOOM_resultset resultset;
     ZOOM_Event event;
+    const char *resultCountPrecision = "exact";
 
     if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
         return;
@@ -1281,6 +1282,8 @@ static void handle_Z3950_search_response(ZOOM_connection c,
     }
     if (sr->resultSetStatus)
     {
+        if (*sr->resultSetStatus == Z_SearchResponse_estimate)
+            resultCountPrecision = "estimate";
         ZOOM_options_set_int(resultset->options, "resultSetStatus",
                              *sr->resultSetStatus);
     }
@@ -1289,13 +1292,16 @@ static void handle_Z3950_search_response(ZOOM_connection c,
         ZOOM_options_set_int(resultset->options, "presentStatus",
                              *sr->presentStatus);
     }
-    handle_search_result(c, resultset, sr->additionalSearchInfo);
+    ZOOM_options_set(resultset->options, "resultCountPrecision",
+                     resultCountPrecision);
+    ZOOM_handle_search_result(c, resultset, sr->additionalSearchInfo);
 
-    handle_facet_result(c, resultset, sr->additionalSearchInfo);
+    ZOOM_handle_facet_result(c, resultset, sr->additionalSearchInfo);
 
     resultset->size = *sr->resultCount;
 
-    ZOOM_memcached_hitcount(c, resultset);
+    ZOOM_memcached_hitcount(c, resultset, sr->additionalSearchInfo,
+                            resultCountPrecision);
     resultset->live_set = 2;
     handle_Z3950_records(c, sr->records, 0);
 }