Filter backend_test does retrieval. Supports fancy USMARC records
authorAdam Dickmeiss <adam@indexdata.dk>
Tue, 17 Jan 2006 16:45:49 +0000 (16:45 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Tue, 17 Jan 2006 16:45:49 +0000 (16:45 +0000)
as well as piggyback.

src/filter_backend_test.cpp
src/filter_virt_db.cpp

index 84b4720..1f60334 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: filter_backend_test.cpp,v 1.15 2006-01-17 13:54:36 adam Exp $
+/* $Id: filter_backend_test.cpp,v 1.16 2006-01-17 16:45:49 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -32,8 +32,12 @@ namespace yp2 {
         };
         class Backend_test::Rep {
             friend class Backend_test;
-            
-        private:
+
+            Z_Records *fetch(
+                ODR odr, Odr_oid *preferredRecordSyntax,
+                int start, int number, int &error_code, std::string &addinfo,
+                int *number_returned, int *next_position);
+
             bool m_support_named_result_sets;
 
             session_map<Session_info> m_sessions;
@@ -43,6 +47,35 @@ namespace yp2 {
 
 using namespace yp2;
 
+static const int result_set_size = 42;
+
+// an ISO2709 USMARC/MARC21 record that we return..
+static const char *marc_record =
+  "\x30\x30\x33\x36\x36\x6E\x61\x6D\x20\x20\x32\x32\x30\x30\x31\x36"
+  "\x39\x38\x61\x20\x34\x35\x30\x30\x30\x30\x31\x30\x30\x31\x33\x30"
+  "\x30\x30\x30\x30\x30\x30\x33\x30\x30\x30\x34\x30\x30\x30\x31\x33"
+  "\x30\x30\x35\x30\x30\x31\x37\x30\x30\x30\x31\x37\x30\x30\x38\x30"
+  "\x30\x34\x31\x30\x30\x30\x33\x34\x30\x31\x30\x30\x30\x31\x37\x30"
+  "\x30\x31\x37\x39\x30\x34\x30\x30\x30\x31\x33\x30\x30\x30\x37\x35"
+  "\x30\x35\x30\x30\x30\x31\x32\x30\x30\x30\x38\x38\x31\x30\x30\x30"
+  "\x30\x31\x37\x30\x30\x31\x30\x30\x32\x34\x35\x30\x30\x33\x30\x30"
+  "\x30\x31\x31\x37\x32\x36\x30\x30\x30\x31\x32\x30\x30\x31\x34\x37"
+  "\x32\x36\x33\x30\x30\x30\x39\x30\x30\x31\x35\x39\x33\x30\x30\x30"
+  "\x30\x31\x31\x30\x30\x31\x36\x38\x1E\x20\x20\x20\x31\x31\x32\x32"
+  "\x34\x34\x36\x36\x20\x1E\x44\x4C\x43\x1E\x30\x30\x30\x30\x30\x30"
+  "\x30\x30\x30\x30\x30\x30\x30\x30\x2E\x30\x1E\x39\x31\x30\x37\x31"
+  "\x30\x63\x31\x39\x39\x31\x30\x37\x30\x31\x6E\x6A\x75\x20\x20\x20"
+  "\x20\x20\x20\x20\x20\x20\x20\x20\x30\x30\x30\x31\x30\x20\x65\x6E"
+  "\x67\x20\x20\x1E\x20\x20\x1F\x61\x44\x4C\x43\x1F\x63\x44\x4C\x43"
+  "\x1E\x30\x30\x1F\x61\x31\x32\x33\x2D\x78\x79\x7A\x1E\x31\x30\x1F"
+  "\x61\x4A\x61\x63\x6B\x20\x43\x6F\x6C\x6C\x69\x6E\x73\x1E\x31\x30"
+  "\x1F\x61\x48\x6F\x77\x20\x74\x6F\x20\x70\x72\x6F\x67\x72\x61\x6D"
+  "\x20\x61\x20\x63\x6F\x6D\x70\x75\x74\x65\x72\x1E\x31\x20\x1F\x61"
+  "\x50\x65\x6E\x67\x75\x69\x6E\x1E\x20\x20\x1F\x61\x38\x37\x31\x30"
+  "\x1E\x20\x20\x1F\x61\x70\x2E\x20\x63\x6D\x2E\x1E\x20\x20\x1F\x61"
+  "\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x1D";
+
+
 yf::Backend_test::Backend_test() : m_p(new Backend_test::Rep) {
     m_p->m_support_named_result_sets = false;
 }
@@ -50,6 +83,66 @@ yf::Backend_test::Backend_test() : m_p(new Backend_test::Rep) {
 yf::Backend_test::~Backend_test() {
 }
 
+Z_Records *yf::Backend_test::Rep::fetch(
+    ODR odr, Odr_oid *preferredRecordSyntax,
+    int start, int number, int &error_code, std::string &addinfo,
+    int *number_returned, int *next_position)
+{
+    oident *prefformat;
+    oid_value form;
+    
+    if (number + start - 1 > result_set_size || start < 1)
+    {
+        error_code = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
+        return 0;
+    }
+
+    if (!(prefformat = oid_getentbyoid(preferredRecordSyntax)))
+        form = VAL_NONE;
+    else
+        form = prefformat->value;
+    switch(form)
+    {
+    case VAL_NONE:
+    case VAL_USMARC:
+        break;
+    default:
+        error_code = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
+        return 0;
+    }
+    
+    Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
+    rec->which = Z_Records_DBOSD;
+    rec->u.databaseOrSurDiagnostics = (Z_NamePlusRecordList *)
+        odr_malloc(odr, sizeof(Z_NamePlusRecordList));
+    rec->u.databaseOrSurDiagnostics->num_records = number;
+    rec->u.databaseOrSurDiagnostics->records = (Z_NamePlusRecord **)
+        odr_malloc(odr, sizeof(Z_NamePlusRecord *) * number);
+    int i;
+    for (i = 0; i<number; i++)
+    {
+        rec->u.databaseOrSurDiagnostics->records[i] = (Z_NamePlusRecord *)
+            odr_malloc(odr, sizeof(Z_NamePlusRecord));
+        Z_NamePlusRecord *npr = rec->u.databaseOrSurDiagnostics->records[i];
+        npr->databaseName = 0;
+        npr->which = Z_NamePlusRecord_databaseRecord;
+
+        char *tmp_rec = odr_strdup(odr, marc_record);
+        char offset_str[30];
+        sprintf(offset_str, "test__%09d_", i+start);
+        memcpy(tmp_rec+186, offset_str, strlen(offset_str));
+        npr->u.databaseRecord = z_ext_record(odr, VAL_USMARC,
+                                             tmp_rec, strlen(tmp_rec));
+
+    }
+    *number_returned = number;
+    if (start + number > result_set_size)
+        *next_position = 0;
+    else
+        *next_position = start + number;
+    return rec;
+}
+
 void yf::Backend_test::process(Package &package) const
 {
     Z_GDU *gdu = package.request().get();
@@ -75,21 +168,19 @@ void yf::Backend_test::process(Package &package) const
             apdu_res = odr.create_initResponse(apdu_req, 0, 0);
             Z_InitRequest *req = apdu_req->u.initRequest;
             Z_InitResponse *resp = apdu_res->u.initResponse;
+
+            resp->implementationName = "backend_test";
+            if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
+                m_p->m_support_named_result_sets = true;
             
             int i;
             static const int masks[] = {
-                Z_Options_search, Z_Options_present, -1 
+                Z_Options_search, Z_Options_present,
+                Z_Options_namedResultSets, -1 
             };
             for (i = 0; masks[i] != -1; i++)
                 if (ODR_MASK_GET(req->options, masks[i]))
                     ODR_MASK_SET(resp->options, masks[i]);
-            if (m_p->m_support_named_result_sets)
-            {
-                if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
-                    ODR_MASK_SET(resp->options, Z_Options_namedResultSets);
-                else
-                    m_p->m_support_named_result_sets = false;
-            }
             static const int versions[] = {
                 Z_ProtocolVersion_1,
                 Z_ProtocolVersion_2,
@@ -107,31 +198,99 @@ void yf::Backend_test::process(Package &package) const
         }
         else if (apdu_req->which == Z_APDU_searchRequest)
         {
-            apdu_res = odr.create_searchResponse(apdu_req, 0, 0);
             Z_SearchRequest *req = apdu_req->u.searchRequest;
-            Z_SearchResponse *resp = apdu_res->u.searchResponse;
                 
             if (!m_p->m_support_named_result_sets && 
                 strcmp(req->resultSetName, "default"))
             {
-                Z_Records *rec = (Z_Records *)
-                    odr_malloc(odr, sizeof(Z_Records));
-                resp->records = rec;
-                rec->which = Z_Records_NSD;
-                rec->u.nonSurrogateDiagnostic =
-                    zget_DefaultDiagFormat(
-                        odr, YAZ_BIB1_RESULT_SET_NAMING_UNSUPP, 0);
+                apdu_res = 
+                    odr.create_searchResponse(
+                        apdu_req,  YAZ_BIB1_RESULT_SET_NAMING_UNSUPP, 0);
             }
             else
-                *resp->resultCount = 42;
+            {
+                Z_Records *records = 0;
+                int number_returned = 0;
+                int next_position = 0;
+                int error_code = 0;
+                std::string addinfo;
+
+                if (result_set_size < *req->smallSetUpperBound)
+                {
+                    // small set . Return all records in set
+                    records = m_p->fetch(
+                        odr, req->preferredRecordSyntax,
+                        1, result_set_size,
+                        error_code, addinfo,
+                        &number_returned,
+                        &next_position);
+                }
+                else if (result_set_size > *req->largeSetLowerBound)
+                {
+                    // large set . Return no records
+                }
+                else
+                {
+                    // medium set .Return mediumSetPresentNumber records
+                    int to_get = *req->mediumSetPresentNumber;
+                    if (to_get > result_set_size)
+                        to_get = result_set_size;
+                    records = m_p->fetch(
+                        odr, req->preferredRecordSyntax,
+                        1, to_get,
+                        error_code, addinfo,
+                        &number_returned,
+                        &next_position);
+                }
+                if (error_code)
+                {
+                    apdu_res = 
+                        odr.create_searchResponse(
+                            apdu_req, error_code, addinfo.c_str());
+                    Z_SearchResponse *resp = apdu_res->u.searchResponse;
+                    *resp->resultCount = result_set_size;
+                }
+                else
+                {
+                    apdu_res = 
+                        odr.create_searchResponse(apdu_req, 0, 0);
+                    Z_SearchResponse *resp = apdu_res->u.searchResponse;
+                    *resp->resultCount = result_set_size;
+                    *resp->numberOfRecordsReturned = number_returned;
+                    *resp->nextResultSetPosition = next_position;
+                    resp->records = records;
+                }
+            }
         }
         else if (apdu_req->which == Z_APDU_presentRequest)
         { 
-            apdu_res =
-                odr.create_presentResponse(
-                    apdu_req,
-                    YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
-                    "backend_test: present not implemented");
+            Z_PresentRequest *req = apdu_req->u.presentRequest;
+            int number_returned = 0;
+            int next_position = 0;
+            int error_code = 0;
+            std::string addinfo;
+            Z_Records *records = m_p->fetch(
+                odr, req->preferredRecordSyntax,
+                *req->resultSetStartPoint, *req->numberOfRecordsRequested,
+                error_code, addinfo,
+                &number_returned,
+                &next_position);
+
+            if (error_code)
+            {
+                apdu_res =
+                    odr.create_presentResponse(apdu_req, error_code,
+                                               addinfo.c_str());
+            }
+            else
+            {
+                apdu_res =
+                    odr.create_presentResponse(apdu_req, 0, 0);
+                Z_PresentResponse *resp = apdu_res->u.presentResponse;
+                resp->records = records;
+                *resp->numberOfRecordsReturned = number_returned;
+                *resp->nextResultSetPosition = next_position;
+            }
         }
         else
         {
index ad60d25..4fc928c 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: filter_virt_db.cpp,v 1.30 2006-01-17 13:54:54 adam Exp $
+/* $Id: filter_virt_db.cpp,v 1.31 2006-01-17 16:45:49 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -195,24 +195,24 @@ yf::Virt_db::BackendPtr yf::Virt_db::Frontend::init_backend(
     init_package.request() = init_apdu;
     
     init_package.move(b->m_route);  // sending init 
-    
-    if (init_package.session().is_closed())
-    {
-        error_code = YAZ_BIB1_DATABASE_UNAVAILABLE;
-        // addinfo = database;
-        BackendPtr null;
-        return null;
-    }
+
     Z_GDU *gdu = init_package.response().get();
     // we hope to get an init response
     if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
         Z_APDU_initResponse)
     {
-        if (ODR_MASK_GET(gdu->u.z3950->u.initResponse->options,
-                         Z_Options_namedResultSets))
+        Z_InitResponse *res = gdu->u.z3950->u.initResponse;
+        if (ODR_MASK_GET(res->options, Z_Options_namedResultSets))
         {
             b->m_named_result_sets = true;
         }
+        std::cout << "GOT INIT res=" << *res->result << "\n";
+        if (!*res->result)
+        {
+            yp2::util::get_init_diagnostics(res, error_code, addinfo);
+            BackendPtr null;
+            return null; 
+        }
     }
     else
     {
@@ -221,6 +221,14 @@ yf::Virt_db::BackendPtr yf::Virt_db::Frontend::init_backend(
         BackendPtr null;
         return null;
     }        
+    if (init_package.session().is_closed())
+    {
+        error_code = YAZ_BIB1_DATABASE_UNAVAILABLE;
+        // addinfo = database;
+        BackendPtr null;
+        return null;
+    }
+
     m_backend_list.push_back(b);
     return b;
 }