Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:02:46

0001 /*
0002  *  See header file for a description of this class.
0003  *
0004  *  This class was originally defined in
0005  *  CondCore/DTPlugins/src/DTConfigPluginHandler.cc
0006  *  It was moved, renamed, and modified to not be a singleton
0007  *  for thread safety, but otherwise little was changed.
0008  *
0009  *  \author Paolo Ronchese INFN Padova
0010  *
0011  */
0012 
0013 //-----------------------
0014 // This Class' Header --
0015 //-----------------------
0016 #include "CondTools/DT/interface/DTKeyedConfigCache.h"
0017 
0018 //-------------------------------
0019 // Collaborating Class Headers --
0020 //-------------------------------
0021 #include "CondFormats/DTObjects/interface/DTKeyedConfig.h"
0022 #include "CondCore/CondDB/interface/KeyList.h"
0023 
0024 #include <memory>
0025 //-------------------
0026 // Initializations --
0027 //-------------------
0028 const int DTKeyedConfigCache::maxBrickNumber = 5000;
0029 const int DTKeyedConfigCache::maxStringNumber = 100000;
0030 const int DTKeyedConfigCache::maxByteNumber = 10000000;
0031 
0032 //----------------
0033 // Constructors --
0034 //----------------
0035 DTKeyedConfigCache::DTKeyedConfigCache() : cachedBrickNumber(0), cachedStringNumber(0), cachedByteNumber(0) {}
0036 
0037 //--------------
0038 // Destructor --
0039 //--------------
0040 DTKeyedConfigCache::~DTKeyedConfigCache() { purge(); }
0041 
0042 int DTKeyedConfigCache::get(const cond::persistency::KeyList& keyList, int cfgId, const DTKeyedConfig*& obj) {
0043   bool cacheFound = false;
0044   int cacheAge = 999999999;
0045   std::map<int, counted_brick>::iterator cache_iter = brickMap.begin();
0046   std::map<int, counted_brick>::iterator cache_icfg = brickMap.find(cfgId);
0047   std::map<int, counted_brick>::iterator cache_iend = brickMap.end();
0048   if (cache_icfg != cache_iend) {
0049     std::pair<const int, counted_brick>& entry = *cache_icfg;
0050     counted_brick& cBrick = entry.second;
0051     cacheAge = cBrick.first;
0052     obj = cBrick.second;
0053     cacheFound = true;
0054   }
0055 
0056   std::map<int, const DTKeyedConfig*> ageMap;
0057   if (cacheFound) {
0058     if (!cacheAge)
0059       return 0;
0060     while (cache_iter != cache_iend) {
0061       std::pair<const int, counted_brick>& entry = *cache_iter++;
0062       counted_brick& cBrick = entry.second;
0063       int& brickAge = cBrick.first;
0064       if (brickAge < cacheAge)
0065         brickAge++;
0066       if (entry.first == cfgId)
0067         brickAge = 0;
0068     }
0069     return 0;
0070   } else {
0071     while (cache_iter != cache_iend) {
0072       std::pair<const int, counted_brick>& entry = *cache_iter++;
0073       counted_brick& cBrick = entry.second;
0074       ageMap.insert(std::pair<int, const DTKeyedConfig*>(++cBrick.first, entry.second.second));
0075     }
0076   }
0077 
0078   std::shared_ptr<DTKeyedConfig> kBrick;
0079   bool brickFound = false;
0080   try {
0081     kBrick = keyList.getUsingKey<DTKeyedConfig>(cfgId);
0082     if (kBrick.get())
0083       brickFound = (kBrick->getId() == cfgId);
0084   } catch (std::exception const& e) {
0085   }
0086   if (brickFound) {
0087     counted_brick cBrick(0, obj = new DTKeyedConfig(*kBrick));
0088     brickMap.insert(std::pair<int, counted_brick>(cfgId, cBrick));
0089     DTKeyedConfig::data_iterator d_iter = kBrick->dataBegin();
0090     DTKeyedConfig::data_iterator d_iend = kBrick->dataEnd();
0091     cachedBrickNumber++;
0092     cachedStringNumber += (d_iend - d_iter);
0093     while (d_iter != d_iend)
0094       cachedByteNumber += (*d_iter++).size();
0095   }
0096   std::map<int, const DTKeyedConfig*>::reverse_iterator iter = ageMap.rbegin();
0097   while ((cachedBrickNumber > maxBrickNumber) || (cachedStringNumber > maxStringNumber) ||
0098          (cachedByteNumber > maxByteNumber)) {
0099     const DTKeyedConfig* oldestBrick = iter->second;
0100     int oldestId = oldestBrick->getId();
0101     cachedBrickNumber--;
0102     DTKeyedConfig::data_iterator d_iter = oldestBrick->dataBegin();
0103     DTKeyedConfig::data_iterator d_iend = oldestBrick->dataEnd();
0104     cachedStringNumber -= (d_iend - d_iter);
0105     while (d_iter != d_iend)
0106       cachedByteNumber -= (*d_iter++).size();
0107     brickMap.erase(oldestId);
0108     delete iter->second;
0109     iter++;
0110   }
0111 
0112   return 999;
0113 }
0114 
0115 void DTKeyedConfigCache::getData(const cond::persistency::KeyList& keyList, int cfgId, std::vector<std::string>& list) {
0116   const DTKeyedConfig* obj = nullptr;
0117   get(keyList, cfgId, obj);
0118   if (obj == nullptr)
0119     return;
0120   DTKeyedConfig::data_iterator d_iter = obj->dataBegin();
0121   DTKeyedConfig::data_iterator d_iend = obj->dataEnd();
0122   while (d_iter != d_iend)
0123     list.push_back(*d_iter++);
0124   DTKeyedConfig::link_iterator l_iter = obj->linkBegin();
0125   DTKeyedConfig::link_iterator l_iend = obj->linkEnd();
0126   while (l_iter != l_iend)
0127     getData(keyList, *l_iter++, list);
0128   return;
0129 }
0130 
0131 void DTKeyedConfigCache::purge() {
0132   std::map<int, counted_brick>::const_iterator iter = brickMap.begin();
0133   std::map<int, counted_brick>::const_iterator iend = brickMap.end();
0134   while (iter != iend) {
0135     delete iter->second.second;
0136     iter++;
0137   }
0138   brickMap.clear();
0139   cachedBrickNumber = 0;
0140   cachedStringNumber = 0;
0141   cachedByteNumber = 0;
0142   return;
0143 }