ZOOM: initial experiments with memcached
[yaz-moved-to-github.git] / src / zoom-z3950.c
index 5fc7c25..a16a809 100644 (file)
@@ -1,11 +1,14 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2010 Index Data
+ * Copyright (C) Index Data
  * See the file LICENSE for details.
  */
 /**
  * \file zoom-z3950.c
  * \brief Implements ZOOM Z39.50 handling
  */
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
 
 #include <assert.h>
 #include <string.h>
@@ -47,15 +50,15 @@ static Z_APDU *create_es_package(ZOOM_package p, const Odr_oid *oid)
     const char *str;
     Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
     Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
-    
+
     str = ZOOM_options_get(p->options, "package-name");
     if (str && *str)
         req->packageName = odr_strdup(p->odr_out, str);
-    
+
     str = ZOOM_options_get(p->options, "user-id");
     if (str)
         req->userId = odr_strdup_null(p->odr_out, str);
-    
+
     req->packageType = odr_oiddup(p->odr_out, oid);
 
     str = ZOOM_options_get(p->options, "function");
@@ -96,13 +99,13 @@ static Z_External *encode_ill_request(ZOOM_package p)
     ILL_Request *req;
     Z_External *r = 0;
     struct ill_get_ctl ctl;
-        
+
     ctl.odr = p->odr_out;
     ctl.clientData = p;
     ctl.f = ill_array_lookup;
-        
+
     req = ill_get_ILLRequest(&ctl, "ill", 0);
-        
+
     if (!ill_Request(out, &req, 0, 0))
     {
         int ill_request_size;
@@ -115,17 +118,15 @@ static Z_External *encode_ill_request(ZOOM_package p)
     {
         int illRequest_size = 0;
         char *illRequest_buf = odr_getbuf(out, &illRequest_size, 0);
-                
+
         r = (Z_External *) odr_malloc(out, sizeof(*r));
         r->direct_reference = odr_oiddup(out, yaz_oid_general_isoill_1);
         r->indirect_reference = 0;
         r->descriptor = 0;
         r->which = Z_External_single;
-                
+
         r->u.single_ASN1_type =
-            odr_create_Odr_oct(out,
-                               (unsigned char *)illRequest_buf,
-                               illRequest_size);
+            odr_create_Odr_oct(out, illRequest_buf, illRequest_size);
     }
     return r;
 }
@@ -135,9 +136,9 @@ static Z_ItemOrder *encode_item_order(ZOOM_package p)
     Z_ItemOrder *req = (Z_ItemOrder *) odr_malloc(p->odr_out, sizeof(*req));
     const char *str;
     int len;
-    
+
     req->which = Z_IOItemOrder_esRequest;
-    req->u.esRequest = (Z_IORequest *) 
+    req->u.esRequest = (Z_IORequest *)
         odr_malloc(p->odr_out,sizeof(Z_IORequest));
 
     /* to keep part ... */
@@ -146,30 +147,30 @@ static Z_ItemOrder *encode_item_order(ZOOM_package p)
     req->u.esRequest->toKeep->supplDescription = 0;
     req->u.esRequest->toKeep->contact = (Z_IOContact *)
         odr_malloc(p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
-        
+
     str = ZOOM_options_get(p->options, "contact-name");
     req->u.esRequest->toKeep->contact->name =
         odr_strdup_null(p->odr_out, str);
-        
+
     str = ZOOM_options_get(p->options, "contact-phone");
     req->u.esRequest->toKeep->contact->phone =
         odr_strdup_null(p->odr_out, str);
-        
+
     str = ZOOM_options_get(p->options, "contact-email");
     req->u.esRequest->toKeep->contact->email =
         odr_strdup_null(p->odr_out, str);
-        
+
     req->u.esRequest->toKeep->addlBilling = 0;
-        
+
     /* not to keep part ... */
     req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
         odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
-        
+
     str = ZOOM_options_get(p->options, "itemorder-setname");
     if (!str)
         str = "default";
 
-    if (!*str) 
+    if (!*str)
         req->u.esRequest->notToKeep->resultSetItem = 0;
     else
     {
@@ -180,7 +181,7 @@ static Z_ItemOrder *encode_item_order(ZOOM_package p)
             odr_strdup(p->odr_out, str);
         req->u.esRequest->notToKeep->resultSetItem->item =
             odr_intdup(p->odr_out, 0);
-        
+
         str = ZOOM_options_get(p->options, "itemorder-item");
         *req->u.esRequest->notToKeep->resultSetItem->item =
             (str ? atoi(str) : 1);
@@ -194,11 +195,11 @@ static Z_ItemOrder *encode_item_order(ZOOM_package p)
     }
     else
         req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
-    
+
     return req;
 }
 
-Z_APDU *create_admin_package(ZOOM_package p, int type, 
+Z_APDU *create_admin_package(ZOOM_package p, int type,
                              Z_ESAdminOriginPartToKeep **toKeepP,
                              Z_ESAdminOriginPartNotToKeep **notToKeepP)
 {
@@ -215,26 +216,26 @@ Z_APDU *create_admin_package(ZOOM_package p, int type,
                                                   p->odr_out);
         if (num_db > 0)
             first_db = db[0];
-            
+
         r->direct_reference = odr_oiddup(p->odr_out, yaz_oid_extserv_admin);
         r->descriptor = 0;
         r->indirect_reference = 0;
         r->which = Z_External_ESAdmin;
-        
+
         r->u.adminService = (Z_Admin *)
             odr_malloc(p->odr_out, sizeof(*r->u.adminService));
         r->u.adminService->which = Z_Admin_esRequest;
         r->u.adminService->u.esRequest = (Z_AdminEsRequest *)
             odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest));
