Merge branch 'master' into normal_cql
[yaz-moved-to-github.git] / ztest / ztest.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2013 Index Data
3  * See the file LICENSE for details.
4  */
5 /** \file
6  * \brief yaz-ztest Generic Frontend Server
7  */
8 #if HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12
13 #include <stdio.h>
14 #include <math.h>
15 #include <stdlib.h>
16
17 #if HAVE_SYS_TIME_H
18 #include <sys/time.h>
19 #endif
20 #if HAVE_UNISTD_H
21 #include <unistd.h>
22 #endif
23 #if HAVE_SYS_SELECT_H
24 #include <sys/select.h>
25 #endif
26 #ifdef WIN32
27 #include <windows.h>
28 #endif
29
30 #include <yaz/log.h>
31 #include <yaz/backend.h>
32 #include <yaz/ill.h>
33 #include <yaz/diagbib1.h>
34 #include <yaz/otherinfo.h>
35 #include <yaz/facet.h>
36
37 #include "ztest.h"
38
39 static int log_level=0;
40 static int log_level_set=0;
41
42 struct delay {
43     double d1;
44     double d2;
45 };
46
47 struct result_set {
48     char *name;
49     char *db;
50     Odr_int hits;
51     struct delay search_delay;
52     struct delay present_delay;
53     struct delay fetch_delay;
54     struct result_set *next;
55 };
56
57 struct session_handle {
58     struct result_set *result_sets;
59 };
60
61 int ztest_search(void *handle, bend_search_rr *rr);
62 int ztest_sort(void *handle, bend_sort_rr *rr);
63 int ztest_present(void *handle, bend_present_rr *rr);
64 int ztest_esrequest(void *handle, bend_esrequest_rr *rr);
65 int ztest_delete(void *handle, bend_delete_rr *rr);
66
67 static struct result_set *get_set(struct session_handle *sh, const char *name)
68 {
69     struct result_set *set = sh->result_sets;
70     for (; set; set = set->next)
71         if (!strcmp(name, set->name))
72             return set;
73     return 0;
74 }
75
76 static void remove_sets(struct session_handle *sh)
77 {
78     struct result_set *set = sh->result_sets;
79     while (set)
80     {
81         struct result_set *set_next = set->next;
82         xfree(set->name);
83         xfree(set->db);
84         xfree(set);
85         set = set_next;
86     }
87     sh->result_sets = 0;
88 }
89
90 /** \brief use term value as hit count
91     \param s RPN structure
92     \return >= 0: search term number or -1: not found
93
94     Traverse RPN tree 'in order' and use term value as hit count.
95     Only terms  that looks a numeric is used.. Returns -1 if
96     no sub tree has a hit count term
97 */
98 static Odr_int get_term_hit(Z_RPNStructure *s)
99 {
100     Odr_int h = -1;
101     switch(s->which)
102     {
103     case Z_RPNStructure_simple:
104         if (s->u.simple->which == Z_Operand_APT)
105         {
106             Z_AttributesPlusTerm *apt = s->u.simple->u.attributesPlusTerm;
107             if (apt->term->which == Z_Term_general)
108             {
109                 Odr_oct *oct = apt->term->u.general;
110                 if (oct->len > 0 && oct->buf[0] >= '0' && oct->buf[0] <= '9')
111                 {
112                     WRBUF hits_str = wrbuf_alloc();
113                     wrbuf_write(hits_str, (const char *) oct->buf, oct->len);
114                     h = odr_atoi(wrbuf_cstr(hits_str));
115                     wrbuf_destroy(hits_str);
116                 }
117             }
118         }
119         break;
120     case Z_RPNStructure_complex:
121         h = get_term_hit(s->u.complex->s1);
122         if (h == -1)
123             h = get_term_hit(s->u.complex->s2);
124         break;
125     }
126     return h;
127 }
128
129 /** \brief gets hit count for numeric terms in RPN queries
130     \param q RPN Query
131     \return number of hits (random or number for term)
132
133     This is just for testing.. A real database of course uses
134     the content of a database to establish a value.. In our case, we
135     have a way to trigger a certain hit count. Good for testing of
136     client applications etc
137 */
138 static Odr_int get_hit_count(Z_Query *q)
139 {
140     if (q->which == Z_Query_type_1 || q->which == Z_Query_type_101)
141     {
142         Odr_int h = -1;
143         h = get_term_hit(q->u.type_1->RPNStructure);
144         if (h == -1)
145             h = rand() % 24;
146         return h;
147     }
148     else
149         return 24;
150 }
151
152 /** \brief checks if it's a dummy Slow database
153     \param basename database name to check
154     \param association backend association (or NULL if not available)
155     \retval 1 is slow database
156     \retval 0 is not a slow database
157
158     The Slow database is for testing.. It allows us to simulate
159     a slow server...
160 */
161 static int check_slow(const char *basename, bend_association association)
162 {
163     if (strncmp(basename, "Slow", 4) == 0)
164     {
165 #if HAVE_UNISTD_H
166         int i, w = 3;
167         if (basename[4])
168             sscanf(basename+4, "%d", &w);
169         /* wait up to 3 seconds and check if connection is still alive */
170         for (i = 0; i < w; i++)
171         {
172             if (association && !bend_assoc_is_alive(association))
173             {
174                 yaz_log(YLOG_LOG, "search aborted");
175                 break;
176             }
177             sleep(1);
178         }
179 #endif
180         return 1;
181     }
182     return 0;
183 }
184
185 static int strcmp_prefix(const char *s, const char *p)
186 {
187     size_t l = strlen(p);
188     if (strlen(s) >= l && !memcmp(s, p, l))
189         return 1;
190     return 0;
191 }
192
193 static void init_delay(struct delay *delayp)
194 {
195     delayp->d1 = delayp->d2 = 0.0;
196 }
197
198 static int parse_delay(struct delay *delayp, const char *value)
199 {
200     if (sscanf(value, "%lf:%lf", &delayp->d1, &delayp->d2) == 2)
201         ;
202     else if (sscanf(value, "%lf", &delayp->d1) == 1)
203         delayp->d2 = 0.0;
204     else
205         return -1;
206     return 0;
207 }
208
209 static void ztest_sleep(double d)
210 {
211 #ifdef WIN32
212     Sleep( (DWORD) (d * 1000));
213 #else
214     struct timeval tv;
215     tv.tv_sec = d;
216     tv.tv_usec = (d - (long) d) * 1000000;
217     select(0, 0, 0, 0, &tv);
218 #endif
219 }
220
221 static void do_delay(const struct delay *delayp)
222 {
223     double d = delayp->d1;
224
225     if (d > 0.0)
226     {
227         if (delayp->d2 > d)
228             d += (rand()) * (delayp->d2 - d) / RAND_MAX;
229         ztest_sleep(d);
230     }
231 }
232
233 static void addterms(ODR odr, Z_FacetField *facet_field, const char *facet_name)
234 {
235     int index;
236     int freq = 100;
237     int length = strlen(facet_name) + 10;
238     char *key = odr_malloc(odr, length);
239     key[0] = '\0';
240     for (index = 0; index < facet_field->num_terms; index++)
241     {
242         Z_FacetTerm *facet_term;
243         sprintf(key, "%s%d", facet_name, index);
244         yaz_log(YLOG_DEBUG, "facet add term %s %d %s", facet_name, index, key);
245
246         facet_term = facet_term_create_cstr(odr, key, freq);
247         freq = freq - 10 ;
248         facet_field_term_set(odr, facet_field, facet_term, index);
249     }
250 }
251
252 Z_OtherInformation *build_facet_response(ODR odr, Z_FacetList *facet_list) {
253     int index, new_index = 0;
254     Z_FacetList *new_list = facet_list_create(odr, facet_list->num);
255
256     for (index = 0; index < facet_list->num; index++) {
257         struct yaz_facet_attr attrvalues;
258         yaz_facet_attr_init(&attrvalues);
259         attrvalues.limit = 10;
260         yaz_facet_attr_get_z_attributes(facet_list->elements[index]->attributes,
261                                         &attrvalues);
262         yaz_log(YLOG_LOG, "Attributes: %s %d ", attrvalues.useattr, attrvalues.limit);
263         if (attrvalues.errstring)
264             yaz_log(YLOG_LOG, "Error parsing attributes: %s", attrvalues.errstring);
265         if (attrvalues.limit > 0 && attrvalues.useattr) {
266             new_list->elements[new_index] = facet_field_create(odr, facet_list->elements[index]->attributes, attrvalues.limit);
267             addterms(odr, new_list->elements[new_index], attrvalues.useattr);
268             new_index++;
269         }
270         else {
271             yaz_log(YLOG_DEBUG, "Facet: skipping %s due to 0 limit.", attrvalues.useattr);
272         }
273
274     }
275     new_list->num = new_index;
276     if (new_index > 0) {
277         Z_OtherInformation *oi = odr_malloc(odr, sizeof(*oi));
278         Z_OtherInformationUnit *oiu = odr_malloc(odr, sizeof(*oiu));
279         oi->num_elements = 1;
280         oi->list = odr_malloc(odr, oi->num_elements * sizeof(*oi->list));
281         oiu->category = 0;
282         oiu->which = Z_OtherInfo_externallyDefinedInfo;
283         oiu->information.externallyDefinedInfo = odr_malloc(odr, sizeof(*oiu->information.externallyDefinedInfo));
284         oiu->information.externallyDefinedInfo->direct_reference = odr_oiddup(odr, yaz_oid_userinfo_facet_1);
285         oiu->information.externallyDefinedInfo->descriptor = 0;
286         oiu->information.externallyDefinedInfo->indirect_reference = 0;
287         oiu->information.externallyDefinedInfo->which = Z_External_userFacets;
288         oiu->information.externallyDefinedInfo->u.facetList = new_list;
289         oi->list[0] = oiu;
290         return oi;
291     }
292     return 0;
293 }
294
295 static void echo_extra_args(ODR stream,
296                             Z_SRW_extra_arg *extra_args, char **extra_response)
297 {
298     if (extra_args)
299     {
300         Z_SRW_extra_arg *a;
301         WRBUF response_xml = wrbuf_alloc();
302         wrbuf_puts(response_xml, "<extra>");
303         for (a = extra_args; a; a = a->next)
304         {
305             wrbuf_puts(response_xml, "<extra name=\"");
306             wrbuf_xmlputs(response_xml, a->name);
307             wrbuf_puts(response_xml, "\"");
308             if (a->value)
309             {
310                 wrbuf_puts(response_xml, " value=\"");
311                 wrbuf_xmlputs(response_xml, a->value);
312                 wrbuf_puts(response_xml, "\"");
313             }
314             wrbuf_puts(response_xml, "/>");
315         }
316         wrbuf_puts(response_xml, "</extra>");
317         *extra_response = odr_strdup(stream, wrbuf_cstr(response_xml));
318         wrbuf_destroy(response_xml);
319     }
320
321 }
322
323 int ztest_search(void *handle, bend_search_rr *rr)
324 {
325     struct session_handle *sh = (struct session_handle*) handle;
326     struct result_set *new_set;
327     const char *db, *db_sep;
328
329     if (rr->num_bases != 1)
330     {
331         rr->errcode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
332         return 0;
333     }
334
335     db = rr->basenames[0];
336
337     /* Allow Default, db.* and Slow */
338     if (strcmp_prefix(db, "Default"))
339         ;  /* Default is OK in our test */
340     else if (strcmp_prefix(db, "db"))
341         ;  /* db.* is OK in our test */
342     else if (check_slow(rr->basenames[0], rr->association))
343     {
344         rr->estimated_hit_count = 1;
345     }
346     else
347     {
348         rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
349         rr->errstring = rr->basenames[0];
350         return 0;
351     }
352
353     new_set = get_set(sh, rr->setname);
354     if (new_set)
355     {
356         if (!rr->replace_set)
357         {
358             rr->errcode = YAZ_BIB1_RESULT_SET_EXISTS_AND_REPLACE_INDICATOR_OFF;
359             return 0;
360         }
361         xfree(new_set->db);
362     }
363     else
364     {
365         new_set = xmalloc(sizeof(*new_set));
366         new_set->next = sh->result_sets;
367         sh->result_sets = new_set;
368         new_set->name = xstrdup(rr->setname);
369     }
370     new_set->hits = 0;
371     new_set->db = xstrdup(db);
372     init_delay(&new_set->search_delay);
373     init_delay(&new_set->present_delay);
374     init_delay(&new_set->fetch_delay);
375
376     db_sep = strchr(db, '?');
377     if (db_sep)
378     {
379         char **names;
380         char **values;
381         int no_parms = yaz_uri_to_array(db_sep+1, rr->stream, &names, &values);
382         int i;
383         for (i = 0; i < no_parms; i++)
384         {
385             const char *name = names[i];
386             const char *value = values[i];
387             if (!strcmp(name, "seed"))
388                 srand(atoi(value));
389             else if (!strcmp(name, "search-delay"))
390                 parse_delay(&new_set->search_delay, value);
391             else if (!strcmp(name, "present-delay"))
392                 parse_delay(&new_set->present_delay, value);
393             else if (!strcmp(name, "fetch-delay"))
394                 parse_delay(&new_set->fetch_delay, value);
395             else
396             {
397                 rr->errcode = YAZ_BIB1_SERVICE_UNSUPP_FOR_THIS_DATABASE;
398                 rr->errstring = odr_strdup(rr->stream, name);
399             }
400         }
401     }
402
403     echo_extra_args(rr->stream, rr->extra_args, &rr->extra_response_data);
404     rr->hits = get_hit_count(rr->query);
405
406     if (1)
407     {
408         Z_FacetList *facet_list = yaz_oi_get_facetlist(&rr->search_input);
409         if (facet_list) {
410             yaz_log(YLOG_LOG, "%d Facets in search request.", facet_list->num);
411             rr->search_info = build_facet_response(rr->stream, facet_list);
412         }
413         else
414             yaz_log(YLOG_DEBUG, "No facets parsed search request.");
415
416     }
417     do_delay(&new_set->search_delay);
418     new_set->hits = rr->hits;
419
420     return 0;
421 }
422
423
424 /* this huge function handles extended services */
425 int ztest_esrequest(void *handle, bend_esrequest_rr *rr)
426 {
427     if (rr->esr->packageName)
428         yaz_log(log_level, "packagename: %s", rr->esr->packageName);
429     yaz_log(log_level, "Waitaction: " ODR_INT_PRINTF, *rr->esr->waitAction);
430
431
432     yaz_log(log_level, "function: " ODR_INT_PRINTF, *rr->esr->function);
433
434     if (!rr->esr->taskSpecificParameters)
435     {
436         yaz_log(log_level, "No task specific parameters");
437     }
438     else if (rr->esr->taskSpecificParameters->which == Z_External_itemOrder)
439     {
440         Z_ItemOrder *it = rr->esr->taskSpecificParameters->u.itemOrder;
441         yaz_log(log_level, "Received ItemOrder");
442         if (it->which == Z_IOItemOrder_esRequest)
443         {
444             Z_IORequest *ir = it->u.esRequest;
445             Z_IOOriginPartToKeep *k = ir->toKeep;
446             Z_IOOriginPartNotToKeep *n = ir->notToKeep;
447             const char *xml_in_response = 0;
448
449             if (k && k->contact)
450             {
451                 if (k->contact->name)
452                     yaz_log(log_level, "contact name %s", k->contact->name);
453                 if (k->contact->phone)
454                     yaz_log(log_level, "contact phone %s", k->contact->phone);
455                 if (k->contact->email)
456                     yaz_log(log_level, "contact email %s", k->contact->email);
457             }
458             if (k->addlBilling)
459             {
460                 yaz_log(log_level, "Billing info (not shown)");
461             }
462
463             if (n->resultSetItem)
464             {
465                 yaz_log(log_level, "resultsetItem");
466                 yaz_log(log_level, "setId: %s", n->resultSetItem->resultSetId);
467                 yaz_log(log_level, "item: " ODR_INT_PRINTF, *n->resultSetItem->item);
468             }
469             if (n->itemRequest)
470             {
471                 Z_External *r = (Z_External*) n->itemRequest;
472                 ILL_ItemRequest *item_req = 0;
473                 ILL_APDU *ill_apdu = 0;
474                 if (r->direct_reference)
475                 {
476                     char oid_name_str[OID_STR_MAX];
477                     oid_class oclass;
478                     const char *oid_name =
479                         yaz_oid_to_string_buf(r->direct_reference,
480                                               &oclass, oid_name_str);
481                     if (oid_name)
482                         yaz_log(log_level, "OID %s", oid_name);
483                     if (!oid_oidcmp(r->direct_reference, yaz_oid_recsyn_xml))
484                     {
485                         yaz_log(log_level, "ILL XML request");
486                         if (r->which == Z_External_octet)
487                             yaz_log(log_level, "%.*s",
488                                     r->u.octet_aligned->len,
489                                     r->u.octet_aligned->buf);
490                         xml_in_response = "<dummy>x</dummy>";
491                     }
492                     if (!oid_oidcmp(r->direct_reference,
493                                     yaz_oid_general_isoill_1))
494                     {
495                         yaz_log(log_level, "Decode ItemRequest begin");
496                         if (r->which == ODR_EXTERNAL_single)
497                         {
498                             odr_setbuf(rr->decode,
499                                        (char *) r->u.single_ASN1_type->buf,
500                                        r->u.single_ASN1_type->len, 0);
501
502                             if (!ill_ItemRequest(rr->decode, &item_req, 0, 0))
503                             {
504                                 yaz_log(log_level,
505                                         "Couldn't decode ItemRequest %s near %ld",
506                                         odr_errmsg(odr_geterror(rr->decode)),
507                                         (long) odr_offset(rr->decode));
508                             }
509                             else
510                                 yaz_log(log_level, "Decode ItemRequest OK");
511                             if (rr->print)
512                             {
513                                 ill_ItemRequest(rr->print, &item_req, 0,
514                                                 "ItemRequest");
515                                 odr_reset(rr->print);
516                             }
517                         }
518                         if (!item_req && r->which == ODR_EXTERNAL_single)
519                         {
520                             yaz_log(log_level, "Decode ILL APDU begin");
521                             odr_setbuf(rr->decode,
522                                        (char*) r->u.single_ASN1_type->buf,
523                                        r->u.single_ASN1_type->len, 0);
524
525                             if (!ill_APDU(rr->decode, &ill_apdu, 0, 0))
526                             {
527                                 yaz_log(log_level,
528                                         "Couldn't decode ILL APDU %s near %ld",
529                                         odr_errmsg(odr_geterror(rr->decode)),
530                                         (long) odr_offset(rr->decode));
531                                 yaz_log(log_level, "PDU dump:");
532                                 odr_dumpBER(yaz_log_file(),
533                                             (char *) r->u.single_ASN1_type->buf,
534                                             r->u.single_ASN1_type->len);
535                             }
536                             else
537                                 yaz_log(log_level, "Decode ILL APDU OK");
538                             if (rr->print)
539                             {
540                                 ill_APDU(rr->print, &ill_apdu, 0,
541                                          "ILL APDU");
542                                 odr_reset(rr->print);
543                             }
544                         }
545                     }
546                 }
547                 if (item_req)
548                 {
549                     yaz_log(log_level, "ILL protocol version = "
550                             ODR_INT_PRINTF,
551                             *item_req->protocol_version_num);
552                 }
553             }
554             if (k)
555             {
556
557                 Z_External *ext = (Z_External *)
558                     odr_malloc(rr->stream, sizeof(*ext));
559                 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
560                     odr_malloc(rr->stream, sizeof(*keep));
561                 Z_IOTargetPart *targetPart = (Z_IOTargetPart *)
562                     odr_malloc(rr->stream, sizeof(*targetPart));
563
564                 rr->taskPackage = (Z_TaskPackage *)
565                     odr_malloc(rr->stream, sizeof(*rr->taskPackage));
566                 rr->taskPackage->packageType =
567                     odr_oiddup(rr->stream, rr->esr->packageType);
568                 rr->taskPackage->packageName = 0;
569                 rr->taskPackage->userId = 0;
570                 rr->taskPackage->retentionTime = 0;
571                 rr->taskPackage->permissions = 0;
572                 rr->taskPackage->description = 0;
573                 rr->taskPackage->targetReference = (Odr_oct *)
574                     odr_malloc(rr->stream, sizeof(Odr_oct));
575                 rr->taskPackage->targetReference->buf =
576                     (unsigned char *) odr_strdup(rr->stream, "911");
577                 rr->taskPackage->targetReference->len =
578                     rr->taskPackage->targetReference->size =
579                     strlen((char *) (rr->taskPackage->targetReference->buf));
580                 rr->taskPackage->creationDateTime = 0;
581                 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
582                 rr->taskPackage->packageDiagnostics = 0;
583                 rr->taskPackage->taskSpecificParameters = ext;
584
585                 ext->direct_reference =
586                     odr_oiddup(rr->stream, rr->esr->packageType);
587                 ext->indirect_reference = 0;
588                 ext->descriptor = 0;
589                 ext->which = Z_External_itemOrder;
590                 ext->u.itemOrder = (Z_ItemOrder *)
591                     odr_malloc(rr->stream, sizeof(*ext->u.update));
592                 ext->u.itemOrder->which = Z_IOItemOrder_taskPackage;
593                 ext->u.itemOrder->u.taskPackage =  (Z_IOTaskPackage *)
594                     odr_malloc(rr->stream, sizeof(Z_IOTaskPackage));
595                 ext->u.itemOrder->u.taskPackage->originPart = k;
596                 ext->u.itemOrder->u.taskPackage->targetPart = targetPart;
597
598                 if (xml_in_response)
599                     targetPart->itemRequest =
600                         z_ext_record_xml(rr->stream, xml_in_response,
601                                          strlen(xml_in_response));
602                 else
603                     targetPart->itemRequest = 0;
604
605                 targetPart->statusOrErrorReport = 0;
606                 targetPart->auxiliaryStatus = 0;
607             }
608         }
609     }
610     else if (rr->esr->taskSpecificParameters->which == Z_External_update)
611     {
612         Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
613         yaz_log(log_level, "Received DB Update");
614         if (up->which == Z_IUUpdate_esRequest)
615         {
616             Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
617             Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
618             Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
619
620             yaz_log(log_level, "action");
621             if (toKeep->action)
622             {
623                 switch (*toKeep->action)
624                 {
625                 case Z_IUOriginPartToKeep_recordInsert:
626                     yaz_log(log_level, " recordInsert");
627                     break;
628                 case Z_IUOriginPartToKeep_recordReplace:
629                     yaz_log(log_level, " recordReplace");
630                     break;
631                 case Z_IUOriginPartToKeep_recordDelete:
632                     yaz_log(log_level, " recordDelete");
633                     break;
634                 case Z_IUOriginPartToKeep_elementUpdate:
635                     yaz_log(log_level, " elementUpdate");
636                     break;
637                 case Z_IUOriginPartToKeep_specialUpdate:
638                     yaz_log(log_level, " specialUpdate");
639                     break;
640                 default:
641                     yaz_log(log_level, " unknown (" ODR_INT_PRINTF ")",
642                             *toKeep->action);
643                 }
644             }
645             if (toKeep->databaseName)
646             {
647                 yaz_log(log_level, "database: %s", toKeep->databaseName);
648                 if (!strcmp(toKeep->databaseName, "fault"))
649                 {
650                     rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
651                     rr->errstring = toKeep->databaseName;
652                 }
653                 if (!strcmp(toKeep->databaseName, "accept"))
654                     rr->errcode = -1;
655             }
656             if (toKeep)
657             {
658                 Z_External *ext = (Z_External *)
659                     odr_malloc(rr->stream, sizeof(*ext));
660                 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
661                     odr_malloc(rr->stream, sizeof(*keep));
662                 Z_IUTargetPart *targetPart = (Z_IUTargetPart *)
663                     odr_malloc(rr->stream, sizeof(*targetPart));
664
665                 rr->taskPackage = (Z_TaskPackage *)
666                     odr_malloc(rr->stream, sizeof(*rr->taskPackage));
667                 rr->taskPackage->packageType =
668                     odr_oiddup(rr->stream, rr->esr->packageType);
669                 rr->taskPackage->packageName = 0;
670                 rr->taskPackage->userId = 0;
671                 rr->taskPackage->retentionTime = 0;
672                 rr->taskPackage->permissions = 0;
673                 rr->taskPackage->description = 0;
674                 rr->taskPackage->targetReference = (Odr_oct *)
675                     odr_malloc(rr->stream, sizeof(Odr_oct));
676                 rr->taskPackage->targetReference->buf =
677                     (unsigned char *) odr_strdup(rr->stream, "123");
678                 rr->taskPackage->targetReference->len =
679                     rr->taskPackage->targetReference->size =
680                     strlen((char *) (rr->taskPackage->targetReference->buf));
681                 rr->taskPackage->creationDateTime = 0;
682                 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
683                 rr->taskPackage->packageDiagnostics = 0;
684                 rr->taskPackage->taskSpecificParameters = ext;
685
686                 ext->direct_reference =
687                     odr_oiddup(rr->stream, rr->esr->packageType);
688                 ext->indirect_reference = 0;
689                 ext->descriptor = 0;
690                 ext->which = Z_External_update;
691                 ext->u.update = (Z_IUUpdate *)
692                     odr_malloc(rr->stream, sizeof(*ext->u.update));
693                 ext->u.update->which = Z_IUUpdate_taskPackage;
694                 ext->u.update->u.taskPackage =  (Z_IUUpdateTaskPackage *)
695                     odr_malloc(rr->stream, sizeof(Z_IUUpdateTaskPackage));
696                 ext->u.update->u.taskPackage->originPart = keep;
697                 ext->u.update->u.taskPackage->targetPart = targetPart;
698
699                 keep->action = odr_intdup(rr->stream, *toKeep->action);
700                 keep->databaseName =
701                     odr_strdup(rr->stream, toKeep->databaseName);
702                 keep->schema = 0;
703                 keep->elementSetName = 0;
704                 keep->actionQualifier = 0;
705
706                 targetPart->updateStatus = odr_intdup(rr->stream, 1);
707                 targetPart->num_globalDiagnostics = 0;
708                 targetPart->globalDiagnostics = (Z_DiagRec **) odr_nullval();
709                 targetPart->num_taskPackageRecords = 1;
710                 targetPart->taskPackageRecords =
711                     (Z_IUTaskPackageRecordStructure **)
712                     odr_malloc(rr->stream,
713                                sizeof(Z_IUTaskPackageRecordStructure *));
714                 targetPart->taskPackageRecords[0] =
715                     (Z_IUTaskPackageRecordStructure *)
716                     odr_malloc(rr->stream,
717                                sizeof(Z_IUTaskPackageRecordStructure));
718
719                 targetPart->taskPackageRecords[0]->which =
720                     Z_IUTaskPackageRecordStructure_record;
721                 targetPart->taskPackageRecords[0]->u.record =
722                     z_ext_record_sutrs(rr->stream, "test", 4);
723                 targetPart->taskPackageRecords[0]->correlationInfo = 0;
724                 targetPart->taskPackageRecords[0]->recordStatus =
725                     odr_intdup(rr->stream,
726                                Z_IUTaskPackageRecordStructure_success);
727                 targetPart->taskPackageRecords[0]->num_supplementalDiagnostics
728                     = 0;
729
730                 targetPart->taskPackageRecords[0]->supplementalDiagnostics = 0;
731             }
732             if (notToKeep)
733             {
734                 int i;
735                 for (i = 0; i < notToKeep->num; i++)
736                 {
737                     Z_External *rec = notToKeep->elements[i]->record;
738
739                     if (rec->direct_reference)
740                     {
741                         char oid_name_str[OID_STR_MAX];
742                         const char *oid_name
743                             = oid_name = yaz_oid_to_string_buf(
744                                 rec->direct_reference, 0,
745                                 oid_name_str);
746                         if (oid_name)
747                             yaz_log(log_level, "record %d type %s", i,
748                                     oid_name);
749                     }
750                     switch (rec->which)
751                     {
752                     case Z_External_sutrs:
753                         if (rec->u.octet_aligned->len > 170)
754                             yaz_log(log_level, "%d bytes:\n%.168s ...",
755                                     rec->u.sutrs->len,
756                                     rec->u.sutrs->buf);
757                         else
758                             yaz_log(log_level, "%d bytes:\n%s",
759                                     rec->u.sutrs->len,
760                                     rec->u.sutrs->buf);
761                         break;
762                     case Z_External_octet        :
763                         if (rec->u.octet_aligned->len > 170)
764                             yaz_log(log_level, "%d bytes:\n%.168s ...",
765                                     rec->u.octet_aligned->len,
766                                     rec->u.octet_aligned->buf);
767                         else
768                             yaz_log(log_level, "%d bytes\n%s",
769                                     rec->u.octet_aligned->len,
770                                     rec->u.octet_aligned->buf);
771                     }
772                 }
773             }
774         }
775     }
776     return 0;
777 }
778
779 /* result set delete */
780 int ztest_delete(void *handle, bend_delete_rr *rr)
781 {
782     if (rr->num_setnames == 1 && !strcmp(rr->setnames[0], "1"))
783         rr->delete_status = Z_DeleteStatus_success;
784     else
785         rr->delete_status = Z_DeleteStatus_resultSetDidNotExist;
786     return 0;
787 }
788
789 /* Our sort handler really doesn't sort... */
790 int ztest_sort(void *handle, bend_sort_rr *rr)
791 {
792     rr->errcode = 0;
793     rr->sort_status = Z_SortResponse_success;
794     return 0;
795 }
796
797
798 /* present request handler */
799 int ztest_present(void *handle, bend_present_rr *rr)
800 {
801     struct session_handle *sh = (struct session_handle*) handle;
802     struct result_set *set = get_set(sh, rr->setname);
803
804     if (!set)
805     {
806         rr->errcode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
807         rr->errstring = odr_strdup(rr->stream, rr->setname);
808         return 0;
809     }
810     do_delay(&set->present_delay);
811     return 0;
812 }
813
814 /* retrieval of a single record (present, and piggy back search) */
815 int ztest_fetch(void *handle, bend_fetch_rr *r)
816 {
817     struct session_handle *sh = (struct session_handle*) handle;
818     char *cp;
819     const Odr_oid *oid = r->request_format;
820     struct result_set *set = get_set(sh, r->setname);
821
822     if (!set)
823     {
824         r->errcode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
825         r->errstring = odr_strdup(r->stream, r->setname);
826         return 0;
827     }
828     do_delay(&set->fetch_delay);
829     r->last_in_set = 0;
830     r->basename = set->db;
831     r->output_format = r->request_format;
832
833     if (r->number < 1 || r->number > set->hits)
834     {
835         r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
836         return 0;
837     }
838     if (!oid || yaz_oid_is_iso2709(oid))
839     {
840         cp = dummy_marc_record(r->number, r->stream);
841         if (!cp)
842         {
843             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
844             r->surrogate_flag = 1;
845             return 0;
846         }
847         else
848         {
849             r->len = strlen(cp);
850             r->record = cp;
851             r->output_format = odr_oiddup(r->stream, yaz_oid_recsyn_usmarc);
852         }
853     }
854     else if (!oid_oidcmp(oid, yaz_oid_recsyn_opac))
855     {
856         cp = dummy_marc_record(r->number, r->stream);
857         if (!cp)
858         {
859             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
860             r->surrogate_flag = 1;
861             return 0;
862         }
863         r->record = (char *) dummy_opac(r->number, r->stream, cp);
864         r->len = -1;
865     }
866     else if (!oid_oidcmp(oid, yaz_oid_recsyn_sutrs))
867     {
868         /* this section returns a small record */
869         char buf[100];
870
871         sprintf(buf, "This is dummy SUTRS record number %d\n", r->number);
872
873         r->len = strlen(buf);
874         r->record = (char *) odr_malloc(r->stream, r->len+1);
875         strcpy(r->record, buf);
876     }
877     else if (!oid_oidcmp(oid, yaz_oid_recsyn_grs_1))
878     {
879         r->len = -1;
880         r->record = (char*) dummy_grs_record(r->number, r->stream);
881         if (!r->record)
882         {
883             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
884             r->surrogate_flag = 1;
885             return 0;
886         }
887     }
888     else if (!oid_oidcmp(oid, yaz_oid_recsyn_postscript))
889     {
890         char fname[20];
891         FILE *f;
892         long size;
893
894         sprintf(fname, "part.%d.ps", r->number);
895         f = fopen(fname, "rb");
896         if (!f)
897         {
898             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
899             r->surrogate_flag = 1;
900             return 0;
901         }
902         fseek(f, 0L, SEEK_END);
903         size = ftell(f);
904         if (size <= 0 || size >= 5000000)
905         {
906             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
907             r->surrogate_flag = 1;
908         }
909         fseek(f, 0L, SEEK_SET);
910         r->record = (char*) odr_malloc(r->stream, size);
911         r->len = size;
912         if (fread(r->record, size, 1, f) != 1)
913         {
914             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
915             r->surrogate_flag = 1;
916         }
917         fclose(f);
918     }
919     else if (!oid_oidcmp(oid, yaz_oid_recsyn_xml))
920     {
921         if ((cp = dummy_xml_record(r->number, r->stream)))
922         {
923             r->len = strlen(cp);
924             r->record = cp;
925         }
926         else
927         {
928             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
929             r->surrogate_flag = 1;
930             return 0;
931         }
932     }
933     else
934     {
935         char buf[OID_STR_MAX];
936         r->errcode = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
937         r->errstring = odr_strdup(r->stream, oid_oid_to_dotstring(oid, buf));
938         return 0;
939     }
940     r->errcode = 0;
941     return 0;
942 }
943
944 /*
945  * silly dummy-scan what reads words from a file.
946  */
947 int ztest_scan(void *handle, bend_scan_rr *q)
948 {
949     static FILE *f = 0;
950     static struct scan_entry list[200];
951     static char entries[200][80];
952     int hits[200];
953     char term[80], *p;
954     int i, pos;
955     int term_position_req = q->term_position;
956     int num_entries_req = q->num_entries;
957
958     /* Throw Database unavailable if other than Default or Slow */
959     if (!yaz_matchstr(q->basenames[0], "Default"))
960         ;  /* Default is OK in our test */
961     else if (check_slow(q->basenames[0], 0 /* no assoc for scan */))
962         ;
963     else
964     {
965         q->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
966         q->errstring = q->basenames[0];
967         return 0;
968     }
969
970     q->errcode = 0;
971     q->errstring = 0;
972     q->entries = list;
973     q->status = BEND_SCAN_SUCCESS;
974     if (!f && !(f = fopen("dummy-words", "r")))
975     {
976         perror("dummy-words");
977         exit(1);
978     }
979     if (q->num_entries > 200)
980     {
981         q->errcode = YAZ_BIB1_RESOURCES_EXHAUSTED_NO_RESULTS_AVAILABLE;
982         return 0;
983     }
984     if (q->term)
985     {
986         int len;
987         if (q->term->term->which != Z_Term_general)
988         {
989             q->errcode = YAZ_BIB1_TERM_TYPE_UNSUPP;
990             return 0;
991         }
992         if (*q->step_size != 0)
993         {
994             q->errcode = YAZ_BIB1_ONLY_ZERO_STEP_SIZE_SUPPORTED_FOR_SCAN;
995             return 0;
996         }
997         len = q->term->term->u.general->len;
998         if (len >= (int ) sizeof(term))
999             len = sizeof(term)-1;
1000         memcpy(term, q->term->term->u.general->buf, len);
1001         term[len] = '\0';
1002     }
1003     else if (q->scanClause)
1004     {
1005         strncpy(term, q->scanClause, sizeof(term)-1);
1006         term[sizeof(term)-1] = '\0';
1007     }
1008     else
1009         strcpy(term, "0");
1010
1011     for (p = term; *p; p++)
1012         if (yaz_islower(*p))
1013             *p = yaz_toupper(*p);
1014
1015     fseek(f, 0, SEEK_SET);
1016     q->num_entries = 0;
1017
1018     for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
1019          i++, pos < 199 ? pos++ : (pos = 0))
1020     {
1021         if (!q->num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
1022         {
1023             if ((q->term_position = term_position_req) > i + 1)
1024             {
1025                 q->term_position = i + 1;
1026                 q->status = BEND_SCAN_PARTIAL;
1027             }
1028             for (; q->num_entries < q->term_position; q->num_entries++)
1029             {
1030                 int po;
1031
1032                 po = pos - q->term_position + q->num_entries+1; /* find pos */
1033                 if (po < 0)
1034                     po += 200;
1035
1036                 if (!strcmp(term, "SD") && q->num_entries == 2)
1037                 {
1038                     list[q->num_entries].term = entries[pos];
1039                     list[q->num_entries].occurrences = -1;
1040                     list[q->num_entries].errcode =
1041                         YAZ_BIB1_SCAN_UNSUPP_VALUE_OF_POSITION_IN_RESPONSE;
1042                     list[q->num_entries].errstring = "SD for Scan Term";
1043                 }
1044                 else
1045                 {
1046                     list[q->num_entries].term = entries[po];
1047                     list[q->num_entries].occurrences = hits[po];
1048                 }
1049             }
1050         }
1051         else if (q->num_entries)
1052         {
1053             list[q->num_entries].term = entries[pos];
1054             list[q->num_entries].occurrences = hits[pos];
1055             q->num_entries++;
1056         }
1057         if (q->num_entries >= num_entries_req)
1058             break;
1059     }
1060     echo_extra_args(q->stream, q->extra_args, &q->extra_response_data);
1061     if (feof(f))
1062         q->status = BEND_SCAN_PARTIAL;
1063     return 0;
1064 }
1065
1066 int ztest_explain(void *handle, bend_explain_rr *rr)
1067 {
1068     if (rr->database && !strcmp(rr->database, "Default"))
1069     {
1070         rr->explain_buf = "<explain>\n"
1071             "\t<serverInfo>\n"
1072             "\t\t<host>localhost</host>\n"
1073             "\t\t<port>210</port>\n"
1074             "\t</serverInfo>\n"
1075             "</explain>\n";
1076     }
1077     return 0;
1078 }
1079
1080 int ztest_update(void *handle, bend_update_rr *rr)
1081 {
1082     rr->operation_status = "success";
1083     return 0;
1084 }
1085
1086 bend_initresult *bend_init(bend_initrequest *q)
1087 {
1088     bend_initresult *r = (bend_initresult *)
1089         odr_malloc(q->stream, sizeof(*r));
1090     struct session_handle *sh = xmalloc(sizeof(*sh));
1091
1092     sh->result_sets = 0;
1093
1094     if (!log_level_set)
1095     {
1096         log_level=yaz_log_module_level("ztest");
1097         log_level_set=1;
1098     }
1099
1100     r->errcode = 0;
1101     r->errstring = 0;
1102     r->handle = sh;                         /* tell GFS about our handle */
1103     q->bend_sort = ztest_sort;              /* register sort handler */
1104     q->bend_search = ztest_search;          /* register search handler */
1105     q->bend_present = ztest_present;        /* register present handle */
1106     q->bend_esrequest = ztest_esrequest;
1107     q->bend_delete = ztest_delete;
1108     q->bend_fetch = ztest_fetch;
1109     q->bend_scan = ztest_scan;
1110 #if 0
1111     q->bend_explain = ztest_explain;
1112 #endif
1113     q->bend_srw_scan = ztest_scan;
1114     q->bend_srw_update = ztest_update;
1115
1116     q->query_charset = "ISO-8859-1";
1117     q->records_in_same_charset = 0;
1118
1119     return r;
1120 }
1121
1122 void bend_close(void *handle)
1123 {
1124     struct session_handle *sh = (struct session_handle*) handle;
1125     remove_sets(sh);
1126     xfree(sh);              /* release our session */
1127     return;
1128 }
1129
1130 int main(int argc, char **argv)
1131 {
1132     return statserv_main(argc, argv, bend_init, bend_close);
1133 }
1134 /*
1135  * Local variables:
1136  * c-basic-offset: 4
1137  * c-file-style: "Stroustrup"
1138  * indent-tabs-mode: nil
1139  * End:
1140  * vim: shiftwidth=4 tabstop=8 expandtab
1141  */
1142