yaz-ztest: complete database delay arguments
[yaz-moved-to-github.git] / ztest / ztest.c
index 974d2da..6d806b2 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2009 Index Data
+ * Copyright (C) 1995-2010 Index Data
  * See the file LICENSE for details.
  */
 
@@ -8,6 +8,7 @@
  */
 
 #include <stdio.h>
+#include <math.h>
 #include <stdlib.h>
 #include <ctype.h>
 #if HAVE_UNISTD_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;
+};
+
+struct session_handle {
+    struct result_set *result_sets;
+};
+
 int ztest_search(void *handle, bend_search_rr *rr);
 int ztest_sort(void *handle, bend_sort_rr *rr);
 int ztest_present(void *handle, bend_present_rr *rr);
 int ztest_esrequest(void *handle, bend_esrequest_rr *rr);
 int ztest_delete(void *handle, bend_delete_rr *rr);
 
+static struct result_set *get_set(struct session_handle *sh, const char *name)
+{
+    struct result_set *set = sh->result_sets;
+    for (; set; set = set->next)
+        if (!strcmp(name, set->name))
+            return set;
+    return 0;
+}
+
+static void remove_sets(struct session_handle *sh)
+{
+    struct result_set *set = sh->result_sets;
+    while (set)
+    {
+        struct result_set *set_next = set->next;
+        xfree(set->name);
+        xfree(set->db);
+        xfree(set);
+        set = set_next;
+    }
+    sh->result_sets = 0;
+}
+
 /** \brief use term value as hit count 
     \param s RPN structure
     \return >= 0: search term number or -1: not found
@@ -38,9 +81,9 @@ int ztest_delete(void *handle, bend_delete_rr *rr);
     Only terms  that looks a numeric is used.. Returns -1 if
     no sub tree has a hit count term
 */
-static int get_term_hit(Z_RPNStructure *s)
+static Odr_int get_term_hit(Z_RPNStructure *s)
 {
-    int h = -1;
+    Odr_int h = -1;
     switch(s->which)
     {
     case Z_RPNStructure_simple:
@@ -51,7 +94,12 @@ static int get_term_hit(Z_RPNStructure *s)
             {
                 Odr_oct *oct = apt->term->u.general;
                 if (oct->len > 0 && oct->buf[0] >= '0' && oct->buf[0] <= '9')
-                    h = atoi_n((const char *) oct->buf, oct->len);
+                {
+                    WRBUF hits_str = wrbuf_alloc();
+                    wrbuf_write(hits_str, (const char *) oct->buf, oct->len);
+                    h = odr_atoi(wrbuf_cstr(hits_str));
+                    wrbuf_destroy(hits_str);
+                }
             }
         }
         break;
@@ -73,9 +121,9 @@ static int get_term_hit(Z_RPNStructure *s)
     have a way to trigger a certain hit count. Good for testing of
     client applications etc
 */
-static int get_hit_count(Z_Query *q)
+static Odr_int get_hit_count(Z_Query *q)
 {
-    int h = -1;
+    Odr_int h = -1;
     if (q->which == Z_Query_type_1 || q->which == Z_Query_type_101)
         h = get_term_hit(q->u.type_1->RPNStructure);
     if (h == -1)
@@ -83,24 +131,27 @@ static int get_hit_count(Z_Query *q)
     return h;
 }
 
-int ztest_search(void *handle, bend_search_rr *rr)
+/** \brief checks if it's a dummy Slow database
+    \param basename database name to check
+    \param association backend association (or NULL if not available)
+    \retval 1 is slow database
+    \retval 0 is not a slow database
+
+    The Slow database is for testing.. It allows us to simulate
+    a slow server...
+*/
+static int check_slow(const char *basename, bend_association association)
 {
-    if (rr->num_bases != 1)
-    {
-        rr->errcode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
-        return 0;
-    }
-    /* Throw Database unavailable if other than Default or Slow */
-    if (!yaz_matchstr(rr->basenames[0], "Default"))
-        ;  /* Default is OK in our test */
-    else if(!yaz_matchstr(rr->basenames[0], "Slow"))
+    if (strncmp(basename, "Slow", 4) == 0)
     {
 #if HAVE_UNISTD_H
+        int i, w = 3;
+        if (basename[4])
+            sscanf(basename+4, "%d", &w);
         /* wait up to 3 seconds and check if connection is still alive */
-        int i;
-        for (i = 0; i<3; i++)
+        for (i = 0; i < w; i++)
         {
-            if (!bend_assoc_is_alive(rr->association))
+            if (association && !bend_assoc_is_alive(association))
             {
                 yaz_log(YLOG_LOG, "search aborted");
                 break;
@@ -108,6 +159,71 @@ int ztest_search(void *handle, bend_search_rr *rr)
             sleep(1);
         }
 #endif
+        return 1;
+    }
+    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 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);
+    }
+}
+
+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 (strcmp_prefix(db, "Default"))
+        ;  /* Default is OK in our test */
+    else if (strcmp_prefix(db, "db"))
+        ;  /* db.* is OK in our test */
+    else if (check_slow(rr->basenames[0], rr->association))
+    {
         rr->estimated_hit_count = 1;
     }
     else
