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