X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=ztest%2Fztest.c;h=0a58c958e693794a2a13fa64bad7a0543e95abd5;hp=a860296e72bade7f4b8d43d2da9b2eb7740f490b;hb=ca1469bd104ff54c175f0ad25afc4cbf6d948551;hpb=d180c9a6dfd641974a84e5107fc759f57e7b2d63 diff --git a/ztest/ztest.c b/ztest/ztest.c index a860296..0a58c95 100644 --- a/ztest/ztest.c +++ b/ztest/ztest.c @@ -11,14 +11,26 @@ #include #include #include + +#if HAVE_SYS_TIME_H +#include +#endif #if HAVE_UNISTD_H #include #endif +#if HAVE_SYS_SELECT_H +#include +#endif +#ifdef WIN32 +#include +#endif #include #include #include #include +#include +#include #include "ztest.h" @@ -123,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 @@ -179,18 +195,69 @@ static void init_delay(struct delay *delayp) static int parse_delay(struct delay *delayp, const char *value) { - delayp->d1 = atof(value); - delayp->d2 = 0.0; + 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) +static void ztest_sleep(double d) { +#ifdef WIN32 + Sleep( (DWORD) (d * 1000)); +#else struct timeval tv; - - tv.tv_sec = floor(delayp->d1); - tv.tv_usec = (delayp->d1 - floor(delayp->d1)) * 1000000; + 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) { + int index; + int freq = 100; + const char *key = "key"; + for (index = 0; index < facet_field->num_terms; index++) { + Z_FacetTerm *facet_term = facet_term_create(odr, term_create(odr, key), 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; + 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, facet_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 = facet_list; + oi->list[0] = oiu; + return oi; } int ztest_search(void *handle, bend_search_rr *rr) @@ -244,6 +311,7 @@ int ztest_search(void *handle, bend_search_rr *rr) 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) @@ -257,7 +325,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")) @@ -297,6 +365,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; @@ -688,6 +764,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; } @@ -705,6 +791,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;