Add fake Facet list. Not yet working
[yaz-moved-to-github.git] / ztest / ztest.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /** \file
7  * \brief yaz-ztest Generic Frontend Server
8  */
9
10 #include <stdio.h>
11 #include <math.h>
12 #include <stdlib.h>
13 #include <ctype.h>
14
15 #if HAVE_SYS_TIME_H
16 #include <sys/time.h>
17 #endif
18 #if HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21 #if HAVE_SYS_SELECT_H
22 #include <sys/select.h>
23 #endif
24 #ifdef WIN32
25 #include <windows.h>
26 #endif
27
28 #include <yaz/log.h>
29 #include <yaz/backend.h>
30 #include <yaz/ill.h>
31 #include <yaz/diagbib1.h>
32 #include <yaz/otherinfo.h>
33
34 #include "ztest.h"
35
36 static int log_level=0;
37 static int log_level_set=0;
38
39 struct delay {
40     double d1;
41     double d2;
42 };
43
44 struct result_set {
45     char *name;
46     char *db;
47     Odr_int hits;
48     struct delay search_delay;
49     struct delay present_delay;
50     struct delay fetch_delay;
51     struct result_set *next;
52 };
53
54 struct session_handle {
55     struct result_set *result_sets;
56 };
57
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);
63
64 static struct result_set *get_set(struct session_handle *sh, const char *name)
65 {
66     struct result_set *set = sh->result_sets;
67     for (; set; set = set->next)
68         if (!strcmp(name, set->name))
69             return set;
70     return 0;
71 }
72
73 static void remove_sets(struct session_handle *sh)
74 {
75     struct result_set *set = sh->result_sets;
76     while (set)
77     {
78         struct result_set *set_next = set->next;
79         xfree(set->name);
80         xfree(set->db);
81         xfree(set);
82         set = set_next;
83     }
84     sh->result_sets = 0;
85 }
86
87 /** \brief use term value as hit count 
88     \param s RPN structure
89     \return >= 0: search term number or -1: not found
90    
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
94 */
95 static Odr_int get_term_hit(Z_RPNStructure *s)
96 {
97     Odr_int h = -1;
98     switch(s->which)
99     {
100     case Z_RPNStructure_simple:
101         if (s->u.simple->which == Z_Operand_APT)
102         {
103             Z_AttributesPlusTerm *apt = s->u.simple->u.attributesPlusTerm;
104             if (apt->term->which == Z_Term_general)
105             {
106                 Odr_oct *oct = apt->term->u.general;
107                 if (oct->len > 0 && oct->buf[0] >= '0' && oct->buf[0] <= '9')
108                 {
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);
113                 }
114             }
115         }
116         break;
117     case Z_RPNStructure_complex:
118         h = get_term_hit(s->u.complex->s1);
119         if (h == -1)
120             h = get_term_hit(s->u.complex->s2);
121         break;
122     }
123     return h;
124 }
125
126 /** \brief gets hit count for numeric terms in RPN queries
127     \param q RPN Query
128     \return number of hits (random or number for term)
129     
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
134 */
135 static Odr_int get_hit_count(Z_Query *q)
136 {
137     if (q->which == Z_Query_type_1 || q->which == Z_Query_type_101)
138     {
139         Odr_int h = -1;
140         h = get_term_hit(q->u.type_1->RPNStructure);
141         if (h == -1)
142             h = rand() % 24;
143         return h;
144     }
145     else
146         return 24;
147 }
148
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
154
155     The Slow database is for testing.. It allows us to simulate
156     a slow server...
157 */
158 static int check_slow(const char *basename, bend_association association)
159 {
160     if (strncmp(basename, "Slow", 4) == 0)
161     {
162 #if HAVE_UNISTD_H
163         int i, w = 3;
164         if (basename[4])
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++)
168         {
169             if (association && !bend_assoc_is_alive(association))
170             {
171                 yaz_log(YLOG_LOG, "search aborted");
172                 break;
173             }
174             sleep(1);
175         }
176 #endif
177         return 1;
178     }
179     return 0;
180 }
181
182 static int strcmp_prefix(const char *s, const char *p)
183 {
184     size_t l = strlen(p);
185     if (strlen(s) >= l && !memcmp(s, p, l))
186         return 1;
187     return 0;
188 }
189
190 static void init_delay(struct delay *delayp)
191 {
192     delayp->d1 = delayp->d2 = 0.0;
193 }
194
195 static int parse_delay(struct delay *delayp, const char *value)
196 {
197     if (sscanf(value, "%lf:%lf", &delayp->d1, &delayp->d2) == 2)
198         ;
199     else if (sscanf(value, "%lf", &delayp->d1) == 1)
200         delayp->d2 = 0.0;
201     else
202         return -1;
203     return 0;
204 }
205
206 static void ztest_sleep(double d)
207 {
208 #ifdef WIN32
209     Sleep( (DWORD) (d * 1000));
210 #else
211     struct timeval tv;
212     tv.tv_sec = floor(d);
213     tv.tv_usec = (d - floor(d)) * 1000000;
214     select(0, 0, 0, 0, &tv);
215 #endif
216 }
217
218 static void do_delay(const struct delay *delayp)
219 {
220     double d = delayp->d1;
221
222     if (d > 0.0)
223     {
224         if (delayp->d2 > d)
225             d += (rand()) * (delayp->d2 - d) / RAND_MAX;
226         ztest_sleep(d);
227     }
228 }
229
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);
233
234     return facet_list;
235 }
236
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);
241     return term;
242 }
243
244 static void addterms(ODR odr, Z_FacetField *facet_field) {
245     int index;
246     int count = 100;
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");
254         count = count - 10 ;
255         facet_field->terms[index] = facet_term;
256     }
257 }
258 Z_OtherInformation *build_facet_response(ODR odr, Z_FacetList *facet_list) {
259     int index;
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]);
264     }
265     oi->list = odr_malloc(odr, sizeof(*oi->list));
266     oiu->category = 0;
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);
270
271     oiu->information.externallyDefinedInfo->u.facetList = facet_list;
272     oi->list[0] = oiu;
273     return oi;
274 }
275
276 int ztest_search(void *handle, bend_search_rr *rr)
277 {
278     struct session_handle *sh = (struct session_handle*) handle;
279     struct result_set *new_set;
280     const char *db, *db_sep;
281
282     if (rr->num_bases != 1)
283     {
284         rr->errcode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
285         return 0;
286     }
287     
288     db = rr->basenames[0];
289
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))
296     {
297         rr->estimated_hit_count = 1;
298     }
299     else
300     {
301         rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
302         rr->errstring = rr->basenames[0];
303         return 0;
304     }
305
306     new_set = get_set(sh, rr->setname);    
307     if (new_set)
308     {
309         if (!rr->replace_set)
310         {
311             rr->errcode = YAZ_BIB1_RESULT_SET_EXISTS_AND_REPLACE_INDICATOR_OFF;
312             return 0;
313         }
314         xfree(new_set->db);
315     }
316     else
317     {
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);
322     }
323     new_set->hits = 0;
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);
328
329     db_sep = strchr(db, '?');
330     if (db_sep)
331     {
332         char **names;
333         char **values;
334         int no_parms = yaz_uri_to_array(db_sep+1, rr->stream, &names, &values);
335         int i;
336         for (i = 0; i < no_parms; i++)
337         {
338             const char *name = names[i];
339             const char *value = values[i];
340             if (!strcmp(name, "seed"))
341                 srand(atoi(value));
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);
348             else
349             {
350                 rr->errcode = YAZ_BIB1_SERVICE_UNSUPP_FOR_THIS_DATABASE;
351                 rr->errstring = odr_strdup(rr->stream, name);
352             }
353         }
354     }
355
356     if (rr->extra_args)
357     {
358         Z_SRW_extra_arg *a;
359         WRBUF response_xml = wrbuf_alloc();
360         wrbuf_puts(response_xml, "<extra>");
361         for (a = rr->extra_args; a; a = a->next)
362         {
363             wrbuf_puts(response_xml, "<extra name=\"");
364             wrbuf_xmlputs(response_xml, a->name);
365             wrbuf_puts(response_xml, "\"");
366             if (a->value)
367             {
368                 wrbuf_puts(response_xml, " value=\"");
369                 wrbuf_xmlputs(response_xml, a->value);
370                 wrbuf_puts(response_xml, "\"");
371             }
372             wrbuf_puts(response_xml, "/>");
373         }
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);
378     }
379     rr->hits = get_hit_count(rr->query);
380
381     if (1)
382     {
383         /* TODO Not general. Only handles one (Facet) OtherInformation. Overwrite  */
384         Z_FacetList *facet_list = extract_facet_request(rr->stream, rr->search_input);
385         if (facet_list) {
386             rr->search_info = build_facet_response(rr->stream, facet_list);
387         }
388     }
389     do_delay(&new_set->search_delay);
390     new_set->hits = rr->hits;
391     
392     return 0;
393 }
394
395
396 /* this huge function handles extended services */
397 int ztest_esrequest(void *handle, bend_esrequest_rr *rr)
398 {
399     /* user-defined handle - created in bend_init */
400     int *counter = (int*) handle;  
401
402     yaz_log(log_level, "ESRequest no %d", *counter);
403
404     (*counter)++;
405
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);
409
410
411     yaz_log(log_level, "function: " ODR_INT_PRINTF, *rr->esr->function);
412
413     if (!rr->esr->taskSpecificParameters)
414     {
415         yaz_log(log_level, "No task specific parameters");
416     }
417     else if (rr->esr->taskSpecificParameters->which == Z_External_itemOrder)
418     {
419         Z_ItemOrder *it = rr->esr->taskSpecificParameters->u.itemOrder;
420         yaz_log(log_level, "Received ItemOrder");
421         if (it->which == Z_IOItemOrder_esRequest)
422         {
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;
427             
428             if (k && k->contact)
429             {
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);
436             }
437             if (k->addlBilling)
438             {
439                 yaz_log(log_level, "Billing info (not shown)");
440             }
441             
442             if (n->resultSetItem)
443             {
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);
447             }
448             if (n->itemRequest)
449             {
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)
454                 {
455                     char oid_name_str[OID_STR_MAX];
456                     oid_class oclass;
457                     const char *oid_name = 
458                         yaz_oid_to_string_buf(r->direct_reference,
459                                               &oclass, oid_name_str);
460                     if (oid_name)
461                         yaz_log(log_level, "OID %s", oid_name);
462                     if (!oid_oidcmp(r->direct_reference, yaz_oid_recsyn_xml))
463                     {
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>";
470                     }
471                     if (!oid_oidcmp(r->direct_reference, 
472                                     yaz_oid_general_isoill_1))
473                     {
474                         yaz_log(log_level, "Decode ItemRequest begin");
475                         if (r->which == ODR_EXTERNAL_single)
476                         {
477                             odr_setbuf(rr->decode,
478                                        (char *) r->u.single_ASN1_type->buf,
479                                        r->u.single_ASN1_type->len, 0);
480                             
481                             if (!ill_ItemRequest(rr->decode, &item_req, 0, 0))
482                             {
483                                 yaz_log(log_level,
484                                         "Couldn't decode ItemRequest %s near %ld",
485                                         odr_errmsg(odr_geterror(rr->decode)),
486                                         (long) odr_offset(rr->decode));
487                             }
488                             else
489                                 yaz_log(log_level, "Decode ItemRequest OK");
490                             if (rr->print)
491                             {
492                                 ill_ItemRequest(rr->print, &item_req, 0,
493                                                 "ItemRequest");
494                                 odr_reset(rr->print);
495                             }
496                         }
497                         if (!item_req && r->which == ODR_EXTERNAL_single)
498                         {
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);
503                             
504                             if (!ill_APDU(rr->decode, &ill_apdu, 0, 0))
505                             {
506                                 yaz_log(log_level,
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);
514                             }
515                             else
516                                 yaz_log(log_level, "Decode ILL APDU OK");
517                             if (rr->print)
518                             {
519                                 ill_APDU(rr->print, &ill_apdu, 0,
520                                          "ILL APDU");
521                                 odr_reset(rr->print);
522                             }
523                         }
524                     }
525                 }
526                 if (item_req)
527                 {
528                     yaz_log(log_level, "ILL protocol version = "
529                             ODR_INT_PRINTF,
530                             *item_req->protocol_version_num);
531                 }
532             }
533             if (k)
534             {
535
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));
542
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;
563
564                 ext->direct_reference =
565                     odr_oiddup(rr->stream, rr->esr->packageType);
566                 ext->indirect_reference = 0;
567                 ext->descriptor = 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;
576
577                 if (xml_in_response)
578                     targetPart->itemRequest =
579                         z_ext_record_xml(rr->stream, xml_in_response,
580                                          strlen(xml_in_response));
581                 else
582                     targetPart->itemRequest = 0;
583                     
584                 targetPart->statusOrErrorReport = 0;
585                 targetPart->auxiliaryStatus = 0;
586             }
587         }
588     }
589     else if (rr->esr->taskSpecificParameters->which == Z_External_update)
590     {
591         Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
592         yaz_log(log_level, "Received DB Update");
593         if (up->which == Z_IUUpdate_esRequest)
594         {
595             Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
596             Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
597             Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
598             
599             yaz_log(log_level, "action");
600             if (toKeep->action)
601             {
602                 switch (*toKeep->action)
603                 {
604                 case Z_IUOriginPartToKeep_recordInsert:
605                     yaz_log(log_level, " recordInsert");
606                     break;
607                 case Z_IUOriginPartToKeep_recordReplace:
608                     yaz_log(log_level, " recordReplace");
609                     break;
610                 case Z_IUOriginPartToKeep_recordDelete:
611                     yaz_log(log_level, " recordDelete");
612                     break;
613                 case Z_IUOriginPartToKeep_elementUpdate:
614                     yaz_log(log_level, " elementUpdate");
615                     break;
616                 case Z_IUOriginPartToKeep_specialUpdate:
617                     yaz_log(log_level, " specialUpdate");
618                     break;
619                 default:
620                     yaz_log(log_level, " unknown (" ODR_INT_PRINTF ")",
621                             *toKeep->action);
622                 }
623             }
624             if (toKeep->databaseName)
625             {
626                 yaz_log(log_level, "database: %s", toKeep->databaseName);
627                 if (!strcmp(toKeep->databaseName, "fault"))
628                 {
629                     rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
630                     rr->errstring = toKeep->databaseName;
631                 }
632                 if (!strcmp(toKeep->databaseName, "accept"))
633                     rr->errcode = -1;
634             }
635             if (toKeep)
636             {
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));
643
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;
664
665                 ext->direct_reference =
666                     odr_oiddup(rr->stream, rr->esr->packageType);
667                 ext->indirect_reference = 0;
668                 ext->descriptor = 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;
677
678                 keep->action = odr_intdup(rr->stream, *toKeep->action);
679                 keep->databaseName =
680                     odr_strdup(rr->stream, toKeep->databaseName);
681                 keep->schema = 0;
682                 keep->elementSetName = 0;
683                 keep->actionQualifier = 0;
684
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));
697                 
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
707                     = 0;
708
709                 targetPart->taskPackageRecords[0]->supplementalDiagnostics = 0;
710             }
711             if (notToKeep)
712             {
713                 int i;
714                 for (i = 0; i < notToKeep->num; i++)
715                 {
716                     Z_External *rec = notToKeep->elements[i]->record;
717
718                     if (rec->direct_reference)
719                     {
720                         char oid_name_str[OID_STR_MAX];
721                         const char *oid_name 
722                             = oid_name = yaz_oid_to_string_buf(
723                                 rec->direct_reference, 0,
724                                 oid_name_str);
725                         if (oid_name)
726                             yaz_log(log_level, "record %d type %s", i,
727                                     oid_name);
728                     }
729                     switch (rec->which)
730                     {
731                     case Z_External_sutrs:
732                         if (rec->u.octet_aligned->len > 170)
733                             yaz_log(log_level, "%d bytes:\n%.168s ...",
734                                     rec->u.sutrs->len,
735                                     rec->u.sutrs->buf);
736                         else
737                             yaz_log(log_level, "%d bytes:\n%s",
738                                     rec->u.sutrs->len,
739                                     rec->u.sutrs->buf);
740                         break;
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);
746                         else
747                             yaz_log(log_level, "%d bytes\n%s",
748                                     rec->u.octet_aligned->len,
749                                     rec->u.octet_aligned->buf);
750                     }
751                 }
752             }
753         }
754     }
755     return 0;
756 }
757
758 /* result set delete */
759 int ztest_delete(void *handle, bend_delete_rr *rr)
760 {
761     if (rr->num_setnames == 1 && !strcmp(rr->setnames[0], "1"))
762         rr->delete_status = Z_DeleteStatus_success;
763     else
764         rr->delete_status = Z_DeleteStatus_resultSetDidNotExist;
765     return 0;
766 }
767
768 /* Our sort handler really doesn't sort... */
769 int ztest_sort(void *handle, bend_sort_rr *rr)
770 {
771     rr->errcode = 0;
772     rr->sort_status = Z_SortResponse_success;
773     return 0;
774 }
775
776
777 /* present request handler */
778 int ztest_present(void *handle, bend_present_rr *rr)
779 {
780     struct session_handle *sh = (struct session_handle*) handle;
781     struct result_set *set = get_set(sh, rr->setname);    
782
783     if (!set)
784     {
785         rr->errcode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
786         rr->errstring = odr_strdup(rr->stream, rr->setname);
787         return 0;
788     }
789     do_delay(&set->present_delay);
790     return 0;
791 }
792
793 /* retrieval of a single record (present, and piggy back search) */
794 int ztest_fetch(void *handle, bend_fetch_rr *r)
795 {
796     struct session_handle *sh = (struct session_handle*) handle;
797     char *cp;
798     const Odr_oid *oid = r->request_format;
799     struct result_set *set = get_set(sh, r->setname);    
800
801     if (!set)
802     {
803         r->errcode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
804         r->errstring = odr_strdup(r->stream, r->setname);
805         return 0;
806     }
807     do_delay(&set->fetch_delay);
808     r->last_in_set = 0;
809     r->basename = set->db;
810     r->output_format = r->request_format;
811
812     if (r->number < 1 || r->number > set->hits)
813     {
814         r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
815         return 0;
816     }
817     if (!oid || yaz_oid_is_iso2709(oid))
818     {
819         cp = dummy_marc_record(r->number, r->stream);
820         if (!cp)
821         {
822             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
823             r->surrogate_flag = 1;
824             return 0;
825         }
826         else
827         {
828             r->len = strlen(cp);
829             r->record = cp;
830             r->output_format = odr_oiddup(r->stream, yaz_oid_recsyn_usmarc);
831         }
832     }
833     else if (!oid_oidcmp(oid, yaz_oid_recsyn_opac))
834     {
835         cp = dummy_marc_record(r->number, r->stream);
836         if (!cp)
837         {
838             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
839             r->surrogate_flag = 1;
840             return 0;
841         }
842         r->record = (char *) dummy_opac(r->number, r->stream, cp);
843         r->len = -1;
844     }
845     else if (!oid_oidcmp(oid, yaz_oid_recsyn_sutrs))
846     {
847         /* this section returns a small record */
848         char buf[100];
849         
850         sprintf(buf, "This is dummy SUTRS record number %d\n", r->number);
851
852         r->len = strlen(buf);
853         r->record = (char *) odr_malloc(r->stream, r->len+1);
854         strcpy(r->record, buf);
855     }
856     else if (!oid_oidcmp(oid, yaz_oid_recsyn_grs_1))
857     {
858         r->len = -1;
859         r->record = (char*) dummy_grs_record(r->number, r->stream);
860         if (!r->record)
861         {
862             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
863             r->surrogate_flag = 1;
864             return 0;
865         }
866     }
867     else if (!oid_oidcmp(oid, yaz_oid_recsyn_postscript))
868     {
869         char fname[20];
870         FILE *f;
871         long size;
872
873         sprintf(fname, "part.%d.ps", r->number);
874         f = fopen(fname, "rb");
875         if (!f)
876         {
877             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
878             r->surrogate_flag = 1;
879             return 0;
880         }
881         fseek(f, 0L, SEEK_END);
882         size = ftell(f);
883         if (size <= 0 || size >= 5000000)
884         {
885             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
886             r->surrogate_flag = 1;
887         }
888         fseek(f, 0L, SEEK_SET);
889         r->record = (char*) odr_malloc(r->stream, size);
890         r->len = size;
891         if (fread(r->record, size, 1, f) != 1)
892         {
893             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
894             r->surrogate_flag = 1;
895         }
896         fclose(f);
897     }
898     else if (!oid_oidcmp(oid, yaz_oid_recsyn_xml))
899     {
900         if ((cp = dummy_xml_record(r->number, r->stream)))
901         {
902             r->len = strlen(cp);
903             r->record = cp;
904         }
905         else 
906         {
907             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
908             r->surrogate_flag = 1;
909             return 0;
910         }
911     }
912     else
913     {
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));
917         return 0;
918     }
919     r->errcode = 0;
920     return 0;
921 }
922
923 /*
924  * silly dummy-scan what reads words from a file.
925  */
926 int ztest_scan(void *handle, bend_scan_rr *q)
927 {
928     static FILE *f = 0;
929     static struct scan_entry list[200];
930     static char entries[200][80];
931     int hits[200];
932     char term[80], *p;
933     int i, pos;
934     int term_position_req = q->term_position;
935     int num_entries_req = q->num_entries;
936
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 */))
941         ;
942     else
943     {
944         q->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
945         q->errstring = q->basenames[0];
946         return 0;
947     }
948
949     q->errcode = 0;
950     q->errstring = 0;
951     q->entries = list;
952     q->status = BEND_SCAN_SUCCESS;
953     if (!f && !(f = fopen("dummy-words", "r")))
954     {
955         perror("dummy-words");
956         exit(1);
957     }
958     if (q->num_entries > 200)
959     {
960         q->errcode = YAZ_BIB1_RESOURCES_EXHAUSTED_NO_RESULTS_AVAILABLE;
961         return 0;
962     }
963     if (q->term)
964     {
965         int len;
966         if (q->term->term->which != Z_Term_general)
967         {
968             q->errcode = YAZ_BIB1_TERM_TYPE_UNSUPP;
969             return 0;
970         }
971         if (*q->step_size != 0)
972         {
973             q->errcode = YAZ_BIB1_ONLY_ZERO_STEP_SIZE_SUPPORTED_FOR_SCAN;
974             return 0;
975         }
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);
980         term[len] = '\0';
981     }
982     else if (q->scanClause)
983     {
984         strncpy(term, q->scanClause, sizeof(term)-1);
985         term[sizeof(term)-1] = '\0';
986     }
987     else
988         strcpy(term, "0");
989
990     for (p = term; *p; p++)
991         if (islower(*(unsigned char *) p))
992             *p = toupper(*p);
993
994     fseek(f, 0, SEEK_SET);
995     q->num_entries = 0;
996
997     for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
998          i++, pos < 199 ? pos++ : (pos = 0))
999     {
1000         if (!q->num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
1001         {
1002             if ((q->term_position = term_position_req) > i + 1)
1003             {
1004                 q->term_position = i + 1;
1005                 q->status = BEND_SCAN_PARTIAL;
1006             }
1007             for (; q->num_entries < q->term_position; q->num_entries++)
1008             {
1009                 int po;
1010
1011                 po = pos - q->term_position + q->num_entries+1; /* find pos */
1012                 if (po < 0)
1013                     po += 200;
1014
1015                 if (!strcmp(term, "SD") && q->num_entries == 2)
1016                 {
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";
1022                 }
1023                 else
1024                 {
1025                     list[q->num_entries].term = entries[po];
1026                     list[q->num_entries].occurrences = hits[po];
1027                 }
1028             }
1029         }
1030         else if (q->num_entries)
1031         {
1032             list[q->num_entries].term = entries[pos];
1033             list[q->num_entries].occurrences = hits[pos];
1034             q->num_entries++;
1035         }
1036         if (q->num_entries >= num_entries_req)
1037             break;
1038     }
1039     if (feof(f))
1040         q->status = BEND_SCAN_PARTIAL;
1041     return 0;
1042 }
1043
1044 int ztest_explain(void *handle, bend_explain_rr *rr)
1045 {
1046     if (rr->database && !strcmp(rr->database, "Default"))
1047     {
1048         rr->explain_buf = "<explain>\n"
1049             "\t<serverInfo>\n"
1050             "\t\t<host>localhost</host>\n"
1051             "\t\t<port>210</port>\n"
1052             "\t</serverInfo>\n"
1053             "</explain>\n";
1054     }
1055     return 0;
1056 }
1057
1058 int ztest_update(void *handle, bend_update_rr *rr)
1059 {
1060     rr->operation_status = "success";
1061     return 0;
1062 }
1063
1064 bend_initresult *bend_init(bend_initrequest *q)
1065 {
1066     bend_initresult *r = (bend_initresult *)
1067         odr_malloc(q->stream, sizeof(*r));
1068     struct session_handle *sh = xmalloc(sizeof(*sh));
1069
1070     sh->result_sets = 0;
1071
1072     if (!log_level_set)
1073     {
1074         log_level=yaz_log_module_level("ztest");
1075         log_level_set=1;
1076     }
1077
1078     r->errcode = 0;
1079     r->errstring = 0;
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;
1088 #if 0
1089     q->bend_explain = ztest_explain;
1090 #endif
1091     q->bend_srw_scan = ztest_scan;
1092     q->bend_srw_update = ztest_update;
1093
1094     q->query_charset = "ISO-8859-1";
1095     q->records_in_same_charset = 0;
1096
1097     return r;
1098 }
1099
1100 void bend_close(void *handle)
1101 {
1102     struct session_handle *sh = (struct session_handle*) handle;
1103     remove_sets(sh);
1104     xfree(sh);              /* release our session */
1105     return;
1106 }
1107
1108 int main(int argc, char **argv)
1109 {
1110     return statserv_main(argc, argv, bend_init, bend_close);
1111 }
1112 /*
1113  * Local variables:
1114  * c-basic-offset: 4
1115  * c-file-style: "Stroustrup"
1116  * indent-tabs-mode: nil
1117  * End:
1118  * vim: shiftwidth=4 tabstop=8 expandtab
1119  */
1120