@@ -117,7 +233,84 @@ int ztest_search(void *handle, bend_search_rr *rr)
         return 0;
     }
 
+    new_set = get_set(sh, rr->setname);    
+    if (new_set)
+    {
+        if (!rr->replace_set)
+        {
+            rr->errcode = YAZ_BIB1_RESULT_SET_EXISTS_AND_REPLACE_INDICATOR_OFF;
+            return 0;
+        }
+        xfree(new_set->db);
+    }
+    else
+    {
+        new_set = xmalloc(sizeof(*new_set));
+        new_set->next = sh->result_sets;
+        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"))
+                srandom(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)
+    {
+        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);
+    }
     rr->hits = get_hit_count(rr->query);
+
+    do_delay(&new_set->search_delay);
+    new_set->hits = rr->hits;
+    
     return 0;
 }
 
@@ -134,10 +327,10 @@ int ztest_esrequest(void *handle, bend_esrequest_rr *rr)
 
     if (rr->esr->packageName)
         yaz_log(log_level, "packagename: %s", rr->esr->packageName);
-    yaz_log(log_level, "Waitaction: %d", *rr->esr->waitAction);
+    yaz_log(log_level, "Waitaction: " ODR_INT_PRINTF, *rr->esr->waitAction);
 
 
-    yaz_log(log_level, "function: %d", *rr->esr->function);
+    yaz_log(log_level, "function: " ODR_INT_PRINTF, *rr->esr->function);
 
     if (!rr->esr->taskSpecificParameters)
     {
@@ -172,7 +365,7 @@ int ztest_esrequest(void *handle, bend_esrequest_rr *rr)
             {
                 yaz_log(log_level, "resultsetItem");
                 yaz_log(log_level, "setId: %s", n->resultSetItem->resultSetId);
-                yaz_log(log_level, "item: %d", *n->resultSetItem->item);
+                yaz_log(log_level, "item: " ODR_INT_PRINTF, *n->resultSetItem->item);
             }
             if (n->itemRequest)
             {
@@ -254,7 +447,8 @@ int ztest_esrequest(void *handle, bend_esrequest_rr *rr)
                 }
                 if (item_req)
                 {
-                    yaz_log(log_level, "ILL protocol version = %d",
+                    yaz_log(log_level, "ILL protocol version = "
+                            ODR_INT_PRINTF,
                             *item_req->protocol_version_num);
                 }
             }
@@ -345,7 +539,8 @@ int ztest_esrequest(void *handle, bend_esrequest_rr *rr)
                     yaz_log(log_level, " specialUpdate");
                     break;
                 default:
-                    yaz_log(log_level, " unknown (%d)", *toKeep->action);
+                    yaz_log(log_level, " unknown (" ODR_INT_PRINTF ")",
+                            *toKeep->action);
                 }
             }
             if (toKeep->databaseName)
@@ -402,8 +597,7 @@ int ztest_esrequest(void *handle, bend_esrequest_rr *rr)
                 ext->u.update->u.taskPackage->originPart = keep;
                 ext->u.update->u.taskPackage->targetPart = targetPart;
 
-                keep->action = (int *) odr_malloc(rr->stream, sizeof(int));
-                *keep->action = *toKeep->action;
+                keep->action = odr_intdup(rr->stream, *toKeep->action);
                 keep->databaseName =
                     odr_strdup(rr->stream, toKeep->databaseName);
                 keep->schema = 0;
