Update source headers for 2008. Omit CVS ID keyword subst.
[yaz-moved-to-github.git] / ztest / ztest.c
index 515d1ac..bf8e82a 100644 (file)
@@ -1,8 +1,6 @@
-/*
- * Copyright (C) 1995-2005, Index Data ApS
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2008 Index Data
  * See the file LICENSE for details.
- *
- * $Id: ztest.c,v 1.82 2006-12-04 14:56:55 adam Exp $
  */
 
 /*
@@ -16,7 +14,7 @@
 #include <unistd.h>
 #endif
 
-#include <yaz/yaz-util.h>
+#include <yaz/log.h>
 #include <yaz/backend.h>
 #include <yaz/ill.h>
 
@@ -33,6 +31,57 @@ int ztest_present(void *handle, bend_present_rr *rr);
 int ztest_esrequest(void *handle, bend_esrequest_rr *rr);
 int ztest_delete(void *handle, bend_delete_rr *rr);
 
+/** \fn get_term_hit
+    \brief use term value as hit count 
+   
+    Traverse RPN tree 'in order' and use term value as hit count.
+    Only terms  that looks a numeric is used.. Returns -1 if
+    no sub tree has a hit count term
+*/
+static int get_term_hit(Z_RPNStructure *s)
+{
+    int h = -1;
+    switch(s->which)
+    {
+    case Z_RPNStructure_simple:
+        if (s->u.simple->which == Z_Operand_APT)
+        {
+            Z_AttributesPlusTerm *apt = s->u.simple->u.attributesPlusTerm;
+            if (apt->term->which == Z_Term_general)
+            {
+                Odr_oct *oct = apt->term->u.general;
+                if (oct->len > 0 && oct->buf[0] >= '0' && oct->buf[0] <= '9')
+                    h = atoi_n((const char *) oct->buf, oct->len);
+            }
+        }
+        break;
+    case Z_RPNStructure_complex:
+        h = get_term_hit(s->u.complex->s1);
+        if (h == -1)
+            h = get_term_hit(s->u.complex->s2);
+        break;
+    }
+    return h;
+}
+
+/** \fn get_hit_count
+    \brief gets hit count for numeric terms in RPN queries
+    
+    This is just for testing.. A real database of course uses
+    the content of a database to establish a value.. In our case, we
+    have a way to trigger a certain hit count. Good for testing of
+    client applications etc
+*/
+static int get_hit_count(Z_Query *q)
+{
+    int h = -1;
+    if (q->which == Z_Query_type_1 || q->which == Z_Query_type_101)
+        h = get_term_hit(q->u.type_1->RPNStructure);
+    if (h == -1)
+        h = rand() % 24;
+    return h;
+}
+
 int ztest_search(void *handle, bend_search_rr *rr)
 {
     if (rr->num_bases != 1)
@@ -40,14 +89,6 @@ int ztest_search(void *handle, bend_search_rr *rr)
         rr->errcode = 23;
         return 0;
     }
-#if NMEM_DEBUG
-    /* if database is stop, stop this process.. For debugging only. */
-    if (!yaz_matchstr (rr->basenames[0], "stop"))
-    {
-        nmem_print_list_l(YLOG_LOG);
-        exit(0);
-    }
-#endif
     /* Throw Database unavailable if other than Default or Slow */
     if (!yaz_matchstr (rr->basenames[0], "Default"))
         ;  /* Default is OK in our test */
@@ -66,7 +107,7 @@ int ztest_search(void *handle, bend_search_rr *rr)
             sleep(1);
         }
 #endif
-        ;
+        rr->estimated_hit_count = 1;
     }
     else
     {
@@ -74,7 +115,8 @@ int ztest_search(void *handle, bend_search_rr *rr)
         rr->errstring = rr->basenames[0];
         return 0;
     }
-    rr->hits = rand() % 24;
+
+    rr->hits = get_hit_count(rr->query);
     return 0;
 }
 
@@ -109,6 +151,7 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
             Z_IORequest *ir = it->u.esRequest;
             Z_IOOriginPartToKeep *k = ir->toKeep;
             Z_IOOriginPartNotToKeep *n = ir->notToKeep;
