+ BackendPtr b(new Backend);
+ std::list<std::string>::const_iterator db_it = databases.begin();
+
+ b->m_number_of_sets = 0;
+ b->m_frontend_databases = databases;
+ b->m_named_result_sets = false;
+
+ bool first_route = true;
+
+ std::map<std::string,bool> targets_dedup;
+ for (; db_it != databases.end(); 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;
+ addinfo = *db_it;
+ BackendPtr ptr;
+ return ptr;
+ }
+ std::list<std::string>::const_iterator t_it =
+ 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->m_route)
+ {
+ // 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->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;
+ }
+ return b;
+}
+
+yf::VirtualDB::BackendPtr yf::VirtualDB::Frontend::init_backend(
+ std::list<std::string> databases, mp::Package &package,
+ int &error_code, std::string &addinfo)
+{
+ BackendPtr b = create_backend_from_databases(databases, error_code,
+ addinfo);
+ if (!b)
+ return b;
+ Package init_package(b->m_backend_session, package.origin());
+ init_package.copy_filter(package);
+
+ mp::odr odr;
+
+ Z_APDU *init_apdu = zget_APDU(odr, Z_APDU_initRequest);
+
+ mp::util::set_vhost_otherinfo(&init_apdu->u.initRequest->otherInfo, odr,
+ b->m_targets);
+ Z_InitRequest *req = init_apdu->u.initRequest;
+
+ // copy stuff from Frontend Init Request
+ Z_GDU *org_gdu = m_init_gdu.get();
+ Z_InitRequest *org_init = org_gdu->u.z3950->u.initRequest;
+
+ req->idAuthentication = org_init->idAuthentication;
+ 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);
+ ODR_MASK_SET(req->options, Z_Options_namedResultSets);
+ ODR_MASK_SET(req->options, Z_Options_scan);
+
+ ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
+ ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
+ ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
+
+ init_package.request() = init_apdu;
+
+ init_package.move(b->m_route); // sending init
+
+ Z_GDU *gdu = init_package.response().get();
+ // we hope to get an init response
+ 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 && !init_package.session().is_closed())
+ {
+ m_backend_list.push_back(b);
+ return b;
+ }
+ mp::util::get_init_diagnostics(res, error_code, addinfo);
+ }
+ if (!init_package.session().is_closed())
+ {
+ 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
+ }
+ BackendPtr null;
+ return null;
+}
+
+void yf::VirtualDB::Frontend::search(mp::Package &package, Z_APDU *apdu_req)
+{
+ 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);
+ }
+
+ 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);
+
+ 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;