Support for skipping facet, if term count is 0.
[yaz-moved-to-github.git] / ztest / ztest.c
index 8deffa6..d5e5d27 100644 (file)
@@ -8,26 +8,47 @@
  */
 
 #include <stdio.h>
+#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 <yaz/facet.h>
 
 #include "ztest.h"
 
 static int log_level=0;
 static int log_level_set=0;
 
+struct delay {
+    double d1;
+    double d2;
+};
+
 struct result_set {
     char *name;
     char *db;
     Odr_int hits;
+    struct delay search_delay;
+    struct delay present_delay;
+    struct delay fetch_delay;
     struct result_set *next;
 };
 
@@ -114,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
@@ -155,20 +180,127 @@ static int check_slow(const char *basename, bend_association association)
     return 0;
 }
 
+static int strcmp_prefix(const char *s, const char *p)
+{
+    size_t l = strlen(p);
+    if (strlen(s) >= l && !memcmp(s, p, l))
+        return 1;
+    return 0;
+}
+
+static void init_delay(struct delay *delayp)
+{
+    delayp->d1 = delayp->d2 = 0.0;
+}
+
+static int parse_delay(struct delay *delayp, const char *value)
+{
+    if (sscanf(value, "%lf:%lf", &delayp->d1, &delayp->d2) == 2)
+        ;
+    else if (sscanf(value, "%lf", &delayp->d1) == 1)
+        delayp->d2 = 0.0;
+    else
+        return -1;
+    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)
+    {
+        if (delayp->d2 > d)
+            d += (rand()) * (delayp->d2 - d) / RAND_MAX;
+        ztest_sleep(d);
+    }
+}
+
+static void addterms(ODR odr, Z_FacetField *facet_field, const char *facet_name) {
+    int index;
+    int freq = 100;
+    int length = strlen(facet_name) + 10;
+    char key[length];
+    key[0] = '\0';
+    for (index = 0; index < facet_field->num_terms; index++) {
+        Z_Term *term;
+        Z_FacetTerm *facet_term;
+        // sprintf(key, "%s%d", facet_name, index);
+        //yaz_log(YLOG_DEBUG, "facet add term %s %d %s", facet_name, index, key);
+        term = term_create(odr, key);
+        facet_term = facet_term_create(odr, term, freq);
+        freq = freq - 10 ;
+        facet_field_term_set(odr, facet_field, facet_term, index);
+    }
+}
+
+Z_OtherInformation *build_facet_response(ODR odr, Z_FacetList *facet_list) {
+    int index, new_index = 0;
+    Z_FacetList *new_list = facet_list_create(odr, facet_list->num);
+
+    for (index = 0; index < facet_list->num; index++) {
+        struct attrvalues attrvalues;
+        facet_struct_init(&attrvalues);
+        attrvalues.limit = 10;
+        yaz_log(YLOG_LOG, "Attributes: %s %s %d ", attrvalues.useattr, attrvalues.useattrbuff, attrvalues.limit);
+        facetattrs(facet_list->elements[index]->attributes, &attrvalues);
+        yaz_log(YLOG_LOG, "Attributes: %s %s %d ", attrvalues.useattr, attrvalues.useattrbuff, attrvalues.limit);
+        if (attrvalues.errstring)
+            yaz_log(YLOG_LOG, "Error parsing attributes: %s", attrvalues.errstring);
+        if (attrvalues.limit > 0) {
+            new_list->elements[new_index] = facet_field_create(odr, facet_list->elements[index]->attributes, attrvalues.limit);
+            addterms(odr, new_list->elements[new_index], attrvalues.useattr);
+            new_index++;
+        }
+    }
+    new_list->num = new_index;
+    if (new_index > 0) {
+        Z_OtherInformation *oi = odr_malloc(odr, sizeof(*oi));
+        Z_OtherInformationUnit *oiu = odr_malloc(odr, sizeof(*oiu));
+        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;
+    }
+    return 0;
+}
+
 int ztest_search(void *handle, bend_search_rr *rr)
 {
     struct session_handle *sh = (struct session_handle*) handle;
     struct result_set *new_set;
+    const char *db, *db_sep;
 
     if (rr->num_bases != 1)
     {
         rr->errcode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
         return 0;
     }
+    
+    db = rr->basenames[0];
+
     /* Allow Default, db.* and Slow */
-    if (!yaz_matchstr(rr->basenames[0], "Default"))
+    if (strcmp_prefix(db, "Default"))
         ;  /* Default is OK in our test */
-    else if (!strncmp(rr->basenames[0], "db", 2))
+    else if (strcmp_prefix(db, "db"))
         ;  /* db.* is OK in our test */
     else if (check_slow(rr->basenames[0], rr->association))
     {
@@ -198,6 +330,38 @@ int ztest_search(void *handle, bend_search_rr *rr)
         sh->result_sets = new_set;
         new_set->name = xstrdup(rr->setname);
     }
+    new_set->hits = 0;
+    new_set->db = xstrdup(db);
+    init_delay(&new_set->search_delay);
+    init_delay(&new_set->present_delay);
+    init_delay(&new_set->fetch_delay);
+
+    db_sep = strchr(db, '?');
+    if (db_sep)
+    {
+        char **names;
+        char **values;
+        int no_parms = yaz_uri_to_array(db_sep+1, rr->stream, &names, &values);
+        int i;
+        for (i = 0; i < no_parms; i++)
+        {
+            const char *name = names[i];
+            const char *value = values[i];
+            if (!strcmp(name, "seed"))
+                srand(atoi(value));
+            else if (!strcmp(name, "search-delay"))
+                parse_delay(&new_set->search_delay, value);
+            else if (!strcmp(name, "present-delay"))
+                parse_delay(&new_set->present_delay, value);
+            else if (!strcmp(name, "fetch-delay"))
+                parse_delay(&new_set->fetch_delay, value);
+            else
+            {
+                rr->errcode = YAZ_BIB1_SERVICE_UNSUPP_FOR_THIS_DATABASE;
+                rr->errstring = odr_strdup(rr->stream, name);
+            }
+        }
+    }
 
     if (rr->extra_args)
     {
@@ -224,8 +388,21 @@ 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;
-    new_set->db = xstrdup(rr->basenames[0]);
     
     return 0;
 }
@@ -615,6 +792,16 @@ int ztest_sort(void *handle, bend_sort_rr *rr)
 /* present request handler */
 int ztest_present(void *handle, bend_present_rr *rr)
 {
+    struct session_handle *sh = (struct session_handle*) handle;
+    struct result_set *set = get_set(sh, rr->setname);    
+
+    if (!set)
+    {
+        rr->errcode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
+        rr->errstring = odr_strdup(rr->stream, rr->setname);
+        return 0;
+    }
+    do_delay(&set->present_delay);
     return 0;
 }
 
@@ -632,6 +819,7 @@ int ztest_fetch(void *handle, bend_fetch_rr *r)
         r->errstring = odr_strdup(r->stream, r->setname);
         return 0;
     }
+    do_delay(&set->fetch_delay);
     r->last_in_set = 0;
     r->basename = set->db;
     r->output_format = r->request_format;