Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-01-16 23:25:22

0001 #ifndef CondCore_ESSources_DataProxy_H
0002 #define CondCore_ESSources_DataProxy_H
0003 
0004 #include <cassert>
0005 //#include <iostream>
0006 #include <memory>
0007 #include <string>
0008 #include <mutex>
0009 
0010 // user include files
0011 #include "FWCore/Framework/interface/ESSourceDataProxyTemplate.h"
0012 #include "FWCore/Framework/interface/DataKey.h"
0013 
0014 #include "CondCore/CondDB/interface/IOVProxy.h"
0015 #include "CondCore/CondDB/interface/PayloadProxy.h"
0016 #include "CondCore/CondDB/interface/Time.h"
0017 #include "CondCore/CondDB/interface/Types.h"
0018 
0019 // expose a cond::PayloadProxy as a eventsetup::DataProxy
0020 namespace cond {
0021   template <typename DataT>
0022   struct DefaultInitializer {
0023     void operator()(DataT&) {}
0024   };
0025 }  // namespace cond
0026 
0027 template <class RecordT, class DataT, typename Initializer = cond::DefaultInitializer<DataT>>
0028 class DataProxy : public edm::eventsetup::ESSourceDataProxyTemplate<DataT> {
0029 public:
0030   explicit DataProxy(std::shared_ptr<cond::persistency::PayloadProxy<DataT>> pdata,
0031                      edm::SerialTaskQueue* iQueue,
0032                      std::mutex* iMutex)
0033       : edm::eventsetup::ESSourceDataProxyTemplate<DataT>(iQueue, iMutex), m_data{pdata} {}
0034   //DataProxy(); // stop default
0035   const DataProxy& operator=(const DataProxy&) = delete;  // stop default
0036 
0037   // ---------- const member functions ---------------------
0038 
0039   // ---------- static member functions --------------------
0040 
0041   // ---------- member functions ---------------------------
0042 protected:
0043   void prefetch(edm::eventsetup::DataKey const& iKey, edm::EventSetupRecordDetails) final {
0044     m_data->make();
0045     m_initializer(const_cast<DataT&>((*m_data)()));
0046   }
0047 
0048   DataT const* fetch() const final { return &(*m_data)(); }
0049 
0050 private:
0051   void initializeForNewIOV() override { m_data->initializeForNewIOV(); }
0052 
0053   // ---------- member data --------------------------------
0054 
0055   std::shared_ptr<cond::persistency::PayloadProxy<DataT>> m_data;
0056   Initializer m_initializer;
0057 };
0058 
0059 namespace cond {
0060 
0061   /* ABI bridging between the cond world and eventsetup world
0062    * keep them separated!
0063    */
0064   class DataProxyWrapperBase {
0065   public:
0066     typedef std::shared_ptr<cond::persistency::BasePayloadProxy> ProxyP;
0067     typedef std::shared_ptr<edm::eventsetup::DataProxy> edmProxyP;
0068 
0069     // limitation of plugin manager...
0070     typedef std::pair<std::string, std::string> Args;
0071 
0072     virtual edm::eventsetup::TypeTag type() const = 0;
0073     virtual ProxyP proxy(unsigned int iovIndex) const = 0;
0074     virtual edmProxyP edmProxy(unsigned int iovIndex) const = 0;
0075 
0076     DataProxyWrapperBase();
0077     // late initialize (to allow to load ALL library first)
0078     virtual void lateInit(persistency::Session& session,
0079                           const std::string& tag,
0080                           const boost::posix_time::ptime& snapshotTime,
0081                           std::string const& il,
0082                           std::string const& cs,
0083                           edm::SerialTaskQueue* queue,
0084                           std::mutex* mutex) = 0;
0085 
0086     virtual void initConcurrentIOVs(unsigned int nConcurrentIOVs) = 0;
0087 
0088     void addInfo(std::string const& il, std::string const& cs, std::string const& tag);
0089 
0090     virtual ~DataProxyWrapperBase();
0091 
0092     std::string const& label() const { return m_label; }
0093     std::string const& connString() const { return m_connString; }
0094     std::string const& tag() const { return m_tag; }
0095     persistency::IOVProxy& iovProxy() { return m_iovProxy; }
0096     persistency::IOVProxy const& iovProxy() const { return m_iovProxy; }
0097     Iov_t const& currentIov() const { return m_currentIov; }
0098     persistency::Session& session() { return m_session; }
0099     persistency::Session const& session() const { return m_session; }
0100     std::shared_ptr<std::vector<Iov_t>> const& requests() const { return m_requests; }
0101 
0102     void setSession(persistency::Session const& v) { m_session = v; }
0103 
0104     void loadTag(std::string const& tag);
0105     void loadTag(std::string const& tag, boost::posix_time::ptime const& snapshotTime);
0106     void reload();
0107 
0108     ValidityInterval setIntervalFor(Time_t target);
0109     TimeType timeType() const { return m_iovProxy.tagInfo().timeType; }
0110 
0111   private:
0112     std::string m_label;
0113     std::string m_connString;
0114     std::string m_tag;
0115     persistency::IOVProxy m_iovProxy;
0116     Iov_t m_currentIov;
0117     persistency::Session m_session;
0118     std::shared_ptr<std::vector<Iov_t>> m_requests;
0119   };
0120 }  // namespace cond
0121 
0122 /* bridge between the cond world and eventsetup world
0123  * keep them separated!
0124  */
0125 template <class RecordT, class DataT, typename Initializer = cond::DefaultInitializer<DataT>>
0126 class DataProxyWrapper : public cond::DataProxyWrapperBase {
0127 public:
0128   typedef ::DataProxy<RecordT, DataT, Initializer> DataProxy;
0129 
0130   // constructor from plugin...
0131   explicit DataProxyWrapper(const char* source = nullptr) : m_source(source ? source : "") {
0132     //NOTE: We do this so that the type 'DataT' will get registered
0133     // when the plugin is dynamically loaded
0134     m_type = edm::eventsetup::DataKey::makeTypeTag<DataT>();
0135   }
0136 
0137   // late initialize (to allow to load ALL library first)
0138   void lateInit(cond::persistency::Session& iSession,
0139                 const std::string& tag,
0140                 const boost::posix_time::ptime& snapshotTime,
0141                 std::string const& il,
0142                 std::string const& cs,
0143                 edm::SerialTaskQueue* queue,
0144                 std::mutex* mutex) override {
0145     setSession(iSession);
0146     // set the IOVProxy
0147     loadTag(tag, snapshotTime);
0148     // Only make the first PayloadProxy object now because we don't know yet
0149     // how many we will need.
0150     m_proxies.push_back(std::make_shared<cond::persistency::PayloadProxy<DataT>>(
0151         &currentIov(), &session(), &requests(), m_source.empty() ? (const char*)nullptr : m_source.c_str()));
0152     m_edmProxies.push_back(std::make_shared<DataProxy>(m_proxies[0], queue, mutex));
0153     addInfo(il, cs, tag);
0154   }
0155 
0156   void initConcurrentIOVs(unsigned int nConcurrentIOVs) override {
0157     // Create additional PayloadProxy objects if we are allowing
0158     // multiple IOVs to run concurrently.
0159     if (m_proxies.size() != nConcurrentIOVs) {
0160       assert(m_proxies.size() == 1);
0161       auto queue = m_edmProxies.front()->queue();
0162       auto mutex = m_edmProxies.front()->mutex();
0163       for (unsigned int i = 1; i < nConcurrentIOVs; ++i) {
0164         m_proxies.push_back(std::make_shared<cond::persistency::PayloadProxy<DataT>>(
0165             &currentIov(), &session(), &requests(), m_source.empty() ? (const char*)nullptr : m_source.c_str()));
0166         m_edmProxies.push_back(std::make_shared<DataProxy>(m_proxies[i], queue, mutex));
0167         // This does nothing except in the special case of a KeyList PayloadProxy.
0168         // They all need to have copies of the same IOVProxy object.
0169         m_proxies[i]->initKeyList(*m_proxies[0]);
0170       }
0171       assert(m_proxies.size() == nConcurrentIOVs);
0172     }
0173     assert(m_proxies.size() == m_edmProxies.size());
0174   }
0175 
0176   edm::eventsetup::TypeTag type() const override { return m_type; }
0177   ProxyP proxy(unsigned int iovIndex) const override { return m_proxies.at(iovIndex); }
0178   edmProxyP edmProxy(unsigned int iovIndex) const override { return m_edmProxies.at(iovIndex); }
0179 
0180 private:
0181   std::string m_source;
0182   edm::eventsetup::TypeTag m_type;
0183   std::vector<std::shared_ptr<cond::persistency::PayloadProxy<DataT>>> m_proxies;
0184   std::vector<std::shared_ptr<DataProxy>> m_edmProxies;
0185 };
0186 
0187 #endif /* CONDCORE_PLUGINSYSTEM_DATAPROXY_H */