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  //
105  bool getTagInfo( const std::string& tag, cond::Tag_t& info );
106 
107  // retrieves an IOV range. Peforms a query at every call.
108  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, cond::TimeType timeType,
132  cond::SynchronizationType synchronizationType=cond::SYNCH_ANY );
133 
134  void clearIov( const std::string& tag );
135 
136  // update an existing iov sequence with the specified tag.
137  // timeType and payloadType can't be modified.
138  IOVEditor editIov( const std::string& tag );
139 
140  // functions to store a payload in the database. return the identifier of the item in the db.
141  template <typename T> cond::Hash storePayload( const T& payload,
142  const boost::posix_time::ptime& creationTime = boost::posix_time::microsec_clock::universal_time() );
143 
144  template <typename T> std::unique_ptr<T> fetchPayload( const cond::Hash& payloadHash );
145 
146  cond::Hash storePayloadData( const std::string& payloadObjectType,
147  const std::pair<Binary,Binary>& payloadAndStreamerInfoData,
148  const boost::posix_time::ptime& creationTime );
149 
150  bool fetchPayloadData( const cond::Hash& payloadHash,
151  std::string& payloadType,
152  cond::Binary& payloadData,
153  cond::Binary& streamerInfoData );
154 
155  // internal functions. creates proxies without loading a specific tag.
156  IOVProxy iovProxy();
157 
158  bool existsGlobalTag( const std::string& name );
159 
160  GTEditor createGlobalTag( const std::string& name );
161  GTEditor editGlobalTag( const std::string& name );
162 
163  GTProxy readGlobalTag( const std::string& name );
164  // essentially for the bridge. useless where ORA disappears.
165  GTProxy readGlobalTag( const std::string& name,
166  const std::string& preFix,
167  const std::string& postFix );
168 
169  // runinfo read only access
171 
172  // runinfo write access
173  RunInfoEditor editRunInfo();
174  public:
175 
176  std::string connectionString();
177 
178  coral::ISessionProxy& coralSession();
179  // TO BE REMOVED in the long term. The new code will use coralSession().
180  coral::ISchema& nominalSchema();
181 
182  private:
183 
184  std::shared_ptr<SessionImpl> m_session;
186  };
187 
188  template <typename T> inline IOVEditor Session::createIov( const std::string& tag, cond::TimeType timeType, cond::SynchronizationType synchronizationType ){
189  return createIov( cond::demangledName( typeid(T) ), tag, timeType, synchronizationType );
190  }
191 
192  template <typename T> inline cond::Hash Session::storePayload( const T& payload, const boost::posix_time::ptime& creationTime ){
193 
194  std::string payloadObjectType = cond::demangledName(typeid(payload));
195  cond::Hash ret;
196  try{
197  ret = storePayloadData( payloadObjectType, serialize( payload ), creationTime );
198  } catch ( const cond::persistency::Exception& e ){
199  std::string em(e.what());
200  throwException( "Payload of type "+payloadObjectType+" could not be stored. "+em,"Session::storePayload");
201  }
202  return ret;
203  }
204 
205  template <> inline cond::Hash Session::storePayload<std::string>( const std::string& payload, const boost::posix_time::ptime& creationTime ){
206 
207  std::string payloadObjectType("std::string");
208  cond::Hash ret;
209  try{
210  ret = storePayloadData( payloadObjectType, serialize( payload ), creationTime );
211  } catch ( const cond::persistency::Exception& e ){
212  std::string em(e.what());
213  throwException( "Payload of type "+payloadObjectType+" could not be stored. "+em,"Session::storePayload");
214  }
215  return ret;
216  }
217 
218  template <typename T> inline std::unique_ptr<T> Session::fetchPayload( const cond::Hash& payloadHash ){
219  cond::Binary payloadData;
220  cond::Binary streamerInfoData;
221  std::string payloadType;
222  if(! fetchPayloadData( payloadHash, payloadType, payloadData, streamerInfoData ) )
223  throwException( "Payload with id "+payloadHash+" has not been found in the database.",
224  "Session::fetchPayload" );
225  std::unique_ptr<T> ret;
226  try{
227  ret = deserialize<T>( payloadType, payloadData, streamerInfoData );
228  } catch ( const cond::persistency::Exception& e ){
229  std::string em(e.what());
230  throwException( "Payload of type "+payloadType+" with id "+payloadHash+" could not be loaded. "+em,"Session::fetchPayload");
231  }
232  return ret;
233  }
234 
236  public:
238 
239  ~TransactionScope();
240 
241  void start( bool readOnly=true );
242 
243  void commit();
244 
245  void close();
246  private:
248  bool m_status;
249 
250  };
251 
252 
253  }
254 }
255 #endif
transaction
Definition: idDealer.py:62
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:62
char const * what() const override
Definition: Exception.cc:103
TimeType
Definition: Time.h:21
unsigned long long Time_t
Definition: Time.h:16
Definition: GenABIO.cc:168
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:185
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:14
std::shared_ptr< SessionImpl > m_session
Definition: Session.h:184