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>
36 static int log_level=0;
37 static int log_level_set=0;
48 struct delay search_delay;
49 struct delay present_delay;
50 struct delay fetch_delay;
51 struct result_set *next;
54 struct session_handle {
55 struct result_set *result_sets;
58 int ztest_search(void *handle, bend_search_rr *rr);
59 int ztest_sort(void *handle, bend_sort_rr *rr);
60 int ztest_present(void *handle, bend_present_rr *rr);
61 int ztest_esrequest(void *handle, bend_esrequest_rr *rr);
62 int ztest_delete(void *handle, bend_delete_rr *rr);
64 static struct result_set *get_set(struct session_handle *sh, const char *name)
66 struct result_set *set = sh->result_sets;
67 for (; set; set = set->next)
68 if (!strcmp(name, set->name))
73 static void remove_sets(struct session_handle *sh)
75 struct result_set *set = sh->result_sets;
78 struct result_set *set_next = set->next;
87 /** \brief use term value as hit count
88 \param s RPN structure
89 \return >= 0: search term number or -1: not found
91 Traverse RPN tree 'in order' and use term value as hit count.
92 Only terms that looks a numeric is used.. Returns -1 if
93 no sub tree has a hit count term
95 static Odr_int get_term_hit(Z_RPNStructure *s)
100 case Z_RPNStructure_simple:
101 if (s->u.simple->which == Z_Operand_APT)
103 Z_AttributesPlusTerm *apt = s->u.simple->u.attributesPlusTerm;
104 if (apt->term->which == Z_Term_general)
106 Odr_oct *oct = apt->term->u.general;
107 if (oct->len > 0 && oct->buf[0] >= '0' && oct->buf[0] <= '9')
109 WRBUF hits_str = wrbuf_alloc();
110 wrbuf_write(hits_str, (const char *) oct->buf, oct->len);
111 h = odr_atoi(wrbuf_cstr(hits_str));
112 wrbuf_destroy(hits_str);
117 case Z_RPNStructure_complex:
118 h = get_term_hit(s->u.complex->s1);
120 h = get_term_hit(s->u.complex->s2);
126 /** \brief gets hit count for numeric terms in RPN queries
128 \return number of hits (random or number for term)
130 This is just for testing.. A real database of course uses
131 the content of a database to establish a value.. In our case, we
132 have a way to trigger a certain hit count. Good for testing of
133 client applications etc
135 static Odr_int get_hit_count(Z_Query *q)
137 if (q->which == Z_Query_type_1 || q->which == Z_Query_type_101)
140 h = get_term_hit(q->u.type_1->RPNStructure);
149 /** \brief checks if it's a dummy Slow database
150 \param basename database name to check
151 \param association backend association (or NULL if not available)
152 \retval 1 is slow database
153 \retval 0 is not a slow database
155 The Slow database is for testing.. It allows us to simulate
158 static int check_slow(const char *basename, bend_association association)
160 if (strncmp(basename, "Slow", 4) == 0)
165 sscanf(basename+4, "%d", &w);
166 /* wait up to 3 seconds and check if connection is still alive */
167 for (i = 0; i < w; i++)
169 if (association && !bend_assoc_is_alive(association))
171 yaz_log(YLOG_LOG, "search aborted");
182 static int strcmp_prefix(const char *s, const char *p)
184 size_t l = strlen(p);
185 if (strlen(s) >= l && !memcmp(s, p, l))
190 static void init_delay(struct delay *delayp)
192 delayp->d1 = delayp->d2 = 0.0;
195 static int parse_delay(struct delay *delayp, const char *value)
197 if (sscanf(value, "%lf:%lf", &delayp->d1, &delayp->d2) == 2)
199 else if (sscanf(value, "%lf", &delayp->d1) == 1)
206 static void ztest_sleep(double d)
209 Sleep( (DWORD) (d * 1000));
212 tv.tv_sec = floor(d);
213 tv.tv_usec = (d - floor(d)) * 1000000;
214 select(0, 0, 0, 0, &tv);
218 static void do_delay(const struct delay *delayp)
220 double d = delayp->d1;
225 d += (rand()) * (delayp->d2 - d) / RAND_MAX;
230 Z_FacetList *extract_facet_request(ODR odr, Z_OtherInformation *search_input) {
231 Z_OtherInformation **oi;
232 Z_FacetList *facet_list = yaz_oi_get_facetlist_oid(oi, odr, yaz_oid_userinfo_facet_1, 1, 0);
237 Z_Term *term_new(ODR odr, const char *cstr) {
238 Z_Term *term = odr_malloc(odr, sizeof(*term));
239 term->which = Z_Term_characterString;
240 term->u.characterString = odr_strdup(odr, cstr);
244 static void addterms(ODR odr, Z_FacetField *facet_field) {
247 facet_field->num_terms = 3;
248 facet_field->terms = odr_malloc(odr, facet_field->num_terms * sizeof(*facet_field->terms));
249 for (index = 0; index < facet_field->num_terms; index++) {
250 Z_FacetTerm *facet_term = odr_malloc(odr, sizeof(*facet_term));
251 facet_term->count = odr_malloc(odr, sizeof(*facet_term->count));
252 *facet_term->count = count;
253 facet_term->term = term_new(odr, "key");
255 facet_field->terms[index] = facet_term;
258 Z_OtherInformation *build_facet_response(ODR odr, Z_FacetList *facet_list) {
260 Z_OtherInformation *oi = odr_malloc(odr, sizeof(*oi));
261 Z_OtherInformationUnit *oiu = odr_malloc(odr, sizeof(*oiu));
262 for (index = 0; index < facet_list->num; index++) {
263 addterms(odr, facet_list->elements[index]);
265 oi->list = odr_malloc(odr, sizeof(*oi->list));
267 oiu->which = Z_OtherInfo_externallyDefinedInfo;
268 oiu->information.externallyDefinedInfo = odr_malloc(odr, sizeof(*oiu->information.externallyDefinedInfo));
269 oiu->information.externallyDefinedInfo->direct_reference = odr_oiddup(odr, yaz_oid_userinfo_facet_1);
271 oiu->information.externallyDefinedInfo->u.facetList = facet_list;
276 int ztest_search(void *handle, bend_search_rr *rr)
278 struct session_handle *sh = (struct session_handle*) handle;
279 struct result_set *new_set;
280 const char *db, *db_sep;
282 if (rr->num_bases != 1)
284 rr->errcode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
288 db = rr->basenames[0];
290 /* Allow Default, db.* and Slow */
291 if (strcmp_prefix(db, "Default"))
292 ; /* Default is OK in our test */
293 else if (strcmp_prefix(db, "db"))
294 ; /* db.* is OK in our test */
295 else if (check_slow(rr->basenames[0], rr->association))
297 rr->estimated_hit_count = 1;
301 rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
302 rr->errstring = rr->basenames[0];
306 new_set = get_set(sh, rr->setname);
309 if (!rr->replace_set)
311 rr->errcode = YAZ_BIB1_RESULT_SET_EXISTS_AND_REPLACE_INDICATOR_OFF;
318 new_set = xmalloc(sizeof(*new_set));
319 new_set->next = sh->result_sets;
320 sh->result_sets = new_set;
321 new_set->name = xstrdup(rr->setname);
324 new_set->db = xstrdup(db);
325 init_delay(&new_set->search_delay);
326 init_delay(&new_set->present_delay);
327 init_delay(&new_set->fetch_delay);
329 db_sep = strchr(db, '?');
334 int no_parms = yaz_uri_to_array(db_sep+1, rr->stream, &names, &values);
336 for (i = 0; i < no_parms; i++)
338 const char *name = names[i];
339 const char *value = values[i];
340 if (!strcmp(name, "seed"))
342 else if (!strcmp(name, "search-delay"))
343 parse_delay(&new_set->search_delay, value);
344 else if (!strcmp(name, "present-delay"))
345 parse_delay(&new_set->present_delay, value);
346 else if (!strcmp(name, "fetch-delay"))
347 parse_delay(&new_set->fetch_delay, value);
350 rr->errcode = YAZ_BIB1_SERVICE_UNSUPP_FOR_THIS_DATABASE;
351 rr->errstring = odr_strdup(rr->stream, name);
359 WRBUF response_xml = wrbuf_alloc();
360 wrbuf_puts(response_xml, "<extra>");
361 for (a = rr->extra_args; a; a = a->next)
363 wrbuf_puts(response_xml, "<extra name=\"");
364 wrbuf_xmlputs(response_xml, a->name);
365 wrbuf_puts(response_xml, "\"");
368 wrbuf_puts(response_xml, " value=\"");
369 wrbuf_xmlputs(response_xml, a->value);
370 wrbuf_puts(response_xml, "\"");
372 wrbuf_puts(response_xml, "/>");
374 wrbuf_puts(response_xml, "</extra>");
375 rr->extra_response_data =
376 odr_strdup(rr->stream, wrbuf_cstr(response_xml));
377 wrbuf_destroy(response_xml);
379 rr->hits = get_hit_count(rr->query);
383 /* TODO Not general. Only handles one (Facet) OtherInformation. Overwrite */
384 Z_FacetList *facet_list = extract_facet_request(rr->stream, rr->search_input);
386 rr->search_info = build_facet_response(rr->stream, facet_list);
389 do_delay(&new_set->search_delay);
390 new_set->hits = rr->hits;
396 /* this huge function handles extended services */
397 int ztest_esrequest(void *handle, bend_esrequest_rr *rr)
399 /* user-defined handle - created in bend_init */
400 int *counter = (int*) handle;
402 yaz_log(log_level, "ESRequest no %d", *counter);
406 if (rr->esr->packageName)
407 yaz_log(log_level, "packagename: %s", rr->esr->packageName);
408 yaz_log(log_level, "Waitaction: " ODR_INT_PRINTF, *rr->esr->waitAction);
411 yaz_log(log_level, "function: " ODR_INT_PRINTF, *rr->esr->function);
413 if (!rr->esr->taskSpecificParameters)
415 yaz_log(log_level, "No task specific parameters");
417 else if (rr->esr->taskSpecificParameters->which == Z_External_itemOrder)
419 Z_ItemOrder *it = rr->esr->taskSpecificParameters->u.itemOrder;
420 yaz_log(log_level, "Received ItemOrder");
421 if (it->which == Z_IOItemOrder_esRequest)
423 Z_IORequest *ir = it->u.esRequest;
424 Z_IOOriginPartToKeep *k = ir->toKeep;
425 Z_IOOriginPartNotToKeep *n = ir->notToKeep;
426 const char *xml_in_response = 0;
430 if (k->contact->name)
431 yaz_log(log_level, "contact name %s", k->contact->name);
432 if (k->contact->phone)
433 yaz_log(log_level, "contact phone %s", k->contact->phone);
434 if (k->contact->email)
435 yaz_log(log_level, "contact email %s", k->contact->email);
439 yaz_log(log_level, "Billing info (not shown)");
442 if (n->resultSetItem)
444 yaz_log(log_level, "resultsetItem");
445 yaz_log(log_level, "setId: %s", n->resultSetItem->resultSetId);
446 yaz_log(log_level, "item: " ODR_INT_PRINTF, *n->resultSetItem->item);
450 Z_External *r = (Z_External*) n->itemRequest;
451 ILL_ItemRequest *item_req = 0;
452 ILL_APDU *ill_apdu = 0;
453 if (r->direct_reference)
455 char oid_name_str[OID_STR_MAX];
457 const char *oid_name =
458 yaz_oid_to_string_buf(r->direct_reference,
459 &oclass, oid_name_str);
461 yaz_log(log_level, "OID %s", oid_name);
462 if (!oid_oidcmp(r->direct_reference, yaz_oid_recsyn_xml))
464 yaz_log(log_level, "ILL XML request");
465 if (r->which == Z_External_octet)
466 yaz_log(log_level, "%.*s",
467 r->u.octet_aligned->len,
468 r->u.octet_aligned->buf);
469 xml_in_response = "<dummy>x</dummy>";
471 if (!oid_oidcmp(r->direct_reference,
472 yaz_oid_general_isoill_1))
474 yaz_log(log_level, "Decode ItemRequest begin");
475 if (r->which == ODR_EXTERNAL_single)
477 odr_setbuf(rr->decode,
478 (char *) r->u.single_ASN1_type->buf,
479 r->u.single_ASN1_type->len, 0);
481 if (!ill_ItemRequest(rr->decode, &item_req, 0, 0))
484 "Couldn't decode ItemRequest %s near %ld",
485 odr_errmsg(odr_geterror(rr->decode)),
486 (long) odr_offset(rr->decode));
489 yaz_log(log_level, "Decode ItemRequest OK");
492 ill_ItemRequest(rr->print, &item_req, 0,
494 odr_reset(rr->print);
497 if (!item_req && r->which == ODR_EXTERNAL_single)
499 yaz_log(log_level, "Decode ILL APDU begin");
500 odr_setbuf(rr->decode,
501 (char*) r->u.single_ASN1_type->buf,
502 r->u.single_ASN1_type->len, 0);
504 if (!ill_APDU(rr->decode, &ill_apdu, 0, 0))
507 "Couldn't decode ILL APDU %s near %ld",
508 odr_errmsg(odr_geterror(rr->decode)),
509 (long) odr_offset(rr->decode));
510 yaz_log(log_level, "PDU dump:");
511 odr_dumpBER(yaz_log_file(),
512 (char *) r->u.single_ASN1_type->buf,
513 r->u.single_ASN1_type->len);
516 yaz_log(log_level, "Decode ILL APDU OK");
519 ill_APDU(rr->print, &ill_apdu, 0,
521 odr_reset(rr->print);
528 yaz_log(log_level, "ILL protocol version = "
530 *item_req->protocol_version_num);
536 Z_External *ext = (Z_External *)
537 odr_malloc(rr->stream, sizeof(*ext));
538 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
539 odr_malloc(rr->stream, sizeof(*keep));
540 Z_IOTargetPart *targetPart = (Z_IOTargetPart *)
541 odr_malloc(rr->stream, sizeof(*targetPart));
543 rr->taskPackage = (Z_TaskPackage *)
544 odr_malloc(rr->stream, sizeof(*rr->taskPackage));
545 rr->taskPackage->packageType =
546 odr_oiddup(rr->stream, rr->esr->packageType);
547 rr->taskPackage->packageName = 0;
548 rr->taskPackage->userId = 0;
549 rr->taskPackage->retentionTime = 0;
550 rr->taskPackage->permissions = 0;
551 rr->taskPackage->description = 0;
552 rr->taskPackage->targetReference = (Odr_oct *)
553 odr_malloc(rr->stream, sizeof(Odr_oct));
554 rr->taskPackage->targetReference->buf =
555 (unsigned char *) odr_strdup(rr->stream, "911");
556 rr->taskPackage->targetReference->len =
557 rr->taskPackage->targetReference->size =
558 strlen((char *) (rr->taskPackage->targetReference->buf));
559 rr->taskPackage->creationDateTime = 0;
560 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
561 rr->taskPackage->packageDiagnostics = 0;
562 rr->taskPackage->taskSpecificParameters = ext;
564 ext->direct_reference =
565 odr_oiddup(rr->stream, rr->esr->packageType);
566 ext->indirect_reference = 0;
568 ext->which = Z_External_itemOrder;
569 ext->u.itemOrder = (Z_ItemOrder *)
570 odr_malloc(rr->stream, sizeof(*ext->u.update));
571 ext->u.itemOrder->which = Z_IOItemOrder_taskPackage;
572 ext->u.itemOrder->u.taskPackage = (Z_IOTaskPackage *)
573 odr_malloc(rr->stream, sizeof(Z_IOTaskPackage));
574 ext->u.itemOrder->u.taskPackage->originPart = k;
575 ext->u.itemOrder->u.taskPackage->targetPart = targetPart;
578 targetPart->itemRequest =
579 z_ext_record_xml(rr->stream, xml_in_response,
580 strlen(xml_in_response));
582 targetPart->itemRequest = 0;
584 targetPart->statusOrErrorReport = 0;
585 targetPart->auxiliaryStatus = 0;
589 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
591 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
592 yaz_log(log_level, "Received DB Update");
593 if (up->which == Z_IUUpdate_esRequest)
595 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
596 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
597 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
599 yaz_log(log_level, "action");
602 switch (*toKeep->action)
604 case Z_IUOriginPartToKeep_recordInsert:
605 yaz_log(log_level, " recordInsert");
607 case Z_IUOriginPartToKeep_recordReplace:
608 yaz_log(log_level, " recordReplace");
610 case Z_IUOriginPartToKeep_recordDelete:
611 yaz_log(log_level, " recordDelete");
613 case Z_IUOriginPartToKeep_elementUpdate:
614 yaz_log(log_level, " elementUpdate");
616 case Z_IUOriginPartToKeep_specialUpdate:
617 yaz_log(log_level, " specialUpdate");
620 yaz_log(log_level, " unknown (" ODR_INT_PRINTF ")",
624 if (toKeep->databaseName)
626 yaz_log(log_level, "database: %s", toKeep->databaseName);
627 if (!strcmp(toKeep->databaseName, "fault"))
629 rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
630 rr->errstring = toKeep->databaseName;
632 if (!strcmp(toKeep->databaseName, "accept"))
637 Z_External *ext = (Z_External *)
638 odr_malloc(rr->stream, sizeof(*ext));
639 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
640 odr_malloc(rr->stream, sizeof(*keep));
641 Z_IUTargetPart *targetPart = (Z_IUTargetPart *)
642 odr_malloc(rr->stream, sizeof(*targetPart));
644 rr->taskPackage = (Z_TaskPackage *)
645 odr_malloc(rr->stream, sizeof(*rr->taskPackage));
646 rr->taskPackage->packageType =
647 odr_oiddup(rr->stream, rr->esr->packageType);
648 rr->taskPackage->packageName = 0;
649 rr->taskPackage->userId = 0;
650 rr->taskPackage->retentionTime = 0;
651 rr->taskPackage->permissions = 0;
652 rr->taskPackage->description = 0;
653 rr->taskPackage->targetReference = (Odr_oct *)
654 odr_malloc(rr->stream, sizeof(Odr_oct));
655 rr->taskPackage->targetReference->buf =
656 (unsigned char *) odr_strdup(rr->stream, "123");
657 rr->taskPackage->targetReference->len =
658 rr->taskPackage->targetReference->size =
659 strlen((char *) (rr->taskPackage->targetReference->buf));
660 rr->taskPackage->creationDateTime = 0;
661 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
662 rr->taskPackage->packageDiagnostics = 0;
663 rr->taskPackage->taskSpecificParameters = ext;
665 ext->direct_reference =
666 odr_oiddup(rr->stream, rr->esr->packageType);
667 ext->indirect_reference = 0;
669 ext->which = Z_External_update;
670 ext->u.update = (Z_IUUpdate *)
671 odr_malloc(rr->stream, sizeof(*ext->u.update));
672 ext->u.update->which = Z_IUUpdate_taskPackage;
673 ext->u.update->u.taskPackage = (Z_IUUpdateTaskPackage *)
674 odr_malloc(rr->stream, sizeof(Z_IUUpdateTaskPackage));
675 ext->u.update->u.taskPackage->originPart = keep;
676 ext->u.update->u.taskPackage->targetPart = targetPart;
678 keep->action = odr_intdup(rr->stream, *toKeep->action);
680 odr_strdup(rr->stream, toKeep->databaseName);
682 keep->elementSetName = 0;
683 keep->actionQualifier = 0;
685 targetPart->updateStatus = odr_intdup(rr->stream, 1);
686 targetPart->num_globalDiagnostics = 0;
687 targetPart->globalDiagnostics = (Z_DiagRec **) odr_nullval();
688 targetPart->num_taskPackageRecords = 1;
689 targetPart->taskPackageRecords =
690 (Z_IUTaskPackageRecordStructure **)
691 odr_malloc(rr->stream,
692 sizeof(Z_IUTaskPackageRecordStructure *));
693 targetPart->taskPackageRecords[0] =
694 (Z_IUTaskPackageRecordStructure *)
695 odr_malloc(rr->stream,
696 sizeof(Z_IUTaskPackageRecordStructure));
698 targetPart->taskPackageRecords[0]->which =
699 Z_IUTaskPackageRecordStructure_record;
700 targetPart->taskPackageRecords[0]->u.record =
701 z_ext_record_sutrs(rr->stream, "test", 4);
702 targetPart->taskPackageRecords[0]->correlationInfo = 0;
703 targetPart->taskPackageRecords[0]->recordStatus =
704 odr_intdup(rr->stream,
705 Z_IUTaskPackageRecordStructure_success);
706 targetPart->taskPackageRecords[0]->num_supplementalDiagnostics
709 targetPart->taskPackageRecords[0]->supplementalDiagnostics = 0;
714 for (i = 0; i < notToKeep->num; i++)
716 Z_External *rec = notToKeep->elements[i]->record;
718 if (rec->direct_reference)
720 char oid_name_str[OID_STR_MAX];
722 = oid_name = yaz_oid_to_string_buf(
723 rec->direct_reference, 0,
726 yaz_log(log_level, "record %d type %s", i,
731 case Z_External_sutrs:
732 if (rec->u.octet_aligned->len > 170)
733 yaz_log(log_level, "%d bytes:\n%.168s ...",
737 yaz_log(log_level, "%d bytes:\n%s",
741 case Z_External_octet :
742 if (rec->u.octet_aligned->len > 170)
743 yaz_log(log_level, "%d bytes:\n%.168s ...",
744 rec->u.octet_aligned->len,
745 rec->u.octet_aligned->buf);
747 yaz_log(log_level, "%d bytes\n%s",
748 rec->u.octet_aligned->len,
749 rec->u.octet_aligned->buf);
758 /* result set delete */
759 int ztest_delete(void *handle, bend_delete_rr *rr)
761 if (rr->num_setnames == 1 && !strcmp(rr->setnames[0], "1"))
762 rr->delete_status = Z_DeleteStatus_success;
764 rr->delete_status = Z_DeleteStatus_resultSetDidNotExist;
768 /* Our sort handler really doesn't sort... */
769 int ztest_sort(void *handle, bend_sort_rr *rr)
772 rr->sort_status = Z_SortResponse_success;
777 /* present request handler */
778 int ztest_present(void *handle, bend_present_rr *rr)
780 struct session_handle *sh = (struct session_handle*) handle;
781 struct result_set *set = get_set(sh, rr->setname);
785 rr->errcode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
786 rr->errstring = odr_strdup(rr->stream, rr->setname);
789 do_delay(&set->present_delay);
793 /* retrieval of a single record (present, and piggy back search) */
794 int ztest_fetch(void *handle, bend_fetch_rr *r)
796 struct session_handle *sh = (struct session_handle*) handle;
798 const Odr_oid *oid = r->request_format;
799 struct result_set *set = get_set(sh, r->setname);
803 r->errcode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
804 r->errstring = odr_strdup(r->stream, r->setname);
807 do_delay(&set->fetch_delay);
809 r->basename = set->db;
810 r->output_format = r->request_format;
812 if (r->number < 1 || r->number > set->hits)
814 r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
817 if (!oid || yaz_oid_is_iso2709(oid))
819 cp = dummy_marc_record(r->number, r->stream);
822 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
823 r->surrogate_flag = 1;
830 r->output_format = odr_oiddup(r->stream, yaz_oid_recsyn_usmarc);
833 else if (!oid_oidcmp(oid, yaz_oid_recsyn_opac))
835 cp = dummy_marc_record(r->number, r->stream);
838 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
839 r->surrogate_flag = 1;
842 r->record = (char *) dummy_opac(r->number, r->stream, cp);
845 else if (!oid_oidcmp(oid, yaz_oid_recsyn_sutrs))
847 /* this section returns a small record */
850 sprintf(buf, "This is dummy SUTRS record number %d\n", r->number);
852 r->len = strlen(buf);
853 r->record = (char *) odr_malloc(r->stream, r->len+1);
854 strcpy(r->record, buf);
856 else if (!oid_oidcmp(oid, yaz_oid_recsyn_grs_1))
859 r->record = (char*) dummy_grs_record(r->number, r->stream);
862 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
863 r->surrogate_flag = 1;
867 else if (!oid_oidcmp(oid, yaz_oid_recsyn_postscript))
873 sprintf(fname, "part.%d.ps", r->number);
874 f = fopen(fname, "rb");
877 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
878 r->surrogate_flag = 1;
881 fseek(f, 0L, SEEK_END);
883 if (size <= 0 || size >= 5000000)
885 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
886 r->surrogate_flag = 1;
888 fseek(f, 0L, SEEK_SET);
889 r->record = (char*) odr_malloc(r->stream, size);
891 if (fread(r->record, size, 1, f) != 1)
893 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
894 r->surrogate_flag = 1;
898 else if (!oid_oidcmp(oid, yaz_oid_recsyn_xml))
900 if ((cp = dummy_xml_record(r->number, r->stream)))
907 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
908 r->surrogate_flag = 1;
914 char buf[OID_STR_MAX];
915 r->errcode = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
916 r->errstring = odr_strdup(r->stream, oid_oid_to_dotstring(oid, buf));
924 * silly dummy-scan what reads words from a file.
926 int ztest_scan(void *handle, bend_scan_rr *q)
929 static struct scan_entry list[200];
930 static char entries[200][80];
934 int term_position_req = q->term_position;
935 int num_entries_req = q->num_entries;
937 /* Throw Database unavailable if other than Default or Slow */
938 if (!yaz_matchstr(q->basenames[0], "Default"))
939 ; /* Default is OK in our test */
940 else if (check_slow(q->basenames[0], 0 /* no assoc for scan */))
944 q->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
945 q->errstring = q->basenames[0];
952 q->status = BEND_SCAN_SUCCESS;
953 if (!f && !(f = fopen("dummy-words", "r")))
955 perror("dummy-words");
958 if (q->num_entries > 200)
960 q->errcode = YAZ_BIB1_RESOURCES_EXHAUSTED_NO_RESULTS_AVAILABLE;
966 if (q->term->term->which != Z_Term_general)
968 q->errcode = YAZ_BIB1_TERM_TYPE_UNSUPP;
971 if (*q->step_size != 0)
973 q->errcode = YAZ_BIB1_ONLY_ZERO_STEP_SIZE_SUPPORTED_FOR_SCAN;
976 len = q->term->term->u.general->len;
977 if (len >= (int ) sizeof(term))
978 len = sizeof(term)-1;
979 memcpy(term, q->term->term->u.general->buf, len);
982 else if (q->scanClause)
984 strncpy(term, q->scanClause, sizeof(term)-1);
985 term[sizeof(term)-1] = '\0';
990 for (p = term; *p; p++)
991 if (islower(*(unsigned char *) p))
994 fseek(f, 0, SEEK_SET);
997 for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
998 i++, pos < 199 ? pos++ : (pos = 0))
1000 if (!q->num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
1002 if ((q->term_position = term_position_req) > i + 1)
1004 q->term_position = i + 1;
1005 q->status = BEND_SCAN_PARTIAL;
1007 for (; q->num_entries < q->term_position; q->num_entries++)
1011 po = pos - q->term_position + q->num_entries+1; /* find pos */
1015 if (!strcmp(term, "SD") && q->num_entries == 2)
1017 list[q->num_entries].term = entries[pos];
1018 list[q->num_entries].occurrences = -1;
1019 list[q->num_entries].errcode =
1020 YAZ_BIB1_SCAN_UNSUPP_VALUE_OF_POSITION_IN_RESPONSE;
1021 list[q->num_entries].errstring = "SD for Scan Term";
1025 list[q->num_entries].term = entries[po];
1026 list[q->num_entries].occurrences = hits[po];
1030 else if (q->num_entries)
1032 list[q->num_entries].term = entries[pos];
1033 list[q->num_entries].occurrences = hits[pos];
1036 if (q->num_entries >= num_entries_req)
1040 q->status = BEND_SCAN_PARTIAL;
1044 int ztest_explain(void *handle, bend_explain_rr *rr)
1046 if (rr->database && !strcmp(rr->database, "Default"))
1048 rr->explain_buf = "<explain>\n"
1050 "\t\t<host>localhost</host>\n"
1051 "\t\t<port>210</port>\n"
1058 int ztest_update(void *handle, bend_update_rr *rr)
1060 rr->operation_status = "success";
1064 bend_initresult *bend_init(bend_initrequest *q)
1066 bend_initresult *r = (bend_initresult *)
1067 odr_malloc(q->stream, sizeof(*r));
1068 struct session_handle *sh = xmalloc(sizeof(*sh));
1070 sh->result_sets = 0;
1074 log_level=yaz_log_module_level("ztest");
1080 r->handle = sh; /* tell GFS about our handle */
1081 q->bend_sort = ztest_sort; /* register sort handler */
1082 q->bend_search = ztest_search; /* register search handler */
1083 q->bend_present = ztest_present; /* register present handle */
1084 q->bend_esrequest = ztest_esrequest;
1085 q->bend_delete = ztest_delete;
1086 q->bend_fetch = ztest_fetch;
1087 q->bend_scan = ztest_scan;
1089 q->bend_explain = ztest_explain;
1091 q->bend_srw_scan = ztest_scan;
1092 q->bend_srw_update = ztest_update;
1094 q->query_charset = "ISO-8859-1";
1095 q->records_in_same_charset = 0;
1100 void bend_close(void *handle)
1102 struct session_handle *sh = (struct session_handle*) handle;
1104 xfree(sh); /* release our session */
1108 int main(int argc, char **argv)
1110 return statserv_main(argc, argv, bend_init, bend_close);
1115 * c-file-style: "Stroustrup"
1116 * indent-tabs-mode: nil
1118 * vim: shiftwidth=4 tabstop=8 expandtab