virt_db: make databases available as virtual targets
[metaproxy-moved-to-github.git] / src / filter_virt_db.cpp
index e8b30f5..e6f2338 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Metaproxy.
-   Copyright (C) 2005-2010 Index Data
+   Copyright (C) 2005-2011 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
@@ -26,11 +26,13 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <boost/shared_ptr.hpp>
 
 #include <metaproxy/util.hpp>
+#include "torus.hpp"
 
 #include <yaz/zgdu.h>
 #include <yaz/otherinfo.h>
 #include <yaz/diagbib1.h>
 #include <yaz/match_glob.h>
+#include <yaz/log.h>
 
 #include <map>
 #include <iostream>
@@ -78,6 +80,7 @@ namespace metaproxy_1 {
             void search(Package &package, Z_APDU *apdu);
             void present(Package &package, Z_APDU *apdu);
             void scan(Package &package, Z_APDU *apdu);
+            int relay_apdu(Package &package, Z_APDU *apdu);
 
             void close(Package &package);
             typedef std::map<std::string,VirtualDB::Set>::iterator Sets_it;
@@ -113,6 +116,7 @@ namespace metaproxy_1 {
             boost::condition m_cond_session_ready;
             std::map<mp::Session, FrontendPtr> m_clients;
             bool pass_vhosts;
+            mp::Torus torus;
         };
     }
 }
@@ -211,6 +215,8 @@ yf::VirtualDB::BackendPtr yf::VirtualDB::Frontend::init_backend(
     req->implementationId = org_init->implementationId;
     req->implementationName = org_init->implementationName;
     req->implementationVersion = org_init->implementationVersion;
+    *req->preferredMessageSize = *org_init->preferredMessageSize;
+    *req->maximumRecordSize = *org_init->maximumRecordSize;
 
     ODR_MASK_SET(req->options, Z_Options_search);
     ODR_MASK_SET(req->options, Z_Options_present);
@@ -227,21 +233,21 @@ yf::VirtualDB::BackendPtr yf::VirtualDB::Frontend::init_backend(
 
     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)
+    error_code = YAZ_BIB1_DATABASE_UNAVAILABLE;
+    if (gdu && gdu->which == Z_GDU_Z3950
+        && gdu->u.z3950->which == Z_APDU_initResponse
+        && *gdu->u.z3950->u.initResponse->result)
     {
         Z_InitResponse *res = gdu->u.z3950->u.initResponse;
         if (ODR_MASK_GET(res->options, Z_Options_namedResultSets))
         {
             b->m_named_result_sets = true;
         }
-        if (*res->result)
+        if (*res->result && !init_package.session().is_closed())
         {
             m_backend_list.push_back(b);
             return b;
-
         }
-        error_code = YAZ_BIB1_DATABASE_UNAVAILABLE;
         mp::util::get_init_diagnostics(res, error_code, addinfo);
     }
     if (!init_package.session().is_closed())
@@ -267,7 +273,6 @@ void yf::VirtualDB::Frontend::search(mp::Package &package, Z_APDU *apdu_req)
     for (i = 0; i<req->num_databaseNames; i++)
         databases.push_back(req->databaseNames[i]);
 
-    BackendPtr b; // null for now
     Sets_it sets_it = m_sets.find(req->resultSetName);
     if (sets_it != m_sets.end())
     {
@@ -286,39 +291,22 @@ void yf::VirtualDB::Frontend::search(mp::Package &package, Z_APDU *apdu_req)
             return;
         } 
         sets_it->second.m_backend->m_number_of_sets--;
-
-        // pick up any existing backend with a database match
-        std::list<BackendPtr>::const_iterator map_it;
-        map_it = m_backend_list.begin();
-        for (; map_it != m_backend_list.end(); map_it++)
-        {
-            BackendPtr tmp = *map_it;
-            if (tmp->m_frontend_databases == databases &&
-                (tmp->m_named_result_sets ||
-                 tmp->m_number_of_sets == 0))
-                break;
-        }
-        if (map_it != m_backend_list.end()) 
-            b = *map_it;
     }
-    else
+    // pick up any existing database with named result sets ..
+    // or one which has no result sets.. yet.
+    BackendPtr b; // null for now
+    std::list<BackendPtr>::const_iterator map_it;
+    map_it = m_backend_list.begin();
+    for (; map_it != m_backend_list.end(); map_it++)
     {
-        // new result set.
-
-        // pick up any existing database with named result sets ..
-        // or one which has no result sets.. yet.
-        std::list<BackendPtr>::const_iterator map_it;
-        map_it = m_backend_list.begin();
-        for (; map_it != m_backend_list.end(); map_it++)
+        BackendPtr tmp = *map_it;
+        if (tmp->m_frontend_databases == databases &&
+            (tmp->m_named_result_sets ||
+             tmp->m_number_of_sets == 0))
         {
-            BackendPtr tmp = *map_it;
-            if (tmp->m_frontend_databases == databases &&
-                (tmp->m_named_result_sets ||
-                 tmp->m_number_of_sets == 0))
-                break;
-        }
-        if (map_it != m_backend_list.end()) 
             b = *map_it;
+            break;
+        }
     }
     if (!b)  // no backend yet. Must create a new one
     {
@@ -451,6 +439,59 @@ void yf::VirtualDB::Rep::release_frontend(mp::Package &package)
     }
 }
 
