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 close(){
106  transaction.reset();
107  database.reset();
108  isOpen = false;
109  }
110 
111  std::auto_ptr<DbConnection> connection;
112  std::auto_ptr<cond::TechnologyProxy> technologyProxy;
113  std::string const blobStreamingService;
114  std::auto_ptr<ora::Database> database;
115  std::auto_ptr<DbTransaction> transaction;
116  bool isOpen;
117  };
118 
119 }
120 
121 const char* cond::DbSession::COND_SCHEMA_VERSION = "2.0.0";
122 const char* cond::DbSession::CHANGE_SCHEMA_VERSION = "2.0.0";
123 
125  m_implementation( new SessionImpl ){
126 }
127 
129  m_implementation( new SessionImpl ( connection ) ){
130 }
131 
133  m_implementation( rhs.m_implementation ){
134 }
135 
137 }
138 
140  if(this!=&rhs) m_implementation = rhs.m_implementation;
141  return *this;
142 }
143 
144 void cond::DbSession::open( const std::string& connectionString, bool readOnly )
145 {
146  std::string emptyRole("");
147  m_implementation->open( connectionString, emptyRole, readOnly );
148 }
149 
150 void cond::DbSession::open( const std::string& connectionString, const std::string& asRole, bool readOnly )
151 {
152  m_implementation->open( connectionString, asRole, readOnly );
153 }
154 
155 void cond::DbSession::openReadOnly( const std::string& connectionString, const std::string& id )
156 {
157  m_implementation->openReadOnly( connectionString, id );
158 }
159 
161 {
162  m_implementation->close();
163 }
164 
166  return m_implementation->isOpen;
167 }
168 
169 const std::string& cond::DbSession::connectionString() const {
170  if(!m_implementation->database.get())
171  throw cond::Exception("DbSession::connectionString: cannot get connection string. Session has not been open.");
172  return m_implementation->database->connectionString();
173 }
174 
176  return *(m_implementation->connection);
177 }
178 
179 
181  return m_implementation->technologyProxy->isTransactional();
182 }
183 
184 const std::string& cond::DbSession::blobStreamingService() const
185 {
186  return m_implementation->blobStreamingService;
187 }
188 
190 {
191  if(!m_implementation->connection.get() || !m_implementation->connection->isOpen())
192  throw cond::Exception("DbSession::transaction: cannot open transaction. Underlying connection is closed.");
193  if(!m_implementation->transaction.get())
194  throw cond::Exception("DbSession::transaction: cannot get transaction. Session has not been open.");
195  return *m_implementation->transaction;
196 }
197 
199  if(!m_implementation->connection.get() || !m_implementation->connection->isOpen())
200  throw cond::Exception("DbSession::storage: cannot access the storage. Underlying connection is closed.");
201  if(!m_implementation->database.get())
202  throw cond::Exception("DbSession::storage: cannot access the database. Session has not been open.");
203  return *m_implementation->database;
204 }
205 
207  bool created = false;
208  if ( !storage().exists() ){
209  created = true;
210  storage().create( std::string(COND_SCHEMA_VERSION) );
211  }
212  return created;
213 }
214 
216 {
217  ora::Version dbVer = storage().schemaVersion();
218  if (dbVer == ora::Version::poolSchemaVersion()) return true;
219  dbVer = storage().schemaVersion( true );
220  return dbVer < ora::Version::fromString( std::string( CHANGE_SCHEMA_VERSION ) );
221 }
222 
223 coral::ISchema& cond::DbSession::schema( const std::string& schemaName )
224 {
225  return storage().storageAccessSession().get().schema( schemaName );
226 }
227 
229 {
230  return storage().storageAccessSession().get().nominalSchema();
231 }
232 
233 bool cond::DbSession::deleteMapping( const std::string& mappingVersion ){
234  ora::DatabaseUtility utility = storage().utility();
235  utility.eraseMapping( mappingVersion );
236  return true;
237 }
238 
239 bool cond::DbSession::importMapping( const std::string& sourceConnectionString,
240  const std::string& contName ){
241  ora::DatabaseUtility utility = storage().utility();
242  std::auto_ptr<cond::TechnologyProxy> technologyProxy = buildTechnologyProxy(sourceConnectionString, *(m_implementation->connection));
243  utility.importContainerSchema( (*technologyProxy).getRealConnectString( sourceConnectionString ), contName );
244  return true;
245 }
246 
247 std::string cond::DbSession::storeObject( const ora::Object& object, const std::string& containerName ){
248  ora::OId oid = storage().insertItem( containerName, object );
249  storage().flush();
250  return oid.toString();
251 }
252 
253 ora::Object cond::DbSession::getObject( const std::string& objectId ){
254  ora::OId oid;
255  oid.fromString( objectId );
256  return storage().fetchItem( oid );
257 }
258 
259 bool cond::DbSession::deleteObject( const std::string& objectId ){
260  ora::OId oid;
261  oid.fromString( objectId );
262  storage().erase( oid );
263  storage().flush();
264  return true;
265 }
266 
267 std::string cond::DbSession::importObject( cond::DbSession& fromDatabase, const std::string& objectId ){
268  ora::OId oid;
269  oid.fromString( objectId );
270  ora::Object data = fromDatabase.getObject( objectId );
271  ora::Container cont = fromDatabase.storage().containerHandle( oid.containerId() );
272  std::string ret = storeObject( data, cont.name() );
273  data.destruct();
274  return ret;
275 }
276 
277 std::string cond::DbSession::classNameForItem( const std::string& objectId ){
278  ora::OId oid;
279  oid.fromString( objectId );
280  std::string ret("");
281  if( !oid.isInvalid() ){
282  ora::Container cont = storage().containerHandle( oid.containerId() );
283  ret = cont.className();
284  }
285  return ret;
286 }
287 
289  storage().flush();
290 }
291 
293  m_db( db ){
294 }
295 
296 ora::OId cond::PoolTokenParser::parse( const std::string& poolToken ){
297  std::pair<std::string,int> oidData = parseToken( poolToken );
298  if( oidData.first.empty() ){
299  throwException("Could not resolve Container name from token=\""+poolToken+"\".","PoolTokenParser::parse");
300  }
301  ora::Container cont = m_db.containerHandle( oidData.first );
302  return ora::OId( cont.id(), oidData.second );
303 }
304 
305 std::string cond::PoolTokenParser::className( const std::string& oraToken ){
306  ora::OId oid;
307  oid.fromString( oraToken );
308  ora::Container cont = m_db.containerHandle( oid.containerId() );
309  return cont.className();
310 }
311 
313  m_db( db ){
314 }
315 
316 std::string cond::PoolTokenWriter::write( const ora::OId& oid ){
317  ora::Container cont = m_db.containerHandle( oid.containerId() );
318  return writeToken( cont.name(), oid.containerId(), oid.itemId(), cont.className() );
319 }
PoolTokenParser(ora::Database &db)
Definition: DbSession.cc:292
boost::shared_ptr< SessionImpl > m_implementation
Definition: DbSession.h:100
const std::string & connectionString() const
Definition: DbSession.cc:169
std::string write(const ora::OId &oid)
Definition: DbSession.cc:316
bool deleteObject(const std::string &objectId)
Definition: DbSession.cc:259
std::string classNameForItem(const std::string &objectId)
Definition: DbSession.cc:277
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:24
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:189
void open(const std::string &connectionString, bool readOnly=false)
Definition: DbSession.cc:144
const std::string & blobStreamingService() const
Definition: DbSession.cc:184
const std::string & name()
Definition: Container.cc:71
void openReadOnly(const std::string &connectionString, const std::string &id)
Definition: DbSession.cc:155
tuple db
Definition: EcalCondDB.py:151
Definition: OId.h:8
std::auto_ptr< DbConnection > connection
Definition: DbSession.cc:111
bool deleteMapping(const std::string &mappingVersion)
Definition: DbSession.cc:233
static std::string automaticContainerCreation()
coral::ISchema & schema(const std::string &schemaName)
Definition: DbSession.cc:223
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:115
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:272
std::auto_ptr< ora::Database > database
Definition: DbSession.cc:114
ora::OId parse(const std::string &poolToken)
Definition: DbSession.cc:296
bool isTransactional() const
Definition: DbSession.cc:180
int containerId() const
Definition: OId.cc:50
bool importMapping(const std::string &sourceConnectionString, const std::string &contName)
Definition: DbSession.cc:239
DbSession & operator=(const DbSession &rhs)
Definition: DbSession.cc:139
std::auto_ptr< cond::TechnologyProxy > technologyProxy
Definition: DbSession.cc:112
virtual ~DbSession()
Definition: DbSession.cc:136
std::string storeObject(const T *object, const std::string &containerName)
Definition: DbSession.h:131
static const std::string COND_READER_ROLE
Definition: Auth.h:19
PoolTokenWriter(ora::Database &db)
Definition: DbSession.cc:312
std::string className(const std::string &poolToken)
Definition: DbSession.cc:305
void throwException(std::string const &message, std::string const &methodName)
Definition: Exception.cc:17
Interface for a Streaming Service.
bool isOldSchema()
Definition: DbSession.cc:215
bool createDatabase()
Definition: DbSession.cc:206
coral::ISchema & nominalSchema()
Definition: DbSession.cc:228
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:267
virtual ~SessionImpl()
Definition: DbSession.cc:54
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
static Version & poolSchemaVersion()
Definition: Version.cc:11
bool isOpen() const
Definition: DbSession.cc:165
std::string const blobStreamingService
Definition: DbSession.cc:113
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:253
bool fromString(const std::string &s)
Definition: OId.cc:64
void destruct()
Definition: Object.cc:63
void eraseMapping(const std::string &mappingVersion)
ora::Database & storage()
Definition: DbSession.cc:198
SurfaceDeformation * create(int type, const std::vector< double > &params)
T get(const Candidate &c)
Definition: component.h:56
void open(const std::string &connectionString, const std::string &role, bool readOnly)
Definition: DbSession.cc:58
int itemId() const
Definition: OId.cc:54
const DbConnection & connection() const
Definition: DbSession.cc:175