Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:28

0001 #include "CondCore/CondDB/interface/Session.h"
0002 #include "SessionImpl.h"
0003 //
0004 
0005 namespace cond {
0006 
0007   namespace persistency {
0008 
0009     Transaction::Transaction(SessionImpl& session) : m_session(&session) {}
0010 
0011     Transaction::Transaction(const Transaction& rhs) : m_session(rhs.m_session) {}
0012 
0013     Transaction& Transaction::operator=(const Transaction& rhs) {
0014       m_session = rhs.m_session;
0015       return *this;
0016     }
0017 
0018     void Transaction::start(bool readOnly) {
0019       if (m_session)
0020         m_session->startTransaction(readOnly);
0021     }
0022 
0023     void Transaction::commit() {
0024       if (m_session)
0025         m_session->commitTransaction();
0026     }
0027 
0028     void Transaction::rollback() {
0029       if (m_session)
0030         m_session->rollbackTransaction();
0031     }
0032 
0033     bool Transaction::isActive() { return m_session ? m_session->isTransactionActive() : false; }
0034 
0035     Session::Session() : m_session(new SessionImpl), m_transaction(*m_session) {}
0036 
0037     Session::Session(const std::shared_ptr<SessionImpl>& sessionImpl)
0038         : m_session(sessionImpl), m_transaction(*m_session) {}
0039 
0040     Session::Session(const Session& rhs) : m_session(rhs.m_session), m_transaction(rhs.m_transaction) {}
0041 
0042     Session::~Session() {}
0043 
0044     Session& Session::operator=(const Session& rhs) {
0045       m_session = rhs.m_session;
0046       m_transaction = rhs.m_transaction;
0047       return *this;
0048     }
0049 
0050     void Session::close() { m_session->close(); }
0051 
0052     Transaction& Session::transaction() { return m_transaction; }
0053 
0054     //
0055     bool Session::existsDatabase() {
0056       m_session->openIovDb(SessionImpl::DO_NOT_THROW);
0057       return m_session->transaction->iovDbExists;
0058     }
0059 
0060     //
0061     void Session::createDatabase() { m_session->openDb(); }
0062 
0063     IOVProxy Session::readIov(const std::string& tagName) {
0064       m_session->openIovDb();
0065       IOVProxy proxy(m_session);
0066       proxy.load(tagName);
0067       return proxy;
0068     }
0069 
0070     IOVProxy Session::readIov(const std::string& tagName, const boost::posix_time::ptime& snapshottime) {
0071       m_session->openIovDb();
0072       IOVProxy proxy(m_session);
0073       proxy.load(tagName, snapshottime);
0074       return proxy;
0075     }
0076 
0077     bool Session::existsIov(const std::string& tag) {
0078       m_session->openIovDb();
0079       return m_session->iovSchema().tagTable().select(tag);
0080     }
0081 
0082     //bool Session::getIovRange(const std::string& tag,
0083     //                          cond::Time_t begin,
0084     //                          cond::Time_t end,
0085     //                          std::vector<std::tuple<cond::Time_t, cond::Hash> >& range) {
0086     //  m_session->openIovDb();
0087     //  boost::posix_time::ptime snapshotTime;
0088     //  return m_session->iovSchema().iovTable().getRange(tag, begin, end, snapshotTime, range);
0089     //}
0090 
0091     IOVEditor Session::createIov(const std::string& payloadType,
0092                                  const std::string& tag,
0093                                  cond::TimeType timeType,
0094                                  cond::SynchronizationType synchronizationType) {
0095       m_session->openDb();
0096       if (m_session->iovSchema().tagTable().select(tag))
0097         throwException("The specified tag \"" + tag + "\" already exist in the database.", "Session::createIov");
0098       IOVEditor editor(m_session, tag, timeType, payloadType, synchronizationType);
0099       return editor;
0100     }
0101 
0102     IOVEditor Session::createIov(const std::string& payloadType,
0103                                  const std::string& tag,
0104                                  cond::TimeType timeType,
0105                                  cond::SynchronizationType synchronizationType,
0106                                  const boost::posix_time::ptime& creationTime) {
0107       m_session->openDb();
0108       if (m_session->iovSchema().tagTable().select(tag))
0109         throwException("The specified tag \"" + tag + "\" already exist in the database.", "Session::createIov");
0110       IOVEditor editor(m_session, tag, timeType, payloadType, synchronizationType, creationTime);
0111       return editor;
0112     }
0113 
0114     IOVEditor Session::createIovForPayload(const Hash& payloadHash,
0115                                            const std::string& tag,
0116                                            cond::TimeType timeType,
0117                                            cond::SynchronizationType synchronizationType) {
0118       m_session->openDb();
0119       if (m_session->iovSchema().tagTable().select(tag))
0120         throwException("The specified tag \"" + tag + "\" already exist in the database.",
0121                        "Session::createIovForPayload");
0122       std::string payloadType("");
0123       if (!m_session->iovSchema().payloadTable().getType(payloadHash, payloadType))
0124         throwException("The specified payloadId \"" + payloadHash + "\" does not exist in the database.",
0125                        "Session::createIovForPayload");
0126       IOVEditor editor(m_session, tag, timeType, payloadType, synchronizationType);
0127       return editor;
0128     }
0129 
0130     IOVEditor Session::editIov(const std::string& tag) {
0131       m_session->openIovDb();
0132       IOVEditor editor(m_session);
0133       editor.load(tag);
0134       return editor;
0135     }
0136 
0137     void Session::clearIov(const std::string& tag) {
0138       m_session->openIovDb();
0139       m_session->iovSchema().iovTable().erase(tag);
0140     }
0141 
0142     bool Session::existsGlobalTag(const std::string& name) {
0143       m_session->openGTDb();
0144       return m_session->gtSchema().gtTable().select(name);
0145     }
0146 
0147     GTEditor Session::createGlobalTag(const std::string& name) {
0148       m_session->openGTDb();
0149       if (m_session->gtSchema().gtTable().select(name))
0150         throwException("The specified Global Tag \"" + name + "\" already exist in the database.",
0151                        "Session::createGlobalTag");
0152       GTEditor editor(m_session, name);
0153       return editor;
0154     }
0155 
0156     GTEditor Session::editGlobalTag(const std::string& name) {
0157       m_session->openGTDb();
0158       GTEditor editor(m_session);
0159       editor.load(name);
0160       return editor;
0161     }
0162 
0163     GTProxy Session::readGlobalTag(const std::string& name) {
0164       m_session->openGTDb();
0165       GTProxy proxy(m_session);
0166       proxy.load(name);
0167       return proxy;
0168     }
0169 
0170     GTProxy Session::readGlobalTag(const std::string& name, const std::string& preFix, const std::string& postFix) {
0171       m_session->openGTDb();
0172       GTProxy proxy(m_session);
0173       proxy.load(name, preFix, postFix);
0174       return proxy;
0175     }
0176 
0177     cond::Hash Session::storePayloadData(const std::string& payloadObjectType,
0178                                          const std::pair<Binary, Binary>& payloadAndStreamerInfoData,
0179                                          const boost::posix_time::ptime& creationTime) {
0180       m_session->openDb();
0181       return m_session->iovSchema().payloadTable().insertIfNew(
0182           payloadObjectType, payloadAndStreamerInfoData.first, payloadAndStreamerInfoData.second, creationTime);
0183     }
0184 
0185     bool Session::fetchPayloadData(const cond::Hash& payloadHash,
0186                                    std::string& payloadType,
0187                                    cond::Binary& payloadData,
0188                                    cond::Binary& streamerInfoData) {
0189       m_session->openIovDb();
0190       return m_session->iovSchema().payloadTable().select(payloadHash, payloadType, payloadData, streamerInfoData);
0191     }
0192 
0193     RunInfoProxy Session::getRunInfo(cond::Time_t start, cond::Time_t end) {
0194       if (!m_session->transaction.get())
0195         throwException("The transaction is not active.", "Session::getRunInfo");
0196       RunInfoProxy proxy(m_session);
0197       proxy.load(start, end);
0198       return proxy;
0199     }
0200 
0201     cond::RunInfo_t Session::getLastRun() {
0202       if (!m_session->transaction.get())
0203         throwException("The transaction is not active.", "Session::getRunInfo");
0204       m_session->openRunInfoDb();
0205       cond::RunInfo_t ret;
0206       ret.run = m_session->runInfoSchema().runInfoTable().getLastInserted(ret.start, ret.end);
0207       return ret;
0208     }
0209 
0210     RunInfoEditor Session::editRunInfo() {
0211       RunInfoEditor editor(m_session);
0212       editor.init();
0213       return editor;
0214     }
0215 
0216     std::string Session::connectionString() { return m_session->connectionString; }
0217 
0218     coral::ISessionProxy& Session::coralSession() {
0219       if (!m_session->coralSession.get())
0220         throwException("The session is not active.", "Session::coralSession");
0221       return *m_session->coralSession;
0222     }
0223 
0224     coral::ISchema& Session::nominalSchema() { return coralSession().nominalSchema(); }
0225 
0226     TransactionScope::TransactionScope(Transaction& transaction) : m_transaction(transaction), m_status(true) {
0227       m_status = !m_transaction.isActive();
0228     }
0229 
0230     TransactionScope::~TransactionScope() {
0231       if (!m_status && m_transaction.isActive()) {
0232         m_transaction.rollback();
0233       }
0234     }
0235 
0236     void TransactionScope::start(bool readOnly) {
0237       m_transaction.start(readOnly);
0238       m_status = false;
0239     }
0240 
0241     void TransactionScope::commit() {
0242       m_transaction.commit();
0243       m_status = true;
0244     }
0245 
0246     void TransactionScope::close() { m_status = true; }
0247 
0248   }  // namespace persistency
0249 }  // namespace cond