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 
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  //
84  Transaction& transaction();
85 
86  //
87  bool existsDatabase();
88 
89  //
90  void createDatabase();
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, bool full = false);
95 
96  // read access to the iov sequence.
97  // the iovs are lazy-loaded in groups when required, with repeatable queries ( for FronTier )
98  IOVProxy readIov(const std::string& tag, const boost::posix_time::ptime& snapshottime, bool full = false);
99 
100  //
101  bool existsIov(const std::string& tag);
102 
103  //
104  bool getTagInfo(const std::string& tag, cond::Tag_t& info);
105 
106  // retrieves an IOV range. Peforms a query at every call.
107  bool getIovRange(const std::string& tag,
110  std::vector<std::tuple<cond::Time_t, cond::Hash> >& range);
111 
112  // create a non-existing iov sequence with the specified tag.
113  // the type is required for consistency with the referenced payloads.
114  // fixme: add creation time - required for the migration
115  template <typename T>
116  IOVEditor createIov(const std::string& tag,
117  cond::TimeType timeType,
118  cond::SynchronizationType synchronizationType = cond::SYNCH_ANY);
119  IOVEditor createIov(const std::string& payloadType,
120  const std::string& tag,
121  cond::TimeType timeType,
122  cond::SynchronizationType synchronizationType = cond::SYNCH_ANY);
123 
124  IOVEditor createIov(const std::string& payloadType,
125  const std::string& tag,
126  cond::TimeType timeType,
127  cond::SynchronizationType synchronizationType,
128  const boost::posix_time::ptime& creationTime);
129 
130  IOVEditor createIovForPayload(const Hash& payloadHash,
131  const std::string& tag,
132  cond::TimeType timeType,
133  cond::SynchronizationType synchronizationType = cond::SYNCH_ANY);
134 
135  void clearIov(const std::string& tag);
136 
137  // update an existing iov sequence with the specified tag.
138  // timeType and payloadType can't be modified.
139  IOVEditor editIov(const std::string& tag);
140 
141  // functions to store a payload in the database. return the identifier of the item in the db.
142  template <typename T>
143  cond::Hash storePayload(
144  const T& payload,
145  const boost::posix_time::ptime& creationTime = boost::posix_time::microsec_clock::universal_time());
146 
147  template <typename T>
148  std::unique_ptr<T> fetchPayload(const cond::Hash& payloadHash);
149 
150  cond::Hash storePayloadData(const std::string& payloadObjectType,
151  const std::pair<Binary, Binary>& payloadAndStreamerInfoData,
152  const boost::posix_time::ptime& creationTime);
153 
154  bool fetchPayloadData(const cond::Hash& payloadHash,
155  std::string& payloadType,
156  cond::Binary& payloadData,
157  cond::Binary& streamerInfoData);
158 
159  // internal functions. creates proxies without loading a specific tag.
160  IOVProxy iovProxy();
161 
162  bool existsGlobalTag(const std::string& name);
163 
164  GTEditor createGlobalTag(const std::string& name);
165  GTEditor editGlobalTag(const std::string& name);
166 
167  GTProxy readGlobalTag(const std::string& name);
168  // essentially for the bridge. useless where ORA disappears.
169  GTProxy readGlobalTag(const std::string& name, const std::string& preFix, const std::string& postFix);
170 
171  // runinfo read only access
173 
174  // runinfo write access
175  RunInfoEditor editRunInfo();
176 
177  public:
179 
180  coral::ISessionProxy& coralSession();
181  // TO BE REMOVED in the long term. The new code will use coralSession().
182  coral::ISchema& nominalSchema();
183 
184  private:
185  std::shared_ptr<SessionImpl> m_session;
187  };
188 
189  template <typename T>
190  inline IOVEditor Session::createIov(const std::string& tag,
191  cond::TimeType timeType,
192  cond::SynchronizationType synchronizationType) {
193  return createIov(cond::demangledName(typeid(T)), tag, timeType, synchronizationType);
194  }
195 
196  template <typename T>
197  inline cond::Hash Session::storePayload(const T& payload, const boost::posix_time::ptime& creationTime) {
198  std::string payloadObjectType = cond::demangledName(typeid(payload));
199  cond::Hash ret;
200  try {
201  ret = storePayloadData(payloadObjectType, serialize(payload), creationTime);
202  } catch (const cond::persistency::Exception& e) {
203  std::string em(e.what());
204  throwException("Payload of type " + payloadObjectType + " could not be stored. " + em, "Session::storePayload");
205  }
206  return ret;
207  }
208 
209  template <>
210  inline cond::Hash Session::storePayload<std::string>(const std::string& payload,
211  const boost::posix_time::ptime& creationTime) {
212  std::string payloadObjectType("std::string");
213  cond::Hash ret;
214  try {
215  ret = storePayloadData(payloadObjectType, serialize(payload), creationTime);
216  } catch (const cond::persistency::Exception& e) {
217  std::string em(e.what());
218  throwException("Payload of type " + payloadObjectType + " could not be stored. " + em, "Session::storePayload");
219  }
220  return ret;
221  }
222 
223  template <typename T>
224  inline std::unique_ptr<T> Session::fetchPayload(const cond::Hash& payloadHash) {
225  cond::Binary payloadData;
226  cond::Binary streamerInfoData;
228  if (!fetchPayloadData(payloadHash, payloadType, payloadData, streamerInfoData))
229  throwException("Payload with id " + payloadHash + " has not been found in the database.",
230  "Session::fetchPayload");
231  std::unique_ptr<T> ret;
232  try {
233  ret = deserialize<T>(payloadType, payloadData, streamerInfoData);
234  } catch (const cond::persistency::Exception& e) {
235  std::string em(e.what());
236  throwException("Payload of type " + payloadType + " with id " + payloadHash + " could not be loaded. " + em,
237  "Session::fetchPayload");
238  }
239  return ret;
240  }
241 
243  public:
244  explicit TransactionScope(Transaction& transaction);
245 
246  ~TransactionScope();
247 
248  void start(bool readOnly = true);
249 
250  void commit();
251 
252  void close();
253 
254  private:
256  bool m_status;
257  };
258 
259  } // namespace persistency
260 } // namespace cond
261 #endif
Definition: start.py:1
static const TGPicture * info(bool iBackgroundIsBlack)
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
ret
prodAgent to be discontinued
char const * what() const override
Definition: Exception.cc:103
TimeType
Definition: Time.h:19
unsigned long long Time_t
Definition: Time.h:14
Definition: GenABIO.cc:168
std::string Hash
Definition: Types.h:43
Definition: Binary.h:9
#define end
Definition: vmac.h:39
Definition: plugin.cc:23
#define begin
Definition: vmac.h:32
SynchronizationType
Definition: Types.h:27
long double T
Transaction m_transaction
Definition: Session.h:186
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:12
std::shared_ptr< SessionImpl > m_session
Definition: Session.h:185