CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DbSession.cc
Go to the documentation of this file.
1 //local includes
8 // CMSSW includes
12 // coral includes
13 #include "RelationalAccess/ISessionProxy.h"
14 
15 namespace cond {
16 
17  inline std::auto_ptr<cond::TechnologyProxy> buildTechnologyProxy(const std::string&userconnect,
18  const DbConnection& connection){
19  std::string protocol;
20  std::size_t pos=userconnect.find_first_of(':');
21  if( pos!=std::string::npos ){
22  protocol=userconnect.substr(0,pos);
23  std::size_t p=protocol.find_first_of('_');
24  if(p!=std::string::npos){
25  protocol=protocol.substr(0,p);
26  }
27  }else{
28  throw cond::Exception(userconnect +":connection string format error");
29  }
30  std::auto_ptr<cond::TechnologyProxy> ptr(cond::TechnologyProxyFactory::get()->create(protocol));
31  (*ptr).initialize(connection);
32  return ptr;
33  }
34 
35  class SessionImpl {
36  public:
38  connection(),
39  blobStreamingService( "COND/Services/BlobStreamingService" ),
40  database(),
41  transaction(),
42  isOpen(false){
43  }
44 
45  explicit SessionImpl( const DbConnection& connection ):
46  connection(new DbConnection(connection)),
47  blobStreamingService( "COND/Services/BlobStreamingService" ),
48  database(),
49  transaction(),
50  isOpen(false){
51  }
52 
53 
54  virtual ~SessionImpl(){
55  close();
56  }
57 
58  void open( const std::string& connectionString,
59  const std::string& role,
60  bool readOnly ){
61  close();
62  if( connection.get() ){
63  if(!connection->isOpen()){
64  throw cond::Exception("DbSession::open: cannot open session. Underlying connection is closed.");
65  }
66  boost::shared_ptr<ora::ConnectionPool> connPool = connection->connectionPool();
67  database.reset( new ora::Database( connPool ) );
68 
70  if(!blobStreamer) throw cond::Exception("DbSession::open: cannot find required plugin. No instance of ora::IBlobStreamingService has been loaded..");
71  database->configuration().setBlobStreamingService( blobStreamer );
72  //database->configuration().properties().setFlag( ora::Configuration::automaticDatabaseCreation() );
73  database->configuration().properties().setFlag( ora::Configuration::automaticContainerCreation() );
74  // open the db connection
75  technologyProxy = buildTechnologyProxy(connectionString, *connection);
76  std::string connStr = (*technologyProxy).getRealConnectString( connectionString );
77  database->connect( connStr, role, readOnly );
78  transaction.reset( new cond::DbTransaction( database->transaction() ) );
79  isOpen = true;
80  }
81  }
82 
83  void openReadOnly( const std::string& connectionString,
84  const std::string& transactionId ){
85  close();
86  if( connection.get() ){
87  if(!connection->isOpen()){
88  throw cond::Exception("DbSession::open: cannot open session. Underlying connection is closed.");
89  }
90  boost::shared_ptr<ora::ConnectionPool> connPool = connection->connectionPool();
91  database.reset( new ora::Database( connPool ) );
92 
94  if(!blobStreamer) throw cond::Exception("DbSession::open: cannot find required plugin. No instance of ora::IBlobStreamingService has been loaded..");
95  database->configuration().setBlobStreamingService( blobStreamer );
96  // open the db connection
97  technologyProxy = buildTechnologyProxy(connectionString, *connection);
98  std::string connStr = (*technologyProxy).getRealConnectString(connectionString, transactionId);
99  database->connect( connStr, Auth::COND_READER_ROLE, true );
100  transaction.reset( new cond::DbTransaction( database->transaction() ) );
101  isOpen = true;
102  }
103  }
104 
105  void open( boost::shared_ptr<coral::ISessionProxy>& coralSession,
106  const std::string& connectionString,
107  const std::string& schemaName ){
108  close();
109  database.reset( new ora::Database );
110 
112  if(!blobStreamer) throw cond::Exception("DbSession::open: cannot find required plugin. No instance of ora::IBlobStreamingService has been loaded..");
113  database->configuration().setBlobStreamingService( blobStreamer );
114  database->configuration().properties().setFlag( ora::Configuration::automaticContainerCreation() );
115  database->connect( coralSession, connectionString, schemaName );
116  transaction.reset( new cond::DbTransaction( database->transaction(), false ) );
117  isOpen = true;
118  }
119 
120  void close(){
121  transaction.reset();
122  database.reset();
123  isOpen = false;
124  }
125 
126  std::auto_ptr<DbConnection> connection;
127  std::auto_ptr<cond::TechnologyProxy> technologyProxy;
129  std::auto_ptr<ora::Database> database;
130  std::auto_ptr<DbTransaction> transaction;
131  bool isOpen;
132  };
133 
134 }
135 
136 const char* cond::DbSession::COND_SCHEMA_VERSION = "2.0.0";
137 const char* cond::DbSession::CHANGE_SCHEMA_VERSION = "2.0.0";
138 
140  m_implementation( new SessionImpl ){
141 }
142 
144  m_implementation( new SessionImpl ( connection ) ){
145 }
146 
148  m_implementation( rhs.m_implementation ){
149 }
150 
152 }
153 
155  if(this!=&rhs) m_implementation = rhs.m_implementation;
156  return *this;
157 }
158 
159 void cond::DbSession::open( const std::string& connectionString, bool readOnly )
160 {
161  std::string emptyRole("");
162  m_implementation->open( connectionString, emptyRole, readOnly );
163 }
164 
165 void cond::DbSession::open( const std::string& connectionString, const std::string& asRole, bool readOnly )
166 {
167  m_implementation->open( connectionString, asRole, readOnly );
168 }
169 
170 void cond::DbSession::openReadOnly( const std::string& connectionString, const std::string& id )
171 {
172  m_implementation->openReadOnly( connectionString, id );
173 }
174 
175 void cond::DbSession::open( boost::shared_ptr<coral::ISessionProxy>& coralSession, const std::string& connectionString, const std::string& schemaName ){
176  m_implementation->open( coralSession, connectionString, schemaName );
177 }
178 
180 {
181  m_implementation->close();
182 }
183 
185  return m_implementation->isOpen;
186 }
187 
189  if(!m_implementation->database.get())
190  throw cond::Exception("DbSession::connectionString: cannot get connection string. Session has not been open.");
191  return m_implementation->database->connectionString();
192 }
193 
195  return *(m_implementation->connection);
196 }
197 
198 
200  return m_implementation->technologyProxy->isTransactional();
201 }
202 
204 {
205  return m_implementation->blobStreamingService;
206 }
207 
209 {
210  if(!m_implementation->transaction.get())
211  throw cond::Exception("DbSession::transaction: cannot get transaction. Session has not been open.");
212  return *m_implementation->transaction;
213 }
214 
216  if(!m_implementation->database.get())
217  throw cond::Exception("DbSession::storage: cannot access the database. Session has not been open.");
218  return *m_implementation->database;
219 }
220 
222  bool created = false;
223  if ( !storage().exists() ){
224  created = true;
225  storage().create( std::string(COND_SCHEMA_VERSION) );
226  }
227  return created;
228 }
229 
231 {
232  ora::Version dbVer = storage().schemaVersion();
233  if (dbVer == ora::Version::poolSchemaVersion()) return true;
234  dbVer = storage().schemaVersion( true );
235  return dbVer < ora::Version::fromString( std::string( CHANGE_SCHEMA_VERSION ) );
236 }
237 
238 coral::ISchema& cond::DbSession::schema( const std::string& schemaName )
239 {
240  return storage().storageAccessSession().get().schema( schemaName );
241 }
242 
244 {
245  return storage().storageAccessSession().get().nominalSchema();
246 }
247 
248 bool cond::DbSession::deleteMapping( const std::string& mappingVersion ){
249  ora::DatabaseUtility utility = storage().utility();
250  utility.eraseMapping( mappingVersion );
251  return true;
252 }
253 
254 bool cond::DbSession::importMapping( const std::string& sourceConnectionString,
255  const std::string& contName ){
256  ora::DatabaseUtility utility = storage().utility();
257  std::auto_ptr<cond::TechnologyProxy> technologyProxy = buildTechnologyProxy(sourceConnectionString, *(m_implementation->connection));
258  utility.importContainerSchema( (*technologyProxy).getRealConnectString( sourceConnectionString ), contName );
259  return true;
260 }
261 
262 std::string cond::DbSession::storeObject( const ora::Object& object, const std::string& containerName ){
263  ora::OId oid = storage().insertItem( containerName, object );
264  storage().flush();
265  return oid.toString();
266 }
267 
269  ora::OId oid;
270  oid.fromString( objectId );
271  return storage().fetchItem( oid );
272 }
273 
275  ora::OId oid;
276  oid.fromString( objectId );
277  storage().erase( oid );
278  storage().flush();
279  return true;
280 }
281 
283  ora::OId oid;
284  oid.fromString( objectId );
285  ora::Object data = fromDatabase.getObject( objectId );
286  ora::Container cont = fromDatabase.storage().containerHandle( oid.containerId() );
287  std::string ret = storeObject( data, cont.name() );
288  data.destruct();
289  return ret;
290 }
291 
293  ora::OId oid;
294  oid.fromString( objectId );
295  std::string ret("");
296  if( !oid.isInvalid() ){
297  ora::Container cont = storage().containerHandle( oid.containerId() );
298  ret = cont.realClassName();
299  }
300  return ret;
301 }
302 
304  storage().flush();
305 }
306 
308  m_db( db ){
309 }
310 
312  std::pair<std::string,int> oidData = parseToken( poolToken );
313  if( oidData.first.empty() ){
314  throwException("Could not resolve Container name from token=\""+poolToken+"\".","PoolTokenParser::parse");
315  }
316  ora::Container cont = m_db.containerHandle( oidData.first );
317  return ora::OId( cont.id(), oidData.second );
318 }
319 
321  ora::OId oid;
322  oid.fromString( oraToken );
323  ora::Container cont = m_db.containerHandle( oid.containerId() );
324  return cont.className();
325 }
326 
328  m_db( db ){
329 }
330 
332  ora::Container cont = m_db.containerHandle( oid.containerId() );
333  return writeToken( cont.name(), oid.containerId(), oid.itemId(), cont.className() );
334 }
PoolTokenParser(ora::Database &db)
Definition: DbSession.cc:307
boost::shared_ptr< SessionImpl > m_implementation
Definition: DbSession.h:101
const std::string & connectionString() const
Definition: DbSession.cc:188
std::string write(const ora::OId &oid)
Definition: DbSession.cc:331
bool deleteObject(const std::string &objectId)
Definition: DbSession.cc:274
std::string classNameForItem(const std::string &objectId)
Definition: DbSession.cc:292
std::auto_ptr< cond::TechnologyProxy > buildTechnologyProxy(const std::string &userconnect, const DbConnection &connection)
Definition: DbSession.cc:17
static Version fromString(const std::string &versionString)
Definition: Version.cc:21
std::pair< std::string, int > parseToken(const std::string &objectId)
Definition: PoolToken.cc:15
bool isInvalid() const
Definition: OId.cc:80
DbTransaction & transaction()
Definition: DbSession.cc:208
void open(const std::string &connectionString, bool readOnly=false)
Definition: DbSession.cc:159
const std::string & blobStreamingService() const
Definition: DbSession.cc:203
const std::string & name()
Definition: Container.cc:71
void openReadOnly(const std::string &connectionString, const std::string &id)
Definition: DbSession.cc:170
tuple db
Definition: EcalCondDB.py:151
Definition: OId.h:8
std::auto_ptr< DbConnection > connection
Definition: DbSession.cc:126
bool deleteMapping(const std::string &mappingVersion)
Definition: DbSession.cc:248
static std::string automaticContainerCreation()
coral::ISchema & schema(const std::string &schemaName)
Definition: DbSession.cc:238
static const char * CHANGE_SCHEMA_VERSION
Definition: DbSession.h:34
SessionImpl(const DbConnection &connection)
Definition: DbSession.cc:45
const std::string & className()
Definition: Container.cc:75
std::auto_ptr< DbTransaction > transaction
Definition: DbSession.cc:130
static const char * COND_SCHEMA_VERSION
Definition: DbSession.h:33
void importContainerSchema(const std::string &sourceConnectionString, const std::string &containerName)
Container containerHandle(const std::string &name)
Definition: Database.cc:275
std::auto_ptr< ora::Database > database
Definition: DbSession.cc:129
ora::OId parse(const std::string &poolToken)
Definition: DbSession.cc:311
bool isTransactional() const
Definition: DbSession.cc:199
static const Version & poolSchemaVersion()
Definition: Version.cc:11
int containerId() const
Definition: OId.cc:50
bool importMapping(const std::string &sourceConnectionString, const std::string &contName)
Definition: DbSession.cc:254
DbSession & operator=(const DbSession &rhs)
Definition: DbSession.cc:154
std::auto_ptr< cond::TechnologyProxy > technologyProxy
Definition: DbSession.cc:127
virtual ~DbSession()
Definition: DbSession.cc:151
std::string storeObject(const T *object, const std::string &containerName)
Definition: DbSession.h:132
static const std::string COND_READER_ROLE
Definition: Auth.h:19
PoolTokenWriter(ora::Database &db)
Definition: DbSession.cc:327
std::string className(const std::string &poolToken)
Definition: DbSession.cc:320
void throwException(std::string const &message, std::string const &methodName)
Definition: Exception.cc:17
Interface for a Streaming Service.
bool isOldSchema()
Definition: DbSession.cc:230
bool createDatabase()
Definition: DbSession.cc:221
coral::ISchema & nominalSchema()
Definition: DbSession.cc:243
void openReadOnly(const std::string &connectionString, const std::string &transactionId)
Definition: DbSession.cc:83
std::string toString() const
Definition: OId.cc:58
int cont
std::string importObject(cond::DbSession &fromDatabase, const std::string &objectId)
Definition: DbSession.cc:282
std::string realClassName()
Definition: Container.cc:79
virtual ~SessionImpl()
Definition: DbSession.cc:54
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
bool isOpen() const
Definition: DbSession.cc:184
volatile std::atomic< bool > shutdown_flag false
std::string const blobStreamingService
Definition: DbSession.cc:128
std::string writeToken(const std::string &containerName, int oid0, int oid1, const std::string &className)
Definition: PoolToken.cc:44
ora::Object getObject(const std::string &objectId)
Definition: DbSession.cc:268
bool fromString(const std::string &s)
Definition: OId.cc:64
void destruct()
Definition: Object.cc:81
void eraseMapping(const std::string &mappingVersion)
ora::Database & storage()
Definition: DbSession.cc:215
SurfaceDeformation * create(int type, const std::vector< double > &params)
T get(const Candidate &c)
Definition: component.h:55
void open(const std::string &connectionString, const std::string &role, bool readOnly)
Definition: DbSession.cc:58
void open(boost::shared_ptr< coral::ISessionProxy > &coralSession, const std::string &connectionString, const std::string &schemaName)
Definition: DbSession.cc:105
int itemId() const
Definition: OId.cc:54
const DbConnection & connection() const
Definition: DbSession.cc:194