+ Z_SearchRequest *req = apdu_req->u.searchRequest;
+ std::string vhost;
+ std::string resultSetId = req->resultSetName;
+ mp::odr odr;
+
+ std::list<std::string> databases;
+ int i;
+ for (i = 0; i<req->num_databaseNames; i++)
+ databases.push_back(req->databaseNames[i]);
+
+ Sets_it sets_it = m_sets.find(req->resultSetName);
+ if (sets_it != m_sets.end())
+ {
+ // result set already exist
+ // if replace indicator is off: we return diagnostic if
+ // result set already exist.
+ if (*req->replaceIndicator == 0)
+ {
+ Z_APDU *apdu =
+ odr.create_searchResponse(
+ apdu_req,
+ YAZ_BIB1_RESULT_SET_EXISTS_AND_REPLACE_INDICATOR_OFF,
+ 0);
+ package.response() = apdu;
+
+ return;
+ }
+ sets_it->second.m_backend->m_number_of_sets--;
+ }
+ // 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++)
+ {
+ BackendPtr tmp = *map_it;
+ if (tmp->m_frontend_databases == databases &&
+ (tmp->m_named_result_sets ||
+ tmp->m_number_of_sets == 0))
+ {
+ b = *map_it;
+ break;
+ }
+ }
+ if (!b) // no backend yet. Must create a new one
+ {
+ int error_code;
+ std::string addinfo;
+ b = init_backend(databases, package, error_code, addinfo);
+ if (!b)
+ {
+ // did not get a backend (unavailable somehow?)
+
+ Z_APDU *apdu =
+ odr.create_searchResponse(
+ apdu_req, error_code, addinfo.c_str());
+ package.response() = apdu;
+ return;
+ }
+ }
+ m_sets.erase(req->resultSetName);
+ // sending search to backend
+ Package search_package(b->m_backend_session, package.origin());
+
+ search_package.copy_filter(package);
+
+ std::string backend_setname;
+ if (b->m_named_result_sets)
+ {
+ backend_setname = std::string(req->resultSetName);
+ }
+ else
+ {
+ backend_setname = "default";
+ req->resultSetName = odr_strdup(odr, backend_setname.c_str());
+ }
+
+ // pick first targets spec and move the databases from it ..
+ std::list<std::string>::const_iterator t_it = b->m_targets.begin();
+ if (t_it != b->m_targets.end())
+ {
+ mp::util::set_databases_from_zurl(odr, *t_it,
+ &req->num_databaseNames,
+ &req->databaseNames);
+ }
+
+ *req->replaceIndicator = 1;
+
+ search_package.request() = yazpp_1::GDU(apdu_req);
+
+ search_package.move(b->m_route);
+
+ if (search_package.session().is_closed())
+ {
+ package.response() = search_package.response();
+ package.session().close();
+ return;
+ }
+ b->m_number_of_sets++;
+
+ m_sets[resultSetId] = VirtualDB::Set(b, backend_setname);
+ fixup_package(search_package, b);
+ package.response() = search_package.response();
+}
+
+yf::VirtualDB::Frontend::Frontend(Rep *rep)
+{
+ m_p = rep;
+ m_is_virtual = false;
+}
+
+void yf::VirtualDB::Frontend::close(mp::Package &package)
+{
+ std::list<BackendPtr>::const_iterator b_it;
+
+ for (b_it = m_backend_list.begin(); b_it != m_backend_list.end(); b_it++)
+ {
+ (*b_it)->m_backend_session.close();
+ Package close_package((*b_it)->m_backend_session, package.origin());
+ close_package.copy_filter(package);
+ close_package.move((*b_it)->m_route);
+ }
+ m_backend_list.clear();
+}
+
+yf::VirtualDB::Frontend::~Frontend()
+{
+}
+
+yf::VirtualDB::FrontendPtr yf::VirtualDB::Rep::get_frontend(mp::Package &package)
+{
+ boost::mutex::scoped_lock lock(m_mutex);
+
+ std::map<mp::Session,yf::VirtualDB::FrontendPtr>::iterator it;
+
+ while(true)
+ {
+ it = m_clients.find(package.session());
+ if (it == m_clients.end())
+ break;
+
+ 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();
+ }