virt_db: make databases available as virtual targets
[metaproxy-moved-to-github.git] / src / filter_virt_db.cpp
index b30e7b4..e6f2338 100644 (file)
@@ -26,6 +26,7 @@ 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>
@@ -79,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;
@@ -114,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;
         };
     }
 }
@@ -212,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);
@@ -434,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)
 {
@@ -601,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;
@@ -668,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));
@@ -677,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);
@@ -736,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;
         }
@@ -770,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
         {
@@ -786,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)
@@ -821,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