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
19 #include <yaz/backend.h>
21 #include <yaz/diagbib1.h>
25 static int log_level=0;
26 static int log_level_set=0;
37 struct delay search_delay;
38 struct delay present_delay;
39 struct delay fetch_delay;
40 struct result_set *next;
43 struct session_handle {
44 struct result_set *result_sets;
47 int ztest_search(void *handle, bend_search_rr *rr);
48 int ztest_sort(void *handle, bend_sort_rr *rr);
49 int ztest_present(void *handle, bend_present_rr *rr);
50 int ztest_esrequest(void *handle, bend_esrequest_rr *rr);
51 int ztest_delete(void *handle, bend_delete_rr *rr);
53 static struct result_set *get_set(struct session_handle *sh, const char *name)
55 struct result_set *set = sh->result_sets;
56 for (; set; set = set->next)
57 if (!strcmp(name, set->name))
62 static void remove_sets(struct session_handle *sh)
64 struct result_set *set = sh->result_sets;
67 struct result_set *set_next = set->next;
76 /** \brief use term value as hit count
77 \param s RPN structure
78 \return >= 0: search term number or -1: not found
80 Traverse RPN tree 'in order' and use term value as hit count.
81 Only terms that looks a numeric is used.. Returns -1 if
82 no sub tree has a hit count term
84 static Odr_int get_term_hit(Z_RPNStructure *s)
89 case Z_RPNStructure_simple:
90 if (s->u.simple->which == Z_Operand_APT)
92 Z_AttributesPlusTerm *apt = s->u.simple->u.attributesPlusTerm;
93 if (apt->term->which == Z_Term_general)
95 Odr_oct *oct = apt->term->u.general;
96 if (oct->len > 0 && oct->buf[0] >= '0' && oct->buf[0] <= '9')
98 WRBUF hits_str = wrbuf_alloc();
99 wrbuf_write(hits_str, (const char *) oct->buf, oct->len);
100 h = odr_atoi(wrbuf_cstr(hits_str));
101 wrbuf_destroy(hits_str);
106 case Z_RPNStructure_complex:
107 h = get_term_hit(s->u.complex->s1);
109 h = get_term_hit(s->u.complex->s2);
115 /** \brief gets hit count for numeric terms in RPN queries
117 \return number of hits (random or number for term)
119 This is just for testing.. A real database of course uses
120 the content of a database to establish a value.. In our case, we
121 have a way to trigger a certain hit count. Good for testing of
122 client applications etc
124 static Odr_int get_hit_count(Z_Query *q)
127 if (q->which == Z_Query_type_1 || q->which == Z_Query_type_101)
128 h = get_term_hit(q->u.type_1->RPNStructure);
134 /** \brief checks if it's a dummy Slow database
135 \param basename database name to check
136 \param association backend association (or NULL if not available)
137 \retval 1 is slow database
138 \retval 0 is not a slow database
140 The Slow database is for testing.. It allows us to simulate
143 static int check_slow(const char *basename, bend_association association)
145 if (strncmp(basename, "Slow", 4) == 0)
150 sscanf(basename+4, "%d", &w);
151 /* wait up to 3 seconds and check if connection is still alive */
152 for (i = 0; i < w; i++)
154 if (association && !bend_assoc_is_alive(association))
156 yaz_log(YLOG_LOG, "search aborted");
167 static int strcmp_prefix(const char *s, const char *p)
169 size_t l = strlen(p);
170 if (strlen(s) >= l && !memcmp(s, p, l))
175 static void init_delay(struct delay *delayp)
177 delayp->d1 = delayp->d2 = 0.0;
180 static int parse_delay(struct delay *delayp, const char *value)
182 delayp->d1 = atof(value);
187 static void do_delay(const struct delay *delayp)
191 tv.tv_sec = floor(delayp->d1);
192 tv.tv_usec = (delayp->d1 - floor(delayp->d1)) * 1000000;
193 select(0, 0, 0, 0, &tv);
196 int ztest_search(void *handle, bend_search_rr *rr)
198 struct session_handle *sh = (struct session_handle*) handle;
199 struct result_set *new_set;
200 const char *db, *db_sep;
202 if (rr->num_bases != 1)
204 rr->errcode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
208 db = rr->basenames[0];
210 /* Allow Default, db.* and Slow */
211 if (strcmp_prefix(db, "Default"))
212 ; /* Default is OK in our test */
213 else if (strcmp_prefix(db, "db"))
214 ; /* db.* is OK in our test */
215 else if (check_slow(rr->basenames[0], rr->association))
217 rr->estimated_hit_count = 1;
221 rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
222 rr->errstring = rr->basenames[0];
226 new_set = get_set(sh, rr->setname);
229 if (!rr->replace_set)
231 rr->errcode = YAZ_BIB1_RESULT_SET_EXISTS_AND_REPLACE_INDICATOR_OFF;
238 new_set = xmalloc(sizeof(*new_set));
239 new_set->next = sh->result_sets;
240 sh->result_sets = new_set;
241 new_set->name = xstrdup(rr->setname);
244 new_set->db = xstrdup(db);
245 init_delay(&new_set->search_delay);
246 init_delay(&new_set->present_delay);
248 db_sep = strchr(db, '?');
253 int no_parms = yaz_uri_to_array(db_sep+1, rr->stream, &names, &values);
255 for (i = 0; i < no_parms; i++)
257 const char *name = names[i];
258 const char *value = values[i];
259 if (!strcmp(name, "seed"))
260 srandom(atoi(value));
261 else if (!strcmp(name, "search-delay"))
262 parse_delay(&new_set->search_delay, value);
263 else if (!strcmp(name, "present-delay"))
264 parse_delay(&new_set->present_delay, value);
265 else if (!strcmp(name, "fetch-delay"))
266 parse_delay(&new_set->fetch_delay, value);
269 rr->errcode = YAZ_BIB1_SERVICE_UNSUPP_FOR_THIS_DATABASE;
270 rr->errstring = odr_strdup(rr->stream, name);
278 WRBUF response_xml = wrbuf_alloc();
279 wrbuf_puts(response_xml, "<extra>");
280 for (a = rr->extra_args; a; a = a->next)
282 wrbuf_puts(response_xml, "<extra name=\"");
283 wrbuf_xmlputs(response_xml, a->name);
284 wrbuf_puts(response_xml, "\"");
287 wrbuf_puts(response_xml, " value=\"");
288 wrbuf_xmlputs(response_xml, a->value);
289 wrbuf_puts(response_xml, "\"");
291 wrbuf_puts(response_xml, "/>");
293 wrbuf_puts(response_xml, "</extra>");
294 rr->extra_response_data =
295 odr_strdup(rr->stream, wrbuf_cstr(response_xml));
296 wrbuf_destroy(response_xml);
298 rr->hits = get_hit_count(rr->query);
300 do_delay(&new_set->search_delay);
301 new_set->hits = rr->hits;
307 /* this huge function handles extended services */
308 int ztest_esrequest(void *handle, bend_esrequest_rr *rr)
310 /* user-defined handle - created in bend_init */
311 int *counter = (int*) handle;
313 yaz_log(log_level, "ESRequest no %d", *counter);
317 if (rr->esr->packageName)
318 yaz_log(log_level, "packagename: %s", rr->esr->packageName);
319 yaz_log(log_level, "Waitaction: " ODR_INT_PRINTF, *rr->esr->waitAction);
322 yaz_log(log_level, "function: " ODR_INT_PRINTF, *rr->esr->function);
324 if (!rr->esr->taskSpecificParameters)
326 yaz_log(log_level, "No task specific parameters");
328 else if (rr->esr->taskSpecificParameters->which == Z_External_itemOrder)
330 Z_ItemOrder *it = rr->esr->taskSpecificParameters->u.itemOrder;
331 yaz_log(log_level, "Received ItemOrder");
332 if (it->which == Z_IOItemOrder_esRequest)
334 Z_IORequest *ir = it->u.esRequest;
335 Z_IOOriginPartToKeep *k = ir->toKeep;
336 Z_IOOriginPartNotToKeep *n = ir->notToKeep;
337 const char *xml_in_response = 0;
341 if (k->contact->name)
342 yaz_log(log_level, "contact name %s", k->contact->name);
343 if (k->contact->phone)
344 yaz_log(log_level, "contact phone %s", k->contact->phone);
345 if (k->contact->email)
346 yaz_log(log_level, "contact email %s", k->contact->email);
350 yaz_log(log_level, "Billing info (not shown)");
353 if (n->resultSetItem)
355 yaz_log(log_level, "resultsetItem");
356 yaz_log(log_level, "setId: %s", n->resultSetItem->resultSetId);
357 yaz_log(log_level, "item: " ODR_INT_PRINTF, *n->resultSetItem->item);
361 Z_External *r = (Z_External*) n->itemRequest;
362 ILL_ItemRequest *item_req = 0;
363 ILL_APDU *ill_apdu = 0;
364 if (r->direct_reference)
366 char oid_name_str[OID_STR_MAX];
368 const char *oid_name =
369 yaz_oid_to_string_buf(r->direct_reference,
370 &oclass, oid_name_str);
372 yaz_log(log_level, "OID %s", oid_name);
373 if (!oid_oidcmp(r->direct_reference, yaz_oid_recsyn_xml))
375 yaz_log(log_level, "ILL XML request");
376 if (r->which == Z_External_octet)
377 yaz_log(log_level, "%.*s",
378 r->u.octet_aligned->len,
379 r->u.octet_aligned->buf);
380 xml_in_response = "<dummy>x</dummy>";
382 if (!oid_oidcmp(r->direct_reference,
383 yaz_oid_general_isoill_1))
385 yaz_log(log_level, "Decode ItemRequest begin");
386 if (r->which == ODR_EXTERNAL_single)
388 odr_setbuf(rr->decode,
389 (char *) r->u.single_ASN1_type->buf,
390 r->u.single_ASN1_type->len, 0);
392 if (!ill_ItemRequest(rr->decode, &item_req, 0, 0))
395 "Couldn't decode ItemRequest %s near %ld",
396 odr_errmsg(odr_geterror(rr->decode)),
397 (long) odr_offset(rr->decode));
400 yaz_log(log_level, "Decode ItemRequest OK");
403 ill_ItemRequest(rr->print, &item_req, 0,
405 odr_reset(rr->print);
408 if (!item_req && r->which == ODR_EXTERNAL_single)
410 yaz_log(log_level, "Decode ILL APDU begin");
411 odr_setbuf(rr->decode,
412 (char*) r->u.single_ASN1_type->buf,
413 r->u.single_ASN1_type->len, 0);
415 if (!ill_APDU(rr->decode, &ill_apdu, 0, 0))
418 "Couldn't decode ILL APDU %s near %ld",
419 odr_errmsg(odr_geterror(rr->decode)),
420 (long) odr_offset(rr->decode));
421 yaz_log(log_level, "PDU dump:");
422 odr_dumpBER(yaz_log_file(),
423 (char *) r->u.single_ASN1_type->buf,
424 r->u.single_ASN1_type->len);
427 yaz_log(log_level, "Decode ILL APDU OK");
430 ill_APDU(rr->print, &ill_apdu, 0,
432 odr_reset(rr->print);
439 yaz_log(log_level, "ILL protocol version = "
441 *item_req->protocol_version_num);
447 Z_External *ext = (Z_External *)
448 odr_malloc(rr->stream, sizeof(*ext));
449 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
450 odr_malloc(rr->stream, sizeof(*keep));
451 Z_IOTargetPart *targetPart = (Z_IOTargetPart *)
452 odr_malloc(rr->stream, sizeof(*targetPart));
454 rr->taskPackage = (Z_TaskPackage *)
455 odr_malloc(rr->stream, sizeof(*rr->taskPackage));
456 rr->taskPackage->packageType =
457 odr_oiddup(rr->stream, rr->esr->packageType);
458 rr->taskPackage->packageName = 0;
459 rr->taskPackage->userId = 0;
460 rr->taskPackage->retentionTime = 0;
461 rr->taskPackage->permissions = 0;
462 rr->taskPackage->description = 0;
463 rr->taskPackage->targetReference = (Odr_oct *)
464 odr_malloc(rr->stream, sizeof(Odr_oct));
465 rr->taskPackage->targetReference->buf =
466 (unsigned char *) odr_strdup(rr->stream, "911");
467 rr->taskPackage->targetReference->len =
468 rr->taskPackage->targetReference->size =
469 strlen((char *) (rr->taskPackage->targetReference->buf));
470 rr->taskPackage->creationDateTime = 0;
471 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
472 rr->taskPackage->packageDiagnostics = 0;
473 rr->taskPackage->taskSpecificParameters = ext;
475 ext->direct_reference =
476 odr_oiddup(rr->stream, rr->esr->packageType);
477 ext->indirect_reference = 0;
479 ext->which = Z_External_itemOrder;
480 ext->u.itemOrder = (Z_ItemOrder *)
481 odr_malloc(rr->stream, sizeof(*ext->u.update));
482 ext->u.itemOrder->which = Z_IOItemOrder_taskPackage;
483 ext->u.itemOrder->u.taskPackage = (Z_IOTaskPackage *)
484 odr_malloc(rr->stream, sizeof(Z_IOTaskPackage));
485 ext->u.itemOrder->u.taskPackage->originPart = k;
486 ext->u.itemOrder->u.taskPackage->targetPart = targetPart;
489 targetPart->itemRequest =
490 z_ext_record_xml(rr->stream, xml_in_response,
491 strlen(xml_in_response));
493 targetPart->itemRequest = 0;
495 targetPart->statusOrErrorReport = 0;
496 targetPart->auxiliaryStatus = 0;
500 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
502 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
503 yaz_log(log_level, "Received DB Update");
504 if (up->which == Z_IUUpdate_esRequest)
506 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
507 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
508 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
510 yaz_log(log_level, "action");
513 switch (*toKeep->action)
515 case Z_IUOriginPartToKeep_recordInsert:
516 yaz_log(log_level, " recordInsert");
518 case Z_IUOriginPartToKeep_recordReplace:
519 yaz_log(log_level, " recordReplace");
521 case Z_IUOriginPartToKeep_recordDelete:
522 yaz_log(log_level, " recordDelete");
524 case Z_IUOriginPartToKeep_elementUpdate:
525 yaz_log(log_level, " elementUpdate");
527 case Z_IUOriginPartToKeep_specialUpdate:
528 yaz_log(log_level, " specialUpdate");
531 yaz_log(log_level, " unknown (" ODR_INT_PRINTF ")",
535 if (toKeep->databaseName)
537 yaz_log(log_level, "database: %s", toKeep->databaseName);
538 if (!strcmp(toKeep->databaseName, "fault"))
540 rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
541 rr->errstring = toKeep->databaseName;
543 if (!strcmp(toKeep->databaseName, "accept"))
548 Z_External *ext = (Z_External *)
549 odr_malloc(rr->stream, sizeof(*ext));
550 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
551 odr_malloc(rr->stream, sizeof(*keep));
552 Z_IUTargetPart *targetPart = (Z_IUTargetPart *)
553 odr_malloc(rr->stream, sizeof(*targetPart));
555 rr->taskPackage = (Z_TaskPackage *)
556 odr_malloc(rr->stream, sizeof(*rr->taskPackage));
557 rr->taskPackage->packageType =
558 odr_oiddup(rr->stream, rr->esr->packageType);
559 rr->taskPackage->packageName = 0;
560 rr->taskPackage->userId = 0;
561 rr->taskPackage->retentionTime = 0;
562 rr->taskPackage->permissions = 0;
563 rr->taskPackage->description = 0;
564 rr->taskPackage->targetReference = (Odr_oct *)
565 odr_malloc(rr->stream, sizeof(Odr_oct));
566 rr->taskPackage->targetReference->buf =
567 (unsigned char *) odr_strdup(rr->stream, "123");
568 rr->taskPackage->targetReference->len =
569 rr->taskPackage->targetReference->size =
570 strlen((char *) (rr->taskPackage->targetReference->buf));
571 rr->taskPackage->creationDateTime = 0;
572 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
573 rr->taskPackage->packageDiagnostics = 0;
574 rr->taskPackage->taskSpecificParameters = ext;
576 ext->direct_reference =
577 odr_oiddup(rr->stream, rr->esr->packageType);
578 ext->indirect_reference = 0;
580 ext->which = Z_External_update;
581 ext->u.update = (Z_IUUpdate *)
582 odr_malloc(rr->stream, sizeof(*ext->u.update));
583 ext->u.update->which = Z_IUUpdate_taskPackage;
584 ext->u.update->u.taskPackage = (Z_IUUpdateTaskPackage *)
585 odr_malloc(rr->stream, sizeof(Z_IUUpdateTaskPackage));
586 ext->u.update->u.taskPackage->originPart = keep;
587 ext->u.update->u.taskPackage->targetPart = targetPart;
589 keep->action = odr_intdup(rr->stream, *toKeep->action);
591 odr_strdup(rr->stream, toKeep->databaseName);
593 keep->elementSetName = 0;
594 keep->actionQualifier = 0;
596 targetPart->updateStatus = odr_intdup(rr->stream, 1);
597 targetPart->num_globalDiagnostics = 0;
598 targetPart->globalDiagnostics = (Z_DiagRec **) odr_nullval();
599 targetPart->num_taskPackageRecords = 1;
600 targetPart->taskPackageRecords =
601 (Z_IUTaskPackageRecordStructure **)
602 odr_malloc(rr->stream,
603 sizeof(Z_IUTaskPackageRecordStructure *));
604 targetPart->taskPackageRecords[0] =
605 (Z_IUTaskPackageRecordStructure *)
606 odr_malloc(rr->stream,
607 sizeof(Z_IUTaskPackageRecordStructure));
609 targetPart->taskPackageRecords[0]->which =
610 Z_IUTaskPackageRecordStructure_record;
611 targetPart->taskPackageRecords[0]->u.record =
612 z_ext_record_sutrs(rr->stream, "test", 4);
613 targetPart->taskPackageRecords[0]->correlationInfo = 0;
614 targetPart->taskPackageRecords[0]->recordStatus =
615 odr_intdup(rr->stream,
616 Z_IUTaskPackageRecordStructure_success);
617 targetPart->taskPackageRecords[0]->num_supplementalDiagnostics
620 targetPart->taskPackageRecords[0]->supplementalDiagnostics = 0;
625 for (i = 0; i < notToKeep->num; i++)
627 Z_External *rec = notToKeep->elements[i]->record;
629 if (rec->direct_reference)
631 char oid_name_str[OID_STR_MAX];
633 = oid_name = yaz_oid_to_string_buf(
634 rec->direct_reference, 0,
637 yaz_log(log_level, "record %d type %s", i,
642 case Z_External_sutrs:
643 if (rec->u.octet_aligned->len > 170)
644 yaz_log(log_level, "%d bytes:\n%.168s ...",
648 yaz_log(log_level, "%d bytes:\n%s",
652 case Z_External_octet :
653 if (rec->u.octet_aligned->len > 170)
654 yaz_log(log_level, "%d bytes:\n%.168s ...",
655 rec->u.octet_aligned->len,
656 rec->u.octet_aligned->buf);
658 yaz_log(log_level, "%d bytes\n%s",
659 rec->u.octet_aligned->len,
660 rec->u.octet_aligned->buf);
669 /* result set delete */
670 int ztest_delete(void *handle, bend_delete_rr *rr)
672 if (rr->num_setnames == 1 && !strcmp(rr->setnames[0], "1"))
673 rr->delete_status = Z_DeleteStatus_success;
675 rr->delete_status = Z_DeleteStatus_resultSetDidNotExist;
679 /* Our sort handler really doesn't sort... */
680 int ztest_sort(void *handle, bend_sort_rr *rr)
683 rr->sort_status = Z_SortResponse_success;
688 /* present request handler */
689 int ztest_present(void *handle, bend_present_rr *rr)
694 /* retrieval of a single record (present, and piggy back search) */
695 int ztest_fetch(void *handle, bend_fetch_rr *r)
697 struct session_handle *sh = (struct session_handle*) handle;
699 const Odr_oid *oid = r->request_format;
700 struct result_set *set = get_set(sh, r->setname);
704 r->errcode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
705 r->errstring = odr_strdup(r->stream, r->setname);
709 r->basename = set->db;
710 r->output_format = r->request_format;
712 if (r->number < 1 || r->number > set->hits)
714 r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
717 if (!oid || yaz_oid_is_iso2709(oid))
719 cp = dummy_marc_record(r->number, r->stream);
722 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
723 r->surrogate_flag = 1;
730 r->output_format = odr_oiddup(r->stream, yaz_oid_recsyn_usmarc);
733 else if (!oid_oidcmp(oid, yaz_oid_recsyn_opac))
735 cp = dummy_marc_record(r->number, r->stream);
738 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
739 r->surrogate_flag = 1;
742 r->record = (char *) dummy_opac(r->number, r->stream, cp);
745 else if (!oid_oidcmp(oid, yaz_oid_recsyn_sutrs))
747 /* this section returns a small record */
750 sprintf(buf, "This is dummy SUTRS record number %d\n", r->number);
752 r->len = strlen(buf);
753 r->record = (char *) odr_malloc(r->stream, r->len+1);
754 strcpy(r->record, buf);
756 else if (!oid_oidcmp(oid, yaz_oid_recsyn_grs_1))
759 r->record = (char*) dummy_grs_record(r->number, r->stream);
762 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
763 r->surrogate_flag = 1;
767 else if (!oid_oidcmp(oid, yaz_oid_recsyn_postscript))
773 sprintf(fname, "part.%d.ps", r->number);
774 f = fopen(fname, "rb");
777 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
778 r->surrogate_flag = 1;
781 fseek(f, 0L, SEEK_END);
783 if (size <= 0 || size >= 5000000)
785 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
786 r->surrogate_flag = 1;
788 fseek(f, 0L, SEEK_SET);
789 r->record = (char*) odr_malloc(r->stream, size);
791 if (fread(r->record, size, 1, f) != 1)
793 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
794 r->surrogate_flag = 1;
798 else if (!oid_oidcmp(oid, yaz_oid_recsyn_xml))
800 if ((cp = dummy_xml_record(r->number, r->stream)))
807 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
808 r->surrogate_flag = 1;
814 char buf[OID_STR_MAX];
815 r->errcode = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
816 r->errstring = odr_strdup(r->stream, oid_oid_to_dotstring(oid, buf));
824 * silly dummy-scan what reads words from a file.
826 int ztest_scan(void *handle, bend_scan_rr *q)
829 static struct scan_entry list[200];
830 static char entries[200][80];
834 int term_position_req = q->term_position;
835 int num_entries_req = q->num_entries;
837 /* Throw Database unavailable if other than Default or Slow */
838 if (!yaz_matchstr(q->basenames[0], "Default"))
839 ; /* Default is OK in our test */
840 else if (check_slow(q->basenames[0], 0 /* no assoc for scan */))
844 q->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
845 q->errstring = q->basenames[0];
852 q->status = BEND_SCAN_SUCCESS;
853 if (!f && !(f = fopen("dummy-words", "r")))
855 perror("dummy-words");
858 if (q->num_entries > 200)
860 q->errcode = YAZ_BIB1_RESOURCES_EXHAUSTED_NO_RESULTS_AVAILABLE;
866 if (q->term->term->which != Z_Term_general)
868 q->errcode = YAZ_BIB1_TERM_TYPE_UNSUPP;
871 if (*q->step_size != 0)
873 q->errcode = YAZ_BIB1_ONLY_ZERO_STEP_SIZE_SUPPORTED_FOR_SCAN;
876 len = q->term->term->u.general->len;
877 if (len >= (int ) sizeof(term))
878 len = sizeof(term)-1;
879 memcpy(term, q->term->term->u.general->buf, len);
882 else if (q->scanClause)
884 strncpy(term, q->scanClause, sizeof(term)-1);
885 term[sizeof(term)-1] = '\0';
890 for (p = term; *p; p++)
891 if (islower(*(unsigned char *) p))
894 fseek(f, 0, SEEK_SET);
897 for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
898 i++, pos < 199 ? pos++ : (pos = 0))
900 if (!q->num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
902 if ((q->term_position = term_position_req) > i + 1)
904 q->term_position = i + 1;
905 q->status = BEND_SCAN_PARTIAL;
907 for (; q->num_entries < q->term_position; q->num_entries++)
911 po = pos - q->term_position + q->num_entries+1; /* find pos */
915 if (!strcmp(term, "SD") && q->num_entries == 2)
917 list[q->num_entries].term = entries[pos];
918 list[q->num_entries].occurrences = -1;
919 list[q->num_entries].errcode =
920 YAZ_BIB1_SCAN_UNSUPP_VALUE_OF_POSITION_IN_RESPONSE;
921 list[q->num_entries].errstring = "SD for Scan Term";
925 list[q->num_entries].term = entries[po];
926 list[q->num_entries].occurrences = hits[po];
930 else if (q->num_entries)
932 list[q->num_entries].term = entries[pos];
933 list[q->num_entries].occurrences = hits[pos];
936 if (q->num_entries >= num_entries_req)
940 q->status = BEND_SCAN_PARTIAL;
944 int ztest_explain(void *handle, bend_explain_rr *rr)
946 if (rr->database && !strcmp(rr->database, "Default"))
948 rr->explain_buf = "<explain>\n"
950 "\t\t<host>localhost</host>\n"
951 "\t\t<port>210</port>\n"
958 int ztest_update(void *handle, bend_update_rr *rr)
960 rr->operation_status = "success";
964 bend_initresult *bend_init(bend_initrequest *q)
966 bend_initresult *r = (bend_initresult *)
967 odr_malloc(q->stream, sizeof(*r));
968 struct session_handle *sh = xmalloc(sizeof(*sh));
974 log_level=yaz_log_module_level("ztest");
980 r->handle = sh; /* tell GFS about our handle */
981 q->bend_sort = ztest_sort; /* register sort handler */
982 q->bend_search = ztest_search; /* register search handler */
983 q->bend_present = ztest_present; /* register present handle */
984 q->bend_esrequest = ztest_esrequest;
985 q->bend_delete = ztest_delete;
986 q->bend_fetch = ztest_fetch;
987 q->bend_scan = ztest_scan;
989 q->bend_explain = ztest_explain;
991 q->bend_srw_scan = ztest_scan;
992 q->bend_srw_update = ztest_update;
994 q->query_charset = "ISO-8859-1";
995 q->records_in_same_charset = 0;
1000 void bend_close(void *handle)
1002 struct session_handle *sh = (struct session_handle*) handle;
1004 xfree(sh); /* release our session */
1008 int main(int argc, char **argv)
1010 return statserv_main(argc, argv, bend_init, bend_close);
1015 * c-file-style: "Stroustrup"
1016 * indent-tabs-mode: nil
1018 * vim: shiftwidth=4 tabstop=8 expandtab