Implementation of fake facets.
[yaz-moved-to-github.git] / ztest / ztest.c
index 4ebf1c1..689f122 100644 (file)
 #if HAVE_SYS_SELECT_H
 #include <sys/select.h>
 #endif
+#ifdef WIN32
+#include <windows.h>
+#endif
 
 #include <yaz/log.h>
 #include <yaz/backend.h>
 #include <yaz/ill.h>
 #include <yaz/diagbib1.h>
+#include <yaz/otherinfo.h>
+#include <yaz/facet.h>
 
 #include "ztest.h"
 
@@ -130,12 +135,16 @@ static Odr_int get_term_hit(Z_RPNStructure *s)
 */
 static Odr_int get_hit_count(Z_Query *q)
 {
-    Odr_int h = -1;
     if (q->which == Z_Query_type_1 || q->which == Z_Query_type_101)
+    {
+        Odr_int h = -1;
         h = get_term_hit(q->u.type_1->RPNStructure);
-    if (h == -1)
-        h = rand() % 24;
-    return h;
+        if (h == -1)
+            h = rand() % 24;
+        return h;
+    }
+    else
+        return 24;
 }
 
 /** \brief checks if it's a dummy Slow database
@@ -219,6 +228,40 @@ static void do_delay(const struct delay *delayp)
     }
 }
 
+static void addterms(ODR odr, Z_FacetField *facet_field) {
+    int index;
+    int freq = 100;
+    const char *key = "key";
+    for (index = 0; index < facet_field->num_terms; index++) {
+        Z_Term *term = term_create(odr, key);
+        Z_FacetTerm *facet_term = facet_term_create(odr, term, freq);
+        freq = freq - 10 ;
+        facet_field_term_set(odr, facet_field, facet_term, index);
+        //yaz_log(YLOG_DEBUG, "Facet term %d %d", term->u.characterString, *facet_term->count);
+    }
+}
+Z_OtherInformation *build_facet_response(ODR odr, Z_FacetList *facet_list) {
+    int index;
+    Z_OtherInformation *oi = odr_malloc(odr, sizeof(*oi));
+    Z_OtherInformationUnit *oiu = odr_malloc(odr, sizeof(*oiu));
+    Z_FacetList *new_list = facet_list_create(odr, facet_list->num);
+
+    for (index = 0; index < facet_list->num; index++) {
+        new_list->elements[index] = facet_field_create(odr, facet_list->elements[index]->attributes, 3);
+        addterms(odr, new_list->elements[index]);
+    }
+    oi->num_elements = 1;
+    oi->list = odr_malloc(odr, oi->num_elements * sizeof(*oi->list));
+    oiu->category = 0;
+    oiu->which = Z_OtherInfo_externallyDefinedInfo;
+    oiu->information.externallyDefinedInfo = odr_malloc(odr, sizeof(*oiu->information.externallyDefinedInfo));
+    oiu->information.externallyDefinedInfo->direct_reference = odr_oiddup(odr, yaz_oid_userinfo_facet_1);
+    oiu->information.externallyDefinedInfo->which = Z_External_userFacets;
+    oiu->information.externallyDefinedInfo->u.facetList = new_list;
+    oi->list[0] = oiu;
+    return oi;
+}
+
 int ztest_search(void *handle, bend_search_rr *rr)
 {
     struct session_handle *sh = (struct session_handle*) handle;
@@ -284,7 +327,7 @@ int ztest_search(void *handle, bend_search_rr *rr)
             const char *name = names[i];
             const char *value = values[i];
             if (!strcmp(name, "seed"))
-                srandom(atoi(value));
+                srand(atoi(value));
             else if (!strcmp(name, "search-delay"))
                 parse_delay(&new_set->search_delay, value);
             else if (!strcmp(name, "present-delay"))
@@ -324,6 +367,19 @@ int ztest_search(void *handle, bend_search_rr *rr)
     }
     rr->hits = get_hit_count(rr->query);
 
+    if (1)
+    {
+        /* TODO Not general. Only handles one (Facet) OtherInformation. Overwrite  */
+        Z_FacetList *facet_list = extract_facet_request(rr->stream, rr->search_input);
+
+        if (facet_list) {
+            yaz_log(YLOG_LOG, "%d Facets in search request.", facet_list->num);
+            rr->search_info = build_facet_response(rr->stream, facet_list);
+        }
+        else
+            yaz_log(YLOG_DEBUG, "No facets parsed search request.");
+
+    }
     do_delay(&new_set->search_delay);
     new_set->hits = rr->hits;