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>
26 #include <yaz/backend.h>
28 #include <yaz/diagbib1.h>
32 static int log_level=0;
33 static int log_level_set=0;
44 struct delay search_delay;
45 struct delay present_delay;
46 struct delay fetch_delay;
47 struct result_set *next;
50 struct session_handle {
51 struct result_set *result_sets;
54 int ztest_search(void *handle, bend_search_rr *rr);
55 int ztest_sort(void *handle, bend_sort_rr *rr);
56 int ztest_present(void *handle, bend_present_rr *rr);
57 int ztest_esrequest(void *handle, bend_esrequest_rr *rr);
58 int ztest_delete(void *handle, bend_delete_rr *rr);
60 static struct result_set *get_set(struct session_handle *sh, const char *name)
62 struct result_set *set = sh->result_sets;
63 for (; set; set = set->next)
64 if (!strcmp(name, set->name))
69 static void remove_sets(struct session_handle *sh)
71 struct result_set *set = sh->result_sets;
74 struct result_set *set_next = set->next;
83 /** \brief use term value as hit count
84 \param s RPN structure
85 \return >= 0: search term number or -1: not found
87 Traverse RPN tree 'in order' and use term value as hit count.
88 Only terms that looks a numeric is used.. Returns -1 if
89 no sub tree has a hit count term
91 static Odr_int get_term_hit(Z_RPNStructure *s)
96 case Z_RPNStructure_simple:
97 if (s->u.simple->which == Z_Operand_APT)
99 Z_AttributesPlusTerm *apt = s->u.simple->u.attributesPlusTerm;
100 if (apt->term->which == Z_Term_general)
102 Odr_oct *oct = apt->term->u.general;
103 if (oct->len > 0 && oct->buf[0] >= '0' && oct->buf[0] <= '9')
105 WRBUF hits_str = wrbuf_alloc();
106 wrbuf_write(hits_str, (const char *) oct->buf, oct->len);
107 h = odr_atoi(wrbuf_cstr(hits_str));
108 wrbuf_destroy(hits_str);
113 case Z_RPNStructure_complex:
114 h = get_term_hit(s->u.complex->s1);
116 h = get_term_hit(s->u.complex->s2);
122 /** \brief gets hit count for numeric terms in RPN queries
124 \return number of hits (random or number for term)
126 This is just for testing.. A real database of course uses
127 the content of a database to establish a value.. In our case, we
128 have a way to trigger a certain hit count. Good for testing of
129 client applications etc
131 static Odr_int get_hit_count(Z_Query *q)
134 if (q->which == Z_Query_type_1 || q->which == Z_Query_type_101)
135 h = get_term_hit(q->u.type_1->RPNStructure);
141 /** \brief checks if it's a dummy Slow database
142 \param basename database name to check
143 \param association backend association (or NULL if not available)
144 \retval 1 is slow database
145 \retval 0 is not a slow database
147 The Slow database is for testing.. It allows us to simulate
150 static int check_slow(const char *basename, bend_association association)
152 if (strncmp(basename, "Slow", 4) == 0)
157 sscanf(basename+4, "%d", &w);
158 /* wait up to 3 seconds and check if connection is still alive */
159 for (i = 0; i < w; i++)
161 if (association && !bend_assoc_is_alive(association))
163 yaz_log(YLOG_LOG, "search aborted");
174 static int strcmp_prefix(const char *s, const char *p)
176 size_t l = strlen(p);
177 if (strlen(s) >= l && !memcmp(s, p, l))
182 static void init_delay(struct delay *delayp)
184 delayp->d1 = delayp->d2 = 0.0;
187 static int parse_delay(struct delay *delayp, const char *value)
189 if (sscanf(value, "%lf:%lf", &delayp->d1, &delayp->d2) == 2)
191 else if (sscanf(value, "%lf", &delayp->d1) == 1)
198 static void ztest_sleep(double d)
201 Sleep( (DWORD) (d * 1000));
204 tv.tv_sec = floor(d);
205 tv.tv_usec = (d - floor(d)) * 1000000;
206 select(0, 0, 0, 0, &tv);
210 static void do_delay(const struct delay *delayp)
212 double d = delayp->d1;
217 d += (rand()) * (delayp->d2 - d) / RAND_MAX;
222 int ztest_search(void *handle, bend_search_rr *rr)
224 struct session_handle *sh = (struct session_handle*) handle;
225 struct result_set *new_set;
226 const char *db, *db_sep;
228 if (rr->num_bases != 1)
230 rr->errcode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
234 db = rr->basenames[0];
236 /* Allow Default, db.* and Slow */
237 if (strcmp_prefix(db, "Default"))
238 ; /* Default is OK in our test */
239 else if (strcmp_prefix(db, "db"))
240 ; /* db.* is OK in our test */
241 else if (check_slow(rr->basenames[0], rr->association))
243 rr->estimated_hit_count = 1;
247 rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
248 rr->errstring = rr->basenames[0];
252 new_set = get_set(sh, rr->setname);
255 if (!rr->replace_set)
257 rr->errcode = YAZ_BIB1_RESULT_SET_EXISTS_AND_REPLACE_INDICATOR_OFF;
264 new_set = xmalloc(sizeof(*new_set));
265 new_set->next = sh->result_sets;
266 sh->result_sets = new_set;
267 new_set->name = xstrdup(rr->setname);
270 new_set->db = xstrdup(db);
271 init_delay(&new_set->search_delay);
272 init_delay(&new_set->present_delay);
273 init_delay(&new_set->fetch_delay);
275 db_sep = strchr(db, '?');
280 int no_parms = yaz_uri_to_array(db_sep+1, rr->stream, &names, &values);
282 for (i = 0; i < no_parms; i++)
284 const char *name = names[i];
285 const char *value = values[i];
286 if (!strcmp(name, "seed"))
287 srandom(atoi(value));
288 else if (!strcmp(name, "search-delay"))
289 parse_delay(&new_set->search_delay, value);
290 else if (!strcmp(name, "present-delay"))
291 parse_delay(&new_set->present_delay, value);
292 else if (!strcmp(name, "fetch-delay"))
293 parse_delay(&new_set->fetch_delay, value);
296 rr->errcode = YAZ_BIB1_SERVICE_UNSUPP_FOR_THIS_DATABASE;
297 rr->errstring = odr_strdup(rr->stream, name);
305 WRBUF response_xml = wrbuf_alloc();
306 wrbuf_puts(response_xml, "<extra>");
307 for (a = rr->extra_args; a; a = a->next)
309 wrbuf_puts(response_xml, "<extra name=\"");
310 wrbuf_xmlputs(response_xml, a->name);
311 wrbuf_puts(response_xml, "\"");
314 wrbuf_puts(response_xml, " value=\"");
315 wrbuf_xmlputs(response_xml, a->value);
316 wrbuf_puts(response_xml, "\"");
318 wrbuf_puts(response_xml, "/>");
320 wrbuf_puts(response_xml, "</extra>");
321 rr->extra_response_data =
322 odr_strdup(rr->stream, wrbuf_cstr(response_xml));
323 wrbuf_destroy(response_xml);
325 rr->hits = get_hit_count(rr->query);
327 do_delay(&new_set->search_delay);
328 new_set->hits = rr->hits;
334 /* this huge function handles extended services */
335 int ztest_esrequest(void *handle, bend_esrequest_rr *rr)
337 /* user-defined handle - created in bend_init */
338 int *counter = (int*) handle;
340 yaz_log(log_level, "ESRequest no %d", *counter);
344 if (rr->esr->packageName)
345 yaz_log(log_level, "packagename: %s", rr->esr->packageName);
346 yaz_log(log_level, "Waitaction: " ODR_INT_PRINTF, *rr->esr->waitAction);
349 yaz_log(log_level, "function: " ODR_INT_PRINTF, *rr->esr->function);
351 if (!rr->esr->taskSpecificParameters)
353 yaz_log(log_level, "No task specific parameters");
355 else if (rr->esr->taskSpecificParameters->which == Z_External_itemOrder)
357 Z_ItemOrder *it = rr->esr->taskSpecificParameters->u.itemOrder;
358 yaz_log(log_level, "Received ItemOrder");
359 if (it->which == Z_IOItemOrder_esRequest)
361 Z_IORequest *ir = it->u.esRequest;
362 Z_IOOriginPartToKeep *k = ir->toKeep;
363 Z_IOOriginPartNotToKeep *n = ir->notToKeep;
364 const char *xml_in_response = 0;
368 if (k->contact->name)
369 yaz_log(log_level, "contact name %s", k->contact->name);
370 if (k->contact->phone)
371 yaz_log(log_level, "contact phone %s", k->contact->phone);
372 if (k->contact->email)
373 yaz_log(log_level, "contact email %s", k->contact->email);
377 yaz_log(log_level, "Billing info (not shown)");
380 if (n->resultSetItem)
382 yaz_log(log_level, "resultsetItem");
383 yaz_log(log_level, "setId: %s", n->resultSetItem->resultSetId);
384 yaz_log(log_level, "item: " ODR_INT_PRINTF, *n->resultSetItem->item);
388 Z_External *r = (Z_External*) n->itemRequest;
389 ILL_ItemRequest *item_req = 0;
390 ILL_APDU *ill_apdu = 0;
391 if (r->direct_reference)
393 char oid_name_str[OID_STR_MAX];
395 const char *oid_name =
396 yaz_oid_to_string_buf(r->direct_reference,
397 &oclass, oid_name_str);
399 yaz_log(log_level, "OID %s", oid_name);
400 if (!oid_oidcmp(r->direct_reference, yaz_oid_recsyn_xml))
402 yaz_log(log_level, "ILL XML request");
403 if (r->which == Z_External_octet)
404 yaz_log(log_level, "%.*s",
405 r->u.octet_aligned->len,
406 r->u.octet_aligned->buf);
407 xml_in_response = "<dummy>x</dummy>";
409 if (!oid_oidcmp(r->direct_reference,
410 yaz_oid_general_isoill_1))
412 yaz_log(log_level, "Decode ItemRequest begin");
413 if (r->which == ODR_EXTERNAL_single)
415 odr_setbuf(rr->decode,
416 (char *) r->u.single_ASN1_type->buf,
417 r->u.single_ASN1_type->len, 0);
419 if (!ill_ItemRequest(rr->decode, &item_req, 0, 0))
422 "Couldn't decode ItemRequest %s near %ld",
423 odr_errmsg(odr_geterror(rr->decode)),
424 (long) odr_offset(rr->decode));
427 yaz_log(log_level, "Decode ItemRequest OK");
430 ill_ItemRequest(rr->print, &item_req, 0,
432 odr_reset(rr->print);
435 if (!item_req && r->which == ODR_EXTERNAL_single)
437 yaz_log(log_level, "Decode ILL APDU begin");
438 odr_setbuf(rr->decode,
439 (char*) r->u.single_ASN1_type->buf,
440 r->u.single_ASN1_type->len, 0);
442 if (!ill_APDU(rr->decode, &ill_apdu, 0, 0))
445 "Couldn't decode ILL APDU %s near %ld",
446 odr_errmsg(odr_geterror(rr->decode)),
447 (long) odr_offset(rr->decode));
448 yaz_log(log_level, "PDU dump:");
449 odr_dumpBER(yaz_log_file(),
450 (char *) r->u.single_ASN1_type->buf,
451 r->u.single_ASN1_type->len);
454 yaz_log(log_level, "Decode ILL APDU OK");
457 ill_APDU(rr->print, &ill_apdu, 0,
459 odr_reset(rr->print);
466 yaz_log(log_level, "ILL protocol version = "
468 *item_req->protocol_version_num);
474 Z_External *ext = (Z_External *)
475 odr_malloc(rr->stream, sizeof(*ext));
476 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
477 odr_malloc(rr->stream, sizeof(*keep));
478 Z_IOTargetPart *targetPart = (Z_IOTargetPart *)
479 odr_malloc(rr->stream, sizeof(*targetPart));
481 rr->taskPackage = (Z_TaskPackage *)
482 odr_malloc(rr->stream, sizeof(*rr->taskPackage));
483 rr->taskPackage->packageType =
484 odr_oiddup(rr->stream, rr->esr->packageType);
485 rr->taskPackage->packageName = 0;
486 rr->taskPackage->userId = 0;
487 rr->taskPackage->retentionTime = 0;
488 rr->taskPackage->permissions = 0;
489 rr->taskPackage->description = 0;
490 rr->taskPackage->targetReference = (Odr_oct *)
491 odr_malloc(rr->stream, sizeof(Odr_oct));
492 rr->taskPackage->targetReference->buf =
493 (unsigned char *) odr_strdup(rr->stream, "911");
494 rr->taskPackage->targetReference->len =
495 rr->taskPackage->targetReference->size =
496 strlen((char *) (rr->taskPackage->targetReference->buf));
497 rr->taskPackage->creationDateTime = 0;
498 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
499 rr->taskPackage->packageDiagnostics = 0;
500 rr->taskPackage->taskSpecificParameters = ext;
502 ext->direct_reference =
503 odr_oiddup(rr->stream, rr->esr->packageType);
504 ext->indirect_reference = 0;
506 ext->which = Z_External_itemOrder;
507 ext->u.itemOrder = (Z_ItemOrder *)
508 odr_malloc(rr->stream, sizeof(*ext->u.update));
509 ext->u.itemOrder->which = Z_IOItemOrder_taskPackage;
510 ext->u.itemOrder->u.taskPackage = (Z_IOTaskPackage *)
511 odr_malloc(rr->stream, sizeof(Z_IOTaskPackage));
512 ext->u.itemOrder->u.taskPackage->originPart = k;
513 ext->u.itemOrder->u.taskPackage->targetPart = targetPart;
516 targetPart->itemRequest =
517 z_ext_record_xml(rr->stream, xml_in_response,
518 strlen(xml_in_response));
520 targetPart->itemRequest = 0;
522 targetPart->statusOrErrorReport = 0;
523 targetPart->auxiliaryStatus = 0;
527 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
529 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
530 yaz_log(log_level, "Received DB Update");
531 if (up->which == Z_IUUpdate_esRequest)
533 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
534 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
535 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
537 yaz_log(log_level, "action");
540 switch (*toKeep->action)
542 case Z_IUOriginPartToKeep_recordInsert:
543 yaz_log(log_level, " recordInsert");
545 case Z_IUOriginPartToKeep_recordReplace:
546 yaz_log(log_level, " recordReplace");
548 case Z_IUOriginPartToKeep_recordDelete:
549 yaz_log(log_level, " recordDelete");
551 case Z_IUOriginPartToKeep_elementUpdate:
552 yaz_log(log_level, " elementUpdate");
554 case Z_IUOriginPartToKeep_specialUpdate:
555 yaz_log(log_level, " specialUpdate");
558 yaz_log(log_level, " unknown (" ODR_INT_PRINTF ")",
562 if (toKeep->databaseName)
564 yaz_log(log_level, "database: %s", toKeep->databaseName);
565 if (!strcmp(toKeep->databaseName, "fault"))
567 rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
568 rr->errstring = toKeep->databaseName;
570 if (!strcmp(toKeep->databaseName, "accept"))
575 Z_External *ext = (Z_External *)
576 odr_malloc(rr->stream, sizeof(*ext));
577 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
578 odr_malloc(rr->stream, sizeof(*keep));
579 Z_IUTargetPart *targetPart = (Z_IUTargetPart *)
580 odr_malloc(rr->stream, sizeof(*targetPart));
582 rr->taskPackage = (Z_TaskPackage *)
583 odr_malloc(rr->stream, sizeof(*rr->taskPackage));
584 rr->taskPackage->packageType =
585 odr_oiddup(rr->stream, rr->esr->packageType);
586 rr->taskPackage->packageName = 0;
587 rr->taskPackage->userId = 0;
588 rr->taskPackage->retentionTime = 0;
589 rr->taskPackage->permissions = 0;
590 rr->taskPackage->description = 0;
591 rr->taskPackage->targetReference = (Odr_oct *)
592 odr_malloc(rr->stream, sizeof(Odr_oct));
593 rr->taskPackage->targetReference->buf =
594 (unsigned char *) odr_strdup(rr->stream, "123");
595 rr->taskPackage->targetReference->len =
596 rr->taskPackage->targetReference->size =
597 strlen((char *) (rr->taskPackage->targetReference->buf));
598 rr->taskPackage->creationDateTime = 0;
599 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
600 rr->taskPackage->packageDiagnostics = 0;
601 rr->taskPackage->taskSpecificParameters = ext;
603 ext->direct_reference =
604 odr_oiddup(rr->stream, rr->esr->packageType);
605 ext->indirect_reference = 0;
607 ext->which = Z_External_update;
608 ext->u.update = (Z_IUUpdate *)
609 odr_malloc(rr->stream, sizeof(*ext->u.update));
610 ext->u.update->which = Z_IUUpdate_taskPackage;
611 ext->u.update->u.taskPackage = (Z_IUUpdateTaskPackage *)
612 odr_malloc(rr->stream, sizeof(Z_IUUpdateTaskPackage));
613 ext->u.update->u.taskPackage->originPart = keep;
614 ext->u.update->u.taskPackage->targetPart = targetPart;
616 keep->action = odr_intdup(rr->stream, *toKeep->action);
618 odr_strdup(rr->stream, toKeep->databaseName);
620 keep->elementSetName = 0;
621 keep->actionQualifier = 0;
623 targetPart->updateStatus = odr_intdup(rr->stream, 1);
624 targetPart->num_globalDiagnostics = 0;
625 targetPart->globalDiagnostics = (Z_DiagRec **) odr_nullval();
626 targetPart->num_taskPackageRecords = 1;
627 targetPart->taskPackageRecords =
628 (Z_IUTaskPackageRecordStructure **)
629 odr_malloc(rr->stream,
630 sizeof(Z_IUTaskPackageRecordStructure *));
631 targetPart->taskPackageRecords[0] =
632 (Z_IUTaskPackageRecordStructure *)
633 odr_malloc(rr->stream,
634 sizeof(Z_IUTaskPackageRecordStructure));
636 targetPart->taskPackageRecords[0]->which =
637 Z_IUTaskPackageRecordStructure_record;
638 targetPart->taskPackageRecords[0]->u.record =
639 z_ext_record_sutrs(rr->stream, "test", 4);
640 targetPart->taskPackageRecords[0]->correlationInfo = 0;
641 targetPart->taskPackageRecords[0]->recordStatus =
642 odr_intdup(rr->stream,
643 Z_IUTaskPackageRecordStructure_success);
644 targetPart->taskPackageRecords[0]->num_supplementalDiagnostics
647 targetPart->taskPackageRecords[0]->supplementalDiagnostics = 0;
652 for (i = 0; i < notToKeep->num; i++)
654 Z_External *rec = notToKeep->elements[i]->record;
656 if (rec->direct_reference)
658 char oid_name_str[OID_STR_MAX];
660 = oid_name = yaz_oid_to_string_buf(
661 rec->direct_reference, 0,
664 yaz_log(log_level, "record %d type %s", i,
669 case Z_External_sutrs:
670 if (rec->u.octet_aligned->len > 170)
671 yaz_log(log_level, "%d bytes:\n%.168s ...",
675 yaz_log(log_level, "%d bytes:\n%s",
679 case Z_External_octet :
680 if (rec->u.octet_aligned->len > 170)
681 yaz_log(log_level, "%d bytes:\n%.168s ...",
682 rec->u.octet_aligned->len,
683 rec->u.octet_aligned->buf);
685 yaz_log(log_level, "%d bytes\n%s",
686 rec->u.octet_aligned->len,
687 rec->u.octet_aligned->buf);
696 /* result set delete */
697 int ztest_delete(void *handle, bend_delete_rr *rr)
699 if (rr->num_setnames == 1 && !strcmp(rr->setnames[0], "1"))
700 rr->delete_status = Z_DeleteStatus_success;
702 rr->delete_status = Z_DeleteStatus_resultSetDidNotExist;
706 /* Our sort handler really doesn't sort... */
707 int ztest_sort(void *handle, bend_sort_rr *rr)
710 rr->sort_status = Z_SortResponse_success;
715 /* present request handler */
716 int ztest_present(void *handle, bend_present_rr *rr)
718 struct session_handle *sh = (struct session_handle*) handle;
719 struct result_set *set = get_set(sh, rr->setname);
723 rr->errcode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
724 rr->errstring = odr_strdup(rr->stream, rr->setname);
727 do_delay(&set->present_delay);
731 /* retrieval of a single record (present, and piggy back search) */
732 int ztest_fetch(void *handle, bend_fetch_rr *r)
734 struct session_handle *sh = (struct session_handle*) handle;
736 const Odr_oid *oid = r->request_format;
737 struct result_set *set = get_set(sh, r->setname);
741 r->errcode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
742 r->errstring = odr_strdup(r->stream, r->setname);
745 do_delay(&set->fetch_delay);
747 r->basename = set->db;
748 r->output_format = r->request_format;
750 if (r->number < 1 || r->number > set->hits)
752 r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
755 if (!oid || yaz_oid_is_iso2709(oid))
757 cp = dummy_marc_record(r->number, r->stream);
760 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
761 r->surrogate_flag = 1;
768 r->output_format = odr_oiddup(r->stream, yaz_oid_recsyn_usmarc);
771 else if (!oid_oidcmp(oid, yaz_oid_recsyn_opac))
773 cp = dummy_marc_record(r->number, r->stream);
776 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
777 r->surrogate_flag = 1;
780 r->record = (char *) dummy_opac(r->number, r->stream, cp);
783 else if (!oid_oidcmp(oid, yaz_oid_recsyn_sutrs))
785 /* this section returns a small record */
788 sprintf(buf, "This is dummy SUTRS record number %d\n", r->number);
790 r->len = strlen(buf);
791 r->record = (char *) odr_malloc(r->stream, r->len+1);
792 strcpy(r->record, buf);
794 else if (!oid_oidcmp(oid, yaz_oid_recsyn_grs_1))
797 r->record = (char*) dummy_grs_record(r->number, r->stream);
800 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
801 r->surrogate_flag = 1;
805 else if (!oid_oidcmp(oid, yaz_oid_recsyn_postscript))
811 sprintf(fname, "part.%d.ps", r->number);
812 f = fopen(fname, "rb");
815 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
816 r->surrogate_flag = 1;
819 fseek(f, 0L, SEEK_END);
821 if (size <= 0 || size >= 5000000)
823 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
824 r->surrogate_flag = 1;
826 fseek(f, 0L, SEEK_SET);
827 r->record = (char*) odr_malloc(r->stream, size);
829 if (fread(r->record, size, 1, f) != 1)
831 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
832 r->surrogate_flag = 1;
836 else if (!oid_oidcmp(oid, yaz_oid_recsyn_xml))
838 if ((cp = dummy_xml_record(r->number, r->stream)))
845 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
846 r->surrogate_flag = 1;
852 char buf[OID_STR_MAX];
853 r->errcode = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
854 r->errstring = odr_strdup(r->stream, oid_oid_to_dotstring(oid, buf));
862 * silly dummy-scan what reads words from a file.
864 int ztest_scan(void *handle, bend_scan_rr *q)
867 static struct scan_entry list[200];
868 static char entries[200][80];
872 int term_position_req = q->term_position;
873 int num_entries_req = q->num_entries;
875 /* Throw Database unavailable if other than Default or Slow */
876 if (!yaz_matchstr(q->basenames[0], "Default"))
877 ; /* Default is OK in our test */
878 else if (check_slow(q->basenames[0], 0 /* no assoc for scan */))
882 q->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
883 q->errstring = q->basenames[0];
890 q->status = BEND_SCAN_SUCCESS;
891 if (!f && !(f = fopen("dummy-words", "r")))
893 perror("dummy-words");
896 if (q->num_entries > 200)
898 q->errcode = YAZ_BIB1_RESOURCES_EXHAUSTED_NO_RESULTS_AVAILABLE;
904 if (q->term->term->which != Z_Term_general)
906 q->errcode = YAZ_BIB1_TERM_TYPE_UNSUPP;
909 if (*q->step_size != 0)
911 q->errcode = YAZ_BIB1_ONLY_ZERO_STEP_SIZE_SUPPORTED_FOR_SCAN;
914 len = q->term->term->u.general->len;
915 if (len >= (int ) sizeof(term))
916 len = sizeof(term)-1;
917 memcpy(term, q->term->term->u.general->buf, len);
920 else if (q->scanClause)
922 strncpy(term, q->scanClause, sizeof(term)-1);
923 term[sizeof(term)-1] = '\0';
928 for (p = term; *p; p++)
929 if (islower(*(unsigned char *) p))
932 fseek(f, 0, SEEK_SET);
935 for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
936 i++, pos < 199 ? pos++ : (pos = 0))
938 if (!q->num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
940 if ((q->term_position = term_position_req) > i + 1)
942 q->term_position = i + 1;
943 q->status = BEND_SCAN_PARTIAL;
945 for (; q->num_entries < q->term_position; q->num_entries++)
949 po = pos - q->term_position + q->num_entries+1; /* find pos */
953 if (!strcmp(term, "SD") && q->num_entries == 2)
955 list[q->num_entries].term = entries[pos];
956 list[q->num_entries].occurrences = -1;
957 list[q->num_entries].errcode =
958 YAZ_BIB1_SCAN_UNSUPP_VALUE_OF_POSITION_IN_RESPONSE;
959 list[q->num_entries].errstring = "SD for Scan Term";
963 list[q->num_entries].term = entries[po];
964 list[q->num_entries].occurrences = hits[po];
968 else if (q->num_entries)
970 list[q->num_entries].term = entries[pos];
971 list[q->num_entries].occurrences = hits[pos];
974 if (q->num_entries >= num_entries_req)
978 q->status = BEND_SCAN_PARTIAL;
982 int ztest_explain(void *handle, bend_explain_rr *rr)
984 if (rr->database && !strcmp(rr->database, "Default"))
986 rr->explain_buf = "<explain>\n"
988 "\t\t<host>localhost</host>\n"
989 "\t\t<port>210</port>\n"
996 int ztest_update(void *handle, bend_update_rr *rr)
998 rr->operation_status = "success";
1002 bend_initresult *bend_init(bend_initrequest *q)
1004 bend_initresult *r = (bend_initresult *)
1005 odr_malloc(q->stream, sizeof(*r));
1006 struct session_handle *sh = xmalloc(sizeof(*sh));
1008 sh->result_sets = 0;
1012 log_level=yaz_log_module_level("ztest");
1018 r->handle = sh; /* tell GFS about our handle */
1019 q->bend_sort = ztest_sort; /* register sort handler */
1020 q->bend_search = ztest_search; /* register search handler */
1021 q->bend_present = ztest_present; /* register present handle */
1022 q->bend_esrequest = ztest_esrequest;
1023 q->bend_delete = ztest_delete;
1024 q->bend_fetch = ztest_fetch;
1025 q->bend_scan = ztest_scan;
1027 q->bend_explain = ztest_explain;
1029 q->bend_srw_scan = ztest_scan;
1030 q->bend_srw_update = ztest_update;
1032 q->query_charset = "ISO-8859-1";
1033 q->records_in_same_charset = 0;
1038 void bend_close(void *handle)
1040 struct session_handle *sh = (struct session_handle*) handle;
1042 xfree(sh); /* release our session */
1046 int main(int argc, char **argv)
1048 return statserv_main(argc, argv, bend_init, bend_close);
1053 * c-file-style: "Stroustrup"
1054 * indent-tabs-mode: nil
1056 * vim: shiftwidth=4 tabstop=8 expandtab