-/* $Id: filter_virt_db.cpp,v 1.52 2007-08-13 10:19:16 adam Exp $
- Copyright (c) 2005-2007, Index Data.
-
-This file is part of Metaproxy.
+/* This file is part of Metaproxy.
+ 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
for more details.
You should have received a copy of the GNU General Public License
-along with Metaproxy; see the file LICENSE. If not, write to the
-Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-02111-1307, USA.
- */
-/* $Id: filter_virt_db.cpp,v 1.52 2007-08-13 10:19:16 adam Exp $
- Copyright (c) 2005-2007, Index Data.
-
- See the LICENSE file for details
- */
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+*/
#include "config.hpp"
-#include "filter.hpp"
-#include "package.hpp"
+#include "filter_virt_db.hpp"
+#include <metaproxy/package.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition.hpp>
#include <boost/shared_ptr.hpp>
-#include "util.hpp"
-#include "filter_virt_db.hpp"
+#include <metaproxy/util.hpp>
#include <yaz/zgdu.h>
#include <yaz/otherinfo.h>
#include <yaz/diagbib1.h>
+#include <yaz/match_glob.h>
+#include <yaz/query-charset.h>
+#include <yaz/log.h>
#include <map>
#include <iostream>
std::string m_setname;
};
struct VirtualDB::Map {
- Map(std::list<std::string> targets, std::string route);
+ Map(std::string database, std::list<std::string> targets, std::string route);
+ Map(std::string database, std::string target, std::string route);
Map();
+ bool match(const std::string db) const;
+ std::string m_dbpattern;
std::list<std::string> m_targets;
std::string m_route;
+ public:
+ std::string query_encoding;
};
struct VirtualDB::Backend {
mp::Session m_backend_session;
std::list<std::string> m_frontend_databases;
std::list<std::string> m_targets;
std::string m_route;
+ std::string query_encoding;
bool m_named_result_sets;
int m_number_of_sets;
};
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;
FrontendPtr get_frontend(Package &package);
void release_frontend(Package &package);
+ void refresh_torus();
private:
- std::map<std::string, VirtualDB::Map>m_maps;
+ std::list<VirtualDB::Map>m_maps;
typedef std::map<std::string,VirtualDB::Set>::iterator Sets_it;
boost::mutex m_mutex;
boost::condition m_cond_session_ready;
std::map<mp::Session, FrontendPtr> m_clients;
+ bool pass_vhosts;
};
}
}
std::map<std::string,bool> targets_dedup;
for (; db_it != databases.end(); db_it++)
{
- std::map<std::string, VirtualDB::Map>::iterator map_it;
- map_it = m_p->m_maps.find(mp::util::database_name_normalize(*db_it));
+ std::list<VirtualDB::Map>::const_iterator map_it;
+ map_it = m_p->m_maps.begin();
+ while (map_it != m_p->m_maps.end())
+ {
+ if (map_it->match(*db_it))
+ break;
+ map_it++;
+ }
+
if (map_it == m_p->m_maps.end()) // database not found
{
error_code = YAZ_BIB1_DATABASE_DOES_NOT_EXIST;
return ptr;
}
std::list<std::string>::const_iterator t_it =
- map_it->second.m_targets.begin();
- for (; t_it != map_it->second.m_targets.end(); t_it++)
- targets_dedup[*t_it] = true;
+ map_it->m_targets.begin();
+ for (; t_it != map_it->m_targets.end(); t_it++) {
+ if (!targets_dedup[*t_it])
+ {
+ targets_dedup[*t_it] = true;
+ b->m_targets.push_back(*t_it);
+ }
+ }
// see if we have a route conflict.
- if (!first_route && b->m_route != map_it->second.m_route)
+ if (!first_route && b->m_route != map_it->m_route)
{
- // we have a conflict..
+ // we have a conflict.. routing must be same for all
error_code = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
BackendPtr ptr;
return ptr;
}
- b->m_route = map_it->second.m_route;
+ b->m_route = map_it->m_route;
+ if (!first_route && b->query_encoding != map_it->query_encoding)
+ {
+ // we have a conflict. query encoding must be same for all
+ error_code = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
+ BackendPtr ptr;
+ return ptr;
+ }
+ b->query_encoding = map_it->query_encoding;
first_route = false;
}
- std::map<std::string,bool>::const_iterator tm_it = targets_dedup.begin();
- for (; tm_it != targets_dedup.end(); tm_it++)
- b->m_targets.push_back(tm_it->first);
-
return b;
}
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);
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)
{
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())
{
- error_code = YAZ_BIB1_DATABASE_UNAVAILABLE;
- mp::util::get_init_diagnostics(res, error_code, addinfo);
- BackendPtr null;
- return null;
+ m_backend_list.push_back(b);
+ return b;
}
+ mp::util::get_init_diagnostics(res, error_code, addinfo);
}
- else
- {
- error_code = YAZ_BIB1_DATABASE_UNAVAILABLE;
- // addinfo = database;
- BackendPtr null;
- return null;
- }
- if (init_package.session().is_closed())
+ if (!init_package.session().is_closed())
{
- error_code = YAZ_BIB1_DATABASE_UNAVAILABLE;
- // addinfo = database;
- BackendPtr null;
- return null;
+ Package close_package(b->m_backend_session, package.origin());
+ close_package.copy_filter(package);
+ close_package.session().close();
+ close_package.move(b->m_route); // closing it
}
-
- m_backend_list.push_back(b);
- return b;
+ BackendPtr null;
+ return null;
}
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())
{
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)
- 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
{
&req->databaseNames);
}
+ if (b->query_encoding.length() &&
+ (req->query->which == Z_Query_type_1 ||
+ req->query->which == Z_Query_type_101))
+ {
+ yaz_iconv_t cd = yaz_iconv_open(b->query_encoding.c_str(), "UTF-8");
+ if (cd)
+ {
+ int r = yaz_query_charset_convert_rpnquery_check(
+ req->query->u.type_1, odr, cd);
+ yaz_iconv_close(cd);
+ if (r)
+ yaz_log(YLOG_LOG, "query could not be converted");
+ }
+ }
*req->replaceIndicator = 1;
search_package.request() = yazpp_1::GDU(apdu_req);
}
}
+
yf::VirtualDB::Set::Set(BackendPtr b, std::string setname)
: m_backend(b), m_setname(setname)
{
{
}
-yf::VirtualDB::Map::Map(std::list<std::string> targets, std::string route)
- : m_targets(targets), m_route(route)
+yf::VirtualDB::Map::Map(std::string database,
+ std::list<std::string> targets, std::string route)
+ : m_dbpattern(database), m_targets(targets), m_route(route)
+{
+}
+
+yf::VirtualDB::Map::Map(std::string database,
+ std::string target, std::string route)
+ : m_dbpattern(database), m_route(route)
{
+ m_targets.push_back(target);
}
+
yf::VirtualDB::Map::Map()
{
}
+bool yf::VirtualDB::Map::match(const std::string db) const
+{
+ std::string norm_db = mp::util::database_name_normalize(db);
+ if (yaz_match_glob(m_dbpattern.c_str(), norm_db.c_str()))
+ return true;
+ return false;
+}
+
yf::VirtualDB::VirtualDB() : m_p(new VirtualDB::Rep)
{
+ m_p->pass_vhosts = false;
}
yf::VirtualDB::~VirtualDB() {
db_it != b->m_frontend_databases.end(); db_it++)
{
// see which target it corresponds to.. (if any)
- std::map<std::string,VirtualDB::Map>::const_iterator map_it;
-
- map_it = m_p->m_maps.find(mp::util::database_name_normalize(*db_it));
+ std::list<VirtualDB::Map>::const_iterator map_it =
+ m_p->m_maps.begin();
+ while (map_it != m_p->m_maps.end())
+ {
+ if (map_it->match(*db_it))
+ break;
+ map_it++;
+ }
if (map_it != m_p->m_maps.end())
{
- VirtualDB::Map m = map_it->second;
-
- std::list<std::string>::const_iterator t;
- for (t = m.m_targets.begin(); t != m.m_targets.end(); t++)
+ std::list<std::string>::const_iterator t
+ = map_it->m_targets.begin();
+ while (t != map_it->m_targets.end())
{
if (*t == b_database)
{
npr->databaseName = odr_strdup(odr, (*db_it).c_str());
return;
}
+ t++;
}
}
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;
&req->num_databaseNames,
&req->databaseNames);
}
+
+ if (b->query_encoding.length())
+ {
+ yaz_iconv_t cd = yaz_iconv_open(b->query_encoding.c_str(), "UTF-8");
+ if (cd)
+ {
+ yaz_query_charset_convert_apt(req->termListAndStartPoint, odr, cd);
+ yaz_iconv_close(cd);
+ }
+ }
+
scan_package.request() = yazpp_1::GDU(apdu_req);
scan_package.move(b->m_route);
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[mp::util::database_name_normalize(db)]
- = VirtualDB::Map(targets, route);
+ m_p->m_maps.push_back(
+ VirtualDB::Map(mp::util::database_name_normalize(db), targets, route));
}
void yf::VirtualDB::add_map_db2target(std::string db,
- std::string target,
- std::string route)
-{
- std::list<std::string> targets;
- targets.push_back(target);
+ std::string target,
+ std::string route)
- m_p->m_maps[mp::util::database_name_normalize(db)]
- = VirtualDB::Map(targets, route);
+{
+ m_p->m_maps.push_back(
+ VirtualDB::Map(mp::util::database_name_normalize(db), target, route));
}
void yf::VirtualDB::process(mp::Package &package) const
std::list<std::string> vhosts;
mp::util::get_vhost_otherinfo(req->otherInfo, vhosts);
- if (vhosts.size() == 0)
+
+ if (vhosts.size() > 0 && m_p->pass_vhosts)
+ {
+ package.move();
+ }
+ else
{
f->m_init_gdu = gdu;
else
break;
+ *resp->preferredMessageSize = *req->preferredMessageSize;
+ *resp->maximumRecordSize = *req->maximumRecordSize;
+
package.response() = apdu;
f->m_is_virtual = true;
}
- else
- package.move();
}
else if (!f->m_is_virtual)
package.move();
}
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
{
m_p->release_frontend(package);
}
-
-void mp::filter::VirtualDB::configure(const xmlNode * ptr)
+void mp::filter::VirtualDB::configure(const xmlNode * ptr, bool test_only)
{
for (ptr = ptr->children; ptr; ptr = ptr->next)
{
if (ptr->type != XML_ELEMENT_NODE)
continue;
- if (!strcmp((const char *) ptr->name, "virtual"))
+ if (!strcmp((const char *) ptr->name, "pass-vhosts"))
+ {
+ m_p->pass_vhosts = mp::xml::get_bool(ptr, false);
+ }
+ else if (!strcmp((const char *) ptr->name, "virtual"))
{
+ std::string query_encoding;
std::string database;
std::list<std::string> targets;
xmlNode *v_node = ptr->children;
database = mp::xml::get_text(v_node);
else if (mp::xml::is_element_mp(v_node, "target"))
targets.push_back(mp::xml::get_text(v_node));
+ else if (mp::xml::is_element_mp(v_node, "query-encoding"))
+ targets.push_back(mp::xml::get_text(v_node));
else
throw mp::filter::FilterException
("Bad element "
);
}
std::string route = mp::xml::get_route(ptr);
- add_map_db2targets(database, targets, route);
+
+ VirtualDB::Map vmap(mp::util::database_name_normalize(database),
+ targets, route);
+ vmap.query_encoding = query_encoding;
+ m_p->m_maps.push_back(vmap);
}
else
{
/*
* 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
*/
+