-        
+
         toKeep = r->u.adminService->u.esRequest->toKeep =
-            (Z_ESAdminOriginPartToKeep *) 
+            (Z_ESAdminOriginPartToKeep *)
             odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest->toKeep));
         toKeep->which = type;
         toKeep->databaseName = odr_strdup(p->odr_out, first_db);
         toKeep->u.create = odr_nullval();
         apdu->u.extendedServicesRequest->taskSpecificParameters = r;
-        
+
         r->u.adminService->u.esRequest->notToKeep = notToKeep =
             (Z_ESAdminOriginPartNotToKeep *)
             odr_malloc(p->odr_out,
@@ -267,10 +268,9 @@ static Z_APDU *create_xmlupdate_package(ZOOM_package p)
     ext->direct_reference = req->packageType;
     ext->descriptor = 0;
     ext->indirect_reference = 0;
-    
+
     ext->which = Z_External_octet;
-    ext->u.single_ASN1_type =
-        odr_create_Odr_oct(p->odr_out, (const unsigned char *) doc, len);
+    ext->u.single_ASN1_type = odr_create_Odr_oct(p->odr_out, doc, len);
     return apdu;
 }
 
@@ -321,12 +321,15 @@ static Z_APDU *create_update_package(ZOOM_package p)
                                            p->odr_out);
     }
     if (!syntax_oid)
+    {
+        ZOOM_set_error(p->connection, ZOOM_ERROR_ES_INVALID_SYNTAX, syntax_str);
         return 0;
+    }
 
     if (num_db > 0)
         first_db = db[0];
-    
-    switch(*version)
+
+    switch (*version)
     {
     case '1':
         package_oid = yaz_oid_extserv_database_update_first_version;
@@ -345,9 +348,10 @@ static Z_APDU *create_update_package(ZOOM_package p)
         package_oid = yaz_oid_extserv_database_update;
         break;
     default:
+        ZOOM_set_error(p->connection, ZOOM_ERROR_ES_INVALID_VERSION, version);
         return 0;
     }
-    
+
     if (!strcmp(action, "recordInsert"))
         action_no = Z_IUOriginPartToKeep_recordInsert;
     else if (!strcmp(action, "recordReplace"))
@@ -359,7 +363,10 @@ static Z_APDU *create_update_package(ZOOM_package p)
     else if (!strcmp(action, "specialUpdate"))
         action_no = Z_IUOriginPartToKeep_specialUpdate;
     else
+    {
+        ZOOM_set_error(p->connection, ZOOM_ERROR_ES_INVALID_ACTION, action);
         return 0;
+    }
 
     apdu = create_es_package(p, package_oid);
     if (apdu)
@@ -370,32 +377,32 @@ static Z_APDU *create_update_package(ZOOM_package p)
             odr_malloc(p->odr_out, sizeof(*r));
         const char *elementSetName =
             ZOOM_options_get(p->options, "elementSetName");
-        
+
         apdu->u.extendedServicesRequest->taskSpecificParameters = r;
-        
+
         r->direct_reference = odr_oiddup(p->odr_out, package_oid);
         r->descriptor = 0;
         r->which = Z_External_update;
         r->indirect_reference = 0;
         r->u.update = (Z_IUUpdate *)
             odr_malloc(p->odr_out, sizeof(*r->u.update));
-        
+
         r->u.update->which = Z_IUUpdate_esRequest;
         r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
             odr_malloc(p->odr_out, sizeof(*r->u.update->u.esRequest));
-        toKeep = r->u.update->u.esRequest->toKeep = 
+        toKeep = r->u.update->u.esRequest->toKeep =
             (Z_IUOriginPartToKeep *)
             odr_malloc(p->odr_out, sizeof(*toKeep));
-        
+
         toKeep->databaseName = odr_strdup(p->odr_out, first_db);
         toKeep->schema = 0;
-        
+
         toKeep->elementSetName = odr_strdup_null(p->odr_out, elementSetName);
-            
+
         toKeep->actionQualifier = 0;
         toKeep->action = odr_intdup(p->odr_out, action_no);
-        
-        notToKeep = r->u.update->u.esRequest->notToKeep = 
+
+        notToKeep = r->u.update->u.esRequest->notToKeep =
             (Z_IUSuppliedRecords *)
             odr_malloc(p->odr_out, sizeof(*notToKeep));
         notToKeep->num = 1;
