CMS 3D CMS Logo

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