Mention that @mask requires YAZ 4.2.58
[yaz-moved-to-github.git] / ztest / ztest.c
index 460a2eb..d92d215 100644 (file)
@@ -89,13 +89,14 @@ static void remove_sets(struct session_handle *sh)
 
 /** \brief use term value as hit count
     \param s RPN structure
+    \param hash value for compuation
     \return >= 0: search term number or -1: not found
 
     Traverse RPN tree 'in order' and use term value as hit count.
     Only terms  that looks a numeric is used.. Returns -1 if
     no sub tree has a hit count term
 */
-static Odr_int get_term_hit(Z_RPNStructure *s)
+static Odr_int get_term_hit(Z_RPNStructure *s, unsigned *hash)
 {
     Odr_int h = -1;
     switch(s->which)
@@ -114,13 +115,19 @@ static Odr_int get_term_hit(Z_RPNStructure *s)
                     h = odr_atoi(wrbuf_cstr(hits_str));
                     wrbuf_destroy(hits_str);
                 }
+                else
+                {
+                    int i;
+                    for (i = 0; i < oct->len; i++)
+                        *hash = *hash * 65509 + oct->buf[i];
+                }
             }
         }
         break;
     case Z_RPNStructure_complex:
-        h = get_term_hit(s->u.complex->s1);
+        h = get_term_hit(s->u.complex->s1, hash);
         if (h == -1)
-            h = get_term_hit(s->u.complex->s2);
+            h = get_term_hit(s->u.complex->s2, hash);
         break;
     }
     return h;
@@ -128,7 +135,7 @@ static Odr_int get_term_hit(Z_RPNStructure *s)
 
 /** \brief gets hit count for numeric terms in RPN queries
     \param q RPN Query
-    \return number of hits (random or number for term)
+    \return number of hits
 
     This is just for testing.. A real database of course uses
     the content of a database to establish a value.. In our case, we
@@ -139,12 +146,23 @@ static Odr_int get_hit_count(Z_Query *q)
 {
     if (q->which == Z_Query_type_1 || q->which == Z_Query_type_101)
     {
+        unsigned hash = 0;
         Odr_int h = -1;
-        h = get_term_hit(q->u.type_1->RPNStructure);
+        h = get_term_hit(q->u.type_1->RPNStructure, &hash);
         if (h == -1)
-            h = rand() % 24;
+            h = hash % 24;
         return h;
     }
+    else if (q->which == Z_Query_type_104 &&
+             q->u.type_104->which == Z_External_CQL)
+    {
+        unsigned hash = 0;
+        const char *cql = q->u.type_104->u.cql;
+        int i;
+        for (i = 0; cql[i]; i++)
+            hash = hash * 65509 + cql[i];
+        return hash % 24;
+    }
     else
         return 24;
 }
@@ -292,6 +310,34 @@ Z_OtherInformation *build_facet_response(ODR odr, Z_FacetList *facet_list) {
     return 0;
 }
 
+static void echo_extra_args(ODR stream,
+                            Z_SRW_extra_arg *extra_args, char **extra_response)
+{
+    if (extra_args)
+    {
+        Z_SRW_extra_arg *a;
+        WRBUF response_xml = wrbuf_alloc();
+        wrbuf_puts(response_xml, "<extra>");
+        for (a = extra_args; a; a = a->next)
+        {
+            wrbuf_puts(response_xml, "<extra name=\"");
+            wrbuf_xmlputs(response_xml, a->name);
+            wrbuf_puts(response_xml, "\"");
+            if (a->value)
+            {
+                wrbuf_puts(response_xml, " value=\"");
+                wrbuf_xmlputs(response_xml, a->value);
+                wrbuf_puts(response_xml, "\"");
+            }
+            wrbuf_puts(response_xml, "/>");
+        }
+        wrbuf_puts(response_xml, "</extra>");
+        *extra_response = odr_strdup(stream, wrbuf_cstr(response_xml));
+        wrbuf_destroy(response_xml);
+    }
+
+}
+
 int ztest_search(void *handle, bend_search_rr *rr)
 {
     struct session_handle *sh = (struct session_handle*) handle;
@@ -372,29 +418,7 @@ int ztest_search(void *handle, bend_search_rr *rr)
         }
     }
 
-    if (rr->extra_args)
-    {
-        Z_SRW_extra_arg *a;
-        WRBUF response_xml = wrbuf_alloc();
-        wrbuf_puts(response_xml, "<extra>");
-        for (a = rr->extra_args; a; a = a->next)
-        {
-            wrbuf_puts(response_xml, "<extra name=\"");
-            wrbuf_xmlputs(response_xml, a->name);
-            wrbuf_puts(response_xml, "\"");
-            if (a->value)
-            {
-                wrbuf_puts(response_xml, " value=\"");
-                wrbuf_xmlputs(response_xml, a->value);
-                wrbuf_puts(response_xml, "\"");
-            }
-            wrbuf_puts(response_xml, "/>");
-        }
-        wrbuf_puts(response_xml, "</extra>");
-        rr->extra_response_data =
-            odr_strdup(rr->stream, wrbuf_cstr(response_xml));
-        wrbuf_destroy(response_xml);
-    }
+    echo_extra_args(rr->stream, rr->extra_args, &rr->extra_response_data);
     rr->hits = get_hit_count(rr->query);
 
     if (1)
@@ -812,6 +836,7 @@ int ztest_fetch(void *handle, bend_fetch_rr *r)
     char *cp;
     const Odr_oid *oid = r->request_format;
     struct result_set *set = get_set(sh, r->setname);
+    const char *esn = yaz_get_esn(r->comp);
 
     if (!set)
     {
@@ -912,7 +937,7 @@ int ztest_fetch(void *handle, bend_fetch_rr *r)
     }
     else if (!oid_oidcmp(oid, yaz_oid_recsyn_xml))
     {
-        if ((cp = dummy_xml_record(r->number, r->stream)))
+        if ((cp = dummy_xml_record(r->number, r->stream, esn)))
         {
             r->len = strlen(cp);
             r->record = cp;
@@ -1051,6 +1076,7 @@ int ztest_scan(void *handle, bend_scan_rr *q)
         if (q->num_entries >= num_entries_req)
             break;
     }
+    echo_extra_args(q->stream, q->extra_args, &q->extra_response_data);
     if (feof(f))
         q->status = BEND_SCAN_PARTIAL;
     return 0;