@@ -406,15 +413,14 @@ static Z_APDU *create_update_package(ZOOM_package p)
         notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
         if (recordIdOpaque)
         {
-            notToKeep->elements[0]->u.opaque = 
-                odr_create_Odr_oct(p->odr_out,
-                                   (const unsigned char *) recordIdOpaque,
+            notToKeep->elements[0]->u.opaque =
+                odr_create_Odr_oct(p->odr_out, recordIdOpaque,
                                    recordIdOpaque_len);
         }
         else if (recordIdNumber)
         {
             notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_number;
-            
+
             notToKeep->elements[0]->u.number =
                 odr_intdup(p->odr_out, atoi(recordIdNumber));
         }
@@ -480,12 +486,12 @@ static void otherInfo_attach(ZOOM_connection c, Z_APDU *a, ODR out)
             len = sizeof(buf)-1;
         memcpy(buf, val, len);
         buf[len] = '\0';
-        
+
         oid = yaz_string_to_oid_odr(yaz_oid_std(), CLASS_USERINFO,
                                     buf, out);
         if (!oid)
             continue;
-        
+
         yaz_oi_APDU(a, &oi);
         yaz_oi_set_string_oid(oi, out, oid, 1, cp+1);
     }
@@ -500,14 +506,14 @@ static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
     {
         Z_OtherInformation **oi;
         yaz_oi_APDU(a, &oi);
-        yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_cookie, 
+        yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_cookie,
                               1, c->cookie_out);
     }
     if (c->client_IP)
     {
         Z_OtherInformation **oi;
         yaz_oi_APDU(a, &oi);
-        yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_client_ip, 
+        yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_client_ip,
                               1, c->client_IP);
     }
     otherInfo_attach(c, a, out);
@@ -529,6 +535,8 @@ static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
     }
     if (c->odr_print)
         z_APDU(c->odr_print, &a, 0, 0);
+    if (c->odr_save)
+        z_APDU(c->odr_save, &a, 0, 0);
     yaz_log(c->log_details, "%p encoding_APDU encoding OK", c);
     return 0;
 }
@@ -560,30 +568,30 @@ zoom_ret ZOOM_connection_Z3950_send_init(ZOOM_connection c)
     ODR_MASK_SET(ireq->options, Z_Options_sort);
     ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
     ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
-    
+
     ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
     ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
     ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
-    
+
     ireq->implementationId =
         odr_prepend(c->odr_out,
                     ZOOM_options_get(c->options, "implementationId"),
                     ireq->implementationId);
-    
-    ireq->implementationName = 
+
+    ireq->implementationName =
         odr_prepend(c->odr_out,
                     ZOOM_options_get(c->options, "implementationName"),
                     odr_prepend(c->odr_out, "ZOOM-C",
                                 ireq->implementationName));
-    
-    ireq->implementationVersion = 
+
+    ireq->implementationVersion =
         odr_prepend(c->odr_out,
                     ZOOM_options_get(c->options, "implementationVersion"),
                                 ireq->implementationVersion);
-    
+
     *ireq->maximumRecordSize = c->maximum_record_size;
     *ireq->preferredMessageSize = c->preferred_message_size;
