ATLAS Offline Software
Loading...
Searching...
No Matches
DBLoader.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "./DBHelper.h"
7
8#include "RelationalAccess/ITransaction.h"
9#include "RelationalAccess/ICursor.h"
10#include "RelationalAccess/IQuery.h"
11#include "RelationalAccess/ITable.h"
12#include "RelationalAccess/ISchema.h"
13#include "RelationalAccess/SchemaException.h"
14
15#include <CoralBase/Attribute.h>
16#include <CoralBase/AttributeList.h>
17
18#include <stdexcept>
19
20using namespace std;
21using namespace TrigConf;
22
23DBLoader::DBLoader( const std::string& name, StorageMgr& sm, coral::ISessionProxy& session ) :
25 m_storageMgr( sm ),
26 m_session( session )
27{}
28
29
30DBLoader::DBLoader( StorageMgr& sm, coral::ISessionProxy& session ) :
31 DBLoader("DBLoader", sm, session)
32{}
33
34
36{
37 if ( ! m_session.transaction().isActive() ) {
38 //std::cout << "DBLoader: startSession(readonly=true)" << std::endl;
39 bool readOnly = true;
40 m_session.transaction().start(readOnly);
41 m_sessionOwner = true;
42 }
43}
44
46{
47 if ( m_session.transaction().isActive() && m_sessionOwner) {
48 m_session.transaction().commit();
49 }
50}
51
52
53bool
55 const static unsigned int run = std::get<1>(loadSchemaVersion());
56 return run == 2;
57}
58
59void
61 msg().setLevel(lvl);
62
63 switch(lvl) {
64 case MSGTC::ALWAYS: m_verbose = 5; break;
65 case MSGTC::VERBOSE: m_verbose = 4; break;
66 case MSGTC::DEBUG: m_verbose = 3; break;
67 case MSGTC::INFO: m_verbose = 2; break;
68 case MSGTC::WARNING:
69 case MSGTC::ERROR:
70 case MSGTC::FATAL: m_verbose = 0; break;
71 default: m_verbose = 0;
72 }
73}
74
75unsigned int
77 return std::get<0>(loadSchemaVersion());
78}
79
80std::tuple<unsigned int,unsigned int>
82{
83 const static auto versions = [&]() -> std::tuple<unsigned int,unsigned int> {
84 bool mySession = false;
85 if ( ! m_session.transaction().isActive() ) {
86 m_session.transaction().start(true);
87 mySession = true;
88 }
89
90 std::unique_ptr< coral::IQuery > q( m_session.nominalSchema().tableHandle( "TRIGGER_SCHEMA").newQuery() );
91 q->setRowCacheSize( 1 );
92
93 //Output data and types
94 coral::AttributeList attList;
95 attList.extend<int>( "TS_ID" );
96 q->defineOutput(attList);
97 q->addToOutputList( "TS_ID" );
98
99 q->addToOrderList("TS_ID desc");
100 coral::ICursor& cursor = q->execute();
101
102 if ( ! cursor.next() ) {
103 TRG_MSG_ERROR("Table TRIGGER_SCHEMA is not filled");
104 if ( mySession ) m_session.transaction().commit();
105 throw std::runtime_error( "DBLoader::loadSchemaVersion() >> Table TRIGGER_SCHEMA is not filled" );
106 }
107
108 const coral::AttributeList& row = cursor.currentRow();
109 const unsigned int triggerDBSchemaVersion = row["TS_ID"].data<int>();
110
111 TRG_MSG_INFO("TriggerDB schema version: " << triggerDBSchemaVersion);
112
113 const unsigned int run = m_session.nominalSchema().existsTable( "ACTIVE_MASTERS" ) ? 2 : 1;
114
115 TRG_MSG_INFO("Database has Run " << run << " schema");
116 TRG_MSG_INFO("Total number of tables : " << m_session.nominalSchema().listTables().size());
117
118 //commitSession();
119 if ( mySession ) m_session.transaction().commit();
120
121 return {triggerDBSchemaVersion, run};
122 }();
123
124 return versions;
125}
126
127bool
128TrigConf::DBLoader::loadL1MasterKey(int smk, int& Lvl1MasterKey) {
129 try {
130 startSession();
131
132 unique_ptr< coral::IQuery > q( m_session.nominalSchema().tableHandle( "SUPER_MASTER_TABLE").newQuery() );
133 q->setRowCacheSize( 5 );
134
135 //Bind list
136 coral::AttributeList bindings;
137 bindings.extend<int>("smtid");
138 bindings[0].data<int>() = smk;
139 q->setCondition( "SMT_ID = :smtid", bindings );
140
141 //Output data and types
142 coral::AttributeList attList;
143 attList.extend<int>( "SMT_L1_MASTER_TABLE_ID" );
144 fillQuery(q.get(), attList);
145
146 coral::ICursor& cursor = q->execute();
147 if ( ! cursor.next() ) {
148 msg() << "DBLoader: No such SuperMaster key exists " << smk << endl;
149 throw runtime_error( "DBLoader: SuperMasterKey not available" );
150 }
151
152 const coral::AttributeList& row = cursor.currentRow();
153 Lvl1MasterKey = row["SMT_L1_MASTER_TABLE_ID"].data<int>();
154 }
155 catch( const std::exception& e ) {
157 msg() << "DBLoader: C++ exception: " << e.what() << std::endl;
158 throw;
159 }
161 return true;
162}
163
164
165
166bool
167TrigConf::DBLoader::loadL1MenuKey(int SuperMasterKey, int& Lvl1MenuKey) {
168 try {
169
170 int l1Master = 0;
171 loadL1MasterKey(SuperMasterKey, l1Master);
172
173 startSession();
174
175 unique_ptr< coral::IQuery > q( m_session.nominalSchema().tableHandle( "L1_MASTER_TABLE").newQuery() );
176 q->setRowCacheSize( 5 );
177
178 //Bind list
179 coral::AttributeList bindings;
180 bindings.extend<int>("l1mtid");
181 bindings[0].data<int>() = l1Master;
182 q->setCondition( "L1MT_ID = :l1mtid" , bindings );
183
184 //Output data and types
185 coral::AttributeList attList;
186 attList.extend<int>( "L1MT_TRIGGER_MENU_ID" );
187 q->defineOutput(attList);
188 q->addToOutputList( "L1MT_TRIGGER_MENU_ID" );
189
190 coral::ICursor& cursor = q->execute();
191 if ( ! cursor.next() ) {
192 msg() << "DBLoader >> No such L1 Master key exists " << l1Master << std::endl;
193 throw std::runtime_error( "DBLoader >> L1MasterKey not available" );
195 }
196
197 const coral::AttributeList& row = cursor.currentRow();
198 Lvl1MenuKey = row["L1MT_TRIGGER_MENU_ID"].data<int>();
199
201
202 }
203 catch( const std::exception& e ) {
204 msg() << "DBLoader >> Standard C++ exception: " << e.what() << std::endl;
205 m_session.transaction().rollback();
206 throw;
207 }
208 return true;
209}
bool loadL1MasterKey(int SuperMasterKey, int &Lvl1MasterKey)
get l1 master from super master
Definition DBLoader.cxx:128
StorageMgr & m_storageMgr
reference to the storage manager
Definition DBLoader.h:67
DBLoader(StorageMgr &sm, coral::ISessionProxy &session)
constructor
Definition DBLoader.cxx:30
void commitSession()
commit session if not already done
Definition DBLoader.cxx:45
bool m_sessionOwner
remember if the loader started the session in the first place
Definition DBLoader.h:69
virtual void setLevel(MSGTC::Level lvl) override
access to output stream
Definition DBLoader.cxx:60
coral::ISessionProxy & m_session
CORAL interface to database session.
Definition DBLoader.h:68
unsigned int triggerDBSchemaVersion()
Definition DBLoader.cxx:76
std::tuple< unsigned int, unsigned int > loadSchemaVersion() const
get DB schema version and run number
Definition DBLoader.cxx:81
void startSession()
start session if not already active
Definition DBLoader.cxx:35
bool loadL1MenuKey(int SuperMasterKey, int &Lvl1MenuKey)
get l1 menu id from super master
Definition DBLoader.cxx:167
void setLevel(MSGTC::Level lvl)
Set message level of stream.
Database Storage Manager, controls the database session and the different loader classes for DB acces...
Definition StorageMgr.h:23
MsgStreamTC & msg() const
The standard message stream.
TrigConfMessaging(const std::string &name)
Constructor with parameters.
Forward iterator to traverse the main components of the trigger configuration.
Definition Config.h:22
void fillQuery(coral::IQuery *q, coral::AttributeList &attList)
Definition DBHelper.cxx:13
Definition run.py:1
STL namespace.
MsgStream & msg
Definition testRead.cxx:32