@@ -505,25 +699,50 @@ 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;
 }
 
 /* retrieval of a single record (present, and piggy back search) */
 int ztest_fetch(void *handle, bend_fetch_rr *r)
 {
+    struct session_handle *sh = (struct session_handle*) handle;
     char *cp;
     const Odr_oid *oid = r->request_format;
+    struct result_set *set = get_set(sh, r->setname);    
 
+    if (!set)
+    {
+        r->errcode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
+        r->errstring = odr_strdup(r->stream, r->setname);
+        return 0;
+    }
+    do_delay(&set->fetch_delay);
     r->last_in_set = 0;
-    r->basename = "Default";
+    r->basename = set->db;
     r->output_format = r->request_format;
 
+    if (r->number < 1 || r->number > set->hits)
+    {
+        r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
+        return 0;
+    }
     if (!oid || yaz_oid_is_iso2709(oid))
     {
         cp = dummy_marc_record(r->number, r->stream);
         if (!cp)
         {
-            r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
+            r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
+            r->surrogate_flag = 1;
             return 0;
         }
         else
@@ -538,7 +757,8 @@ int ztest_fetch(void *handle, bend_fetch_rr *r)
         cp = dummy_marc_record(r->number, r->stream);
         if (!cp)
         {
-            r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
+            r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
+            r->surrogate_flag = 1;
             return 0;
         }
         r->record = (char *) dummy_opac(r->number, r->stream, cp);
@@ -561,7 +781,8 @@ int ztest_fetch(void *handle, bend_fetch_rr *r)
         r->record = (char*) dummy_grs_record(r->number, r->stream);
         if (!r->record)
         {
-            r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
+            r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
+            r->surrogate_flag = 1;
             return 0;
         }
     }
@@ -575,7 +796,8 @@ int ztest_fetch(void *handle, bend_fetch_rr *r)
         f = fopen(fname, "rb");
         if (!f)
         {
-            r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
+            r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
+            r->surrogate_flag = 1;
             return 0;
         }
         fseek(f, 0L, SEEK_END);
@@ -583,6 +805,7 @@ int ztest_fetch(void *handle, bend_fetch_rr *r)
         if (size <= 0 || size >= 5000000)
         {
             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
+            r->surrogate_flag = 1;
         }
         fseek(f, 0L, SEEK_SET);
         r->record = (char*) odr_malloc(r->stream, size);
@@ -590,6 +813,7 @@ int ztest_fetch(void *handle, bend_fetch_rr *r)
         if (fread(r->record, size, 1, f) != 1)
         {
             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
+            r->surrogate_flag = 1;
         }
         fclose(f);
     }
@@ -602,7 +826,7 @@ int ztest_fetch(void *handle, bend_fetch_rr *r)
         }
         else 
         {
-            r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
+            r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
             r->surrogate_flag = 1;
             return 0;
         }
@@ -635,13 +859,8 @@ int ztest_scan(void *handle, bend_scan_rr *q)
     /* Throw Database unavailable if other than Default or Slow */
     if (!yaz_matchstr(q->basenames[0], "Default"))
         ;  /* Default is OK in our test */
-    else if(!yaz_matchstr(q->basenames[0], "Slow"))
-    {
-#if HAVE_UNISTD_H
-        sleep(3);
-#endif
+    else if (check_slow(q->basenames[0], 0 /* no assoc for scan */))
         ;
-    }
     else
     {
         q->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
@@ -677,7 +896,7 @@ int ztest_scan(void *handle, bend_scan_rr *q)
             return 0;
         }
         len = q->term->term->u.general->len;
-        if (len >= sizeof(term))
+        if (len >= (int ) sizeof(term))
             len = sizeof(term)-1;
         memcpy(term, q->term->term->u.general->buf, len);
         term[len] = '\0';
@@ -768,7 +987,9 @@ bend_initresult *bend_init(bend_initrequest *q)
 {
     bend_initresult *r = (bend_initresult *)
         odr_malloc(q->stream, sizeof(*r));
-    int *counter = (int *) xmalloc(sizeof(int));
+    struct session_handle *sh = xmalloc(sizeof(*sh));
+
+    sh->result_sets = 0;
 
     if (!log_level_set)
     {
@@ -776,10 +997,9 @@ bend_initresult *bend_init(bend_initrequest *q)
         log_level_set=1;
     }
 
-    *counter = 0;
     r->errcode = 0;
     r->errstring = 0;
-    r->handle = counter;         /* user handle, in this case a simple int */
+    r->handle = sh;                         /* tell GFS about our handle */
     q->bend_sort = ztest_sort;              /* register sort handler */
     q->bend_search = ztest_search;          /* register search handler */
     q->bend_present = ztest_present;        /* register present handle */
@@ -801,7 +1021,9 @@ bend_initresult *bend_init(bend_initrequest *q)
 
 void bend_close(void *handle)
 {
-    xfree(handle);              /* release our user-defined handle */
+    struct session_handle *sh = (struct session_handle*) handle;
+    remove_sets(sh);
+    xfree(sh);              /* release our session */
     return;
 }