9 #include "CoralBase/Exception.h"
10 #include "CoralBase/Blob.h"
12 #include "RelationalAccess/ConnectionService.h"
13 #include "RelationalAccess/IConnectionServiceConfiguration.h"
14 #include "RelationalAccess/ISessionProxy.h"
15 #include "RelationalAccess/ISchema.h"
17 #include "CrestApi/CrestApiBase.h"
18 #include "CrestApi/CrestRequest.h"
20 #include "boost/property_tree/ptree.hpp"
28 m_connection(connection)
40 static const std::string versionTagPrefix(
"Trigger-Run3-Schema-v");
43 if(! session->nominalSchema().existsTable(
"TRIGGER_SCHEMA") ) {
44 throw std::runtime_error(
"Trigger schema has no schema version table" );
56 throw std::runtime_error(
"Trigger schema has schema version table but it is empty" );
60 std::string versionTag =
row[
"TS_TAG"].data<std::string>();
61 if( ! versionTag.starts_with(versionTagPrefix)) {
62 throw std::runtime_error(
std::format(
"Tag format error: Trigger schema version tag {} does not start with {}", versionTag, versionTagPrefix));
65 std::string vstr = versionTag.substr(versionTagPrefix.size());
66 size_t schemaVersion{0};
68 schemaVersion = std::stoi(vstr);
70 catch (
const std::invalid_argument& ia) {
71 TRG_MSG_ERROR(
"Invalid argument when interpreting the version part " << vstr <<
" of schema tag " << versionTag <<
". " << ia.what());
75 TRG_MSG_INFO(
"TriggerDB schema version: " << schemaVersion);
83 if(m_crestServer.ends_with(
'/')) {
84 m_crestServer.pop_back();
88 m_useCrest = ! m_crestServer.empty();
92 m_crestVersion = DEFAULT_CREST_API_VERSION;
96 if(!m_crestVersion.starts_with(
'/')) {
97 m_crestVersion =
"/" + m_crestVersion;
103 m_crestTrigDb = crestTrigDB;
106 std::unique_ptr<coral::ISessionProxy>
109 coral::ConnectionService connSvc;
110 coral::IConnectionServiceConfiguration& csc = connSvc.configuration();
111 csc.setConnectionRetrialPeriod( m_retrialPeriod );
112 csc.setConnectionRetrialTimeOut( m_retrialTimeout );
113 csc.setConnectionTimeOut( m_connectionTimeout );
125 auto proxy = std::unique_ptr<coral::ISessionProxy>( connSvc.connect(m_connection, coral::AccessMode::ReadOnly) );
127 TRG_MSG_INFO(
"Opened session " << m_connection <<
" with retrialPeriod/retrialTimeout/connectionTimeout: "
128 << m_retrialPeriod <<
"/" << m_retrialTimeout <<
"/" << m_connectionTimeout);
145 std::string
url = m_crestServer + m_crestVersion;
155 Crest::CrestApi capi = Crest::CrestApi(
url);
163 Crest::CrestRequest request = Crest::CrestRequest();
165 std::string current_path =
"payloads/data?format=BLOB&hash=" +
query;
175 const std::map<size_t, TrigConf::QueryDefinition> & queries)
const
178 size_t maxDefVersion = 0;
179 for(
auto &
entry : queries) {
180 size_t vkey =
entry.first;
181 if(vkey>maxDefVersion and vkey<=schemaVersion) {
182 maxDefVersion = vkey;
186 if( maxDefVersion==0 ) {
187 TRG_MSG_ERROR(
"No query for schema version " << schemaVersion <<
" defined" );
190 return queries.at(maxDefVersion);
196 const std::string & query_type)
const
202 catch(Crest::CrestException & ex) {
203 TRG_MSG_ERROR(
"When reading " <<
description <<
" for key " <<
key <<
" from crest a CrestException was caught ( " << ex.what() <<
" )" );
213 catch(boost::property_tree::json_parser_error & ex) {
214 TRG_MSG_ERROR(
"When reading " <<
description <<
" for key " <<
key <<
" from crest a ptree json parser error was caught ( " << ex.what() <<
" )" );
222 const std::map<size_t, QueryDefinition> & queries)
const
224 auto session = createDBSession();
225 session->transaction().start(
true);
226 const size_t sv = schemaVersion(session.get());
245 catch(coral::QueryException & ex) {
246 TRG_MSG_ERROR(
"When reading " <<
description <<
" for key " <<
key <<
" a coral::QueryException was caught ( " << ex.what() <<
" )" );
249 catch(boost::property_tree::json_parser_error & ex) {
250 TRG_MSG_ERROR(
"When reading " <<
description <<
" for key " <<
key <<
" a ptree json parser error was caught ( " << ex.what() <<
" )" );