CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/CondCore/DTPlugins/src/DTConfigPluginHandler.cc

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