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