+ if (!it->second->m_in_use)
+ {
+ it->second->m_in_use = true;
+ return it->second;
+ }
+ m_cond_session_ready.wait(lock);
+ }
+ FrontendPtr f(new Frontend(this));
+ m_clients[package.session()] = f;
+ f->m_in_use = true;
+ return f;
+}
+
+void yf::VirtualDB::Rep::release_frontend(mp::Package &package)
+{
+ boost::mutex::scoped_lock lock(m_mutex);
+ std::map<mp::Session,yf::VirtualDB::FrontendPtr>::iterator it;
+
+ it = m_clients.find(package.session());
+ if (it != m_clients.end())
+ {
+ if (package.session().is_closed())
+ {
+ it->second->close(package);
+ m_clients.erase(it);
+ }
+ else
+ {
+ it->second->m_in_use = false;
+ }
+ m_cond_session_ready.notify_all();
+ }
+}
+
+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)
+{
+}
+
+
+yf::VirtualDB::Set::Set()
+{
+}
+
+
+yf::VirtualDB::Set::~Set()
+{
+}
+
+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()
+{
+}
+
+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() {
+}
+
+void yf::VirtualDB::Frontend::fixup_npr_record(ODR odr, Z_NamePlusRecord *npr,
+ BackendPtr b)
+{
+ if (npr->databaseName)
+ {
+ std::string b_database = std::string(npr->databaseName);
+
+ // consider each of the frontend databases..
+ std::list<std::string>::const_iterator db_it;
+ for (db_it = b->m_frontend_databases.begin();
+ db_it != b->m_frontend_databases.end(); db_it++)
+ {
+ // see which target it corresponds to.. (if any)
+ 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())
+ {
+ 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++;
+ }
+ }
+
+ }
+ db_it = b->m_frontend_databases.begin();
+ if (db_it != b->m_frontend_databases.end())
+ {
+ std::string database = *db_it;
+ npr->databaseName = odr_strdup(odr, database.c_str());
+ }
+ }
+}
+
+void yf::VirtualDB::Frontend::fixup_npr_records(ODR odr, Z_Records *records,
+ BackendPtr b)
+{
+ if (records && records->which == Z_Records_DBOSD)
+ {
+ Z_NamePlusRecordList *nprlist = records->u.databaseOrSurDiagnostics;