+void yf::VirtualDB::refresh_torus(void)
+{
+    xmlDoc *doc = m_p->torus.get_doc();
+    xmlNode *ptr1 = 0;
+    if (doc && (ptr1 = xmlDocGetRootElement(doc)))
+    {
+        for (ptr1 = ptr1->children; ptr1; ptr1 = ptr1->next)
+        {
+            if (ptr1->type != XML_ELEMENT_NODE)
+                continue;
+            if (!strcmp((const char *) ptr1->name, "record"))
+            {
+                xmlNode *ptr2 = ptr1;
+                for (ptr2 = ptr2->children; ptr2; ptr2 = ptr2->next)
+                {
+                    if (ptr2->type != XML_ELEMENT_NODE)
+                        continue;
+                    if (!strcmp((const char *) ptr2->name, "layer"))
+                    {
+                        std::string database;
+                        std::string target;
+                        std::string route;
+                        std::string solr;
+                        xmlNode *ptr3 = ptr2;
+                        for (ptr3 = ptr3->children; ptr3; ptr3 = ptr3->next)
+                        {
+                            if (ptr3->type != XML_ELEMENT_NODE)
+                                continue;
+                            if (!strcmp((const char *) ptr3->name, "id"))
+                            {
+                                database = mp::xml::get_text(ptr3);
+                            }
+                            else if (!strcmp((const char *) ptr3->name, "zurl"))
+                            {
+                                target = mp::xml::get_text(ptr3);
+                            }
+                            else if (!strcmp((const char *) ptr3->name, "sru"))
+                            {
+                                solr = mp::xml::get_text(ptr3);
+                            }
+                        }
+                        if (solr.length() == 0 && 
+                            database.length() && target.length())
+                        {
+                            add_map_db2target(database, target, route);
+                        }
+                    }
+                }
+            }
+        }
+    }
+}
+
 yf::VirtualDB::Set::Set(BackendPtr b, std::string setname)
     :  m_backend(b), m_setname(setname)
 {
@@ -618,6 +659,27 @@ void yf::VirtualDB::Frontend::present(mp::Package &package, Z_APDU *apdu_req)
     delete id;
 }
 
+int yf::VirtualDB::Frontend::relay_apdu(mp::Package &package, Z_APDU *apdu_req)
+{
+    int no = 0;
+    std::list<BackendPtr>::const_iterator map_it;
+    map_it = m_backend_list.begin();
+    for (; map_it != m_backend_list.end(); map_it++)
+    {
+        BackendPtr b = *map_it;
+        
+        Package relay_package(b->m_backend_session, package.origin());
+        relay_package.copy_filter(package);
+
+        relay_package.request() = yazpp_1::GDU(apdu_req);
+
+        relay_package.move(b->m_route);
+        package.response() = relay_package.response();
+        no++;
+    }
+    return no;
+}
+
 void yf::VirtualDB::Frontend::scan(mp::Package &package, Z_APDU *apdu_req)
 {
     Z_ScanRequest *req = apdu_req->u.scanRequest;
@@ -685,8 +747,8 @@ void yf::VirtualDB::Frontend::scan(mp::Package &package, Z_APDU *apdu_req)
 
 
 void yf::VirtualDB::add_map_db2targets(std::string db, 
-                                     std::list<std::string> targets,
-                                     std::string route)
+                                       std::list<std::string> targets,
+                                       std::string route)
 {
     m_p->m_maps.push_back(
         VirtualDB::Map(mp::util::database_name_normalize(db), targets, route));
@@ -694,8 +756,8 @@ void yf::VirtualDB::add_map_db2targets(std::string db,
 
 
 void yf::VirtualDB::add_map_db2target(std::string db, 
-                                    std::string target,
-                                    std::string route)
+                                      std::string target,
+                                      std::string route)
 {
     std::list<std::string> targets;
     targets.push_back(target);
@@ -753,6 +815,9 @@ void yf::VirtualDB::process(mp::Package &package) const
                 else
                     break;
             
+            *resp->preferredMessageSize = *req->preferredMessageSize;
+            *resp->maximumRecordSize = *req->maximumRecordSize;
+
             package.response() = apdu;
             f->m_is_virtual = true;
         }
@@ -787,7 +852,15 @@ void yf::VirtualDB::process(mp::Package &package) const
         }
         else if (apdu->which == Z_APDU_close)
         {
-            package.session().close();
+            if (f->relay_apdu(package, apdu) == 0)
+            {
+                mp::odr odr;
+                
+                package.response() = odr.create_close(
+                    apdu, Z_Close_finished, "virt_db");
+                
+                package.session().close();
+            }
         }
         else
         {
@@ -803,7 +876,6 @@ void yf::VirtualDB::process(mp::Package &package) const
     m_p->release_frontend(package);
 }
 
-
 void mp::filter::VirtualDB::configure(const xmlNode * ptr, bool test_only)
 {
     for (ptr = ptr->children; ptr; ptr = ptr->next)
@@ -838,6 +910,22 @@ void mp::filter::VirtualDB::configure(const xmlNode * ptr, bool test_only)
             std::string route = mp::xml::get_route(ptr);
             add_map_db2targets(database, targets, route);
         }
+        else if (!strcmp((const char *) ptr->name, "torus"))
+        {
+            std::string url;
+            const struct _xmlAttr *attr;
+            for (attr = ptr->properties; attr; attr = attr->next)
+            {
+                if (!strcmp((const char *) attr->name, "url"))
+                    url = mp::xml::get_text(attr->children);
+                else
+                    throw mp::filter::FilterException(
+                        "Bad attribute " + std::string((const char *)
+                                                       attr->name));
+            }
+            m_p->torus.read_searchables(url);
+            refresh_torus();
+        }
         else
         {
             throw mp::filter::FilterException