X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=ztest%2Fztest.c;h=1a0579f5e227f0d0989027b0087ace92ce6c289c;hp=a6b71d270069ea5ace3f99586ac7310d66c13f4f;hb=ec7571c09ddbe4580775d3a8a60271a657ae02a6;hpb=fb6d99a0c7e07d9cc4a315c447deaf6564a85505 diff --git a/ztest/ztest.c b/ztest/ztest.c index a6b71d2..1a0579f 100644 --- a/ztest/ztest.c +++ b/ztest/ztest.c @@ -1,58 +1,123 @@ -/* - * Copyright (C) 1995-2005, Index Data ApS +/* This file is part of the YAZ toolkit. + * Copyright (C) 1995-2008 Index Data * See the file LICENSE for details. - * - * $Id: ztest.c,v 1.75 2005-06-25 15:46:09 adam Exp $ */ -/* - * Demonstration of simple server +/** \file + * \brief Demonstration of server */ #include #include #include +#if HAVE_UNISTD_H +#include +#endif -#include +#include #include #include +#include + +#include "ztest.h" static int log_level=0; static int log_level_set=0; -Z_GenericRecord *dummy_grs_record (int num, ODR o); -char *dummy_marc_record (int num, ODR odr); -char *dummy_xml_record (int num, ODR odr); - 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); +/** \brief use term value as hit count + \param s RPN structure + \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 int get_term_hit(Z_RPNStructure *s) +{ + int h = -1; + switch(s->which) + { + case Z_RPNStructure_simple: + if (s->u.simple->which == Z_Operand_APT) + { + Z_AttributesPlusTerm *apt = s->u.simple->u.attributesPlusTerm; + if (apt->term->which == Z_Term_general) + { + 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); + } + } + break; + case Z_RPNStructure_complex: + h = get_term_hit(s->u.complex->s1); + if (h == -1) + h = get_term_hit(s->u.complex->s2); + break; + } + return h; +} + +/** \brief gets hit count for numeric terms in RPN queries + \param q RPN Query + \return number of hits (random or number for term) + + 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 + client applications etc +*/ +static int get_hit_count(Z_Query *q) +{ + 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) + h = rand() % 24; + return h; +} + int ztest_search(void *handle, bend_search_rr *rr) { if (rr->num_bases != 1) { - rr->errcode = 23; + rr->errcode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP; return 0; } -#if NMEM_DEBUG - /* if database is stop, stop this process.. For debugging only. */ - if (!yaz_matchstr (rr->basenames[0], "stop")) + /* 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")) { - nmem_print_list_l(YLOG_LOG); - exit(0); - } +#if HAVE_UNISTD_H + /* wait up to 3 seconds and check if connection is still alive */ + int i; + for (i = 0; i<3; i++) + { + if (!bend_assoc_is_alive(rr->association)) + { + yaz_log(YLOG_LOG, "search aborted"); + break; + } + sleep(1); + } #endif - /* Throw Database unavailable if other than Default */ - if (yaz_matchstr (rr->basenames[0], "Default")) + rr->estimated_hit_count = 1; + } + else { - rr->errcode = 109; + rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE; rr->errstring = rr->basenames[0]; return 0; } - rr->hits = rand() % 24; + + rr->hits = get_hit_count(rr->query); return 0; } @@ -87,6 +152,7 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr) Z_IORequest *ir = it->u.esRequest; Z_IOOriginPartToKeep *k = ir->toKeep; Z_IOOriginPartNotToKeep *n = ir->notToKeep; + const char *xml_in_response = 0; if (k && k->contact) { @@ -115,17 +181,24 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr) ILL_APDU *ill_apdu = 0; if (r->direct_reference) { - oident *ent = oid_getentbyoid(r->direct_reference); - if (ent) - yaz_log(log_level, "OID %s", ent->desc); - if (ent && ent->value == VAL_TEXT_XML) + char oid_name_str[OID_STR_MAX]; + oid_class oclass; + const char *oid_name = + yaz_oid_to_string_buf(r->direct_reference, + &oclass, oid_name_str); + if (oid_name) + yaz_log(log_level, "OID %s", oid_name); + if (!oid_oidcmp(r->direct_reference, yaz_oid_recsyn_xml)) { yaz_log (log_level, "ILL XML request"); if (r->which == Z_External_octet) - yaz_log (log_level, "%.*s", r->u.octet_aligned->len, + yaz_log (log_level, "%.*s", + r->u.octet_aligned->len, r->u.octet_aligned->buf); + xml_in_response = "x"; } - if (ent && ent->value == VAL_ISO_ILL_1) + if (!oid_oidcmp(r->direct_reference, + yaz_oid_general_isoill_1)) { yaz_log (log_level, "Decode ItemRequest begin"); if (r->which == ODR_EXTERNAL_single) @@ -137,9 +210,9 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr) if (!ill_ItemRequest (rr->decode, &item_req, 0, 0)) { yaz_log (log_level, - "Couldn't decode ItemRequest %s near %d", + "Couldn't decode ItemRequest %s near %ld", odr_errmsg(odr_geterror(rr->decode)), - odr_offset(rr->decode)); + (long) odr_offset(rr->decode)); } else yaz_log(log_level, "Decode ItemRequest OK"); @@ -160,9 +233,9 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr) if (!ill_APDU (rr->decode, &ill_apdu, 0, 0)) { yaz_log (log_level, - "Couldn't decode ILL APDU %s near %d", + "Couldn't decode ILL APDU %s near %ld", odr_errmsg(odr_geterror(rr->decode)), - odr_offset(rr->decode)); + (long) odr_offset(rr->decode)); yaz_log(log_level, "PDU dump:"); odr_dumpBER(yaz_log_file(), (char *) r->u.single_ASN1_type->buf, @@ -229,7 +302,13 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr) ext->u.itemOrder->u.taskPackage->originPart = k; ext->u.itemOrder->u.taskPackage->targetPart = targetPart; - targetPart->itemRequest = 0; + if (xml_in_response) + targetPart->itemRequest = + z_ext_record_xml(rr->stream, xml_in_response, + strlen(xml_in_response)); + else + targetPart->itemRequest = 0; + targetPart->statusOrErrorReport = 0; targetPart->auxiliaryStatus = 0; } @@ -274,7 +353,7 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr) yaz_log (log_level, "database: %s", toKeep->databaseName); if (!strcmp(toKeep->databaseName, "fault")) { - rr->errcode = 109; + rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE; rr->errstring = toKeep->databaseName; } if (!strcmp(toKeep->databaseName, "accept")) @@ -347,7 +426,7 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr) targetPart->taskPackageRecords[0]->which = Z_IUTaskPackageRecordStructure_record; targetPart->taskPackageRecords[0]->u.record = - z_ext_record (rr->stream, VAL_SUTRS, "test", 4); + z_ext_record_sutrs(rr->stream, "test", 4); targetPart->taskPackageRecords[0]->correlationInfo = 0; targetPart->taskPackageRecords[0]->recordStatus = odr_intdup (rr->stream, @@ -366,11 +445,14 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr) if (rec->direct_reference) { - struct oident *oident; - oident = oid_getentbyoid(rec->direct_reference); - if (oident) + char oid_name_str[OID_STR_MAX]; + const char *oid_name + = oid_name = yaz_oid_to_string_buf( + rec->direct_reference, 0, + oid_name_str); + if (oid_name) yaz_log (log_level, "record %d type %s", i, - oident->desc); + oid_name); } switch (rec->which) { @@ -398,16 +480,6 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr) } } } - else if (rr->esr->taskSpecificParameters->which == Z_External_update0) - { - yaz_log(log_level, "Received DB Update (version 0)"); - } - else - { - yaz_log (YLOG_WARN, "Unknown Extended Service(%d)", - rr->esr->taskSpecificParameters->which); - - } return 0; } @@ -440,11 +512,84 @@ int ztest_present (void *handle, bend_present_rr *rr) int ztest_fetch(void *handle, bend_fetch_rr *r) { char *cp; + const Odr_oid *oid = r->request_format; r->last_in_set = 0; r->basename = "Default"; - r->output_format = r->request_format; - if (r->request_format == VAL_SUTRS) + r->output_format = r->request_format; + + 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; + return 0; + } + else + { + r->len = strlen(cp); + r->record = cp; + r->output_format = odr_oiddup(r->stream, yaz_oid_recsyn_usmarc); + } + } + else if (!oid_oidcmp(oid, yaz_oid_recsyn_opac)) + { + Z_OPACRecord *rec; + int i; + cp = dummy_marc_record(r->number, r->stream); + if (!cp) + { + r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE; + return 0; + } + rec = odr_malloc(r->stream, sizeof(*rec)); + rec->bibliographicRecord = + z_ext_record_usmarc(r->stream, cp, strlen(cp)); + rec->num_holdingsData = 1; + rec->holdingsData = odr_malloc(r->stream, sizeof(*rec->holdingsData)); + for (i = 0; i < rec->num_holdingsData; i++) + { + Z_HoldingsRecord *hr = odr_malloc(r->stream, sizeof(*hr)); + Z_HoldingsAndCircData *hc = odr_malloc(r->stream, sizeof(*hc)); + + rec->holdingsData[i] = hr; + hr->which = Z_HoldingsRecord_holdingsAndCirc; + hr->u.holdingsAndCirc = hc; + + hc->typeOfRecord = odr_strdup(r->stream, "x"); + hc->typeOfRecord[0] = cp[5]; /* LDR 6 */ + + hc->encodingLevel = odr_strdup(r->stream, "x"); + hc->encodingLevel[0] = cp[16]; /* LDR 17 */ + + hc->format = 0; /* OPT */ + hc->receiptAcqStatus = 0; /* OPT */ + hc->generalRetention = 0; /* OPT */ + hc->completeness = 0; /* OPT */ + hc->dateOfReport = 0; /* OPT */ + hc->nucCode = 0; /* OPT */ + hc->localLocation = 0; /* OPT */ + hc->shelvingLocation = 0; /* OPT */ + hc->callNumber = 0; /* OPT */ + hc->shelvingData = 0; /* OPT */ + hc->copyNumber = 0; /* OPT */ + hc->publicNote = 0; /* OPT */ + hc->reproductionNote = 0; /* OPT */ + hc->termsUseRepro = 0; /* OPT */ + hc->enumAndChron = 0; /* OPT */ + + hc->num_volumes = 0; + hc->volumes = 0; + + hc->num_circulationData = 0; + hc->circulationData = 0; + } + + r->len = -1; + r->record = (char*) rec; + } + else if (!oid_oidcmp(oid, yaz_oid_recsyn_sutrs)) { /* this section returns a small record */ char buf[100]; @@ -455,17 +600,17 @@ int ztest_fetch(void *handle, bend_fetch_rr *r) r->record = (char *) odr_malloc (r->stream, r->len+1); strcpy(r->record, buf); } - else if (r->request_format == VAL_GRS1) + else if (!oid_oidcmp(oid, yaz_oid_recsyn_grs_1)) { r->len = -1; r->record = (char*) dummy_grs_record(r->number, r->stream); if (!r->record) { - r->errcode = 13; + r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE; return 0; } } - else if (r->request_format == VAL_POSTSCRIPT) + else if (!oid_oidcmp(oid, yaz_oid_recsyn_postscript)) { char fname[20]; FILE *f; @@ -475,47 +620,41 @@ int ztest_fetch(void *handle, bend_fetch_rr *r) f = fopen(fname, "rb"); if (!f) { - r->errcode = 13; + r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE; return 0; } fseek (f, 0L, SEEK_END); size = ftell (f); if (size <= 0 || size >= 5000000) { - r->errcode = 14; + r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS; return 0; } fseek (f, 0L, SEEK_SET); r->record = (char*) odr_malloc (r->stream, size); r->len = size; - r->output_format = VAL_POSTSCRIPT; fread (r->record, size, 1, f); fclose (f); } - else if (r->request_format == VAL_TEXT_XML) + else if (!oid_oidcmp(oid, yaz_oid_recsyn_xml)) { if ((cp = dummy_xml_record (r->number, r->stream))) { r->len = strlen(cp); r->record = cp; - r->output_format = VAL_TEXT_XML; } else { - r->errcode = 14; + r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE; r->surrogate_flag = 1; return 0; } } - else if ((cp = dummy_marc_record(r->number, r->stream))) - { - r->len = strlen(cp); - r->record = cp; - r->output_format = VAL_USMARC; - } else { - r->errcode = 13; + char buf[OID_STR_MAX]; + r->errcode = YAZ_BIB1_RECORD_SYNTAX_UNSUPP; + r->errstring = odr_strdup(r->stream, oid_oid_to_dotstring(oid, buf)); return 0; } r->errcode = 0; @@ -536,9 +675,19 @@ int ztest_scan(void *handle, bend_scan_rr *q) int term_position_req = q->term_position; int num_entries_req = q->num_entries; - if (yaz_matchstr (q->basenames[0], "Default")) + /* 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 { - q->errcode = 109; + q->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE; q->errstring = q->basenames[0]; return 0; } @@ -554,7 +703,7 @@ int ztest_scan(void *handle, bend_scan_rr *q) } if (q->num_entries > 200) { - q->errcode = 31; + q->errcode = YAZ_BIB1_RESOURCES_EXHAUSTED_NO_RESULTS_AVAILABLE; return 0; } if (q->term) @@ -562,12 +711,12 @@ int ztest_scan(void *handle, bend_scan_rr *q) int len; if (q->term->term->which != Z_Term_general) { - q->errcode = 229; /* unsupported term type */ + q->errcode = YAZ_BIB1_TERM_TYPE_UNSUPP; return 0; } if (*q->step_size != 0) { - q->errcode = 205; /*Only zero step size supported for Scan */ + q->errcode = YAZ_BIB1_ONLY_ZERO_STEP_SIZE_SUPPORTED_FOR_SCAN; return 0; } len = q->term->term->u.general->len; @@ -613,7 +762,8 @@ int ztest_scan(void *handle, bend_scan_rr *q) { list[q->num_entries].term = entries[pos]; list[q->num_entries].occurrences = -1; - list[q->num_entries].errcode = 233; + list[q->num_entries].errcode = + YAZ_BIB1_SCAN_UNSUPP_VALUE_OF_POSITION_IN_RESPONSE; list[q->num_entries].errstring = "SD for Scan Term"; } else @@ -637,7 +787,7 @@ int ztest_scan(void *handle, bend_scan_rr *q) return 0; } -static int ztest_explain(void *handle, bend_explain_rr *rr) +int ztest_explain(void *handle, bend_explain_rr *rr) { if (rr->database && !strcmp(rr->database, "Default")) { @@ -651,6 +801,12 @@ static int ztest_explain(void *handle, bend_explain_rr *rr) return 0; } +int ztest_update(void *handle, bend_update_rr *rr) +{ + rr->operation_status = "success"; + return 0; +} + bend_initresult *bend_init(bend_initrequest *q) { bend_initresult *r = (bend_initresult *) @@ -678,6 +834,10 @@ bend_initresult *bend_init(bend_initrequest *q) q->bend_explain = ztest_explain; #endif q->bend_srw_scan = ztest_scan; + q->bend_srw_update = ztest_update; + + q->query_charset = "ISO-8859-1"; + q->records_in_same_charset = 0; return r; }