1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
6 * \brief yaz-ztest Generic Frontend Server
24 #include <sys/select.h>
31 #include <yaz/backend.h>
33 #include <yaz/diagbib1.h>
34 #include <yaz/otherinfo.h>
35 #include <yaz/facet.h>
39 static int log_level=0;
40 static int log_level_set=0;
51 struct delay search_delay;
52 struct delay present_delay;
53 struct delay fetch_delay;
54 struct result_set *next;
57 struct session_handle {
58 struct result_set *result_sets;
61 int ztest_search(void *handle, bend_search_rr *rr);
62 int ztest_sort(void *handle, bend_sort_rr *rr);
63 int ztest_present(void *handle, bend_present_rr *rr);
64 int ztest_esrequest(void *handle, bend_esrequest_rr *rr);
65 int ztest_delete(void *handle, bend_delete_rr *rr);
67 static struct result_set *get_set(struct session_handle *sh, const char *name)
69 struct result_set *set = sh->result_sets;
70 for (; set; set = set->next)
71 if (!strcmp(name, set->name))
76 static void remove_sets(struct session_handle *sh)
78 struct result_set *set = sh->result_sets;
81 struct result_set *set_next = set->next;
90 /** \brief use term value as hit count
91 \param s RPN structure
92 \param hash value for compuation
93 \return >= 0: search term number or -1: not found
95 Traverse RPN tree 'in order' and use term value as hit count.
96 Only terms that looks a numeric is used.. Returns -1 if
97 no sub tree has a hit count term
99 static Odr_int get_term_hit(Z_RPNStructure *s, unsigned *hash)
104 case Z_RPNStructure_simple:
105 if (s->u.simple->which == Z_Operand_APT)
107 Z_AttributesPlusTerm *apt = s->u.simple->u.attributesPlusTerm;
108 if (apt->term->which == Z_Term_general)
110 Odr_oct *oct = apt->term->u.general;
111 if (oct->len > 0 && oct->buf[0] >= '0' && oct->buf[0] <= '9')
113 WRBUF hits_str = wrbuf_alloc();
114 wrbuf_write(hits_str, (const char *) oct->buf, oct->len);
115 h = odr_atoi(wrbuf_cstr(hits_str));
116 wrbuf_destroy(hits_str);
121 for (i = 0; i < oct->len; i++)
122 *hash = *hash * 65509 + oct->buf[i];
127 case Z_RPNStructure_complex:
128 h = get_term_hit(s->u.complex->s1, hash);
130 h = get_term_hit(s->u.complex->s2, hash);
136 /** \brief gets hit count for numeric terms in RPN queries
138 \return number of hits
140 This is just for testing.. A real database of course uses
141 the content of a database to establish a value.. In our case, we
142 have a way to trigger a certain hit count. Good for testing of
143 client applications etc
145 static Odr_int get_hit_count(Z_Query *q)
147 if (q->which == Z_Query_type_1 || q->which == Z_Query_type_101)
151 h = get_term_hit(q->u.type_1->RPNStructure, &hash);
156 else if (q->which == Z_Query_type_104 &&
157 q->u.type_104->which == Z_External_CQL)
160 const char *cql = q->u.type_104->u.cql;
162 for (i = 0; cql[i]; i++)
163 hash = hash * 65509 + cql[i];
170 /** \brief checks if it's a dummy Slow database
171 \param basename database name to check
172 \param association backend association (or NULL if not available)
173 \retval 1 is slow database
174 \retval 0 is not a slow database
176 The Slow database is for testing.. It allows us to simulate
179 static int check_slow(const char *basename, bend_association association)
181 if (strncmp(basename, "Slow", 4) == 0)
186 sscanf(basename+4, "%d", &w);
187 /* wait up to 3 seconds and check if connection is still alive */
188 for (i = 0; i < w; i++)
190 if (association && !bend_assoc_is_alive(association))
192 yaz_log(YLOG_LOG, "search aborted");
203 static int strcmp_prefix(const char *s, const char *p)
205 size_t l = strlen(p);
206 if (strlen(s) >= l && !memcmp(s, p, l))
211 static void init_delay(struct delay *delayp)
213 delayp->d1 = delayp->d2 = 0.0;
216 static int parse_delay(struct delay *delayp, const char *value)
218 if (sscanf(value, "%lf:%lf", &delayp->d1, &delayp->d2) == 2)
220 else if (sscanf(value, "%lf", &delayp->d1) == 1)
227 static void ztest_sleep(double d)
230 Sleep( (DWORD) (d * 1000));
234 tv.tv_usec = (d - (long) d) * 1000000;
235 select(0, 0, 0, 0, &tv);
239 static void do_delay(const struct delay *delayp)
241 double d = delayp->d1;
246 d += (rand()) * (delayp->d2 - d) / RAND_MAX;
251 static void addterms(ODR odr, Z_FacetField *facet_field, const char *facet_name)
255 int length = strlen(facet_name) + 10;
256 char *key = odr_malloc(odr, length);
258 for (index = 0; index < facet_field->num_terms; index++)
260 Z_FacetTerm *facet_term;
261 sprintf(key, "%s%d", facet_name, index);
262 yaz_log(YLOG_DEBUG, "facet add term %s %d %s", facet_name, index, key);
264 facet_term = facet_term_create_cstr(odr, key, freq);
266 facet_field_term_set(odr, facet_field, facet_term, index);
270 Z_OtherInformation *build_facet_response(ODR odr, Z_FacetList *facet_list) {
271 int index, new_index = 0;
272 Z_FacetList *new_list = facet_list_create(odr, facet_list->num);
274 for (index = 0; index < facet_list->num; index++) {
275 struct yaz_facet_attr attrvalues;
276 yaz_facet_attr_init(&attrvalues);
277 attrvalues.limit = 10;
278 yaz_facet_attr_get_z_attributes(facet_list->elements[index]->attributes,
280 yaz_log(YLOG_LOG, "Attributes: %s limit=%d start=%d sort=%d",
281 attrvalues.useattr ? attrvalues.useattr : "NONE",
284 attrvalues.sortorder);
285 if (attrvalues.errstring)
286 yaz_log(YLOG_LOG, "Error parsing attributes: %s", attrvalues.errstring);
287 if (attrvalues.limit > 0 && attrvalues.useattr) {
288 new_list->elements[new_index] = facet_field_create(odr, facet_list->elements[index]->attributes, attrvalues.limit);
289 addterms(odr, new_list->elements[new_index], attrvalues.useattr);
293 yaz_log(YLOG_DEBUG, "Facet: skipping %s due to 0 limit.", attrvalues.useattr);
297 new_list->num = new_index;
299 Z_OtherInformation *oi = odr_malloc(odr, sizeof(*oi));
300 Z_OtherInformationUnit *oiu = odr_malloc(odr, sizeof(*oiu));
301 oi->num_elements = 1;
302 oi->list = odr_malloc(odr, oi->num_elements * sizeof(*oi->list));
304 oiu->which = Z_OtherInfo_externallyDefinedInfo;
305 oiu->information.externallyDefinedInfo = odr_malloc(odr, sizeof(*oiu->information.externallyDefinedInfo));
306 oiu->information.externallyDefinedInfo->direct_reference = odr_oiddup(odr, yaz_oid_userinfo_facet_1);
307 oiu->information.externallyDefinedInfo->descriptor = 0;
308 oiu->information.externallyDefinedInfo->indirect_reference = 0;
309 oiu->information.externallyDefinedInfo->which = Z_External_userFacets;
310 oiu->information.externallyDefinedInfo->u.facetList = new_list;
317 static void echo_extra_args(ODR stream,
318 Z_SRW_extra_arg *extra_args, char **extra_response)
323 WRBUF response_xml = wrbuf_alloc();
324 wrbuf_puts(response_xml, "<extra>");
325 for (a = extra_args; a; a = a->next)
327 wrbuf_puts(response_xml, "<extra name=\"");
328 wrbuf_xmlputs(response_xml, a->name);
329 wrbuf_puts(response_xml, "\"");
332 wrbuf_puts(response_xml, " value=\"");
333 wrbuf_xmlputs(response_xml, a->value);
334 wrbuf_puts(response_xml, "\"");
336 wrbuf_puts(response_xml, "/>");
338 wrbuf_puts(response_xml, "</extra>");
339 *extra_response = odr_strdup(stream, wrbuf_cstr(response_xml));
340 wrbuf_destroy(response_xml);
345 int ztest_search(void *handle, bend_search_rr *rr)
347 struct session_handle *sh = (struct session_handle*) handle;
348 struct result_set *new_set;
349 const char *db, *db_sep;
351 if (rr->num_bases != 1)
353 rr->errcode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
357 db = rr->basenames[0];
359 /* Allow Default, db.* and Slow */
360 if (strcmp_prefix(db, "Default"))
361 ; /* Default is OK in our test */
362 else if (strcmp_prefix(db, "db"))
363 ; /* db.* is OK in our test */
364 else if (check_slow(rr->basenames[0], rr->association))
366 rr->estimated_hit_count = 1;
370 rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
371 rr->errstring = rr->basenames[0];
375 new_set = get_set(sh, rr->setname);
378 if (!rr->replace_set)
380 rr->errcode = YAZ_BIB1_RESULT_SET_EXISTS_AND_REPLACE_INDICATOR_OFF;
387 new_set = xmalloc(sizeof(*new_set));
388 new_set->next = sh->result_sets;
389 sh->result_sets = new_set;
390 new_set->name = xstrdup(rr->setname);
393 new_set->db = xstrdup(db);
394 init_delay(&new_set->search_delay);
395 init_delay(&new_set->present_delay);
396 init_delay(&new_set->fetch_delay);
398 db_sep = strchr(db, '?');
403 int no_parms = yaz_uri_to_array(db_sep+1, rr->stream, &names, &values);
405 for (i = 0; i < no_parms; i++)
407 const char *name = names[i];
408 const char *value = values[i];
409 if (!strcmp(name, "seed"))
411 else if (!strcmp(name, "search-delay"))
412 parse_delay(&new_set->search_delay, value);
413 else if (!strcmp(name, "present-delay"))
414 parse_delay(&new_set->present_delay, value);
415 else if (!strcmp(name, "fetch-delay"))
416 parse_delay(&new_set->fetch_delay, value);
419 rr->errcode = YAZ_BIB1_SERVICE_UNSUPP_FOR_THIS_DATABASE;
420 rr->errstring = odr_strdup(rr->stream, name);
425 echo_extra_args(rr->stream, rr->extra_args, &rr->extra_response_data);
426 rr->hits = get_hit_count(rr->query);
430 Z_FacetList *facet_list = yaz_oi_get_facetlist(&rr->search_input);
432 yaz_log(YLOG_LOG, "%d Facets in search request.", facet_list->num);
433 rr->search_info = build_facet_response(rr->stream, facet_list);
436 yaz_log(YLOG_DEBUG, "No facets parsed search request.");
439 do_delay(&new_set->search_delay);
440 new_set->hits = rr->hits;
446 /* this huge function handles extended services */
447 int ztest_esrequest(void *handle, bend_esrequest_rr *rr)
449 if (rr->esr->packageName)
450 yaz_log(log_level, "packagename: %s", rr->esr->packageName);
451 yaz_log(log_level, "Waitaction: " ODR_INT_PRINTF, *rr->esr->waitAction);
454 yaz_log(log_level, "function: " ODR_INT_PRINTF, *rr->esr->function);
456 if (!rr->esr->taskSpecificParameters)
458 yaz_log(log_level, "No task specific parameters");
460 else if (rr->esr->taskSpecificParameters->which == Z_External_itemOrder)
462 Z_ItemOrder *it = rr->esr->taskSpecificParameters->u.itemOrder;
463 yaz_log(log_level, "Received ItemOrder");
464 if (it->which == Z_IOItemOrder_esRequest)
466 Z_IORequest *ir = it->u.esRequest;
467 Z_IOOriginPartToKeep *k = ir->toKeep;
468 Z_IOOriginPartNotToKeep *n = ir->notToKeep;
469 const char *xml_in_response = 0;
473 if (k->contact->name)
474 yaz_log(log_level, "contact name %s", k->contact->name);
475 if (k->contact->phone)
476 yaz_log(log_level, "contact phone %s", k->contact->phone);
477 if (k->contact->email)
478 yaz_log(log_level, "contact email %s", k->contact->email);
482 yaz_log(log_level, "Billing info (not shown)");
485 if (n->resultSetItem)
487 yaz_log(log_level, "resultsetItem");
488 yaz_log(log_level, "setId: %s", n->resultSetItem->resultSetId);
489 yaz_log(log_level, "item: " ODR_INT_PRINTF, *n->resultSetItem->item);
493 Z_External *r = (Z_External*) n->itemRequest;
494 ILL_ItemRequest *item_req = 0;
495 ILL_APDU *ill_apdu = 0;
496 if (r->direct_reference)
498 char oid_name_str[OID_STR_MAX];
500 const char *oid_name =
501 yaz_oid_to_string_buf(r->direct_reference,
502 &oclass, oid_name_str);
504 yaz_log(log_level, "OID %s", oid_name);
505 if (!oid_oidcmp(r->direct_reference, yaz_oid_recsyn_xml))
507 yaz_log(log_level, "ILL XML request");
508 if (r->which == Z_External_octet)
509 yaz_log(log_level, "%.*s",
510 r->u.octet_aligned->len,
511 r->u.octet_aligned->buf);
512 xml_in_response = "<dummy>x</dummy>";
514 if (!oid_oidcmp(r->direct_reference,
515 yaz_oid_general_isoill_1))
517 yaz_log(log_level, "Decode ItemRequest begin");
518 if (r->which == ODR_EXTERNAL_single)
520 odr_setbuf(rr->decode,
521 (char *) r->u.single_ASN1_type->buf,
522 r->u.single_ASN1_type->len, 0);
524 if (!ill_ItemRequest(rr->decode, &item_req, 0, 0))
527 "Couldn't decode ItemRequest %s near %ld",
528 odr_errmsg(odr_geterror(rr->decode)),
529 (long) odr_offset(rr->decode));
532 yaz_log(log_level, "Decode ItemRequest OK");
535 ill_ItemRequest(rr->print, &item_req, 0,
537 odr_reset(rr->print);
540 if (!item_req && r->which == ODR_EXTERNAL_single)
542 yaz_log(log_level, "Decode ILL APDU begin");
543 odr_setbuf(rr->decode,
544 (char*) r->u.single_ASN1_type->buf,
545 r->u.single_ASN1_type->len, 0);
547 if (!ill_APDU(rr->decode, &ill_apdu, 0, 0))
550 "Couldn't decode ILL APDU %s near %ld",
551 odr_errmsg(odr_geterror(rr->decode)),
552 (long) odr_offset(rr->decode));
553 yaz_log(log_level, "PDU dump:");
554 odr_dumpBER(yaz_log_file(),
555 (char *) r->u.single_ASN1_type->buf,
556 r->u.single_ASN1_type->len);
559 yaz_log(log_level, "Decode ILL APDU OK");
562 ill_APDU(rr->print, &ill_apdu, 0,
564 odr_reset(rr->print);
571 yaz_log(log_level, "ILL protocol version = "
573 *item_req->protocol_version_num);
579 Z_External *ext = (Z_External *)
580 odr_malloc(rr->stream, sizeof(*ext));
581 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
582 odr_malloc(rr->stream, sizeof(*keep));
583 Z_IOTargetPart *targetPart = (Z_IOTargetPart *)
584 odr_malloc(rr->stream, sizeof(*targetPart));
586 rr->taskPackage = (Z_TaskPackage *)
587 odr_malloc(rr->stream, sizeof(*rr->taskPackage));
588 rr->taskPackage->packageType =
589 odr_oiddup(rr->stream, rr->esr->packageType);
590 rr->taskPackage->packageName = 0;
591 rr->taskPackage->userId = 0;
592 rr->taskPackage->retentionTime = 0;
593 rr->taskPackage->permissions = 0;
594 rr->taskPackage->description = 0;
595 rr->taskPackage->targetReference =
596 odr_create_Odr_oct(rr->stream, "911", 3);
597 rr->taskPackage->creationDateTime = 0;
598 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
599 rr->taskPackage->packageDiagnostics = 0;
600 rr->taskPackage->taskSpecificParameters = ext;
602 ext->direct_reference =
603 odr_oiddup(rr->stream, rr->esr->packageType);
604 ext->indirect_reference = 0;
606 ext->which = Z_External_itemOrder;
607 ext->u.itemOrder = (Z_ItemOrder *)
608 odr_malloc(rr->stream, sizeof(*ext->u.update));
609 ext->u.itemOrder->which = Z_IOItemOrder_taskPackage;
610 ext->u.itemOrder->u.taskPackage = (Z_IOTaskPackage *)
611 odr_malloc(rr->stream, sizeof(Z_IOTaskPackage));
612 ext->u.itemOrder->u.taskPackage->originPart = k;
613 ext->u.itemOrder->u.taskPackage->targetPart = targetPart;
616 targetPart->itemRequest =
617 z_ext_record_xml(rr->stream, xml_in_response,
618 strlen(xml_in_response));
620 targetPart->itemRequest = 0;
622 targetPart->statusOrErrorReport = 0;
623 targetPart->auxiliaryStatus = 0;
627 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
629 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
630 yaz_log(log_level, "Received DB Update");
631 if (up->which == Z_IUUpdate_esRequest)
633 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
634 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
635 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
637 yaz_log(log_level, "action");
640 switch (*toKeep->action)
642 case Z_IUOriginPartToKeep_recordInsert:
643 yaz_log(log_level, " recordInsert");
645 case Z_IUOriginPartToKeep_recordReplace:
646 yaz_log(log_level, " recordReplace");
648 case Z_IUOriginPartToKeep_recordDelete:
649 yaz_log(log_level, " recordDelete");
651 case Z_IUOriginPartToKeep_elementUpdate:
652 yaz_log(log_level, " elementUpdate");
654 case Z_IUOriginPartToKeep_specialUpdate:
655 yaz_log(log_level, " specialUpdate");
658 yaz_log(log_level, " unknown (" ODR_INT_PRINTF ")",
662 if (toKeep->databaseName)
664 yaz_log(log_level, "database: %s", toKeep->databaseName);
665 if (!strcmp(toKeep->databaseName, "fault"))
667 rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
668 rr->errstring = toKeep->databaseName;
670 if (!strcmp(toKeep->databaseName, "accept"))
675 Z_External *ext = (Z_External *)
676 odr_malloc(rr->stream, sizeof(*ext));
677 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
678 odr_malloc(rr->stream, sizeof(*keep));
679 Z_IUTargetPart *targetPart = (Z_IUTargetPart *)
680 odr_malloc(rr->stream, sizeof(*targetPart));
682 rr->taskPackage = (Z_TaskPackage *)
683 odr_malloc(rr->stream, sizeof(*rr->taskPackage));
684 rr->taskPackage->packageType =
685 odr_oiddup(rr->stream, rr->esr->packageType);
686 rr->taskPackage->packageName = 0;
687 rr->taskPackage->userId = 0;
688 rr->taskPackage->retentionTime = 0;
689 rr->taskPackage->permissions = 0;
690 rr->taskPackage->description = 0;
691 rr->taskPackage->targetReference =
692 odr_create_Odr_oct(rr->stream, "123", 3);
693 rr->taskPackage->creationDateTime = 0;
694 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
695 rr->taskPackage->packageDiagnostics = 0;
696 rr->taskPackage->taskSpecificParameters = ext;
698 ext->direct_reference =
699 odr_oiddup(rr->stream, rr->esr->packageType);
700 ext->indirect_reference = 0;
702 ext->which = Z_External_update;
703 ext->u.update = (Z_IUUpdate *)
704 odr_malloc(rr->stream, sizeof(*ext->u.update));
705 ext->u.update->which = Z_IUUpdate_taskPackage;
706 ext->u.update->u.taskPackage = (Z_IUUpdateTaskPackage *)
707 odr_malloc(rr->stream, sizeof(Z_IUUpdateTaskPackage));
708 ext->u.update->u.taskPackage->originPart = keep;
709 ext->u.update->u.taskPackage->targetPart = targetPart;
711 keep->action = odr_intdup(rr->stream, *toKeep->action);
713 odr_strdup(rr->stream, toKeep->databaseName);
715 keep->elementSetName = 0;
716 keep->actionQualifier = 0;
718 targetPart->updateStatus = odr_intdup(rr->stream, 1);
719 targetPart->num_globalDiagnostics = 0;
720 targetPart->globalDiagnostics = (Z_DiagRec **) odr_nullval();
721 targetPart->num_taskPackageRecords = 1;
722 targetPart->taskPackageRecords =
723 (Z_IUTaskPackageRecordStructure **)
724 odr_malloc(rr->stream,
725 sizeof(Z_IUTaskPackageRecordStructure *));
726 targetPart->taskPackageRecords[0] =
727 (Z_IUTaskPackageRecordStructure *)
728 odr_malloc(rr->stream,
729 sizeof(Z_IUTaskPackageRecordStructure));
731 targetPart->taskPackageRecords[0]->which =
732 Z_IUTaskPackageRecordStructure_record;
733 targetPart->taskPackageRecords[0]->u.record =
734 z_ext_record_sutrs(rr->stream, "test", 4);
735 targetPart->taskPackageRecords[0]->correlationInfo = 0;
736 targetPart->taskPackageRecords[0]->recordStatus =
737 odr_intdup(rr->stream,
738 Z_IUTaskPackageRecordStructure_success);
739 targetPart->taskPackageRecords[0]->num_supplementalDiagnostics
742 targetPart->taskPackageRecords[0]->supplementalDiagnostics = 0;
747 for (i = 0; i < notToKeep->num; i++)
749 Z_External *rec = notToKeep->elements[i]->record;
751 if (rec->direct_reference)
753 char oid_name_str[OID_STR_MAX];
755 = oid_name = yaz_oid_to_string_buf(
756 rec->direct_reference, 0,
759 yaz_log(log_level, "record %d type %s", i,
764 case Z_External_sutrs:
765 if (rec->u.octet_aligned->len > 170)
766 yaz_log(log_level, "%d bytes:\n%.168s ...",
770 yaz_log(log_level, "%d bytes:\n%s",
774 case Z_External_octet :
775 if (rec->u.octet_aligned->len > 170)
776 yaz_log(log_level, "%d bytes:\n%.168s ...",
777 rec->u.octet_aligned->len,
778 rec->u.octet_aligned->buf);
780 yaz_log(log_level, "%d bytes\n%s",
781 rec->u.octet_aligned->len,
782 rec->u.octet_aligned->buf);
791 /* result set delete */
792 int ztest_delete(void *handle, bend_delete_rr *rr)
794 if (rr->num_setnames == 1 && !strcmp(rr->setnames[0], "1"))
795 rr->delete_status = Z_DeleteStatus_success;
797 rr->delete_status = Z_DeleteStatus_resultSetDidNotExist;
801 /* Our sort handler really doesn't sort... */
802 int ztest_sort(void *handle, bend_sort_rr *rr)
805 rr->sort_status = Z_SortResponse_success;
810 /* present request handler */
811 int ztest_present(void *handle, bend_present_rr *rr)
813 struct session_handle *sh = (struct session_handle*) handle;
814 struct result_set *set = get_set(sh, rr->setname);
818 rr->errcode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
819 rr->errstring = odr_strdup(rr->stream, rr->setname);
822 do_delay(&set->present_delay);
826 /* retrieval of a single record (present, and piggy back search) */
827 int ztest_fetch(void *handle, bend_fetch_rr *r)
829 struct session_handle *sh = (struct session_handle*) handle;
831 const Odr_oid *oid = r->request_format;
832 struct result_set *set = get_set(sh, r->setname);
833 const char *esn = yaz_get_esn(r->comp);
837 r->errcode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
838 r->errstring = odr_strdup(r->stream, r->setname);
841 do_delay(&set->fetch_delay);
843 r->basename = set->db;
844 r->output_format = r->request_format;
846 if (r->number < 1 || r->number > set->hits)
848 r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
851 if (!oid || yaz_oid_is_iso2709(oid))
853 cp = dummy_marc_record(r->number, r->stream);
856 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
857 r->surrogate_flag = 1;
864 r->output_format = odr_oiddup(r->stream, yaz_oid_recsyn_usmarc);
867 else if (!oid_oidcmp(oid, yaz_oid_recsyn_opac))
869 cp = dummy_marc_record(r->number, r->stream);
872 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
873 r->surrogate_flag = 1;
876 r->record = (char *) dummy_opac(r->number, r->stream, cp);
879 else if (!oid_oidcmp(oid, yaz_oid_recsyn_sutrs))
881 /* this section returns a small record */
884 sprintf(buf, "This is dummy SUTRS record number %d\n", r->number);
886 r->len = strlen(buf);
887 r->record = (char *) odr_malloc(r->stream, r->len+1);
888 strcpy(r->record, buf);
890 else if (!oid_oidcmp(oid, yaz_oid_recsyn_grs_1))
893 r->record = (char*) dummy_grs_record(r->number, r->stream);
896 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
897 r->surrogate_flag = 1;
901 else if (!oid_oidcmp(oid, yaz_oid_recsyn_postscript))
907 sprintf(fname, "part.%d.ps", r->number);
908 f = fopen(fname, "rb");
911 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
912 r->surrogate_flag = 1;
915 fseek(f, 0L, SEEK_END);
917 if (size <= 0 || size >= 5000000)
919 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
920 r->surrogate_flag = 1;
922 fseek(f, 0L, SEEK_SET);
923 r->record = (char*) odr_malloc(r->stream, size);
925 if (fread(r->record, size, 1, f) != 1)
927 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
928 r->surrogate_flag = 1;
932 else if (!oid_oidcmp(oid, yaz_oid_recsyn_xml))
934 if ((cp = dummy_xml_record(r->number, r->stream, esn)))
941 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
942 r->surrogate_flag = 1;
948 char buf[OID_STR_MAX];
949 r->errcode = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
950 r->errstring = odr_strdup(r->stream, oid_oid_to_dotstring(oid, buf));
958 * silly dummy-scan what reads words from a file.
960 int ztest_scan(void *handle, bend_scan_rr *q)
963 static struct scan_entry list[200];
964 static char entries[200][80];
968 int term_position_req = q->term_position;
969 int num_entries_req = q->num_entries;
971 /* Throw Database unavailable if other than Default or Slow */
972 if (!yaz_matchstr(q->basenames[0], "Default"))
973 ; /* Default is OK in our test */
974 else if (check_slow(q->basenames[0], 0 /* no assoc for scan */))
978 q->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
979 q->errstring = q->basenames[0];
986 q->status = BEND_SCAN_SUCCESS;
987 if (!f && !(f = fopen("dummy-words", "r")))
989 perror("dummy-words");
992 if (q->num_entries > 200)
994 q->errcode = YAZ_BIB1_RESOURCES_EXHAUSTED_NO_RESULTS_AVAILABLE;
1000 if (q->term->term->which != Z_Term_general)
1002 q->errcode = YAZ_BIB1_TERM_TYPE_UNSUPP;
1005 if (*q->step_size != 0)
1007 q->errcode = YAZ_BIB1_ONLY_ZERO_STEP_SIZE_SUPPORTED_FOR_SCAN;
1010 len = q->term->term->u.general->len;
1011 if (len >= (int ) sizeof(term))
1012 len = sizeof(term)-1;
1013 memcpy(term, q->term->term->u.general->buf, len);
1016 else if (q->scanClause)
1018 strncpy(term, q->scanClause, sizeof(term)-1);
1019 term[sizeof(term)-1] = '\0';
1024 for (p = term; *p; p++)
1025 if (yaz_islower(*p))
1026 *p = yaz_toupper(*p);
1028 fseek(f, 0, SEEK_SET);
1031 for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
1032 i++, pos < 199 ? pos++ : (pos = 0))
1034 if (!q->num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
1036 if ((q->term_position = term_position_req) > i + 1)
1038 q->term_position = i + 1;
1039 q->status = BEND_SCAN_PARTIAL;
1041 for (; q->num_entries < q->term_position; q->num_entries++)
1045 po = pos - q->term_position + q->num_entries+1; /* find pos */
1049 if (!strcmp(term, "SD") && q->num_entries == 2)
1051 list[q->num_entries].term = entries[pos];
1052 list[q->num_entries].occurrences = -1;
1053 list[q->num_entries].errcode =
1054 YAZ_BIB1_SCAN_UNSUPP_VALUE_OF_POSITION_IN_RESPONSE;
1055 list[q->num_entries].errstring = "SD for Scan Term";
1059 list[q->num_entries].term = entries[po];
1060 list[q->num_entries].occurrences = hits[po];
1064 else if (q->num_entries)
1066 list[q->num_entries].term = entries[pos];
1067 list[q->num_entries].occurrences = hits[pos];
1070 if (q->num_entries >= num_entries_req)
1073 echo_extra_args(q->stream, q->extra_args, &q->extra_response_data);
1075 q->status = BEND_SCAN_PARTIAL;
1079 int ztest_explain(void *handle, bend_explain_rr *rr)
1081 if (rr->database && !strcmp(rr->database, "Default"))
1083 rr->explain_buf = "<explain>\n"
1085 "\t\t<host>localhost</host>\n"
1086 "\t\t<port>210</port>\n"
1093 int ztest_update(void *handle, bend_update_rr *rr)
1095 rr->operation_status = "success";
1099 bend_initresult *bend_init(bend_initrequest *q)
1101 bend_initresult *r = (bend_initresult *)
1102 odr_malloc(q->stream, sizeof(*r));
1103 struct session_handle *sh = xmalloc(sizeof(*sh));
1105 sh->result_sets = 0;
1109 log_level=yaz_log_module_level("ztest");
1115 r->handle = sh; /* tell GFS about our handle */
1116 q->bend_sort = ztest_sort; /* register sort handler */
1117 q->bend_search = ztest_search; /* register search handler */
1118 q->bend_present = ztest_present; /* register present handle */
1119 q->bend_esrequest = ztest_esrequest;
1120 q->bend_delete = ztest_delete;
1121 q->bend_fetch = ztest_fetch;
1122 q->bend_scan = ztest_scan;
1124 q->bend_explain = ztest_explain;
1126 q->bend_srw_scan = ztest_scan;
1127 q->bend_srw_update = ztest_update;
1129 q->query_charset = "ISO-8859-1";
1130 q->records_in_same_charset = 0;
1135 void bend_close(void *handle)
1137 struct session_handle *sh = (struct session_handle*) handle;
1139 xfree(sh); /* release our session */
1143 int main(int argc, char **argv)
1145 return statserv_main(argc, argv, bend_init, bend_close);
1150 * c-file-style: "Stroustrup"
1151 * indent-tabs-mode: nil
1153 * vim: shiftwidth=4 tabstop=8 expandtab