1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
7 * \brief yaz-ztest Generic Frontend Server
22 #include <sys/select.h>
29 #include <yaz/backend.h>
31 #include <yaz/diagbib1.h>
32 #include <yaz/otherinfo.h>
33 #include <yaz/facet.h>
37 static int log_level=0;
38 static int log_level_set=0;
49 struct delay search_delay;
50 struct delay present_delay;
51 struct delay fetch_delay;
52 struct result_set *next;
55 struct session_handle {
56 struct result_set *result_sets;
59 int ztest_search(void *handle, bend_search_rr *rr);
60 int ztest_sort(void *handle, bend_sort_rr *rr);
61 int ztest_present(void *handle, bend_present_rr *rr);
62 int ztest_esrequest(void *handle, bend_esrequest_rr *rr);
63 int ztest_delete(void *handle, bend_delete_rr *rr);
65 static struct result_set *get_set(struct session_handle *sh, const char *name)
67 struct result_set *set = sh->result_sets;
68 for (; set; set = set->next)
69 if (!strcmp(name, set->name))
74 static void remove_sets(struct session_handle *sh)
76 struct result_set *set = sh->result_sets;
79 struct result_set *set_next = set->next;
88 /** \brief use term value as hit count
89 \param s RPN structure
90 \return >= 0: search term number or -1: not found
92 Traverse RPN tree 'in order' and use term value as hit count.
93 Only terms that looks a numeric is used.. Returns -1 if
94 no sub tree has a hit count term
96 static Odr_int get_term_hit(Z_RPNStructure *s)
101 case Z_RPNStructure_simple:
102 if (s->u.simple->which == Z_Operand_APT)
104 Z_AttributesPlusTerm *apt = s->u.simple->u.attributesPlusTerm;
105 if (apt->term->which == Z_Term_general)
107 Odr_oct *oct = apt->term->u.general;
108 if (oct->len > 0 && oct->buf[0] >= '0' && oct->buf[0] <= '9')
110 WRBUF hits_str = wrbuf_alloc();
111 wrbuf_write(hits_str, (const char *) oct->buf, oct->len);
112 h = odr_atoi(wrbuf_cstr(hits_str));
113 wrbuf_destroy(hits_str);
118 case Z_RPNStructure_complex:
119 h = get_term_hit(s->u.complex->s1);
121 h = get_term_hit(s->u.complex->s2);
127 /** \brief gets hit count for numeric terms in RPN queries
129 \return number of hits (random or number for term)
131 This is just for testing.. A real database of course uses
132 the content of a database to establish a value.. In our case, we
133 have a way to trigger a certain hit count. Good for testing of
134 client applications etc
136 static Odr_int get_hit_count(Z_Query *q)
138 if (q->which == Z_Query_type_1 || q->which == Z_Query_type_101)
141 h = get_term_hit(q->u.type_1->RPNStructure);
150 /** \brief checks if it's a dummy Slow database
151 \param basename database name to check
152 \param association backend association (or NULL if not available)
153 \retval 1 is slow database
154 \retval 0 is not a slow database
156 The Slow database is for testing.. It allows us to simulate
159 static int check_slow(const char *basename, bend_association association)
161 if (strncmp(basename, "Slow", 4) == 0)
166 sscanf(basename+4, "%d", &w);
167 /* wait up to 3 seconds and check if connection is still alive */
168 for (i = 0; i < w; i++)
170 if (association && !bend_assoc_is_alive(association))
172 yaz_log(YLOG_LOG, "search aborted");
183 static int strcmp_prefix(const char *s, const char *p)
185 size_t l = strlen(p);
186 if (strlen(s) >= l && !memcmp(s, p, l))
191 static void init_delay(struct delay *delayp)
193 delayp->d1 = delayp->d2 = 0.0;
196 static int parse_delay(struct delay *delayp, const char *value)
198 if (sscanf(value, "%lf:%lf", &delayp->d1, &delayp->d2) == 2)
200 else if (sscanf(value, "%lf", &delayp->d1) == 1)
207 static void ztest_sleep(double d)
210 Sleep( (DWORD) (d * 1000));
213 tv.tv_sec = floor(d);
214 tv.tv_usec = (d - floor(d)) * 1000000;
215 select(0, 0, 0, 0, &tv);
219 static void do_delay(const struct delay *delayp)
221 double d = delayp->d1;
226 d += (rand()) * (delayp->d2 - d) / RAND_MAX;
231 static void addterms(ODR odr, Z_FacetField *facet_field, const char *facet_name) {
234 int length = strlen(facet_name) + 10;
237 for (index = 0; index < facet_field->num_terms; index++) {
239 Z_FacetTerm *facet_term;
240 // sprintf(key, "%s%d", facet_name, index);
241 //yaz_log(YLOG_DEBUG, "facet add term %s %d %s", facet_name, index, key);
242 term = term_create(odr, key);
243 facet_term = facet_term_create(odr, term, freq);
245 facet_field_term_set(odr, facet_field, facet_term, index);
249 Z_OtherInformation *build_facet_response(ODR odr, Z_FacetList *facet_list) {
250 int index, new_index = 0;
251 Z_FacetList *new_list = facet_list_create(odr, facet_list->num);
253 for (index = 0; index < facet_list->num; index++) {
254 struct attrvalues attrvalues;
255 facet_struct_init(&attrvalues);
256 attrvalues.limit = 10;
257 yaz_log(YLOG_LOG, "Attributes: %s %s %d ", attrvalues.useattr, attrvalues.useattrbuff, attrvalues.limit);
258 facetattrs(facet_list->elements[index]->attributes, &attrvalues);
259 yaz_log(YLOG_LOG, "Attributes: %s %s %d ", attrvalues.useattr, attrvalues.useattrbuff, attrvalues.limit);
260 if (attrvalues.errstring)
261 yaz_log(YLOG_LOG, "Error parsing attributes: %s", attrvalues.errstring);
262 if (attrvalues.limit > 0) {
263 new_list->elements[new_index] = facet_field_create(odr, facet_list->elements[index]->attributes, attrvalues.limit);
264 addterms(odr, new_list->elements[new_index], attrvalues.useattr);
268 new_list->num = new_index;
270 Z_OtherInformation *oi = odr_malloc(odr, sizeof(*oi));
271 Z_OtherInformationUnit *oiu = odr_malloc(odr, sizeof(*oiu));
272 oi->num_elements = 1;
273 oi->list = odr_malloc(odr, oi->num_elements * sizeof(*oi->list));
275 oiu->which = Z_OtherInfo_externallyDefinedInfo;
276 oiu->information.externallyDefinedInfo = odr_malloc(odr, sizeof(*oiu->information.externallyDefinedInfo));
277 oiu->information.externallyDefinedInfo->direct_reference = odr_oiddup(odr, yaz_oid_userinfo_facet_1);
278 oiu->information.externallyDefinedInfo->which = Z_External_userFacets;
279 oiu->information.externallyDefinedInfo->u.facetList = new_list;
286 int ztest_search(void *handle, bend_search_rr *rr)
288 struct session_handle *sh = (struct session_handle*) handle;
289 struct result_set *new_set;
290 const char *db, *db_sep;
292 if (rr->num_bases != 1)
294 rr->errcode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
298 db = rr->basenames[0];
300 /* Allow Default, db.* and Slow */
301 if (strcmp_prefix(db, "Default"))
302 ; /* Default is OK in our test */
303 else if (strcmp_prefix(db, "db"))
304 ; /* db.* is OK in our test */
305 else if (check_slow(rr->basenames[0], rr->association))
307 rr->estimated_hit_count = 1;
311 rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
312 rr->errstring = rr->basenames[0];
316 new_set = get_set(sh, rr->setname);
319 if (!rr->replace_set)
321 rr->errcode = YAZ_BIB1_RESULT_SET_EXISTS_AND_REPLACE_INDICATOR_OFF;
328 new_set = xmalloc(sizeof(*new_set));
329 new_set->next = sh->result_sets;
330 sh->result_sets = new_set;
331 new_set->name = xstrdup(rr->setname);
334 new_set->db = xstrdup(db);
335 init_delay(&new_set->search_delay);
336 init_delay(&new_set->present_delay);
337 init_delay(&new_set->fetch_delay);
339 db_sep = strchr(db, '?');
344 int no_parms = yaz_uri_to_array(db_sep+1, rr->stream, &names, &values);
346 for (i = 0; i < no_parms; i++)
348 const char *name = names[i];
349 const char *value = values[i];
350 if (!strcmp(name, "seed"))
352 else if (!strcmp(name, "search-delay"))
353 parse_delay(&new_set->search_delay, value);
354 else if (!strcmp(name, "present-delay"))
355 parse_delay(&new_set->present_delay, value);
356 else if (!strcmp(name, "fetch-delay"))
357 parse_delay(&new_set->fetch_delay, value);
360 rr->errcode = YAZ_BIB1_SERVICE_UNSUPP_FOR_THIS_DATABASE;
361 rr->errstring = odr_strdup(rr->stream, name);
369 WRBUF response_xml = wrbuf_alloc();
370 wrbuf_puts(response_xml, "<extra>");
371 for (a = rr->extra_args; a; a = a->next)
373 wrbuf_puts(response_xml, "<extra name=\"");
374 wrbuf_xmlputs(response_xml, a->name);
375 wrbuf_puts(response_xml, "\"");
378 wrbuf_puts(response_xml, " value=\"");
379 wrbuf_xmlputs(response_xml, a->value);
380 wrbuf_puts(response_xml, "\"");
382 wrbuf_puts(response_xml, "/>");
384 wrbuf_puts(response_xml, "</extra>");
385 rr->extra_response_data =
386 odr_strdup(rr->stream, wrbuf_cstr(response_xml));
387 wrbuf_destroy(response_xml);
389 rr->hits = get_hit_count(rr->query);
393 /* TODO Not general. Only handles one (Facet) OtherInformation. Overwrite */
394 Z_FacetList *facet_list = extract_facet_request(rr->stream, rr->search_input);
397 yaz_log(YLOG_LOG, "%d Facets in search request.", facet_list->num);
398 rr->search_info = build_facet_response(rr->stream, facet_list);
401 yaz_log(YLOG_DEBUG, "No facets parsed search request.");
404 do_delay(&new_set->search_delay);
405 new_set->hits = rr->hits;
411 /* this huge function handles extended services */
412 int ztest_esrequest(void *handle, bend_esrequest_rr *rr)
414 /* user-defined handle - created in bend_init */
415 int *counter = (int*) handle;
417 yaz_log(log_level, "ESRequest no %d", *counter);
421 if (rr->esr->packageName)
422 yaz_log(log_level, "packagename: %s", rr->esr->packageName);
423 yaz_log(log_level, "Waitaction: " ODR_INT_PRINTF, *rr->esr->waitAction);
426 yaz_log(log_level, "function: " ODR_INT_PRINTF, *rr->esr->function);
428 if (!rr->esr->taskSpecificParameters)
430 yaz_log(log_level, "No task specific parameters");
432 else if (rr->esr->taskSpecificParameters->which == Z_External_itemOrder)
434 Z_ItemOrder *it = rr->esr->taskSpecificParameters->u.itemOrder;
435 yaz_log(log_level, "Received ItemOrder");
436 if (it->which == Z_IOItemOrder_esRequest)
438 Z_IORequest *ir = it->u.esRequest;
439 Z_IOOriginPartToKeep *k = ir->toKeep;
440 Z_IOOriginPartNotToKeep *n = ir->notToKeep;
441 const char *xml_in_response = 0;
445 if (k->contact->name)
446 yaz_log(log_level, "contact name %s", k->contact->name);
447 if (k->contact->phone)
448 yaz_log(log_level, "contact phone %s", k->contact->phone);
449 if (k->contact->email)
450 yaz_log(log_level, "contact email %s", k->contact->email);
454 yaz_log(log_level, "Billing info (not shown)");
457 if (n->resultSetItem)
459 yaz_log(log_level, "resultsetItem");
460 yaz_log(log_level, "setId: %s", n->resultSetItem->resultSetId);
461 yaz_log(log_level, "item: " ODR_INT_PRINTF, *n->resultSetItem->item);
465 Z_External *r = (Z_External*) n->itemRequest;
466 ILL_ItemRequest *item_req = 0;
467 ILL_APDU *ill_apdu = 0;
468 if (r->direct_reference)
470 char oid_name_str[OID_STR_MAX];
472 const char *oid_name =
473 yaz_oid_to_string_buf(r->direct_reference,
474 &oclass, oid_name_str);
476 yaz_log(log_level, "OID %s", oid_name);
477 if (!oid_oidcmp(r->direct_reference, yaz_oid_recsyn_xml))
479 yaz_log(log_level, "ILL XML request");
480 if (r->which == Z_External_octet)
481 yaz_log(log_level, "%.*s",
482 r->u.octet_aligned->len,
483 r->u.octet_aligned->buf);
484 xml_in_response = "<dummy>x</dummy>";
486 if (!oid_oidcmp(r->direct_reference,
487 yaz_oid_general_isoill_1))
489 yaz_log(log_level, "Decode ItemRequest begin");
490 if (r->which == ODR_EXTERNAL_single)
492 odr_setbuf(rr->decode,
493 (char *) r->u.single_ASN1_type->buf,
494 r->u.single_ASN1_type->len, 0);
496 if (!ill_ItemRequest(rr->decode, &item_req, 0, 0))
499 "Couldn't decode ItemRequest %s near %ld",
500 odr_errmsg(odr_geterror(rr->decode)),
501 (long) odr_offset(rr->decode));
504 yaz_log(log_level, "Decode ItemRequest OK");
507 ill_ItemRequest(rr->print, &item_req, 0,
509 odr_reset(rr->print);
512 if (!item_req && r->which == ODR_EXTERNAL_single)
514 yaz_log(log_level, "Decode ILL APDU begin");
515 odr_setbuf(rr->decode,
516 (char*) r->u.single_ASN1_type->buf,
517 r->u.single_ASN1_type->len, 0);
519 if (!ill_APDU(rr->decode, &ill_apdu, 0, 0))
522 "Couldn't decode ILL APDU %s near %ld",
523 odr_errmsg(odr_geterror(rr->decode)),
524 (long) odr_offset(rr->decode));
525 yaz_log(log_level, "PDU dump:");
526 odr_dumpBER(yaz_log_file(),
527 (char *) r->u.single_ASN1_type->buf,
528 r->u.single_ASN1_type->len);
531 yaz_log(log_level, "Decode ILL APDU OK");
534 ill_APDU(rr->print, &ill_apdu, 0,
536 odr_reset(rr->print);
543 yaz_log(log_level, "ILL protocol version = "
545 *item_req->protocol_version_num);
551 Z_External *ext = (Z_External *)
552 odr_malloc(rr->stream, sizeof(*ext));
553 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
554 odr_malloc(rr->stream, sizeof(*keep));
555 Z_IOTargetPart *targetPart = (Z_IOTargetPart *)
556 odr_malloc(rr->stream, sizeof(*targetPart));
558 rr->taskPackage = (Z_TaskPackage *)
559 odr_malloc(rr->stream, sizeof(*rr->taskPackage));
560 rr->taskPackage->packageType =
561 odr_oiddup(rr->stream, rr->esr->packageType);
562 rr->taskPackage->packageName = 0;
563 rr->taskPackage->userId = 0;
564 rr->taskPackage->retentionTime = 0;
565 rr->taskPackage->permissions = 0;
566 rr->taskPackage->description = 0;
567 rr->taskPackage->targetReference = (Odr_oct *)
568 odr_malloc(rr->stream, sizeof(Odr_oct));
569 rr->taskPackage->targetReference->buf =
570 (unsigned char *) odr_strdup(rr->stream, "911");
571 rr->taskPackage->targetReference->len =
572 rr->taskPackage->targetReference->size =
573 strlen((char *) (rr->taskPackage->targetReference->buf));
574 rr->taskPackage->creationDateTime = 0;
575 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
576 rr->taskPackage->packageDiagnostics = 0;
577 rr->taskPackage->taskSpecificParameters = ext;
579 ext->direct_reference =
580 odr_oiddup(rr->stream, rr->esr->packageType);
581 ext->indirect_reference = 0;
583 ext->which = Z_External_itemOrder;
584 ext->u.itemOrder = (Z_ItemOrder *)
585 odr_malloc(rr->stream, sizeof(*ext->u.update));
586 ext->u.itemOrder->which = Z_IOItemOrder_taskPackage;
587 ext->u.itemOrder->u.taskPackage = (Z_IOTaskPackage *)
588 odr_malloc(rr->stream, sizeof(Z_IOTaskPackage));
589 ext->u.itemOrder->u.taskPackage->originPart = k;
590 ext->u.itemOrder->u.taskPackage->targetPart = targetPart;
593 targetPart->itemRequest =
594 z_ext_record_xml(rr->stream, xml_in_response,
595 strlen(xml_in_response));
597 targetPart->itemRequest = 0;
599 targetPart->statusOrErrorReport = 0;
600 targetPart->auxiliaryStatus = 0;
604 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
606 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
607 yaz_log(log_level, "Received DB Update");
608 if (up->which == Z_IUUpdate_esRequest)
610 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
611 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
612 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
614 yaz_log(log_level, "action");
617 switch (*toKeep->action)
619 case Z_IUOriginPartToKeep_recordInsert:
620 yaz_log(log_level, " recordInsert");
622 case Z_IUOriginPartToKeep_recordReplace:
623 yaz_log(log_level, " recordReplace");
625 case Z_IUOriginPartToKeep_recordDelete:
626 yaz_log(log_level, " recordDelete");
628 case Z_IUOriginPartToKeep_elementUpdate:
629 yaz_log(log_level, " elementUpdate");
631 case Z_IUOriginPartToKeep_specialUpdate:
632 yaz_log(log_level, " specialUpdate");
635 yaz_log(log_level, " unknown (" ODR_INT_PRINTF ")",
639 if (toKeep->databaseName)
641 yaz_log(log_level, "database: %s", toKeep->databaseName);
642 if (!strcmp(toKeep->databaseName, "fault"))
644 rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
645 rr->errstring = toKeep->databaseName;
647 if (!strcmp(toKeep->databaseName, "accept"))
652 Z_External *ext = (Z_External *)
653 odr_malloc(rr->stream, sizeof(*ext));
654 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
655 odr_malloc(rr->stream, sizeof(*keep));
656 Z_IUTargetPart *targetPart = (Z_IUTargetPart *)
657 odr_malloc(rr->stream, sizeof(*targetPart));
659 rr->taskPackage = (Z_TaskPackage *)
660 odr_malloc(rr->stream, sizeof(*rr->taskPackage));
661 rr->taskPackage->packageType =
662 odr_oiddup(rr->stream, rr->esr->packageType);
663 rr->taskPackage->packageName = 0;
664 rr->taskPackage->userId = 0;
665 rr->taskPackage->retentionTime = 0;
666 rr->taskPackage->permissions = 0;
667 rr->taskPackage->description = 0;
668 rr->taskPackage->targetReference = (Odr_oct *)
669 odr_malloc(rr->stream, sizeof(Odr_oct));
670 rr->taskPackage->targetReference->buf =
671 (unsigned char *) odr_strdup(rr->stream, "123");
672 rr->taskPackage->targetReference->len =
673 rr->taskPackage->targetReference->size =
674 strlen((char *) (rr->taskPackage->targetReference->buf));
675 rr->taskPackage->creationDateTime = 0;
676 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
677 rr->taskPackage->packageDiagnostics = 0;
678 rr->taskPackage->taskSpecificParameters = ext;
680 ext->direct_reference =
681 odr_oiddup(rr->stream, rr->esr->packageType);
682 ext->indirect_reference = 0;
684 ext->which = Z_External_update;
685 ext->u.update = (Z_IUUpdate *)
686 odr_malloc(rr->stream, sizeof(*ext->u.update));
687 ext->u.update->which = Z_IUUpdate_taskPackage;
688 ext->u.update->u.taskPackage = (Z_IUUpdateTaskPackage *)
689 odr_malloc(rr->stream, sizeof(Z_IUUpdateTaskPackage));
690 ext->u.update->u.taskPackage->originPart = keep;
691 ext->u.update->u.taskPackage->targetPart = targetPart;
693 keep->action = odr_intdup(rr->stream, *toKeep->action);
695 odr_strdup(rr->stream, toKeep->databaseName);
697 keep->elementSetName = 0;
698 keep->actionQualifier = 0;
700 targetPart->updateStatus = odr_intdup(rr->stream, 1);
701 targetPart->num_globalDiagnostics = 0;
702 targetPart->globalDiagnostics = (Z_DiagRec **) odr_nullval();
703 targetPart->num_taskPackageRecords = 1;
704 targetPart->taskPackageRecords =
705 (Z_IUTaskPackageRecordStructure **)
706 odr_malloc(rr->stream,
707 sizeof(Z_IUTaskPackageRecordStructure *));
708 targetPart->taskPackageRecords[0] =
709 (Z_IUTaskPackageRecordStructure *)
710 odr_malloc(rr->stream,
711 sizeof(Z_IUTaskPackageRecordStructure));
713 targetPart->taskPackageRecords[0]->which =
714 Z_IUTaskPackageRecordStructure_record;
715 targetPart->taskPackageRecords[0]->u.record =
716 z_ext_record_sutrs(rr->stream, "test", 4);
717 targetPart->taskPackageRecords[0]->correlationInfo = 0;
718 targetPart->taskPackageRecords[0]->recordStatus =
719 odr_intdup(rr->stream,
720 Z_IUTaskPackageRecordStructure_success);
721 targetPart->taskPackageRecords[0]->num_supplementalDiagnostics
724 targetPart->taskPackageRecords[0]->supplementalDiagnostics = 0;
729 for (i = 0; i < notToKeep->num; i++)
731 Z_External *rec = notToKeep->elements[i]->record;
733 if (rec->direct_reference)
735 char oid_name_str[OID_STR_MAX];
737 = oid_name = yaz_oid_to_string_buf(
738 rec->direct_reference, 0,
741 yaz_log(log_level, "record %d type %s", i,
746 case Z_External_sutrs:
747 if (rec->u.octet_aligned->len > 170)
748 yaz_log(log_level, "%d bytes:\n%.168s ...",
752 yaz_log(log_level, "%d bytes:\n%s",
756 case Z_External_octet :
757 if (rec->u.octet_aligned->len > 170)
758 yaz_log(log_level, "%d bytes:\n%.168s ...",
759 rec->u.octet_aligned->len,
760 rec->u.octet_aligned->buf);
762 yaz_log(log_level, "%d bytes\n%s",
763 rec->u.octet_aligned->len,
764 rec->u.octet_aligned->buf);
773 /* result set delete */
774 int ztest_delete(void *handle, bend_delete_rr *rr)
776 if (rr->num_setnames == 1 && !strcmp(rr->setnames[0], "1"))
777 rr->delete_status = Z_DeleteStatus_success;
779 rr->delete_status = Z_DeleteStatus_resultSetDidNotExist;
783 /* Our sort handler really doesn't sort... */
784 int ztest_sort(void *handle, bend_sort_rr *rr)
787 rr->sort_status = Z_SortResponse_success;
792 /* present request handler */
793 int ztest_present(void *handle, bend_present_rr *rr)
795 struct session_handle *sh = (struct session_handle*) handle;
796 struct result_set *set = get_set(sh, rr->setname);
800 rr->errcode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
801 rr->errstring = odr_strdup(rr->stream, rr->setname);
804 do_delay(&set->present_delay);
808 /* retrieval of a single record (present, and piggy back search) */
809 int ztest_fetch(void *handle, bend_fetch_rr *r)
811 struct session_handle *sh = (struct session_handle*) handle;
813 const Odr_oid *oid = r->request_format;
814 struct result_set *set = get_set(sh, r->setname);
818 r->errcode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
819 r->errstring = odr_strdup(r->stream, r->setname);
822 do_delay(&set->fetch_delay);
824 r->basename = set->db;
825 r->output_format = r->request_format;
827 if (r->number < 1 || r->number > set->hits)
829 r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
832 if (!oid || yaz_oid_is_iso2709(oid))
834 cp = dummy_marc_record(r->number, r->stream);
837 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
838 r->surrogate_flag = 1;
845 r->output_format = odr_oiddup(r->stream, yaz_oid_recsyn_usmarc);
848 else if (!oid_oidcmp(oid, yaz_oid_recsyn_opac))
850 cp = dummy_marc_record(r->number, r->stream);
853 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
854 r->surrogate_flag = 1;
857 r->record = (char *) dummy_opac(r->number, r->stream, cp);
860 else if (!oid_oidcmp(oid, yaz_oid_recsyn_sutrs))
862 /* this section returns a small record */
865 sprintf(buf, "This is dummy SUTRS record number %d\n", r->number);
867 r->len = strlen(buf);
868 r->record = (char *) odr_malloc(r->stream, r->len+1);
869 strcpy(r->record, buf);
871 else if (!oid_oidcmp(oid, yaz_oid_recsyn_grs_1))
874 r->record = (char*) dummy_grs_record(r->number, r->stream);
877 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
878 r->surrogate_flag = 1;
882 else if (!oid_oidcmp(oid, yaz_oid_recsyn_postscript))
888 sprintf(fname, "part.%d.ps", r->number);
889 f = fopen(fname, "rb");
892 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
893 r->surrogate_flag = 1;
896 fseek(f, 0L, SEEK_END);
898 if (size <= 0 || size >= 5000000)
900 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
901 r->surrogate_flag = 1;
903 fseek(f, 0L, SEEK_SET);
904 r->record = (char*) odr_malloc(r->stream, size);
906 if (fread(r->record, size, 1, f) != 1)
908 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
909 r->surrogate_flag = 1;
913 else if (!oid_oidcmp(oid, yaz_oid_recsyn_xml))
915 if ((cp = dummy_xml_record(r->number, r->stream)))
922 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
923 r->surrogate_flag = 1;
929 char buf[OID_STR_MAX];
930 r->errcode = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
931 r->errstring = odr_strdup(r->stream, oid_oid_to_dotstring(oid, buf));
939 * silly dummy-scan what reads words from a file.
941 int ztest_scan(void *handle, bend_scan_rr *q)
944 static struct scan_entry list[200];
945 static char entries[200][80];
949 int term_position_req = q->term_position;
950 int num_entries_req = q->num_entries;
952 /* Throw Database unavailable if other than Default or Slow */
953 if (!yaz_matchstr(q->basenames[0], "Default"))
954 ; /* Default is OK in our test */
955 else if (check_slow(q->basenames[0], 0 /* no assoc for scan */))
959 q->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
960 q->errstring = q->basenames[0];
967 q->status = BEND_SCAN_SUCCESS;
968 if (!f && !(f = fopen("dummy-words", "r")))
970 perror("dummy-words");
973 if (q->num_entries > 200)
975 q->errcode = YAZ_BIB1_RESOURCES_EXHAUSTED_NO_RESULTS_AVAILABLE;
981 if (q->term->term->which != Z_Term_general)
983 q->errcode = YAZ_BIB1_TERM_TYPE_UNSUPP;
986 if (*q->step_size != 0)
988 q->errcode = YAZ_BIB1_ONLY_ZERO_STEP_SIZE_SUPPORTED_FOR_SCAN;
991 len = q->term->term->u.general->len;
992 if (len >= (int ) sizeof(term))
993 len = sizeof(term)-1;
994 memcpy(term, q->term->term->u.general->buf, len);
997 else if (q->scanClause)
999 strncpy(term, q->scanClause, sizeof(term)-1);
1000 term[sizeof(term)-1] = '\0';
1005 for (p = term; *p; p++)
1006 if (islower(*(unsigned char *) p))
1009 fseek(f, 0, SEEK_SET);
1012 for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
1013 i++, pos < 199 ? pos++ : (pos = 0))
1015 if (!q->num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
1017 if ((q->term_position = term_position_req) > i + 1)
1019 q->term_position = i + 1;
1020 q->status = BEND_SCAN_PARTIAL;
1022 for (; q->num_entries < q->term_position; q->num_entries++)
1026 po = pos - q->term_position + q->num_entries+1; /* find pos */
1030 if (!strcmp(term, "SD") && q->num_entries == 2)
1032 list[q->num_entries].term = entries[pos];
1033 list[q->num_entries].occurrences = -1;
1034 list[q->num_entries].errcode =
1035 YAZ_BIB1_SCAN_UNSUPP_VALUE_OF_POSITION_IN_RESPONSE;
1036 list[q->num_entries].errstring = "SD for Scan Term";
1040 list[q->num_entries].term = entries[po];
1041 list[q->num_entries].occurrences = hits[po];
1045 else if (q->num_entries)
1047 list[q->num_entries].term = entries[pos];
1048 list[q->num_entries].occurrences = hits[pos];
1051 if (q->num_entries >= num_entries_req)
1055 q->status = BEND_SCAN_PARTIAL;
1059 int ztest_explain(void *handle, bend_explain_rr *rr)
1061 if (rr->database && !strcmp(rr->database, "Default"))
1063 rr->explain_buf = "<explain>\n"
1065 "\t\t<host>localhost</host>\n"
1066 "\t\t<port>210</port>\n"
1073 int ztest_update(void *handle, bend_update_rr *rr)
1075 rr->operation_status = "success";
1079 bend_initresult *bend_init(bend_initrequest *q)
1081 bend_initresult *r = (bend_initresult *)
1082 odr_malloc(q->stream, sizeof(*r));
1083 struct session_handle *sh = xmalloc(sizeof(*sh));
1085 sh->result_sets = 0;
1089 log_level=yaz_log_module_level("ztest");
1095 r->handle = sh; /* tell GFS about our handle */
1096 q->bend_sort = ztest_sort; /* register sort handler */
1097 q->bend_search = ztest_search; /* register search handler */
1098 q->bend_present = ztest_present; /* register present handle */
1099 q->bend_esrequest = ztest_esrequest;
1100 q->bend_delete = ztest_delete;
1101 q->bend_fetch = ztest_fetch;
1102 q->bend_scan = ztest_scan;
1104 q->bend_explain = ztest_explain;
1106 q->bend_srw_scan = ztest_scan;
1107 q->bend_srw_update = ztest_update;
1109 q->query_charset = "ISO-8859-1";
1110 q->records_in_same_charset = 0;
1115 void bend_close(void *handle)
1117 struct session_handle *sh = (struct session_handle*) handle;
1119 xfree(sh); /* release our session */
1123 int main(int argc, char **argv)
1125 return statserv_main(argc, argv, bend_init, bend_close);
1130 * c-file-style: "Stroustrup"
1131 * indent-tabs-mode: nil
1133 * vim: shiftwidth=4 tabstop=8 expandtab