-    
+
     if (c->group || c->password)
     {
         Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
@@ -609,9 +617,9 @@ zoom_ret ZOOM_connection_Z3950_send_init(ZOOM_connection c)
     {
         Z_OtherInformation **oi;
         Z_OtherInformationUnit *oi_unit;
-        
+
         yaz_oi_APDU(apdu, &oi);
-        
+
         if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
         {
             ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
@@ -630,6 +638,7 @@ zoom_ret ZOOM_connection_Z3950_send_search(ZOOM_connection c)
     ZOOM_resultset r;
     int lslb, ssub, mspn;
     const char *syntax;
+    const char *schema;
     Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
     Z_SearchRequest *search_req = apdu->u.searchRequest;
     const char *elementSetName;
@@ -661,12 +670,11 @@ zoom_ret ZOOM_connection_Z3950_send_search(ZOOM_connection c)
     if (facets) {
         Z_FacetList *facet_list = yaz_pqf_parse_facet_list(c->odr_out, facets);
         if (facet_list) {
-            Z_OtherInformation **oi;
-            yaz_oi_APDU(apdu, &oi);
-            if (facet_list) {
-                yaz_oi_set_facetlist(oi, c->odr_out, facet_list);
-            }
+            Z_OtherInformation **oi = &search_req->additionalSearchInfo;
+            yaz_oi_set_facetlist(oi, c->odr_out, facet_list);
         }
+        else
+            yaz_log(YLOG_WARN, "Unable to parse facets: %s", facets);
     }
 
     assert(r);
@@ -679,7 +687,7 @@ zoom_ret ZOOM_connection_Z3950_send_search(ZOOM_connection c)
         ZOOM_set_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
         return zoom_complete;
     }
-    if (search_req->query->which == Z_Query_type_1 || 
+    if (search_req->query->which == Z_Query_type_1 ||
         search_req->query->which == Z_Query_type_101)
     {
         const char *cp = ZOOM_options_get(r->options, "rpnCharset");
@@ -691,7 +699,7 @@ zoom_ret ZOOM_connection_Z3950_send_search(ZOOM_connection c)
                 int r;
                 search_req->query = yaz_copy_Z_Query(search_req->query,
                                                      c->odr_out);
-                
+
                 r = yaz_query_charset_convert_rpnquery_check(
                     search_req->query->u.type_1,
                     c->odr_out, cd);
@@ -710,6 +718,8 @@ zoom_ret ZOOM_connection_Z3950_send_search(ZOOM_connection c)
     /* get syntax (no need to provide unless piggyback is in effect) */
     syntax = c->tasks->u.search.syntax;
 
+    schema = c->tasks->u.search.schema;
+
     lslb = ZOOM_options_get_int(r->options, "largeSetLowerBound", -1);
     ssub = ZOOM_options_get_int(r->options, "smallSetUpperBound", -1);
     mspn = ZOOM_options_get_int(r->options, "mediumSetPresentNumber", -1);
@@ -721,12 +731,12 @@ zoom_ret ZOOM_connection_Z3950_send_search(ZOOM_connection c)
         *search_req->mediumSetPresentNumber = mspn;
     }
     else if (c->tasks->u.search.start == 0 && c->tasks->u.search.count > 0
-             && r->piggyback && !r->r_sort_spec && !r->schema)
+             && r->piggyback && !r->r_sort_spec && !schema)
     {
         /* Regular piggyback - do it unless we're going to do sort */
         *search_req->largeSetLowerBound = 2000000000;
         *search_req->smallSetUpperBound = 1;
-        *search_req->mediumSetPresentNumber = 
+        *search_req->mediumSetPresentNumber =
             r->step>0 ? r->step : c->tasks->u.search.count;
     }
     else
@@ -740,7 +750,7 @@ zoom_ret ZOOM_connection_Z3950_send_search(ZOOM_connection c)
     {
         Z_ElementSetNames *esn = (Z_ElementSetNames *)
             odr_malloc(c->odr_out, sizeof(*esn));
-        
+
         esn->which = Z_ElementSetNames_generic;
         esn->u.generic = odr_strdup(c->odr_out, smallSetElementSetName);
         search_req->smallSetElementSetNames = esn;
@@ -749,7 +759,7 @@ zoom_ret ZOOM_connection_Z3950_send_search(ZOOM_connection c)
     {
         Z_ElementSetNames *esn =(Z_ElementSetNames *)
             odr_malloc(c->odr_out, sizeof(*esn));
-        
+
         esn->which = Z_ElementSetNames_generic;
         esn->u.generic = odr_strdup(c->odr_out, mediumSetElementSetName);
         search_req->mediumSetElementSetNames = esn;
@@ -757,7 +767,7 @@ zoom_ret ZOOM_connection_Z3950_send_search(ZOOM_connection c)
     if (syntax)
         search_req->preferredRecordSyntax =
             zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
-    
+
     if (!r->setname)
     {
         if (c->support_named_resultsets)
@@ -768,15 +778,6 @@ zoom_ret ZOOM_connection_Z3950_send_search(ZOOM_connection c)
                result sets on the server. */
             for (ord = 1; ; ord++)
             {
-#if ZOOM_RESULT_LISTS
-                ZOOM_resultsets rsp;
-                sprintf(setname, "%d", ord);
-                for (rsp = c->resultsets; rsp; rsp = rsp->next)
-                    if (rsp->resultset->setname && !strcmp(rsp->resultset->setname, setname))
-                        break;
-                if (!rsp)
-                    break;
-#else
                 ZOOM_resultset rp;
                 sprintf(setname, "%d", ord);
                 for (rp = c->resultsets; rp; rp = rp->next)
@@ -784,8 +785,6 @@ zoom_ret ZOOM_connection_Z3950_send_search(ZOOM_connection c)
                         break;
                 if (!rp)
                     break;
-#endif
-
             }
             r->setname = xstrdup(setname);
             yaz_log(c->log_details, "%p ZOOM_connection_send_search: "
@@ -867,7 +866,7 @@ zoom_ret ZOOM_connection_Z3950_send_scan(ZOOM_connection c)
     req->stepSize =
         odr_intdup(c->odr_out,
                    ZOOM_options_get_int(scan->options, "stepSize", 0));
-    
+
     req->databaseNames = scan->databaseNames;
     req->num_databaseNames = scan->num_databaseNames;
 
@@ -891,8 +890,8 @@ ZOOM_API(void)
         if (apdu)
         {
             Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
-            
-            r->direct_reference = 
+
+            r->direct_reference =
                 odr_oiddup(p->odr_out, yaz_oid_extserv_item_order);
             r->descriptor = 0;
             r->which = Z_External_itemOrder;
@@ -906,7 +905,7 @@ ZOOM_API(void)
     {
         apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_create,
                                     0, 0);
-    }   
+    }
     else if (!strcmp(type, "drop"))  /* drop database */
     {
         apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_drop,
@@ -936,7 +935,7 @@ ZOOM_API(void)
             buf = odr_getbuf(p->odr_out, &p->len_out, 0);
             p->buf_out = (char *) xmalloc(p->len_out);
             memcpy(p->buf_out, buf, p->len_out);
-            
+
             (p->refcount)++;
             if (!c->async)
             {
@@ -947,7 +946,6 @@ ZOOM_API(void)
     }
 }
 
-
 static void handle_Z3950_records(ZOOM_connection c, Z_Records *sr,
                                  int present_phase);
 
@@ -981,63 +979,60 @@ static void response_diag(ZOOM_connection c, Z_DiagRec *p)
 }
 
 static int es_response_taskpackage_update(ZOOM_connection c,
-               Z_IUUpdateTaskPackage *utp)
+                                          Z_IUUpdateTaskPackage *utp)
 {
-       if (utp && utp->targetPart)
-       {
-               Z_IUTargetPart *targetPart = utp->targetPart;
-               switch ( *targetPart->updateStatus ) {
-                       case Z_IUTargetPart_success:
-                               ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "success");
-                               break;
-                       case Z_IUTargetPart_partial:
-                               ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "partial");
-                               break;
-                       case Z_IUTargetPart_failure:
-                               ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "failure");
-                               if (targetPart->globalDiagnostics && targetPart->num_globalDiagnostics > 0)
-                                       response_diag(c, targetPart->globalDiagnostics[0]);
-                               break;
-               }
-               // NOTE: Individual record status, surrogate diagnostics, and supplemental diagnostics ARE NOT REPORTED.
-       }
+    if (utp && utp->targetPart)
+    {
+        Z_IUTargetPart *targetPart = utp->targetPart;
+        switch ( *targetPart->updateStatus ) {
+        case Z_IUTargetPart_success:
+            ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "success");
+            break;
+        case Z_IUTargetPart_partial:
+            ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "partial");
+            break;
+        case Z_IUTargetPart_failure:
+            ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "failure");
+            if (targetPart->globalDiagnostics && targetPart->num_globalDiagnostics > 0)
+                response_diag(c, targetPart->globalDiagnostics[0]);
+            break;
+        }
+        /* NOTE: Individual record status, surrogate diagnostics, and supplemental diagnostics ARE NOT REPORTED. */
+    }
     return 1;
 }
 
 static int es_response_taskpackage(ZOOM_connection c,
                                    Z_TaskPackage *taskPackage)
 {
-       // targetReference
-       Odr_oct *id = taskPackage->targetReference;
-       if (id)
-               ZOOM_options_setl(c->tasks->u.package->options,
-                                                       "targetReference", (char*) id->buf, id->len);
-       
-       // taskStatus
-       switch ( *taskPackage->taskStatus ) {
-               case Z_TaskPackage_pending:
-                       ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "pending");
-                       break;
-               case Z_TaskPackage_active:
-                       ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "active");
-                       break;
-               case Z_TaskPackage_complete:
-                       ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "complete");
-                       break;
-               case Z_TaskPackage_aborted:
-                       ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "aborted");
-                       if ( taskPackage->num_packageDiagnostics && taskPackage->packageDiagnostics )
-                               response_diag(c, taskPackage->packageDiagnostics[0]);
-                       break;
-       }
-       
-       // taskSpecificParameters
-       // NOTE: Only Update implemented, no others.
-       if ( taskPackage->taskSpecificParameters->which == Z_External_update ) {
-                       Z_IUUpdateTaskPackage *utp = taskPackage->taskSpecificParameters->u.update->u.taskPackage;
-                       es_response_taskpackage_update(c, utp);
-       }
-       return 1;
+    Odr_oct *id = taskPackage->targetReference;
+    if (id)
+        ZOOM_options_setl(c->tasks->u.package->options,
+                          "targetReference", (char*) id->buf, id->len);
+
+    switch ( *taskPackage->taskStatus ) {
+    case Z_TaskPackage_pending:
+        ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "pending");
+        break;
+    case Z_TaskPackage_active:
+        ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "active");
+        break;
+    case Z_TaskPackage_complete:
+        ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "complete");
+        break;
+    case Z_TaskPackage_aborted:
+        ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "aborted");
+        if ( taskPackage->num_packageDiagnostics && taskPackage->packageDiagnostics )
+            response_diag(c, taskPackage->packageDiagnostics[0]);
+        break;
+    }
+    /* NOTE: Only Update implemented, no others. */
+    if ( taskPackage->taskSpecificParameters->which == Z_External_update )
+    {
+        Z_IUUpdateTaskPackage *utp = taskPackage->taskSpecificParameters->u.update->u.taskPackage;
+        es_response_taskpackage_update(c, utp);
+    }
+    return 1;
 }
 
 
