X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=ztest%2Fztest.c;h=cc7736d5862bab96fc0e65048387fa81410cd73b;hp=c9d4af0fdc2bc512de806c88a35d37fbf4993ffe;hb=51e1ca7946289042c3c09e92afc1c1c465f5d870;hpb=62a5a33362d50820f3ac509fa1412e946d576cff diff --git a/ztest/ztest.c b/ztest/ztest.c index c9d4af0..cc7736d 100644 --- a/ztest/ztest.c +++ b/ztest/ztest.c @@ -1,16 +1,18 @@ /* This file is part of the YAZ toolkit. - * Copyright (C) 1995-2010 Index Data + * Copyright (C) Index Data * See the file LICENSE for details. */ - /** \file * \brief yaz-ztest Generic Frontend Server */ +#if HAVE_CONFIG_H +#include +#endif + #include #include #include -#include #if HAVE_SYS_TIME_H #include @@ -31,6 +33,7 @@ #include #include #include +#include #include "ztest.h" @@ -85,15 +88,16 @@ static void remove_sets(struct session_handle *sh) sh->result_sets = 0; } -/** \brief use term value as hit count +/** \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) @@ -112,13 +116,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; @@ -126,8 +136,8 @@ 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 have a way to trigger a certain hit count. Good for testing of @@ -137,12 +147,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; } @@ -210,8 +231,8 @@ static void ztest_sleep(double d) Sleep( (DWORD) (d * 1000)); #else struct timeval tv; - tv.tv_sec = floor(d); - tv.tv_usec = (d - floor(d)) * 1000000; + tv.tv_sec = d; + tv.tv_usec = (d - (long) d) * 1000000; select(0, 0, 0, 0, &tv); #endif } @@ -228,19 +249,20 @@ static void do_delay(const struct delay *delayp) } } -static void addterms(ODR odr, Z_FacetField *facet_field, const char *facet_name) { +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]; + char *key = odr_malloc(odr, length); key[0] = '\0'; - for (index = 0; index < facet_field->num_terms; index++) { - Z_Term *term; + for (index = 0; index < facet_field->num_terms; index++) + { 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); + + facet_term = facet_term_create_cstr(odr, key, freq); freq = freq - 10 ; facet_field_term_set(odr, facet_field, facet_term, index); } @@ -251,14 +273,19 @@ Z_OtherInformation *build_facet_response(ODR odr, Z_FacetList *facet_list) { 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); + struct yaz_facet_attr attrvalues; + yaz_facet_attr_init(&attrvalues); attrvalues.limit = 10; - facetattrs(facet_list->elements[index]->attributes, &attrvalues); - yaz_log(YLOG_LOG, "Attributes: %s %d ", attrvalues.useattr, attrvalues.limit); + yaz_facet_attr_get_z_attributes(facet_list->elements[index]->attributes, + &attrvalues); + yaz_log(YLOG_LOG, "Attributes: %s limit=%d start=%d sort=%d", + attrvalues.useattr ? attrvalues.useattr : "NONE", + attrvalues.limit, + attrvalues.start, + attrvalues.sortorder); if (attrvalues.errstring) yaz_log(YLOG_LOG, "Error parsing attributes: %s", attrvalues.errstring); - if (attrvalues.limit > 0) { + if (attrvalues.limit > 0 && attrvalues.useattr) { 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++; @@ -278,6 +305,8 @@ Z_OtherInformation *build_facet_response(ODR odr, Z_FacetList *facet_list) { 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->descriptor = 0; + oiu->information.externallyDefinedInfo->indirect_reference = 0; oiu->information.externallyDefinedInfo->which = Z_External_userFacets; oiu->information.externallyDefinedInfo->u.facetList = new_list; oi->list[0] = oiu; @@ -286,6 +315,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, ""); + for (a = extra_args; a; a = a->next) + { + wrbuf_puts(response_xml, "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_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; @@ -297,7 +354,7 @@ int ztest_search(void *handle, bend_search_rr *rr) rr->errcode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP; return 0; } - + db = rr->basenames[0]; /* Allow Default, db.* and Slow */ @@ -316,7 +373,7 @@ int ztest_search(void *handle, bend_search_rr *rr) return 0; } - new_set = get_set(sh, rr->setname); + new_set = get_set(sh, rr->setname); if (new_set) { if (!rr->replace_set) @@ -366,36 +423,12 @@ 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, ""); - for (a = rr->extra_args; a; a = a->next) - { - wrbuf_puts(response_xml, "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, ""); - 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) { - /* TODO Not general. Only handles one (Facet) OtherInformation. Overwrite */ - Z_FacetList *facet_list = extract_facet_request(rr->stream, rr->search_input); - + Z_FacetList *facet_list = yaz_oi_get_facetlist(&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); @@ -406,7 +439,7 @@ int ztest_search(void *handle, bend_search_rr *rr) } do_delay(&new_set->search_delay); new_set->hits = rr->hits; - + return 0; } @@ -414,13 +447,6 @@ int ztest_search(void *handle, bend_search_rr *rr) /* this huge function handles extended services */ int ztest_esrequest(void *handle, bend_esrequest_rr *rr) { - /* user-defined handle - created in bend_init */ - int *counter = (int*) handle; - - yaz_log(log_level, "ESRequest no %d", *counter); - - (*counter)++; - if (rr->esr->packageName) yaz_log(log_level, "packagename: %s", rr->esr->packageName); yaz_log(log_level, "Waitaction: " ODR_INT_PRINTF, *rr->esr->waitAction); @@ -442,7 +468,7 @@ int ztest_esrequest(void *handle, bend_esrequest_rr *rr) Z_IOOriginPartToKeep *k = ir->toKeep; Z_IOOriginPartNotToKeep *n = ir->notToKeep; const char *xml_in_response = 0; - + if (k && k->contact) { if (k->contact->name) @@ -456,7 +482,7 @@ int ztest_esrequest(void *handle, bend_esrequest_rr *rr) { yaz_log(log_level, "Billing info (not shown)"); } - + if (n->resultSetItem) { yaz_log(log_level, "resultsetItem"); @@ -472,7 +498,7 @@ int ztest_esrequest(void *handle, bend_esrequest_rr *rr) { char oid_name_str[OID_STR_MAX]; oid_class oclass; - const char *oid_name = + const char *oid_name = yaz_oid_to_string_buf(r->direct_reference, &oclass, oid_name_str); if (oid_name) @@ -483,10 +509,10 @@ int ztest_esrequest(void *handle, bend_esrequest_rr *rr) if (r->which == Z_External_octet) yaz_log(log_level, "%.*s", r->u.octet_aligned->len, - r->u.octet_aligned->buf); + r->u.octet_aligned->buf); xml_in_response = "x"; } - if (!oid_oidcmp(r->direct_reference, + if (!oid_oidcmp(r->direct_reference, yaz_oid_general_isoill_1)) { yaz_log(log_level, "Decode ItemRequest begin"); @@ -495,7 +521,7 @@ int ztest_esrequest(void *handle, bend_esrequest_rr *rr) odr_setbuf(rr->decode, (char *) r->u.single_ASN1_type->buf, r->u.single_ASN1_type->len, 0); - + if (!ill_ItemRequest(rr->decode, &item_req, 0, 0)) { yaz_log(log_level, @@ -518,7 +544,7 @@ int ztest_esrequest(void *handle, bend_esrequest_rr *rr) odr_setbuf(rr->decode, (char*) r->u.single_ASN1_type->buf, r->u.single_ASN1_type->len, 0); - + if (!ill_APDU(rr->decode, &ill_apdu, 0, 0)) { yaz_log(log_level, @@ -567,13 +593,8 @@ int ztest_esrequest(void *handle, bend_esrequest_rr *rr) rr->taskPackage->retentionTime = 0; rr->taskPackage->permissions = 0; rr->taskPackage->description = 0; - rr->taskPackage->targetReference = (Odr_oct *) - odr_malloc(rr->stream, sizeof(Odr_oct)); - rr->taskPackage->targetReference->buf = - (unsigned char *) odr_strdup(rr->stream, "911"); - rr->taskPackage->targetReference->len = - rr->taskPackage->targetReference->size = - strlen((char *) (rr->taskPackage->targetReference->buf)); + rr->taskPackage->targetReference = + odr_create_Odr_oct(rr->stream, "911", 3); rr->taskPackage->creationDateTime = 0; rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0); rr->taskPackage->packageDiagnostics = 0; @@ -598,7 +619,7 @@ int ztest_esrequest(void *handle, bend_esrequest_rr *rr) strlen(xml_in_response)); else targetPart->itemRequest = 0; - + targetPart->statusOrErrorReport = 0; targetPart->auxiliaryStatus = 0; } @@ -613,7 +634,7 @@ int ztest_esrequest(void *handle, bend_esrequest_rr *rr) Z_IUUpdateEsRequest *esRequest = up->u.esRequest; Z_IUOriginPartToKeep *toKeep = esRequest->toKeep; Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep; - + yaz_log(log_level, "action"); if (toKeep->action) { @@ -668,13 +689,8 @@ int ztest_esrequest(void *handle, bend_esrequest_rr *rr) rr->taskPackage->retentionTime = 0; rr->taskPackage->permissions = 0; rr->taskPackage->description = 0; - rr->taskPackage->targetReference = (Odr_oct *) - odr_malloc(rr->stream, sizeof(Odr_oct)); - rr->taskPackage->targetReference->buf = - (unsigned char *) odr_strdup(rr->stream, "123"); - rr->taskPackage->targetReference->len = - rr->taskPackage->targetReference->size = - strlen((char *) (rr->taskPackage->targetReference->buf)); + rr->taskPackage->targetReference = + odr_create_Odr_oct(rr->stream, "123", 3); rr->taskPackage->creationDateTime = 0; rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0); rr->taskPackage->packageDiagnostics = 0; @@ -704,7 +720,7 @@ int ztest_esrequest(void *handle, bend_esrequest_rr *rr) targetPart->num_globalDiagnostics = 0; targetPart->globalDiagnostics = (Z_DiagRec **) odr_nullval(); targetPart->num_taskPackageRecords = 1; - targetPart->taskPackageRecords = + targetPart->taskPackageRecords = (Z_IUTaskPackageRecordStructure **) odr_malloc(rr->stream, sizeof(Z_IUTaskPackageRecordStructure *)); @@ -712,15 +728,15 @@ int ztest_esrequest(void *handle, bend_esrequest_rr *rr) (Z_IUTaskPackageRecordStructure *) odr_malloc(rr->stream, sizeof(Z_IUTaskPackageRecordStructure)); - + targetPart->taskPackageRecords[0]->which = Z_IUTaskPackageRecordStructure_record; - targetPart->taskPackageRecords[0]->u.record = + targetPart->taskPackageRecords[0]->u.record = z_ext_record_sutrs(rr->stream, "test", 4); - targetPart->taskPackageRecords[0]->correlationInfo = 0; + targetPart->taskPackageRecords[0]->correlationInfo = 0; targetPart->taskPackageRecords[0]->recordStatus = odr_intdup(rr->stream, - Z_IUTaskPackageRecordStructure_success); + Z_IUTaskPackageRecordStructure_success); targetPart->taskPackageRecords[0]->num_supplementalDiagnostics = 0; @@ -736,7 +752,7 @@ int ztest_esrequest(void *handle, bend_esrequest_rr *rr) if (rec->direct_reference) { char oid_name_str[OID_STR_MAX]; - const char *oid_name + const char *oid_name = oid_name = yaz_oid_to_string_buf( rec->direct_reference, 0, oid_name_str); @@ -796,7 +812,7 @@ int ztest_sort(void *handle, bend_sort_rr *rr) 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); + struct result_set *set = get_set(sh, rr->setname); if (!set) { @@ -814,7 +830,8 @@ 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); + struct result_set *set = get_set(sh, r->setname); + const char *esn = yaz_get_esn(r->comp); if (!set) { @@ -864,7 +881,7 @@ int ztest_fetch(void *handle, bend_fetch_rr *r) { /* this section returns a small record */ char buf[100]; - + sprintf(buf, "This is dummy SUTRS record number %d\n", r->number); r->len = strlen(buf); @@ -915,12 +932,28 @@ 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; + r->schema = "info:srw/schema/1/marcxml-1.1"; } - else + else + { + r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS; + r->surrogate_flag = 1; + return 0; + } + } + else if (!oid_oidcmp(oid, yaz_oid_recsyn_json)) + { + if ((cp = dummy_json_record(r->number, r->stream, esn))) + { + r->len = strlen(cp); + r->record = cp; + r->schema = "info:srw/schema/1/marcxml-1.1"; + } + else { r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS; r->surrogate_flag = 1; @@ -1006,8 +1039,8 @@ int ztest_scan(void *handle, bend_scan_rr *q) strcpy(term, "0"); for (p = term; *p; p++) - if (islower(*(unsigned char *) p)) - *p = toupper(*p); + if (yaz_islower(*p)) + *p = yaz_toupper(*p); fseek(f, 0, SEEK_SET); q->num_entries = 0; @@ -1054,6 +1087,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; @@ -1125,6 +1159,8 @@ void bend_close(void *handle) int main(int argc, char **argv) { + yaz_enable_panic_backtrace(argv[0]); + return statserv_main(argc, argv, bend_init, bend_close); } /*