ZOOM: save record schema per request; fix caching
[yaz-moved-to-github.git] / src / zoom-z3950.c
index cf1b12f..fd605f5 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2012 Index Data
+ * Copyright (C) 1995-2013 Index Data
  * See the file LICENSE for details.
  */
 /**
@@ -642,6 +642,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;
@@ -722,6 +723,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);
@@ -733,7 +736,7 @@ 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;
@@ -959,7 +962,6 @@ ZOOM_API(void)
     }
 }
 
-
 static void handle_Z3950_records(ZOOM_connection c, Z_Records *sr,
                                  int present_phase);
 
@@ -1085,35 +1087,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) {
@@ -1147,7 +1120,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);
@@ -1173,7 +1147,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);
             }
         }
     }
@@ -1488,7 +1462,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)
@@ -1505,6 +1480,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;
@@ -1512,6 +1488,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;
@@ -1533,7 +1510,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
@@ -1572,7 +1549,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));
@@ -1590,14 +1567,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)
         {
@@ -1680,12 +1657,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
         {