Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "CondCore/DTPlugins/interface/DTConfigPluginHandler.h"
00014
00015
00016
00017
00018 #include "CondFormats/DTObjects/interface/DTKeyedConfig.h"
00019 #include "CondFormats/DataRecord/interface/DTKeyedConfigListRcd.h"
00020 #include "CondCore/DBOutputService/interface/KeyedElement.h"
00021 #include "CondCore/IOVService/interface/KeyList.h"
00022 #include "FWCore/Framework/interface/EventSetup.h"
00023 #include "FWCore/Framework/interface/ESHandle.h"
00024
00025
00026
00027
00028
00029 #include <cstdio>
00030
00031
00032
00033
00034 int DTConfigPluginHandler::maxBrickNumber = 5000;
00035 int DTConfigPluginHandler::maxStringNumber = 100000;
00036 int DTConfigPluginHandler::maxByteNumber = 10000000;
00037
00038
00039
00040
00041
00042 DTConfigPluginHandler::DTConfigPluginHandler():
00043 cachedBrickNumber( 0 ),
00044 cachedStringNumber( 0 ),
00045 cachedByteNumber( 0 ) {
00046
00047
00048
00049
00050
00051
00052 }
00053
00054
00055
00056
00057
00058 DTConfigPluginHandler::~DTConfigPluginHandler() {
00059 purge();
00060 }
00061
00062
00063
00064
00065
00066 void DTConfigPluginHandler::build() {
00067 if ( instance == 0 ) instance = new DTConfigPluginHandler;
00068 }
00069
00070
00071 int DTConfigPluginHandler::get( const edm::EventSetup& context,
00072 int cfgId, const DTKeyedConfig*& obj ) {
00073 return get( context.get<DTKeyedConfigListRcd>(), cfgId, obj );
00074 }
00075
00076
00077 int DTConfigPluginHandler::get( const DTKeyedConfigListRcd& keyRecord,
00078 int cfgId, const DTKeyedConfig*& obj ) {
00079
00080 bool cacheFound = false;
00081 int cacheAge = 999999999;
00082 std::map<int,counted_brick>::iterator cache_iter = brickMap.begin();
00083 std::map<int,counted_brick>::iterator cache_icfg = brickMap.find( cfgId );
00084 std::map<int,counted_brick>::iterator cache_iend = brickMap.end();
00085 if ( cache_icfg != cache_iend ) {
00086 std::pair<const int,counted_brick>& entry = *cache_icfg;
00087 counted_brick& cBrick = entry.second;
00088 cacheAge = cBrick.first;
00089 obj = cBrick.second;
00090 cacheFound = true;
00091 }
00092 if ( cacheFound && !cacheAge ) return 0;
00093 std::map<int,DTKeyedConfig*> ageMap;
00094 while ( cache_iter != cache_iend ) {
00095 std::pair<const int,counted_brick>& entry = *cache_iter++;
00096 counted_brick& cBrick = entry.second;
00097 int& brickAge = cBrick.first;
00098 if ( brickAge < cacheAge ) brickAge++;
00099 if ( entry.first == cfgId ) brickAge = 0;
00100 ageMap.insert( std::pair<int,DTKeyedConfig*>(
00101 brickAge, new DTKeyedConfig( *cBrick.second ) ) );
00102 }
00103
00104 if ( cacheFound ) return 0;
00105
00106
00107 edm::ESHandle<cond::KeyList> klh;
00108 keyRecord.get( klh );
00109 cond::KeyList const & kl= *klh.product();
00110 cond::KeyList* keyList = const_cast<cond::KeyList*>( &kl );
00111 if ( keyList == 0 ) return 999;
00112
00113 std::vector<unsigned long long> checkedKeys;
00114 const DTKeyedConfig* kBrick = 0;
00115 checkedKeys.push_back( cfgId );
00116 bool brickFound = false;
00117 try {
00118 keyList->load( checkedKeys );
00119 kBrick = keyList->get<DTKeyedConfig>( 0 );
00120 if ( kBrick != 0 ) brickFound = ( kBrick->getId() == cfgId );
00121 }
00122 catch ( std::exception e ) {
00123 }
00124 if ( brickFound ) {
00125 counted_brick cBrick( 0, obj = new DTKeyedConfig( *kBrick ) );
00126 brickMap.insert( std::pair<int,counted_brick>( cfgId, cBrick ) );
00127 DTKeyedConfig::data_iterator d_iter = kBrick->dataBegin();
00128 DTKeyedConfig::data_iterator d_iend = kBrick->dataEnd();
00129 cachedBrickNumber++;
00130 cachedStringNumber += ( d_iend - d_iter );
00131 while ( d_iter != d_iend ) cachedByteNumber += ( *d_iter++ ).size();
00132 }
00133 std::map<int,DTKeyedConfig*>::reverse_iterator iter = ageMap.rbegin();
00134 while ( ( cachedBrickNumber > maxBrickNumber ) ||
00135 ( cachedStringNumber > maxStringNumber ) ||
00136 ( cachedByteNumber > maxByteNumber ) ) {
00137 const DTKeyedConfig* oldestBrick = iter->second;
00138 int oldestId = oldestBrick->getId();
00139 cachedBrickNumber--;
00140 DTKeyedConfig::data_iterator d_iter = oldestBrick->dataBegin();
00141 DTKeyedConfig::data_iterator d_iend = oldestBrick->dataEnd();
00142 cachedStringNumber -= ( d_iend - d_iter );
00143 while ( d_iter != d_iend ) cachedByteNumber -= ( *d_iter++ ).size();
00144 brickMap.erase( oldestId );
00145 delete iter->second;
00146 iter++;
00147 }
00148
00149 return 999;
00150
00151 }
00152
00153
00154 void DTConfigPluginHandler::getData( const edm::EventSetup& context, int cfgId,
00155 std::vector<std::string>& list ) {
00156 getData( context.get<DTKeyedConfigListRcd>(), cfgId, list );
00157 return;
00158 }
00159
00160
00161 void DTConfigPluginHandler::getData( const DTKeyedConfigListRcd& keyRecord,
00162 int cfgId,
00163 std::vector<std::string>& list ) {
00164 const DTKeyedConfig* obj = 0;
00165 get( keyRecord, cfgId, obj );
00166 if ( obj == 0 ) return;
00167 DTKeyedConfig::data_iterator d_iter = obj->dataBegin();
00168 DTKeyedConfig::data_iterator d_iend = obj->dataEnd();
00169 while ( d_iter != d_iend ) list.push_back( *d_iter++ );
00170 DTKeyedConfig::link_iterator l_iter = obj->linkBegin();
00171 DTKeyedConfig::link_iterator l_iend = obj->linkEnd();
00172 while ( l_iter != l_iend ) getData( keyRecord, *l_iter++, list );
00173 return;
00174 }
00175
00176
00177 void DTConfigPluginHandler::purge() {
00178 std::cout << "DTConfigPluginHandler::purge "
00179 << this << " "
00180 << cachedBrickNumber << " "
00181 << cachedStringNumber << " "
00182 << cachedByteNumber << std::endl;
00183 std::map<int,counted_brick>::const_iterator iter = brickMap.begin();
00184 std::map<int,counted_brick>::const_iterator iend = brickMap.end();
00185 while ( iter != iend ) {
00186 delete iter->second.second;
00187 iter++;
00188 }
00189 brickMap.clear();
00190 cachedBrickNumber = 0;
00191 cachedStringNumber = 0;
00192 cachedByteNumber = 0;
00193 return;
00194 }
00195