Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 
0002 #ifndef CondCore_CondDB_KeyList_h
0003 #define CondCore_CondDB_KeyList_h
0004 
0005 #include "CondCore/CondDB/interface/IOVProxy.h"
0006 #include "CondCore/CondDB/interface/Binary.h"
0007 #include "CondCore/CondDB/interface/Serialization.h"
0008 #include "CondCore/CondDB/interface/Exception.h"
0009 #include "CondFormats/Common/interface/BaseKeyed.h"
0010 //
0011 #include <map>
0012 #include <memory>
0013 #include <vector>
0014 #include <string>
0015 
0016 /*
0017  * KeyList represents a set of payloads each identified by a key  and "valid" at given time
0018  * Usually these payloads are configuration objects loaded in anvance
0019  * The model used here calls for all payloads to be "stored" in a single IOVSequence each identified by a unique key 
0020  * (properly hashed to be mapped in 64bits)
0021  *
0022  * the keylist is just a vector of the hashes each corresponding to a key
0023  * the correspondence position in the vector user-friendly name is kept in 
0024  * a list of all "names" that is defined in advance and kept in a dictionary at IOVSequence level
0025  
0026  *
0027  */
0028 
0029 namespace cond {
0030 
0031   namespace persistency {
0032 
0033     class KeyList {
0034     public:
0035       ///Called by PoolDBESSource
0036       void init(IOVProxy iovProxy);
0037       void init(KeyList const&);
0038 
0039       /// determines which keys to use to read from the DB. Should only be used by PoolDBESSource
0040       void setKeys(const std::vector<unsigned long long>& keys);
0041 
0042       ///Retrieves the pre-fetched data. The index is the same order as the keys used in setKeys.
0043       template <typename T>
0044       std::shared_ptr<T> getUsingIndex(size_t n) const {
0045         if (n >= size())
0046           throwException("Index outside the bounds of the key array.", "KeyList::getUsingIndex");
0047         if (m_keys[n] == 0 or m_data[n].first.empty()) {
0048           throwException("Payload for index " + std::to_string(n) + " has not been found.", "KeyList::getUsingIndex");
0049         }
0050         auto const& i = m_data[n];
0051         return deserialize<T>(i.first, i.second.first, i.second.second);
0052       }
0053 
0054       /** Retrieve the item associated with the key directly from the DB.
0055           The function is non-const since it is not thread-safe. */
0056       template <typename T>
0057       std::shared_ptr<T> getUsingKey(unsigned long long key) const {
0058         auto item = loadFromDB(key);
0059         return deserialize<T>(item.first, item.second.first, item.second.second);
0060       }
0061 
0062       ///Number of keys based on container passed to setKeys.
0063       size_t size() const { return m_data.size(); }
0064 
0065     private:
0066       std::pair<std::string, std::pair<cond::Binary, cond::Binary>> loadFromDB(unsigned long long key) const;
0067       // the db session, protected by a mutex
0068       mutable IOVProxy m_proxy;
0069       // the key selection:
0070       std::vector<unsigned long long> m_keys;
0071       std::vector<std::pair<std::string, std::pair<cond::Binary, cond::Binary>>> m_data;
0072     };
0073 
0074   }  // namespace persistency
0075 }  // namespace cond
0076 
0077 #endif