CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 
25 //
26 //#include <vector>
27 //#include <tuple>
28 // temporarely
29 
30 // TO BE REMOVED AFTER THE TRANSITION
31 namespace coral {
32  class ISessionProxy;
33  class ISchema;
34 } // namespace coral
35 // END TO BE REMOVED
36 
37 namespace cond {
38 
39  namespace persistency {
40 
41  class SessionConfiguration;
42 
43  // transaction class
44  class Transaction {
45  public:
46  explicit Transaction(SessionImpl& session);
47  Transaction(const Transaction& rhs);
48  Transaction& operator=(const Transaction& rhs);
49 
50  void start(bool readOnly = true);
51 
52  void commit();
53 
54  void rollback();
55 
56  bool isActive();
57 
58  private:
60  };
61 
62  // with full value semantics. destruction will close the DB connection.
63  class Session {
64  public:
65  // default constructor
66  Session();
67 
68  // constructor
69  explicit Session(const std::shared_ptr<SessionImpl>& sessionImpl);
70 
71  //
72  Session(const Session& rhs);
73 
74  //
75  virtual ~Session();
76 
77  //
78  Session& operator=(const Session& rhs);
79 
80  //
81  void close();
82 
83  //
85 
86  //
87  bool existsDatabase();
88 
89  //
90  void createDatabase();
91 
92  // read access to the iov sequence.
94 
95  // read access to the iov sequence.
96  IOVProxy readIov(const std::string& tag, const boost::posix_time::ptime& snapshottime);
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,
121  cond::TimeType timeType,
122  cond::SynchronizationType synchronizationType = cond::SYNCH_ANY);
123 
124  void clearIov(const std::string& tag);
125 
126  // update an existing iov sequence with the specified tag.
127  // timeType and payloadType can't be modified.
128  IOVEditor editIov(const std::string& tag);
129 
130  // functions to store a payload in the database. return the identifier of the item in the db.
131  template <typename T>
133  const T& payload,
134  const boost::posix_time::ptime& creationTime = boost::posix_time::microsec_clock::universal_time());
135 
136  template <typename T>
137  std::unique_ptr<T> fetchPayload(const cond::Hash& payloadHash);
138 
139  cond::Hash storePayloadData(const std::string& payloadObjectType,
140  const std::pair<Binary, Binary>& payloadAndStreamerInfoData,
141  const boost::posix_time::ptime& creationTime);
142 
143  bool fetchPayloadData(const cond::Hash& payloadHash,
144  std::string& payloadType,
145  cond::Binary& payloadData,
146  cond::Binary& streamerInfoData);
147 
148  bool existsGlobalTag(const std::string& name);
149 
150  GTEditor createGlobalTag(const std::string& name);
151  GTEditor editGlobalTag(const std::string& name);
152 
153  GTProxy readGlobalTag(const std::string& name);
154  // essentially for the bridge. useless where ORA disappears.
155  GTProxy readGlobalTag(const std::string& name, const std::string& preFix, const std::string& postFix);
156 
157  // runinfo read only access
159 
160  // get the ongoing run
162 
163  // runinfo write access
165 
166  public:
168 
169  coral::ISessionProxy& coralSession();
170  // TO BE REMOVED in the long term. The new code will use coralSession().
171  coral::ISchema& nominalSchema();
172 
173  private:
174  std::shared_ptr<SessionImpl> m_session;
176  };
177 
178  template <typename T>
180  cond::TimeType timeType,
181  cond::SynchronizationType synchronizationType) {
182  return createIov(cond::demangledName(typeid(T)), tag, timeType, synchronizationType);
183  }
184 
185  template <typename T>
186  inline cond::Hash Session::storePayload(const T& payload, const boost::posix_time::ptime& creationTime) {
187  std::string payloadObjectType = cond::demangledName(typeid(payload));
188  cond::Hash ret;
189  try {
190  ret = storePayloadData(payloadObjectType, serialize(payload), creationTime);
191  } catch (const cond::persistency::Exception& e) {
192  std::string em(e.what());
193  throwException("Payload of type " + payloadObjectType + " could not be stored. " + em, "Session::storePayload");
194  }
195  return ret;
196  }
197 
198  template <>
199  inline cond::Hash Session::storePayload<std::string>(const std::string& payload,
200  const boost::posix_time::ptime& creationTime) {
201  std::string payloadObjectType("std::string");
202  cond::Hash ret;
203  try {
204  ret = storePayloadData(payloadObjectType, serialize(payload), creationTime);
205  } catch (const cond::persistency::Exception& e) {
206  std::string em(e.what());
207  throwException("Payload of type " + payloadObjectType + " could not be stored. " + em, "Session::storePayload");
208  }
209  return ret;
210  }
211 
212  template <typename T>
213  inline std::unique_ptr<T> Session::fetchPayload(const cond::Hash& payloadHash) {
214  cond::Binary payloadData;
215  cond::Binary streamerInfoData;
217  if (!fetchPayloadData(payloadHash, payloadType, payloadData, streamerInfoData))
218  throwException("Payload with id " + payloadHash + " has not been found in the database.",
219  "Session::fetchPayload");
220  std::unique_ptr<T> ret;
221  try {
222  ret = deserialize<T>(payloadType, payloadData, streamerInfoData);
223  } catch (const cond::persistency::Exception& e) {
224  std::string em(e.what());
225  throwException("Payload of type " + payloadType + " with id " + payloadHash + " could not be loaded. " + em,
226  "Session::fetchPayload");
227  }
228  return ret;
229  }
230 
232  public:
233  explicit TransactionScope(Transaction& transaction);
234 
236 
237  void start(bool readOnly = true);
238 
239  void commit();
240 
241  void close();
242 
243  private:
245  bool m_status;
246  };
247 
248  } // namespace persistency
249 } // namespace cond
250 #endif
GTEditor editGlobalTag(const std::string &name)
Definition: Session.cc:156
Transaction(SessionImpl &session)
Definition: Session.cc:9
bool existsGlobalTag(const std::string &name)
Definition: Session.cc:142
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:66
void clearIov(const std::string &tag)
Definition: Session.cc:137
void start(bool readOnly=true)
Definition: Session.cc:18
RunInfoEditor editRunInfo()
Definition: Session.cc:210
IOVEditor createIov(const std::string &tag, cond::TimeType timeType, cond::SynchronizationType synchronizationType=cond::SYNCH_ANY)
Definition: Session.h:179
std::unique_ptr< T > fetchPayload(const cond::Hash &payloadHash)
Definition: Session.h:213
TransactionScope(Transaction &transaction)
Definition: Session.cc:226
Transaction & transaction()
Definition: Session.cc:52
TimeType
Definition: Time.h:19
unsigned long long Time_t
Definition: Time.h:14
Transaction & operator=(const Transaction &rhs)
Definition: Session.cc:13
coral::ISchema & nominalSchema()
Definition: Session.cc:224
char const * what() const noexceptoverride
Definition: Exception.cc:103
void start(bool readOnly=true)
Definition: Session.cc:236
std::string Hash
Definition: Types.h:43
bool fetchPayloadData(const cond::Hash &payloadHash, std::string &payloadType, cond::Binary &payloadData, cond::Binary &streamerInfoData)
Definition: Session.cc:185
std::string connectionString()
Definition: Session.cc:216
cond::Hash storePayloadData(const std::string &payloadObjectType, const std::pair< Binary, Binary > &payloadAndStreamerInfoData, const boost::posix_time::ptime &creationTime)
Definition: Session.cc:177
IOVProxy readIov(const std::string &tag)
Definition: Session.cc:63
IOVEditor editIov(const std::string &tag)
Definition: Session.cc:130
cond::RunInfo_t getLastRun()
Definition: Session.cc:201
bool existsIov(const std::string &tag)
Definition: Session.cc:77
cond::Hash storePayload(const T &payload, const boost::posix_time::ptime &creationTime=boost::posix_time::microsec_clock::universal_time())
Definition: Session.h:186
coral::ISessionProxy & coralSession()
Definition: Session.cc:218
string end
Definition: dataset.py:937
SynchronizationType
Definition: Types.h:27
IOVEditor createIovForPayload(const Hash &payloadHash, const std::string &tag, cond::TimeType timeType, cond::SynchronizationType synchronizationType=cond::SYNCH_ANY)
Definition: Session.cc:114
GTEditor createGlobalTag(const std::string &name)
Definition: Session.cc:147
Session & operator=(const Session &rhs)
Definition: Session.cc:44
GTProxy readGlobalTag(const std::string &name)
Definition: Session.cc:163
long double T
Transaction m_transaction
Definition: Session.h:175
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:12
std::shared_ptr< SessionImpl > m_session
Definition: Session.h:174
RunInfoProxy getRunInfo(cond::Time_t start, cond::Time_t end)
Definition: Session.cc:193