Bump year in copyright msg in source
[metaproxy-moved-to-github.git] / src / filter_session_shared.cpp
index 8f7c44d..720a71d 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Metaproxy.
-   Copyright (C) 2005-2008 Index Data
+   Copyright (C) 2005-2009 Index Data
 
 Metaproxy is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
@@ -36,6 +36,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <yaz/otherinfo.h>
 #include <yaz/diagbib1.h>
 #include <yazpp/z-query.h>
+#include <yazpp/record-cache.h>
 #include <map>
 #include <iostream>
 #include <time.h>
@@ -77,6 +78,7 @@ namespace metaproxy_1 {
             yazpp_1::Yaz_Z_Query m_query;
             time_t m_time_last_use;
             void timestamp();
+            yazpp_1::RecordCache m_record_cache;
             BackendSet(
                 const std::string &result_set_id,
                 const Databases &databases,
@@ -629,6 +631,9 @@ void yf::SessionShared::Frontend::get_set(mp::Package &package,
                                           BackendInstancePtr &found_backend,
                                           BackendSetPtr &found_set)
 {
+    bool session_restarted = false;
+
+restart:
     std::string result_set_id;
     BackendClassPtr bc = m_backend_class;
     {
@@ -709,6 +714,14 @@ void yf::SessionShared::Frontend::get_set(mp::Package &package,
             bc->release_backend(found_backend);
         return; // search error 
     }
+    if (!session_restarted && new_set->m_result_set_size < 0)
+    {
+        bc->remove_backend(found_backend);
+        session_restarted = true;
+        found_backend.reset();
+        goto restart;
+    }
+
     found_set = new_set;
     found_set->timestamp();
     found_backend->m_sets.push_back(found_set);
@@ -798,6 +811,33 @@ void yf::SessionShared::Frontend::present(mp::Package &package,
     if (!found_set)
         return;
 
+    Z_NamePlusRecordList *npr_res = 0;
+    if (found_set->m_record_cache.lookup(odr, &npr_res, 
+                                         *req->resultSetStartPoint,
+                                         *req->numberOfRecordsRequested,
+                                         req->preferredRecordSyntax,
+                                         req->recordComposition))
+    {
+        Z_APDU *f_apdu_res = odr.create_presentResponse(apdu_req, 0, 0);
+        Z_PresentResponse *f_resp = f_apdu_res->u.presentResponse;
+
+        yaz_log(YLOG_LOG, "Found %d+%d records in cache %p",
+                *req->resultSetStartPoint,                      
+                *req->numberOfRecordsRequested,
+                &found_set->m_record_cache);        
+
+        *f_resp->numberOfRecordsReturned = *req->numberOfRecordsRequested;
+        *f_resp->nextResultSetPosition = 
+            *req->resultSetStartPoint + *req->numberOfRecordsRequested;
+        // f_resp->presentStatus assumed OK.
+        f_resp->records = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
+        f_resp->records->which = Z_Records_DBOSD;
+        f_resp->records->u.databaseOrSurDiagnostics = npr_res;
+        package.response() = f_apdu_res;
+        bc->release_backend(found_backend);
+        return;
+    }
+                              
     Z_APDU *p_apdu = zget_APDU(odr, Z_APDU_presentRequest);
     Z_PresentRequest *p_req = p_apdu->u.presentRequest;
     p_req->preferredRecordSyntax = req->preferredRecordSyntax;
@@ -829,6 +869,19 @@ void yf::SessionShared::Frontend::present(mp::Package &package,
         f_resp->records = b_resp->records;
         f_resp->otherInfo = b_resp->otherInfo;
         package.response() = f_apdu_res;
+
+        if (b_resp->records && b_resp->records->which ==  Z_Records_DBOSD)
+        {
+            yaz_log(YLOG_LOG, "Adding %d+%d records to cache %p",
+                    *req->resultSetStartPoint,                      
+                    *f_resp->numberOfRecordsReturned,
+                    &found_set->m_record_cache);        
+            found_set->m_record_cache.add(
+                odr,
+                b_resp->records->u.databaseOrSurDiagnostics,
+                *req->resultSetStartPoint,                      
+                *f_resp->numberOfRecordsReturned);
+        }
         bc->release_backend(found_backend);
     }
     else
@@ -1123,8 +1176,9 @@ extern "C" {
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
- * c-file-style: "stroustrup"
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab
  */
+