File indexing completed on 2023-03-17 10:45:49
0001 #ifndef CondCore_CondDB_PayloadProxy_h
0002 #define CondCore_CondDB_PayloadProxy_h
0003
0004 #include "CondCore/CondDB/interface/Exception.h"
0005 #include "CondCore/CondDB/interface/IOVProxy.h"
0006 #include "CondCore/CondDB/interface/Session.h"
0007 #include "CondCore/CondDB/interface/Types.h"
0008
0009 #include <memory>
0010 #include <string>
0011 #include <vector>
0012
0013 namespace cond {
0014
0015 namespace persistency {
0016
0017
0018
0019
0020 class CondGetter {
0021 public:
0022 virtual ~CondGetter() {}
0023 virtual IOVProxy get(std::string name) const = 0;
0024 };
0025
0026
0027 class BasePayloadProxy {
0028 public:
0029
0030 BasePayloadProxy(Iov_t const* mostRecentCurrentIov,
0031 Session const* mostRecentSession,
0032 std::shared_ptr<std::vector<Iov_t>> const* mostRecentRequests);
0033
0034 virtual ~BasePayloadProxy();
0035
0036 virtual void make() = 0;
0037
0038 bool isValid() const;
0039
0040 virtual void loadMore(CondGetter const&) {}
0041
0042 void initializeForNewIOV();
0043
0044 private:
0045 virtual void loadPayload() = 0;
0046
0047 protected:
0048 Iov_t m_iovAtInitialization;
0049 Session m_session;
0050 std::shared_ptr<std::vector<Iov_t>> m_requests;
0051
0052 Iov_t const* m_mostRecentCurrentIov;
0053 Session const* m_mostRecentSession;
0054 std::shared_ptr<std::vector<Iov_t>> const* m_mostRecentRequests;
0055 };
0056
0057
0058
0059
0060 template <typename DataT>
0061 class PayloadProxy : public BasePayloadProxy {
0062 public:
0063 explicit PayloadProxy(Iov_t const* mostRecentCurrentIov,
0064 Session const* mostRecentSession,
0065 std::shared_ptr<std::vector<Iov_t>> const* mostRecentRequests,
0066 const char* source = nullptr)
0067 : BasePayloadProxy(mostRecentCurrentIov, mostRecentSession, mostRecentRequests) {}
0068
0069 ~PayloadProxy() override {}
0070
0071 void initKeyList(PayloadProxy const&) {}
0072
0073
0074 const DataT& operator()() const {
0075 if (!m_data) {
0076 throwException("The Payload has not been loaded.", "PayloadProxy::operator()");
0077 }
0078 return (*m_data);
0079 }
0080
0081 void make() override {
0082 if (isValid()) {
0083 if (m_iovAtInitialization.payloadId == m_currentPayloadId)
0084 return;
0085 m_session.transaction().start(true);
0086 loadPayload();
0087 m_session.transaction().commit();
0088 }
0089 }
0090
0091 protected:
0092 void loadPayload() override {
0093 if (m_iovAtInitialization.payloadId.empty()) {
0094 throwException("Can't load payload: no valid IOV found.", "PayloadProxy::loadPayload");
0095 }
0096 m_data = m_session.fetchPayload<DataT>(m_iovAtInitialization.payloadId);
0097 m_currentPayloadId = m_iovAtInitialization.payloadId;
0098 m_requests->push_back(m_iovAtInitialization);
0099 }
0100
0101 private:
0102 std::unique_ptr<DataT> m_data;
0103 Hash m_currentPayloadId;
0104 };
0105
0106 }
0107 }
0108 #endif