CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Session.h
Go to the documentation of this file.
1 #ifndef CondCore_CondDB_Session_h
2 #define CondCore_CondDB_Session_h
3 //
4 // Package: CondDB
5 // Class : Session
6 //
10 //
11 // Author: Giacomo Govi
12 // Created: Apr 2013
13 //
14 
23 //
24 // temporarely
25 #include <boost/shared_ptr.hpp>
26 
27 // TO BE REMOVED AFTER THE TRANSITION
28 namespace coral {
29  class ISessionProxy;
30  class ISchema;
31 }
32 // END TO BE REMOVED
33 
34 namespace cond {
35 
36  namespace persistency {
37 
38  class SessionConfiguration;
39 
40  // transaction class
41  class Transaction {
42  public:
43  explicit Transaction( SessionImpl& session );
44  Transaction( const Transaction& rhs );
45  Transaction& operator=( const Transaction& rhs );
46 
47  void start( bool readOnly=true );
48 
49  void commit();
50 
51  void rollback();
52 
53  bool isActive();
54  private:
56  };
57 
58  // with full value semantics. destruction will close the DB connection.
59  class Session {
60  public:
61  // default constructor
62  Session();
63 
64  // constructor
65  explicit Session( const std::shared_ptr<SessionImpl>& sessionImpl );
66 
67  //
68  Session( const Session& rhs );
69 
70  //
71  virtual ~Session();
72 
73  //
74  Session& operator=( const Session& rhs );
75 
76  //
77  void close();
78 
79  //
81 
82  //
83  bool existsDatabase();
84 
85  //
86  void createDatabase();
87 
88  // read access to the iov sequence.
89  // the iovs are lazy-loaded in groups when required, with repeatable queries ( for FronTier )
90  IOVProxy readIov( const std::string& tag, bool full=false );
91 
92  // read access to the iov sequence.
93  // the iovs are lazy-loaded in groups when required, with repeatable queries ( for FronTier )
94  IOVProxy readIov( const std::string& tag,
95  const boost::posix_time::ptime& snapshottime,
96  bool full=false );
97 
98  //
99  bool existsIov( const std::string& tag );
100 
101  // create a non-existing iov sequence with the specified tag.
102  // the type is required for consistency with the referenced payloads.
103  // fixme: add creation time - required for the migration
104  template <typename T>
105  IOVEditor createIov( const std::string& tag,
106  cond::TimeType timeType,
107  cond::SynchronizationType synchronizationType=cond::SYNCH_ANY );
109  const std::string& tag,
110  cond::TimeType timeType,
111  cond::SynchronizationType synchronizationType=cond::SYNCH_ANY );
112 
113  IOVEditor createIov( const std::string& payloadType,
114  const std::string& tag,
115  cond::TimeType timeType,
116  cond::SynchronizationType synchronizationType,
117  const boost::posix_time::ptime& creationTime );
118 
119  IOVEditor createIovForPayload( const Hash& payloadHash,
120  const std::string& tag, cond::TimeType timeType,
121  cond::SynchronizationType synchronizationType=cond::SYNCH_ANY );
122 
123  void clearIov( const std::string& tag );
124 
125  // update an existing iov sequence with the specified tag.
126  // timeType and payloadType can't be modified.
127  IOVEditor editIov( const std::string& tag );
128 
129  // functions to store a payload in the database. return the identifier of the item in the db.
130  template <typename T> cond::Hash storePayload( const T& payload,
131  const boost::posix_time::ptime& creationTime = boost::posix_time::microsec_clock::universal_time() );
132  template <typename T> boost::shared_ptr<T> fetchPayload( const cond::Hash& payloadHash );
133 
134  // low-level function to access the payload data as a blob. mainly used for the data migration and testing.
135  // the version for ROOT
136  bool fetchPayloadData( const cond::Hash& payloadHash,
137  std::string& payloadType,
138  cond::Binary& payloadData,
139  cond::Binary& streamerInfoData );
140 
141  // internal functions. creates proxies without loading a specific tag.
142  IOVProxy iovProxy();
143 
144  bool existsGlobalTag( const std::string& name );
145 
146  GTEditor createGlobalTag( const std::string& name );
147  GTEditor editGlobalTag( const std::string& name );
148 
149  GTProxy readGlobalTag( const std::string& name );
150  // essentially for the bridge. useless where ORA disappears.
151  GTProxy readGlobalTag( const std::string& name,
152  const std::string& preFix,
153  const std::string& postFix );
154  public:
155 
157 
158  coral::ISessionProxy& coralSession();
159  // TO BE REMOVED in the long term. The new code will use coralSession().
160  coral::ISchema& nominalSchema();
161 
162  private:
163  cond::Hash storePayloadData( const std::string& payloadObjectType,
164  const std::pair<Binary,Binary>& payloadAndStreamerInfoData,
165  const boost::posix_time::ptime& creationTime );
166 
167  private:
168 
169  std::shared_ptr<SessionImpl> m_session;
171  };
172 
173  template <typename T> inline IOVEditor Session::createIov( const std::string& tag, cond::TimeType timeType, cond::SynchronizationType synchronizationType ){
174  return createIov( cond::demangledName( typeid(T) ), tag, timeType, synchronizationType );
175  }
176 
177  template <typename T> inline cond::Hash Session::storePayload( const T& payload, const boost::posix_time::ptime& creationTime ){
178 
179  std::string payloadObjectType = cond::demangledName(typeid(payload));
180  cond::Hash ret;
181  try{
182  ret = storePayloadData( payloadObjectType, serialize( payload ), creationTime );
183  } catch ( const cond::persistency::Exception& e ){
184  std::string em(e.what());
185  throwException( "Payload of type "+payloadObjectType+" could not be stored. "+em,"Session::storePayload");
186  }
187  return ret;
188  }
189 
190  template <typename T> inline boost::shared_ptr<T> Session::fetchPayload( const cond::Hash& payloadHash ){
191  cond::Binary payloadData;
192  cond::Binary streamerInfoData;
194  if(! fetchPayloadData( payloadHash, payloadType, payloadData, streamerInfoData ) )
195  throwException( "Payload with id "+payloadHash+" has not been found in the database.",
196  "Session::fetchPayload" );
197  boost::shared_ptr<T> ret;
198  try{
199  ret = deserialize<T>( payloadType, payloadData, streamerInfoData );
200  } catch ( const cond::persistency::Exception& e ){
201  std::string em(e.what());
202  throwException( "Payload of type "+payloadType+" with id "+payloadHash+" could not be loaded. "+em,"Session::fetchPayload");
203  }
204  return ret;
205  }
206 
208  public:
210 
212 
213  void start( bool readOnly=true );
214 
215  void commit();
216 
217  void close();
218  private:
220  bool m_status;
221 
222  };
223 
224 
225  }
226 }
227 #endif
virtual char const * what() const
Definition: Exception.cc:141
GTEditor editGlobalTag(const std::string &name)
Definition: Session.cc:166
Transaction(SessionImpl &session)
Definition: Session.cc:9
bool existsGlobalTag(const std::string &name)
Definition: Session.cc:153
tuple ret
prodAgent to be discontinued
Base exception class for the object to relational access.
Definition: Exception.h:11
std::pair< Binary, Binary > serialize(const T &payload)
Definition: Serialization.h:54
void clearIov(const std::string &tag)
Definition: Session.cc:148
boost::shared_ptr< T > fetchPayload(const cond::Hash &payloadHash)
Definition: Session.h:190
void start(bool readOnly=true)
Definition: Session.cc:22
IOVEditor createIov(const std::string &tag, cond::TimeType timeType, cond::SynchronizationType synchronizationType=cond::SYNCH_ANY)
Definition: Session.h:173
TransactionScope(Transaction &transaction)
Definition: Session.cc:216
Transaction & transaction()
Definition: Session.cc:66
TimeType
Definition: Time.h:21
std::string demangledName(const std::type_info &typeInfo)
Definition: ClassUtils.cc:159
IOVProxy readIov(const std::string &tag, bool full=false)
Definition: Session.cc:81
Transaction & operator=(const Transaction &rhs)
Definition: Session.cc:17
coral::ISchema & nominalSchema()
Definition: Session.cc:212
Definition: GenABIO.cc:180
void start(bool readOnly=true)
Definition: Session.cc:227
tuple transaction
Definition: idDealer.py:60
std::string Hash
Definition: Types.h:45
bool fetchPayloadData(const cond::Hash &payloadHash, std::string &payloadType, cond::Binary &payloadData, cond::Binary &streamerInfoData)
Definition: Session.cc:195
std::string connectionString()
Definition: Session.cc:203
cond::Hash storePayloadData(const std::string &payloadObjectType, const std::pair< Binary, Binary > &payloadAndStreamerInfoData, const boost::posix_time::ptime &creationTime)
Definition: Session.cc:187
IOVEditor editIov(const std::string &tag)
Definition: Session.cc:141
bool existsIov(const std::string &tag)
Definition: Session.cc:97
cond::Hash storePayload(const T &payload, const boost::posix_time::ptime &creationTime=boost::posix_time::microsec_clock::universal_time())
Definition: Session.h:177
coral::ISessionProxy & coralSession()
Definition: Session.cc:207
SynchronizationType
Definition: Types.h:29
IOVEditor createIovForPayload(const Hash &payloadHash, const std::string &tag, cond::TimeType timeType, cond::SynchronizationType synchronizationType=cond::SYNCH_ANY)
Definition: Session.cc:129
session
Definition: models.py:201
GTEditor createGlobalTag(const std::string &name)
Definition: Session.cc:158
Session & operator=(const Session &rhs)
Definition: Session.cc:56
GTProxy readGlobalTag(const std::string &name)
Definition: Session.cc:173
long double T
Transaction m_transaction
Definition: Session.h:170
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:14
std::shared_ptr< SessionImpl > m_session
Definition: Session.h:169