@@ -1046,18 +1041,19 @@ static int handle_Z3950_es_response(ZOOM_connection c,
 {
     if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE)
         return 0;
-    switch (*res->operationStatus) {
-        case Z_ExtendedServicesResponse_done:
-            ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "done");
-            break;
-        case Z_ExtendedServicesResponse_accepted:
-            ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "accepted");
-            break;
-        case Z_ExtendedServicesResponse_failure:
-            ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "failure");
-            if (res->diagnostics && res->num_diagnostics > 0)
-                response_diag(c, res->diagnostics[0]);
-            break;
+    switch (*res->operationStatus)
+    {
+    case Z_ExtendedServicesResponse_done:
+        ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "done");
+        break;
+    case Z_ExtendedServicesResponse_accepted:
+        ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "accepted");
+        break;
+    case Z_ExtendedServicesResponse_failure:
+        ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "failure");
+        if (res->diagnostics && res->num_diagnostics > 0)
+            response_diag(c, res->diagnostics[0]);
+        break;
     }
     if (res->taskPackage &&
         res->taskPackage->which == Z_External_extendedService)
@@ -1065,7 +1061,7 @@ static int handle_Z3950_es_response(ZOOM_connection c,
         Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
         es_response_taskpackage(c, taskPackage);
     }
-    if (res->taskPackage && 
+    if (res->taskPackage &&
         res->taskPackage->which == Z_External_octet)
     {
         Odr_oct *doc = res->taskPackage->u.octet_aligned;
@@ -1075,35 +1071,6 @@ static int handle_Z3950_es_response(ZOOM_connection c,
     return 1;
 }
 
-static void interpret_init_diag(ZOOM_connection c,
-                                Z_DiagnosticFormat *diag)
-{
-    if (diag->num > 0)
-    {
-        Z_DiagnosticFormat_s *ds = diag->elements[0];
-        if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
-            response_default_diag(c, ds->u.defaultDiagRec);
-    }
-}
-
-
-static void interpret_otherinformation_field(ZOOM_connection c,
-                                             Z_OtherInformation *ui)
-{
-    int i;
-    for (i = 0; i < ui->num_elements; i++)
-    {
-        Z_OtherInformationUnit *unit = ui->list[i];
-        if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
-            unit->information.externallyDefinedInfo &&
-            unit->information.externallyDefinedInfo->which ==
-            Z_External_diag1) 
-        {
-            interpret_init_diag(c, unit->information.externallyDefinedInfo->u.diag1);
-        } 
-    }
-}
-
 static char *get_term_cstr(ODR odr, Z_Term *term) {
 
     switch (term->which) {
@@ -1137,7 +1104,8 @@ static ZOOM_facet_field get_zoom_facet_field(ODR odr, Z_FacetField *facet) {
 }
 
 /* Can be share with SOLR/SRU/SRW requests */
-void handle_facet_list(ZOOM_resultset r, Z_FacetList *fl) {
+void ZOOM_handle_facet_list(ZOOM_resultset r, Z_FacetList *fl)
+{
     int j;
     r->num_facets   = fl->num;
     yaz_log(YLOG_DEBUG, "Facets found: %d", fl->num);
@@ -1163,7 +1131,7 @@ static void handle_facet_result(ZOOM_connection c, ZOOM_resultset r,
             Z_External *ext = o->list[i]->information.externallyDefinedInfo;
             if (ext->which == Z_External_userFacets)
             {
-                handle_facet_list(r, ext->u.facetList);
+                ZOOM_handle_facet_list(r, ext->u.facetList);
             }
         }
     }
@@ -1176,7 +1144,7 @@ static void handle_queryExpressionTerm(ZOOM_options opt, const char *name,
     {
     case Z_Term_general:
         ZOOM_options_setl(opt, name,
-                          (const char *)(term->u.general->buf), 
+                          (const char *)(term->u.general->buf),
                           term->u.general->len);
         break;
     case Z_Term_characterString:
@@ -1192,7 +1160,7 @@ static void handle_queryExpression(ZOOM_options opt, const char *name,
                                    Z_QueryExpression *exp)
 {
     char opt_name[80];
-    
+
     switch (exp->which)
     {
     case Z_QueryExpression_term:
@@ -1217,12 +1185,12 @@ static void handle_search_result(ZOOM_connection c, ZOOM_resultset resultset,
         if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
         {
             Z_External *ext = o->list[i]->information.externallyDefinedInfo;
-            
+
             if (ext->which == Z_External_searchResult1)
             {
                 int j;
                 Z_SearchInfoReport *sr = ext->u.searchResult1;
-                
+
                 if (sr->num)
                     ZOOM_options_set_int(
                         resultset->options, "searchresult.size", sr->num);
@@ -1232,7 +1200,7 @@ static void handle_search_result(ZOOM_connection c, ZOOM_resultset resultset,
                     Z_SearchInfoReport_s *ent =
                         ext->u.searchResult1->elements[j];
                     char pref[80];
-                    
+
                     sprintf(pref, "searchresult.%d", j);
 
                     if (ent->subqueryId)
@@ -1269,7 +1237,7 @@ static void handle_search_result(ZOOM_connection c, ZOOM_resultset resultset,
                         sprintf(opt_name, "%s.count", pref);
                         ZOOM_options_set_int(resultset->options, opt_name,
                                              *ent->subqueryCount);
-                    }                                             
+                    }
                 }
             }
         }
@@ -1305,6 +1273,24 @@ static void handle_Z3950_search_response(ZOOM_connection c,
     handle_facet_result(c, resultset, sr->additionalSearchInfo);
 
     resultset->size = *sr->resultCount;
+
+#if HAVE_LIBMEMCACHED_MEMCACHED_H
+    if (c->mc_st)
+    {
+        uint32_t flags = 0;
+        memcached_return_t rc;
+        time_t expiration = 36000;
+        char str[40];
+
+        sprintf(str, ODR_INT_PRINTF, *sr->resultCount);
+        rc = memcached_set(c->mc_st,
+                           wrbuf_buf(resultset->mc_key),wrbuf_len(resultset->mc_key),
+                           str, strlen(str), expiration, flags);
+        yaz_log(YLOG_LOG, "Key=%s value=%s rc=%u %s",
+                wrbuf_cstr(resultset->mc_key), str, (unsigned) rc,
+                memcached_last_error_message(c->mc_st));
+    }
+#endif
     handle_Z3950_records(c, sr->records, 0);
 }
 
@@ -1346,7 +1332,7 @@ static void handle_Z3950_records(ZOOM_connection c, Z_Records *sr,
 {
     ZOOM_resultset resultset;
     int *start, *count;
-    const char *syntax = 0, *elementSetName = 0;
+    const char *syntax = 0, *elementSetName = 0, *schema = 0;
 
     if (!c->tasks)
         return ;
@@ -1358,13 +1344,15 @@ static void handle_Z3950_records(ZOOM_connection c, Z_Records *sr,
         count = &c->tasks->u.search.count;
         syntax = c->tasks->u.search.syntax;
         elementSetName = c->tasks->u.search.elementSetName;
+        schema =  c->tasks->u.search.schema;
         break;
     case ZOOM_TASK_RETRIEVE:
-        resultset = c->tasks->u.retrieve.resultset;        
+        resultset = c->tasks->u.retrieve.resultset;
         start = &c->tasks->u.retrieve.start;
         count = &c->tasks->u.retrieve.count;
         syntax = c->tasks->u.retrieve.syntax;
         elementSetName = c->tasks->u.retrieve.elementSetName;
+        schema =  c->tasks->u.retrieve.schema;
         break;
     default:
         return;
@@ -1378,7 +1366,7 @@ static void handle_Z3950_records(ZOOM_connection c, Z_Records *sr,
         else
             ZOOM_set_error(c, ZOOM_ERROR_DECODE, 0);
     }
-    else 
+    else
     {
         if (*count + *start > resultset->size)
             *count = resultset->size - *start;
@@ -1393,14 +1381,13 @@ static void handle_Z3950_records(ZOOM_connection c, Z_Records *sr,
             for (i = 0; i<p->num_records; i++)
             {
                 ZOOM_record_cache_add(resultset, p->records[i], i + *start,
-                                      syntax, elementSetName,
-                                      elementSetName, 0);
+                                      syntax, elementSetName, schema, 0);
             }
             *count -= i;
             if (*count < 0)
                 *count = 0;
             *start += i;
-            yaz_log(c->log_details, 
+            yaz_log(c->log_details,
                     "handle_records resultset=%p start=%d count=%d",
                     resultset, *start, *count);
 
@@ -1410,25 +1397,27 @@ static void handle_Z3950_records(ZOOM_connection c, Z_Records *sr,
             if (present_phase && p->num_records == 0)
             {
                 /* present response and we didn't get any records! */
-                Z_NamePlusRecord *myrec = 
+                Z_NamePlusRecord *myrec =
                     zget_surrogateDiagRec(
-                        resultset->odr, 0, 
+                        resultset->odr, 0,
                         YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
                         "ZOOM C generated. Present phase and no records");
                 ZOOM_record_cache_add(resultset, myrec, *start,
-                                      syntax, elementSetName, 0, 0);
+                                      syntax, elementSetName, schema, 0);
+                *count = 0;
             }
         }
         else if (present_phase)
         {
             /* present response and we didn't get any records! */
-            Z_NamePlusRecord *myrec = 
+            Z_NamePlusRecord *myrec =
                 zget_surrogateDiagRec(
                     resultset->odr, 0,
                     YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
                     "ZOOM C generated: Present response and no records");
             ZOOM_record_cache_add(resultset, myrec, *start,
-                                  syntax, elementSetName, 0, 0);
+                                  syntax, elementSetName, schema, 0);
+            *count = 0;
         }
     }
 }
@@ -1457,7 +1446,7 @@ zoom_ret send_Z3950_sort(ZOOM_connection c, ZOOM_resultset resultset)
     {
         Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
         Z_SortRequest *req = apdu->u.sortRequest;
-        
+
         req->num_inputResultSetNames = 1;
         req->inputResultSetNames = (Z_InternationalString **)
             odr_malloc(c->odr_out, sizeof(*req->inputResultSetNames));
@@ -1478,7 +1467,8 @@ zoom_ret send_Z3950_present(ZOOM_connection c)
     int i = 0;
     const char *syntax = 0;
     const char *elementSetName = 0;
-    ZOOM_resultset  resultset;
+    const char *schema = 0;
+    ZOOM_resultset resultset;
     int *start, *count;
 
     if (!c->tasks)
@@ -1486,7 +1476,7 @@ zoom_ret send_Z3950_present(ZOOM_connection c)
         yaz_log(c->log_details, "%p send_present no tasks", c);
         return zoom_complete;
     }
-    
+
     switch (c->tasks->which)
     {
     case ZOOM_TASK_SEARCH:
@@ -1495,6 +1485,7 @@ zoom_ret send_Z3950_present(ZOOM_connection c)
         count = &c->tasks->u.search.count;
         syntax = c->tasks->u.search.syntax;
         elementSetName = c->tasks->u.search.elementSetName;
+        schema =  c->tasks->u.search.schema;
         break;
     case ZOOM_TASK_RETRIEVE:
         resultset = c->tasks->u.retrieve.resultset;
@@ -1502,6 +1493,7 @@ zoom_ret send_Z3950_present(ZOOM_connection c)
         count = &c->tasks->u.retrieve.count;
         syntax = c->tasks->u.retrieve.syntax;
         elementSetName = c->tasks->u.retrieve.elementSetName;
+        schema = c->tasks->u.retrieve.schema;
         break;
     default:
         return zoom_complete;
@@ -1523,7 +1515,7 @@ zoom_ret send_Z3950_present(ZOOM_connection c)
     {
         ZOOM_record rec =
             ZOOM_record_cache_lookup(resultset, i + *start,
-                                     syntax, elementSetName);
+                                     syntax, elementSetName, schema);
         if (!rec)
             break;
         else
@@ -1553,7 +1545,7 @@ zoom_ret send_Z3950_present(ZOOM_connection c)
         *req->numberOfRecordsRequested = resultset->step;
     else
         *req->numberOfRecordsRequested = *count;
-    
+
     if (*req->numberOfRecordsRequested + *start > resultset->size)
         *req->numberOfRecordsRequested = resultset->size - *start;
     assert(*req->numberOfRecordsRequested > 0);
@@ -1562,7 +1554,7 @@ zoom_ret send_Z3950_present(ZOOM_connection c)
         req->preferredRecordSyntax =
             zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
 
-    if (resultset->schema && *resultset->schema)
+    if (schema && *schema)
     {
         Z_RecordComposition *compo = (Z_RecordComposition *)
             odr_malloc(c->odr_out, sizeof(*compo));
@@ -1571,7 +1563,7 @@ zoom_ret send_Z3950_present(ZOOM_connection c)
         compo->which = Z_RecordComp_complex;
         compo->u.complex = (Z_CompSpec *)
             odr_malloc(c->odr_out, sizeof(*compo->u.complex));
-        compo->u.complex->selectAlternativeSyntax = (bool_t *) 
+        compo->u.complex->selectAlternativeSyntax = (bool_t *)
             odr_malloc(c->odr_out, sizeof(bool_t));
         *compo->u.complex->selectAlternativeSyntax = 0;
 
@@ -1580,14 +1572,14 @@ zoom_ret send_Z3950_present(ZOOM_connection c)
 
         compo->u.complex->generic->which = Z_Schema_oid;
         compo->u.complex->generic->schema.oid = (Odr_oid *)
-            zoom_yaz_str_to_z3950oid (c, CLASS_SCHEMA, resultset->schema);
+            zoom_yaz_str_to_z3950oid(c, CLASS_SCHEMA, schema);
 
         if (!compo->u.complex->generic->schema.oid)
         {
             /* OID wasn't a schema! Try record syntax instead. */
 
             compo->u.complex->generic->schema.oid = (Odr_oid *)
-                zoom_yaz_str_to_z3950oid (c, CLASS_RECSYN, resultset->schema);
+                zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, schema);
         }
         if (elementSetName && *elementSetName)
         {
@@ -1611,7 +1603,7 @@ zoom_ret send_Z3950_present(ZOOM_connection c)
             odr_malloc(c->odr_out, sizeof(*esn));
         Z_RecordComposition *compo = (Z_RecordComposition *)
             odr_malloc(c->odr_out, sizeof(*compo));
-        
+
         esn->which = Z_ElementSetNames_generic;
         esn->u.generic = odr_strdup(c->odr_out, elementSetName);
         compo->which = Z_RecordComp_simple;
@@ -1636,7 +1628,7 @@ static zoom_ret send_Z3950_sort_present(ZOOM_connection c)
 void ZOOM_handle_Z3950_apdu(ZOOM_connection c, Z_APDU *apdu)
 {
     Z_InitResponse *initrs;
-    
+
     ZOOM_connection_set_mask(c, 0);
     yaz_log(c->log_details, "%p handle_Z3950_apdu apdu->which=%d",
             c, apdu->which);
@@ -1670,12 +1662,11 @@ void ZOOM_handle_Z3950_apdu(ZOOM_connection c, Z_APDU *apdu)
 
         if (!*initrs->result)
         {
-            Z_External *uif = initrs->userInformationField;
-
-            ZOOM_set_error(c, ZOOM_ERROR_INIT, 0); /* default error */
-
-            if (uif && uif->which == Z_External_userInfo1)
-                interpret_otherinformation_field(c, uif->u.userInfo1);
+            Z_DefaultDiagFormat *df = yaz_decode_init_diag(0, initrs);
+            if (df)
+                response_default_diag(c, df);
+            else
+                ZOOM_set_error(c, ZOOM_ERROR_INIT, 0); /* default error */
         }
         else
         {
@@ -1701,12 +1692,12 @@ void ZOOM_handle_Z3950_apdu(ZOOM_connection c, Z_APDU *apdu)
             NMEM tmpmem = nmem_create();
             Z_CharSetandLanguageNegotiation *p =
                 yaz_get_charneg_record(initrs->otherInfo);
-            
+
             if (p)
             {
                 char *charset = NULL, *lang = NULL;
                 int sel;
-                
+
                 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel);
                 yaz_log(c->log_details, "%p handle_Z3950_apdu target accepted: "
                         "charset %s, language %s, select %d",
@@ -1724,7 +1715,7 @@ void ZOOM_handle_Z3950_apdu(ZOOM_connection c, Z_APDU *apdu)
                     (sel != 0) ? "1" : "0");
                 nmem_destroy(tmpmem);
             }
-        }       
+        }
         break;
     case Z_APDU_searchResponse:
         yaz_log(c->log_api, "%p handle_Z3950_apdu Search response", c);
@@ -1758,7 +1749,7 @@ void ZOOM_handle_Z3950_apdu(ZOOM_connection c, Z_APDU *apdu)
         yaz_log(c->log_api, "%p handle_Z3950_apdu Close PDU", c);
         if (!ZOOM_test_reconnect(c))
         {
-            ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
+            ZOOM_set_dset_error(c, ZOOM_ERROR_CONNECTION_LOST, "ZOOM", c->host_port, apdu->u.close->diagnosticInformation);
             ZOOM_connection_close(c);
         }
         break;