+            const char *xml_in_response = 0;
             
             if (k && k->contact)
             {
@@ -137,17 +180,24 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
                 ILL_APDU *ill_apdu = 0;
                 if (r->direct_reference)
                 {
-                    oident *ent = oid_getentbyoid(r->direct_reference);
-                    if (ent)
-                        yaz_log(log_level, "OID %s", ent->desc);
-                    if (ent && ent->value == VAL_TEXT_XML)
+                    char oid_name_str[OID_STR_MAX];
+                    oid_class oclass;
+                    const char *oid_name = 
+                        yaz_oid_to_string_buf(r->direct_reference,
+                                          &oclass, oid_name_str);
+                    if (oid_name)
+                        yaz_log(log_level, "OID %s", oid_name);
+                    if (!oid_oidcmp(r->direct_reference, yaz_oid_recsyn_xml))
                     {
                         yaz_log (log_level, "ILL XML request");
                         if (r->which == Z_External_octet)
-                            yaz_log (log_level, "%.*s", r->u.octet_aligned->len,
+                            yaz_log (log_level, "%.*s",
+                                     r->u.octet_aligned->len,
                                      r->u.octet_aligned->buf); 
+                        xml_in_response = "<dummy>x</dummy>";
                     }
-                    if (ent && ent->value == VAL_ISO_ILL_1)
+                    if (!oid_oidcmp(r->direct_reference, 
+                                    yaz_oid_general_isoill_1))
                     {
                         yaz_log (log_level, "Decode ItemRequest begin");
                         if (r->which == ODR_EXTERNAL_single)
@@ -251,7 +301,13 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
                 ext->u.itemOrder->u.taskPackage->originPart = k;
                 ext->u.itemOrder->u.taskPackage->targetPart = targetPart;
 
-                targetPart->itemRequest = 0;
+                if (xml_in_response)
+                    targetPart->itemRequest =
+                        z_ext_record_xml(rr->stream, xml_in_response,
+                                         strlen(xml_in_response));
+                else
+                    targetPart->itemRequest = 0;
+                    
                 targetPart->statusOrErrorReport = 0;
                 targetPart->auxiliaryStatus = 0;
             }
@@ -369,7 +425,7 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
                 targetPart->taskPackageRecords[0]->which =
                     Z_IUTaskPackageRecordStructure_record;
                 targetPart->taskPackageRecords[0]->u.record = 
-                    z_ext_record (rr->stream, VAL_SUTRS, "test", 4);
+                    z_ext_record_sutrs(rr->stream, "test", 4);
                 targetPart->taskPackageRecords[0]->correlationInfo = 0; 
                 targetPart->taskPackageRecords[0]->recordStatus =
                     odr_intdup (rr->stream,
@@ -388,11 +444,14 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
 
                     if (rec->direct_reference)
                     {
-                        struct oident *oident;
-                        oident = oid_getentbyoid(rec->direct_reference);
-                        if (oident)
+                        char oid_name_str[OID_STR_MAX];
+                        const char *oid_name 
+                            = oid_name = yaz_oid_to_string_buf(
+                                rec->direct_reference, 0,
+                                oid_name_str);
+                        if (oid_name)
                             yaz_log (log_level, "record %d type %s", i,
-                                     oident->desc);
+                                     oid_name);
                     }
                     switch (rec->which)
                     {
@@ -452,11 +511,13 @@ int ztest_present (void *handle, bend_present_rr *rr)
 int ztest_fetch(void *handle, bend_fetch_rr *r)
 {
     char *cp;
+    const Odr_oid *oid = r->request_format;
 
     r->last_in_set = 0;
     r->basename = "Default";
-    r->output_format = r->request_format;  
-    if (r->request_format == VAL_SUTRS)
+    r->output_format = r->request_format;
+
+    if (oid && !oid_oidcmp(oid, yaz_oid_recsyn_sutrs))
     {
         /* this section returns a small record */
         char buf[100];
@@ -467,7 +528,7 @@ int ztest_fetch(void *handle, bend_fetch_rr *r)
         r->record = (char *) odr_malloc (r->stream, r->len+1);
         strcpy(r->record, buf);
     }
-    else if (r->request_format == VAL_GRS1)
+    else if (oid &&  !oid_oidcmp(oid, yaz_oid_recsyn_grs_1))
     {
         r->len = -1;
         r->record = (char*) dummy_grs_record(r->number, r->stream);
@@ -477,7 +538,7 @@ int ztest_fetch(void *handle, bend_fetch_rr *r)
             return 0;
         }
     }
-    else if (r->request_format == VAL_POSTSCRIPT)
+    else if (oid && !oid_oidcmp(oid, yaz_oid_recsyn_postscript))
     {
         char fname[20];
         FILE *f;
@@ -500,17 +561,15 @@ int ztest_fetch(void *handle, bend_fetch_rr *r)
         fseek (f, 0L, SEEK_SET);
         r->record = (char*) odr_malloc (r->stream, size);
         r->len = size;
-        r->output_format = VAL_POSTSCRIPT;
         fread (r->record, size, 1, f);
         fclose (f);
     }
-    else if (r->request_format == VAL_TEXT_XML)
+    else if (oid && !oid_oidcmp(oid, yaz_oid_recsyn_xml))
     {
         if ((cp = dummy_xml_record (r->number, r->stream)))
         {
             r->len = strlen(cp);
             r->record = cp;
-            r->output_format = VAL_TEXT_XML;
         }
         else 
         {
@@ -523,7 +582,7 @@ int ztest_fetch(void *handle, bend_fetch_rr *r)
     {
         r->len = strlen(cp);
         r->record = cp;
-        r->output_format = VAL_USMARC;
+        r->output_format = odr_oiddup(r->stream, yaz_oid_recsyn_usmarc);
     }
     else
     {
@@ -673,6 +732,12 @@ int ztest_explain(void *handle, bend_explain_rr *rr)
     return 0;
 }
 
+int ztest_update(void *handle, bend_update_rr *rr)
+{
+    rr->operation_status = "success";
+    return 0;
+}
+
 bend_initresult *bend_init(bend_initrequest *q)
 {
     bend_initresult *r = (bend_initresult *)
@@ -700,6 +765,10 @@ bend_initresult *bend_init(bend_initrequest *q)
     q->bend_explain = ztest_explain;
 #endif
     q->bend_srw_scan = ztest_scan;
+    q->bend_srw_update = ztest_update;
+
+    q->query_charset = "ISO-8859-1";
+    q->records_in_same_charset = 0;
 
     return r;
 }