CMS 3D CMS Logo

DTKeyedConfigCache.cc
Go to the documentation of this file.
1 /*
2  * See header file for a description of this class.
3  *
4  * This class was originally defined in
5  * CondCore/DTPlugins/src/DTConfigPluginHandler.cc
6  * It was moved, renamed, and modified to not be a singleton
7  * for thread safety, but otherwise little was changed.
8  *
9  * \author Paolo Ronchese INFN Padova
10  *
11  */
12 
13 //-----------------------
14 // This Class' Header --
15 //-----------------------
17 
18 //-------------------------------
19 // Collaborating Class Headers --
20 //-------------------------------
23 
24 #include <memory>
25 //-------------------
26 // Initializations --
27 //-------------------
28 const int DTKeyedConfigCache::maxBrickNumber = 5000;
29 const int DTKeyedConfigCache::maxStringNumber = 100000;
30 const int DTKeyedConfigCache::maxByteNumber = 10000000;
31 
32 //----------------
33 // Constructors --
34 //----------------
35 DTKeyedConfigCache::DTKeyedConfigCache() : cachedBrickNumber(0), cachedStringNumber(0), cachedByteNumber(0) {}
36 
37 //--------------
38 // Destructor --
39 //--------------
41 
42 int DTKeyedConfigCache::get(const cond::persistency::KeyList& keyList, int cfgId, const DTKeyedConfig*& obj) {
43  bool cacheFound = false;
44  int cacheAge = 999999999;
45  std::map<int, counted_brick>::iterator cache_iter = brickMap.begin();
46  std::map<int, counted_brick>::iterator cache_icfg = brickMap.find(cfgId);
47  std::map<int, counted_brick>::iterator cache_iend = brickMap.end();
48  if (cache_icfg != cache_iend) {
49  std::pair<const int, counted_brick>& entry = *cache_icfg;
50  counted_brick& cBrick = entry.second;
51  cacheAge = cBrick.first;
52  obj = cBrick.second;
53  cacheFound = true;
54  }
55 
56  std::map<int, const DTKeyedConfig*> ageMap;
57  if (cacheFound) {
58  if (!cacheAge)
59  return 0;
60  while (cache_iter != cache_iend) {
61  std::pair<const int, counted_brick>& entry = *cache_iter++;
62  counted_brick& cBrick = entry.second;
63  int& brickAge = cBrick.first;
64  if (brickAge < cacheAge)
65  brickAge++;
66  if (entry.first == cfgId)
67  brickAge = 0;
68  }
69  return 0;
70  } else {
71  while (cache_iter != cache_iend) {
72  std::pair<const int, counted_brick>& entry = *cache_iter++;
73  counted_brick& cBrick = entry.second;
74  ageMap.insert(std::pair<int, const DTKeyedConfig*>(++cBrick.first, entry.second.second));
75  }
76  }
77 
78  std::shared_ptr<DTKeyedConfig> kBrick;
79  bool brickFound = false;
80  try {
81  kBrick = keyList.getUsingKey<DTKeyedConfig>(cfgId);
82  if (kBrick.get())
83  brickFound = (kBrick->getId() == cfgId);
84  } catch (std::exception const& e) {
85  }
86  if (brickFound) {
87  counted_brick cBrick(0, obj = new DTKeyedConfig(*kBrick));
88  brickMap.insert(std::pair<int, counted_brick>(cfgId, cBrick));
89  DTKeyedConfig::data_iterator d_iter = kBrick->dataBegin();
90  DTKeyedConfig::data_iterator d_iend = kBrick->dataEnd();
92  cachedStringNumber += (d_iend - d_iter);
93  while (d_iter != d_iend)
94  cachedByteNumber += (*d_iter++).size();
95  }
96  std::map<int, const DTKeyedConfig*>::reverse_iterator iter = ageMap.rbegin();
99  const DTKeyedConfig* oldestBrick = iter->second;
100  int oldestId = oldestBrick->getId();
102  DTKeyedConfig::data_iterator d_iter = oldestBrick->dataBegin();
103  DTKeyedConfig::data_iterator d_iend = oldestBrick->dataEnd();
104  cachedStringNumber -= (d_iend - d_iter);
105  while (d_iter != d_iend)
106  cachedByteNumber -= (*d_iter++).size();
107  brickMap.erase(oldestId);
108  delete iter->second;
109  iter++;
110  }
111 
112  return 999;
113 }
114 
115 void DTKeyedConfigCache::getData(const cond::persistency::KeyList& keyList, int cfgId, std::vector<std::string>& list) {
116  const DTKeyedConfig* obj = nullptr;
117  get(keyList, cfgId, obj);
118  if (obj == nullptr)
119  return;
120  DTKeyedConfig::data_iterator d_iter = obj->dataBegin();
121  DTKeyedConfig::data_iterator d_iend = obj->dataEnd();
122  while (d_iter != d_iend)
123  list.push_back(*d_iter++);
124  DTKeyedConfig::link_iterator l_iter = obj->linkBegin();
125  DTKeyedConfig::link_iterator l_iend = obj->linkEnd();
126  while (l_iter != l_iend)
127  getData(keyList, *l_iter++, list);
128  return;
129 }
130 
132  std::map<int, counted_brick>::const_iterator iter = brickMap.begin();
133  std::map<int, counted_brick>::const_iterator iend = brickMap.end();
134  while (iter != iend) {
135  delete iter->second.second;
136  iter++;
137  }
138  brickMap.clear();
139  cachedBrickNumber = 0;
140  cachedStringNumber = 0;
141  cachedByteNumber = 0;
142  return;
143 }
size
Write out results.
std::map< int, counted_brick > brickMap
data_iterator dataBegin() const
std::pair< int, const DTKeyedConfig * > counted_brick
data_iterator dataEnd() const
void getData(const cond::persistency::KeyList &keyList, int cfgId, std::vector< std::string > &list)
static const int maxByteNumber
std::vector< int >::const_iterator link_iterator
Definition: DTKeyedConfig.h:55
std::vector< std::string >::const_iterator data_iterator
Definition: DTKeyedConfig.h:54
int getId() const
static const int maxBrickNumber
static const int maxStringNumber
int get(const cond::persistency::KeyList &keyList, int cfgId, const DTKeyedConfig *&obj)
std::shared_ptr< T > getUsingKey(unsigned long long key) const
Definition: KeyList.h:57