Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:50:21

0001 #ifndef CondCore_CondDB_Session_h
0002 #define CondCore_CondDB_Session_h
0003 //
0004 // Package:     CondDB
0005 // Class  :     Session
0006 //
0007 /**\class Session Session.h CondCore/CondDB/interface/Session.h
0008    Description: service for accessing conditions in/from DB.  
0009 */
0010 //
0011 // Author:      Giacomo Govi
0012 // Created:     Apr 2013
0013 //
0014 
0015 #include "CondCore/CondDB/interface/IOVProxy.h"
0016 #include "CondCore/CondDB/interface/IOVEditor.h"
0017 #include "CondCore/CondDB/interface/GTProxy.h"
0018 #include "CondCore/CondDB/interface/GTEditor.h"
0019 #include "CondCore/CondDB/interface/RunInfoProxy.h"
0020 #include "CondCore/CondDB/interface/RunInfoEditor.h"
0021 #include "CondCore/CondDB/interface/Binary.h"
0022 #include "CondCore/CondDB/interface/Serialization.h"
0023 #include "CondCore/CondDB/interface/Types.h"
0024 #include "CondCore/CondDB/interface/Utils.h"
0025 //
0026 //#include <vector>
0027 //#include <tuple>
0028 // temporarely
0029 
0030 // TO BE REMOVED AFTER THE TRANSITION
0031 namespace coral {
0032   class ISessionProxy;
0033   class ISchema;
0034 }  // namespace coral
0035 // END TO BE REMOVED
0036 
0037 namespace cond {
0038 
0039   namespace persistency {
0040 
0041     class SessionConfiguration;
0042 
0043     // transaction class
0044     class Transaction {
0045     public:
0046       explicit Transaction(SessionImpl& session);
0047       Transaction(const Transaction& rhs);
0048       Transaction& operator=(const Transaction& rhs);
0049 
0050       void start(bool readOnly = true);
0051 
0052       void commit();
0053 
0054       void rollback();
0055 
0056       bool isActive();
0057 
0058     private:
0059       SessionImpl* m_session;
0060     };
0061 
0062     // with full value semantics. destruction will close the DB connection.
0063     class Session {
0064     public:
0065       // default constructor
0066       Session();
0067 
0068       // constructor
0069       explicit Session(const std::shared_ptr<SessionImpl>& sessionImpl);
0070 
0071       //
0072       Session(const Session& rhs);
0073 
0074       //
0075       virtual ~Session();
0076 
0077       //
0078       Session& operator=(const Session& rhs);
0079 
0080       //
0081       void close();
0082 
0083       //
0084       Transaction& transaction();
0085 
0086       //
0087       bool existsDatabase();
0088 
0089       //
0090       void createDatabase();
0091 
0092       // read access to the iov sequence.
0093       IOVProxy readIov(const std::string& tag);
0094 
0095       // read access to the iov sequence.
0096       IOVProxy readIov(const std::string& tag, const boost::posix_time::ptime& snapshottime);
0097 
0098       //
0099       bool existsIov(const std::string& tag);
0100 
0101       // create a non-existing iov sequence with the specified tag.
0102       // the type is required for consistency with the referenced payloads.
0103       // fixme: add creation time - required for the migration
0104       template <typename T>
0105       IOVEditor createIov(const std::string& tag,
0106                           cond::TimeType timeType,
0107                           cond::SynchronizationType synchronizationType = cond::SYNCH_ANY);
0108       IOVEditor createIov(const std::string& payloadType,
0109                           const std::string& tag,
0110                           cond::TimeType timeType,
0111                           cond::SynchronizationType synchronizationType = cond::SYNCH_ANY);
0112 
0113       IOVEditor createIov(const std::string& payloadType,
0114                           const std::string& tag,
0115                           cond::TimeType timeType,
0116                           cond::SynchronizationType synchronizationType,
0117                           const boost::posix_time::ptime& creationTime);
0118 
0119       IOVEditor createIovForPayload(const Hash& payloadHash,
0120                                     const std::string& tag,
0121                                     cond::TimeType timeType,
0122                                     cond::SynchronizationType synchronizationType = cond::SYNCH_ANY);
0123 
0124       void clearIov(const std::string& tag);
0125 
0126       // update an existing iov sequence with the specified tag.
0127       // timeType and payloadType can't be modified.
0128       IOVEditor editIov(const std::string& tag);
0129 
0130       // functions to store a payload in the database. return the identifier of the item in the db.
0131       template <typename T>
0132       cond::Hash storePayload(
0133           const T& payload,
0134           const boost::posix_time::ptime& creationTime = boost::posix_time::microsec_clock::universal_time());
0135 
0136       template <typename T>
0137       std::unique_ptr<T> fetchPayload(const cond::Hash& payloadHash);
0138 
0139       cond::Hash storePayloadData(const std::string& payloadObjectType,
0140                                   const std::pair<Binary, Binary>& payloadAndStreamerInfoData,
0141                                   const boost::posix_time::ptime& creationTime);
0142 
0143       bool fetchPayloadData(const cond::Hash& payloadHash,
0144                             std::string& payloadType,
0145                             cond::Binary& payloadData,
0146                             cond::Binary& streamerInfoData);
0147 
0148       bool existsGlobalTag(const std::string& name);
0149 
0150       GTEditor createGlobalTag(const std::string& name);
0151       GTEditor editGlobalTag(const std::string& name);
0152 
0153       GTProxy readGlobalTag(const std::string& name);
0154       // essentially for the bridge. useless where ORA disappears.
0155       GTProxy readGlobalTag(const std::string& name, const std::string& preFix, const std::string& postFix);
0156 
0157       // runinfo read only access
0158       RunInfoProxy getRunInfo(cond::Time_t start, cond::Time_t end);
0159 
0160       // get the ongoing run
0161       cond::RunInfo_t getLastRun();
0162 
0163       // runinfo write access
0164       RunInfoEditor editRunInfo();
0165 
0166     public:
0167       std::string connectionString();
0168 
0169       coral::ISessionProxy& coralSession();
0170       // TO BE REMOVED in the long term. The new code will use coralSession().
0171       coral::ISchema& nominalSchema();
0172 
0173     private:
0174       std::shared_ptr<SessionImpl> m_session;
0175       Transaction m_transaction;
0176     };
0177 
0178     template <typename T>
0179     inline IOVEditor Session::createIov(const std::string& tag,
0180                                         cond::TimeType timeType,
0181                                         cond::SynchronizationType synchronizationType) {
0182       return createIov(cond::demangledName(typeid(T)), tag, timeType, synchronizationType);
0183     }
0184 
0185     template <typename T>
0186     inline cond::Hash Session::storePayload(const T& payload, const boost::posix_time::ptime& creationTime) {
0187       std::string payloadObjectType = cond::demangledName(typeid(payload));
0188       cond::Hash ret;
0189       try {
0190         ret = storePayloadData(payloadObjectType, serialize(payload), creationTime);
0191       } catch (const cond::persistency::Exception& e) {
0192         std::string em(e.what());
0193         throwException("Payload of type " + payloadObjectType + " could not be stored. " + em, "Session::storePayload");
0194       }
0195       return ret;
0196     }
0197 
0198     template <>
0199     inline cond::Hash Session::storePayload<std::string>(const std::string& payload,
0200                                                          const boost::posix_time::ptime& creationTime) {
0201       std::string payloadObjectType("std::string");
0202       cond::Hash ret;
0203       try {
0204         ret = storePayloadData(payloadObjectType, serialize(payload), creationTime);
0205       } catch (const cond::persistency::Exception& e) {
0206         std::string em(e.what());
0207         throwException("Payload of type " + payloadObjectType + " could not be stored. " + em, "Session::storePayload");
0208       }
0209       return ret;
0210     }
0211 
0212     template <typename T>
0213     inline std::unique_ptr<T> Session::fetchPayload(const cond::Hash& payloadHash) {
0214       cond::Binary payloadData;
0215       cond::Binary streamerInfoData;
0216       std::string payloadType;
0217       if (!fetchPayloadData(payloadHash, payloadType, payloadData, streamerInfoData))
0218         throwException("Payload with id " + payloadHash + " has not been found in the database.",
0219                        "Session::fetchPayload");
0220       std::unique_ptr<T> ret;
0221       try {
0222         ret = deserialize<T>(payloadType, payloadData, streamerInfoData);
0223       } catch (const cond::persistency::Exception& e) {
0224         std::string em(e.what());
0225         throwException("Payload of type " + payloadType + " with id " + payloadHash + " could not be loaded. " + em,
0226                        "Session::fetchPayload");
0227       }
0228       return ret;
0229     }
0230 
0231     class TransactionScope {
0232     public:
0233       explicit TransactionScope(Transaction& transaction);
0234 
0235       ~TransactionScope();
0236 
0237       void start(bool readOnly = true);
0238 
0239       void commit();
0240 
0241       void close();
0242 
0243     private:
0244       Transaction& m_transaction;
0245       bool m_status;
0246     };
0247 
0248   }  // namespace persistency
0249 }  // namespace cond
0250 #endif