Add fake Facet list. Not yet working
[yaz-moved-to-github.git] / ztest / ztest.c
index 6d806b2..e5b7ebe 100644 (file)
 #include <math.h>
 #include <stdlib.h>
 #include <ctype.h>
+
+#if HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
 #if HAVE_UNISTD_H
 #include <unistd.h>
 #endif
+#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 "ztest.h"
 
@@ -123,12 +134,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
@@ -188,21 +203,76 @@ static int parse_delay(struct delay *delayp, const char *value)
     return 0;
 }
 
+static void ztest_sleep(double d)
+{
+#ifdef WIN32
+    Sleep( (DWORD) (d * 1000));
+#else
+    struct timeval tv;
+    tv.tv_sec = floor(d);
+    tv.tv_usec = (d - floor(d)) * 1000000;
+    select(0, 0, 0, 0, &tv);
+#endif
+}
+
 static void do_delay(const struct delay *delayp)
 {
     double d = delayp->d1;
 
     if (d > 0.0)
     {
-        struct timeval tv;
         if (delayp->d2 > d)
             d += (rand()) * (delayp->d2 - d) / RAND_MAX;
-        tv.tv_sec = floor(d);
-        tv.tv_usec = (d - floor(d)) * 1000000;
-        select(0, 0, 0, 0, &tv);
+        ztest_sleep(d);
     }
 }
 
+Z_FacetList *extract_facet_request(ODR odr, Z_OtherInformation *search_input) {
+    Z_OtherInformation **oi;
+    Z_FacetList *facet_list = yaz_oi_get_facetlist_oid(oi, odr, yaz_oid_userinfo_facet_1, 1, 0);
+
+    return facet_list;
+}
+
+Z_Term *term_new(ODR odr, const char *cstr) {
+    Z_Term *term = odr_malloc(odr, sizeof(*term));
+    term->which = Z_Term_characterString;
+    term->u.characterString = odr_strdup(odr, cstr);
+    return term;
+}
+
+static void addterms(ODR odr, Z_FacetField *facet_field) {
+    int index;
+    int count = 100;
+    facet_field->num_terms = 3;
+    facet_field->terms = odr_malloc(odr, facet_field->num_terms * sizeof(*facet_field->terms));
+    for (index = 0; index < facet_field->num_terms; index++) {
+        Z_FacetTerm *facet_term = odr_malloc(odr, sizeof(*facet_term));
+        facet_term->count = odr_malloc(odr, sizeof(*facet_term->count));
+        *facet_term->count = count;
+        facet_term->term = term_new(odr, "key");
+        count = count - 10 ;
+        facet_field->terms[index] = facet_term;
+    }
+}
+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));
+    for (index = 0; index < facet_list->num; index++) {
+        addterms(odr, facet_list->elements[index]);
+    }
+    oi->list = odr_malloc(odr, 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->u.facetList = facet_list;
+    oi->list[0] = oiu;
+    return oi;
+}
+
 int ztest_search(void *handle, bend_search_rr *rr)
 {
     struct session_handle *sh = (struct session_handle*) handle;
@@ -268,7 +338,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"))
@@ -308,6 +378,14 @@ 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) {
+            rr->search_info = build_facet_response(rr->stream, facet_list);
+        }
+    }
     do_delay(&new_set->search_delay);
     new_set->hits = rr->hits;