CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/CondTools/DT/src/DTKeyedConfigHandler.cc

Go to the documentation of this file.
00001 /*
00002  *  See header file for a description of this class.
00003  *
00004  *  $Date: 2012/02/15 16:04:16 $
00005  *  $Revision: 1.9 $
00006  *  \author Paolo Ronchese INFN Padova
00007  *
00008  */
00009 
00010 //-----------------------
00011 // This Class' Header --
00012 //-----------------------
00013 #include "CondTools/DT/interface/DTKeyedConfigHandler.h"
00014 
00015 //-------------------------------
00016 // Collaborating Class Headers --
00017 //-------------------------------
00018 #include "CondFormats/DTObjects/interface/DTCCBConfig.h"
00019 #include "CondFormats/DTObjects/interface/DTKeyedConfig.h"
00020 
00021 
00022 #include "CondCore/DBCommon/interface/DbTransaction.h"
00023 
00024 #include "FWCore/ServiceRegistry/interface/Service.h"
00025 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
00026 #include "CondCore/DBOutputService/interface/KeyedElement.h"
00027 #include "CondCore/IOVService/interface/KeyList.h"
00028 
00029 #include "RelationalAccess/ISchema.h"
00030 #include "RelationalAccess/ITable.h"
00031 #include "RelationalAccess/ICursor.h"
00032 #include "RelationalAccess/IQuery.h"
00033 #include "CoralBase/AttributeList.h"
00034 #include "CoralBase/AttributeSpecification.h"
00035 #include "CoralBase/Attribute.h"
00036 
00037 //---------------
00038 // C++ Headers --
00039 //---------------
00040 
00041 
00042 //-------------------
00043 // Initializations --
00044 //-------------------
00045 cond::KeyList* DTKeyedConfigHandler::keyList = 0;
00046 
00047 //----------------
00048 // Constructors --
00049 //----------------
00050 DTKeyedConfigHandler::DTKeyedConfigHandler( const edm::ParameterSet& ps ):
00051  copyData(   ps.getUntrackedParameter<bool> ( "copyData", true ) ),
00052  minBrickId( ps.getUntrackedParameter<int> ( "minBrick", 0 ) ),
00053  maxBrickId( ps.getUntrackedParameter<int> ( "maxBrick", 999999999 ) ),
00054  minRunId(   ps.getUntrackedParameter<int> ( "minRun", 0 ) ),
00055  maxRunId(   ps.getUntrackedParameter<int> ( "maxRun", 999999999 ) ),
00056  dataTag(               ps.getParameter<std::string> ( "tag" ) ),
00057  onlineConnect(         ps.getParameter<std::string> ( "onlineDB" ) ),
00058  onlineAuthentication(  ps.getParameter<std::string> ( 
00059                         "onlineAuthentication" ) ),
00060  brickContainer(        ps.getParameter<std::string> ( "container" ) ),
00061  connection(),
00062  isession() {
00063   std::cout << " PopCon application for DT configuration export "
00064             <<  onlineAuthentication << std::endl;
00065 }
00066 
00067 //--------------
00068 // Destructor --
00069 //--------------
00070 DTKeyedConfigHandler::~DTKeyedConfigHandler() {
00071 }
00072 
00073 //--------------
00074 // Operations --
00075 //--------------
00076 void DTKeyedConfigHandler::getNewObjects() {
00077 
00078   //to access the information on the tag inside the offline database:
00079   cond::TagInfo const & ti = tagInfo();
00080   unsigned int last = ti.lastInterval.first;
00081   std::cout << "last configuration key already copied for run: "
00082             << last << std::endl;
00083 
00084   std::vector<DTConfigKey> lastKey;
00085   std::cout << "check for last " << std::endl;
00086   if ( last == 0 ) {
00087     DTCCBConfig* dummyConf = new DTCCBConfig( dataTag );
00088     dummyConf->setStamp( 0 );
00089     dummyConf->setFullKey( lastKey );
00090     cond::Time_t snc = 1;
00091     std::cout << "write dummy " << std::endl;
00092     m_to_transfer.push_back( std::make_pair( dummyConf, snc ) );
00093   }
00094   else {
00095     std::cout << "get last payload" << std::endl;
00096     Ref payload = lastPayload();
00097     std::cout << "get last full key" << std::endl;
00098     lastKey = payload->fullKey();
00099     std::cout << "last key: " << std::endl;
00100     std::vector<DTConfigKey>::const_iterator keyIter = lastKey.begin();
00101     std::vector<DTConfigKey>::const_iterator keyIend = lastKey.end();
00102     while ( keyIter != keyIend ) {
00103       const DTConfigKey& cfgKeyList = *keyIter++;
00104       std::cout << cfgKeyList.confType << " : "
00105                 << cfgKeyList.confKey << std::endl;
00106     }
00107   }
00108 
00109   //to access the information on last successful log entry for this tag:
00110 //  cond::LogDBEntry const & lde = logDBEntry();     
00111 
00112   //to access the lastest payload (Ref is a smart pointer)
00113 //  Ref payload = lastPayload();
00114 
00115   if ( !copyData ) return;
00116 
00117   unsigned lastRun = last;
00118   std::cout << "check for new runs since " << lastRun << std::endl;
00119 
00120   std::cout << "configure DbConnection" << std::endl;
00121   //  conn->configure( cond::CmsDefaults );
00122   connection.configuration().setAuthenticationPath( onlineAuthentication );
00123   connection.configure();
00124   std::cout << "create DbSession" << std::endl;
00125   isession = connection.createSession();
00126   std::cout << "open session" << std::endl;
00127   isession.open( onlineConnect );
00128   std::cout << "start transaction" << std::endl;
00129   isession.transaction().start();
00130 
00131   // =========== Update configuration data
00132   chkConfigList();
00133 
00134   // =========== Find latest runs
00135   std::cout << "get run config..." << std::endl;
00136   std::map<int,std::vector<int>*> runMap;
00137   std::map<int,std::vector<DTConfigKey>*> rhcMap;
00138   coral::ITable& runHistoryTable =
00139     isession.nominalSchema().tableHandle( "RUNHISTORY" );
00140   std::auto_ptr<coral::IQuery>
00141     runHistoryQuery( runHistoryTable.newQuery() );
00142   runHistoryQuery->addToOutputList( "RUN" );
00143   runHistoryQuery->addToOutputList( "RHID" );
00144   coral::ICursor& runHistoryCursor = runHistoryQuery->execute();
00145   while( runHistoryCursor.next() ) {
00146     const coral::AttributeList& row = runHistoryCursor.currentRow();
00147     int runId = row[    "RUN"].data<int>();
00148     int rhcId = static_cast<int>( row["RHID"].data<int>() );
00149     // ----------- ignore already copied runs
00150     if ( static_cast<unsigned>( runId ) <= lastRun ) continue;
00151     // ----------- ignore runs outside required range
00152     if ( runId < minRunId ) continue;
00153     if ( runId > maxRunId ) continue;
00154     std::cout << "schedule config key copy for run "
00155               << runId << " ---> RHID " << rhcId << std::endl;
00156     // ----------- retrieve or create RH relation list for this run
00157     std::vector<int>* rhlPtr = 0;
00158     std::map<int,std::vector<int>*>::const_iterator runIter;
00159     if ( ( runIter = runMap.find( runId ) ) != runMap.end() )
00160          rhlPtr = runIter->second;
00161     else runMap.insert( std::pair<int,std::vector<int>*>( runId,
00162          rhlPtr = new std::vector<int> ) );
00163     // ----------- append RH relation to the list
00164     rhlPtr->push_back( rhcId );
00165     // ----------- create key list for this RH relation
00166     if ( rhcMap.find( rhcId ) == rhcMap.end() )
00167          rhcMap.insert( std::pair<int,std::vector<DTConfigKey>*>( rhcId,
00168                         new std::vector<DTConfigKey> ) );
00169   }
00170   if ( !runMap.size() ) std::cout << "no new run found" << std::endl;
00171 
00172   // =========== get ccb identifiers map
00173   std::cout << "retrieve CCB map" << std::endl;
00174   std::map<int,DTCCBId> ccbMap;
00175   coral::ITable& ccbMapTable =
00176     isession.nominalSchema().tableHandle( "CCBMAP" );
00177   std::auto_ptr<coral::IQuery>
00178     ccbMapQuery( ccbMapTable.newQuery() );
00179   ccbMapQuery->addToOutputList( "CCBID" );
00180   ccbMapQuery->addToOutputList( "WHEEL" );
00181   ccbMapQuery->addToOutputList( "SECTOR" );
00182   ccbMapQuery->addToOutputList( "STATION" );
00183   coral::ICursor& ccbMapCursor = ccbMapQuery->execute();
00184   while( ccbMapCursor.next() ) {
00185     const coral::AttributeList& row = ccbMapCursor.currentRow();
00186     int ccb     = row["CCBID"  ].data<int>();
00187     int wheel   = row["WHEEL"  ].data<int>();
00188     int sector  = row["SECTOR" ].data<int>();
00189     int station = row["STATION"].data<int>();
00190     DTCCBId ccbId;
00191     ccbId.  wheelId =   wheel;
00192     ccbId.stationId = station;
00193     ccbId. sectorId =  sector;
00194     ccbMap.insert( std::pair<int,DTCCBId>( ccb, ccbId ) );
00195   }
00196 
00197   // =========== get brick types
00198   std::cout << "retrieve brick types" << std::endl;
00199   std::map<int,int> bktMap;
00200   coral::AttributeList emptyBindVariableList;
00201   std::auto_ptr<coral::IQuery>
00202          brickTypeQuery( isession.nominalSchema().newQuery() );
00203   brickTypeQuery->addToTableList( "CFGBRICKS" );
00204   brickTypeQuery->addToTableList( "BRKT2CSETT" );
00205   // ----------- join brick type (1-11) to subsystem part (1-6)
00206   std::string bTypeCondition = "CFGBRICKS.BRKTYPE=BRKT2CSETT.BRKTYPE";
00207   brickTypeQuery->addToOutputList( "CFGBRICKS.BRKID" );
00208   brickTypeQuery->addToOutputList( "BRKT2CSETT.CSETTYPE" );
00209   brickTypeQuery->setCondition( bTypeCondition, emptyBindVariableList );
00210   coral::ICursor& brickTypeCursor = brickTypeQuery->execute();
00211   while( brickTypeCursor.next() ) {
00212     const coral::AttributeList& row = brickTypeCursor.currentRow();
00213     int id = row["CFGBRICKS.BRKID"    ].data<int>();
00214     int bt = row["BRKT2CSETT.CSETTYPE"].data<short>();
00215 //    std::cout << "brick " << id << " type " << bt << std::endl;
00216 // @@FIX - TEMPORARY PATCH
00217 //    if ( bt > 3 ) bt = 3;
00218     bktMap.insert( std::pair<int,int>( id, bt ) );
00219   }
00220 
00221   // =========== get RH relations
00222   std::cout << "retrieve RH relations" << std::endl;
00223   std::map<int,int> cfgMap;
00224   coral::ITable& rhcRelTable =
00225     isession.nominalSchema().tableHandle( "RHRELATIONS" );
00226   std::auto_ptr<coral::IQuery>
00227     rhcRelQuery( rhcRelTable.newQuery() );
00228   rhcRelQuery->addToOutputList( "RHID" );
00229   rhcRelQuery->addToOutputList( "CONFKEY" );
00230   rhcRelQuery->addToOutputList( "CSETTYPEID" );
00231   coral::ICursor& rhcRelCursor = rhcRelQuery->execute();
00232   while( rhcRelCursor.next() ) {
00233     const coral::AttributeList& row = rhcRelCursor.currentRow();
00234     int rhc     = row["RHID"      ].data<int>();
00235     int key     = row["CONFKEY"   ].data<int>();
00236     int cfg     = row["CSETTYPEID"].data<int>();
00237     // ----------- check for used RH relations, skip unused
00238     std::map<int,std::vector<DTConfigKey>*>::iterator rhcIter =
00239                                                       rhcMap.find( rhc );
00240     std::map<int,std::vector<DTConfigKey>*>::iterator rhcIend =
00241                                                       rhcMap.end();
00242     if ( rhcIter == rhcIend ) continue;
00243     std::vector<DTConfigKey>* keyPtr = rhcIter->second;
00244     DTConfigKey confList;
00245     confList.confType = cfg;
00246     confList.confKey  = key;
00247     keyPtr->push_back( confList );
00248     // ----------- flag config key as used
00249     if ( cfgMap.find( cfg ) == cfgMap.end() )
00250          cfgMap.insert( std::pair<int,int>( key, rhc ) );
00251   }
00252 
00253   // =========== get ccb config keys
00254   std::cout << "retrieve CCB configuration keys" << std::endl;
00255   std::map<int,std::map<int,int>*> keyMap;
00256   std::map<int,int> cckMap;
00257   coral::ITable& ccbRelTable =
00258     isession.nominalSchema().tableHandle( "CCBRELATIONS" );
00259   std::auto_ptr<coral::IQuery>
00260     ccbRelQuery( ccbRelTable.newQuery() );
00261   ccbRelQuery->addToOutputList( "CONFKEY" );
00262   ccbRelQuery->addToOutputList( "CCBID" );
00263   ccbRelQuery->addToOutputList( "CONFCCBKEY" );
00264   coral::ICursor& ccbRelCursor = ccbRelQuery->execute();
00265   while( ccbRelCursor.next() ) {
00266     const coral::AttributeList& row = ccbRelCursor.currentRow();
00267     int cfg     = row["CONFKEY"   ].data<int>();
00268     int ccb     = row["CCBID"     ].data<int>();
00269     int key     = row["CONFCCBKEY"].data<int>();
00270     // ----------- check for used configurations, skip unused
00271     if ( cfgMap.find( cfg ) == cfgMap.end() ) continue;
00272     // ----------- retrieve or create ccb id-key map for this configuration
00273     std::map<int,int>* mapPtr = 0;
00274     std::map<int,std::map<int,int>*>::const_iterator keyIter;
00275     if ( ( keyIter = keyMap.find( cfg ) ) != keyMap.end() )
00276          mapPtr = keyIter->second;
00277     else keyMap.insert( std::pair<int,std::map<int,int>*>( cfg,
00278          mapPtr = new std::map<int,int> ) );
00279 //    std::map<int,std::map<int,int>*>::const_iterator keyIter =
00280 //                                                     keyMap.find( cfg );
00281 //    std::map<int,std::map<int,int>*>::const_iterator keyIend =
00282 //                                                     keyMap.end();
00283 //    if ( keyIter != keyIend ) mapPtr = keyIter->second;
00284 //    else                      keyMap.insert(
00285 //                              std::pair<int,std::map<int,int>*>( cfg,
00286 //                              mapPtr = new std::map<int,int> ) );
00287     // ----------- store config key for this ccb
00288     std::map<int,int>& mapRef( *mapPtr );
00289     mapRef.insert( std::pair<int,int>( ccb, key ) );
00290     // ----------- flag ccb config key as used
00291     if ( cckMap.find( key ) == cckMap.end() )
00292          cckMap.insert( std::pair<int,int>( key, ccb ) );
00293   }
00294 
00295   // =========== get brick keys
00296   std::cout << "retrieve CCB configuration bricks" << std::endl;
00297   std::map<int,std::vector<int>*> brkMap;
00298   coral::ITable& confBrickTable =
00299     isession.nominalSchema().tableHandle( "CFG2BRKREL" );
00300   std::auto_ptr<coral::IQuery>
00301     confBrickQuery( confBrickTable.newQuery() );
00302   confBrickQuery->addToOutputList( "CONFID" );
00303   confBrickQuery->addToOutputList( "BRKID"  );
00304   coral::ICursor& confBrickCursor = confBrickQuery->execute();
00305   while( confBrickCursor.next() ) {
00306     const coral::AttributeList& row = confBrickCursor.currentRow();
00307     int key = row["CONFID"].data<int>();
00308     int brk = row["BRKID" ].data<int>();
00309     // ----------- check for used ccb config key, skip unused
00310     if ( cckMap.find( key ) == cckMap.end() ) continue;
00311 //    std::map<int,std::vector<int>*>::const_iterator brkIter =
00312 //                                                    brkMap.find( key );
00313 //    std::map<int,std::vector<int>*>::const_iterator brkIend =
00314 //                                                    brkMap.end();
00315     // ----------- retrieve or create brick list for this ccb config
00316     std::vector<int>* brkPtr = 0;
00317     std::map<int,std::vector<int>*>::const_iterator brkIter;
00318 //check for new ccb config key
00319     if ( ( brkIter = brkMap.find( key ) ) != brkMap.end() )
00320          brkPtr = brkIter->second;
00321     else brkMap.insert( std::pair<int,std::vector<int>*>( key,
00322          brkPtr = new std::vector<int> ) );
00323 //    if ( brkIter != brkIend ) brkPtr = brkIter->second;
00324 //    else                      brkMap.insert(
00325 //                              std::pair<int,std::vector<int>*>( key,
00326 //                              brkPtr = new std::vector<int> ) );
00327     // ----------- store brick key
00328     brkPtr->push_back( brk );
00329   }
00330 
00331   // =========== loop over new runs
00332   std::map<int,std::vector<int>*>::const_iterator runIter = runMap.begin();
00333   std::map<int,std::vector<int>*>::const_iterator runIend = runMap.end();
00334   while ( runIter != runIend ) {
00335     const std::pair<int,std::vector<int>*>& runEntry = *runIter++;
00336     // ----------- get full configuration
00337     int run = runEntry.first;
00338     std::vector<DTConfigKey> cfl;
00339     // ----------- get RH relation list
00340     std::vector<int>* rhlPtr = runEntry.second;
00341     std::vector<int>::const_iterator rhlIter = rhlPtr->begin();
00342     std::vector<int>::const_iterator rhlIend = rhlPtr->end();
00343     // ----------- loop over RH relations
00344     while ( rhlIter != rhlIend ) {
00345       int rhc = *rhlIter++;
00346       std::cout << "retrieve configuration bricks for run " << run
00347                 << " ---> RH " << rhc << std::endl;
00348 //      std::map<int,std::vector<DTConfigKey>*>::const_iterator
00349 //               rhcIter = rhcMap.find( rhc );
00350 //      std::map<int,std::vector<DTConfigKey>*>::const_iterator
00351 //               rhcIend = rhcMap.end();
00352       std::map<int,std::vector<DTConfigKey>*>::const_iterator rhcIter;
00353       // ----------- redundant check
00354       if ( ( rhcIter = rhcMap.find( rhc ) ) == rhcMap.end() ) continue;
00355       std::vector<DTConfigKey>* listPtr = rhcIter->second;
00356       // ----------- redundant check
00357       if ( listPtr == 0 ) continue;
00358       std::vector<DTConfigKey>::const_iterator bkiIter = listPtr->begin();
00359       std::vector<DTConfigKey>::const_iterator bkiIend = listPtr->end();
00360       while ( bkiIter != bkiIend ) cfl.push_back( *bkiIter++ );
00361     }
00362     if ( sameConfigList( cfl, lastKey ) ) continue;
00363     lastKey = cfl;
00364     DTCCBConfig* fullConf = new DTCCBConfig( dataTag );
00365     // set run and full configuration in payload
00366     fullConf->setStamp(   run );
00367     fullConf->setFullKey( cfl );
00368 
00369     std::vector<DTConfigKey>::const_iterator cfgIter = cfl.begin();
00370     std::vector<DTConfigKey>::const_iterator cfgIend = cfl.end();
00371     while ( cfgIter != cfgIend ) {
00372       const DTConfigKey& cfgEntry = *cfgIter++;
00373       int cft = cfgEntry.confType;
00374       int cfg = cfgEntry.confKey;
00375 
00376       // =========== retrieve ccb config map
00377 //    std::map<int,std::map<int,int>*>::const_iterator keyIter =
00378 //                                                     keyMap.find( cfg );
00379 //    std::map<int,std::map<int,int>*>::const_iterator keyIend =
00380 //                                                     keyMap.end();
00381       std::map<int,int>* mapPtr = 0;
00382       std::map<int,std::map<int,int>*>::const_iterator keyIter;
00383       // ----------- redundant check
00384 //      if ( keyIter != keyIend )
00385       if ( ( keyIter = keyMap.find( cfg ) ) != keyMap.end() )
00386            mapPtr = keyIter->second;
00387       if ( mapPtr == 0 ) continue;
00388       std::map<int,int>::const_iterator ccmIter = mapPtr->begin();
00389       std::map<int,int>::const_iterator ccmIend = mapPtr->end();
00390       while ( ccmIter != ccmIend ) {
00391         const std::pair<int,int>& ccmEntry = *ccmIter++;
00392         // ----------- get ccb config key
00393         int ccb = ccmEntry.first;
00394         int key = ccmEntry.second;
00395         // retrieve chamber id
00396 //        std::map<int,DTCCBId>::const_iterator ccbIter = ccbMap.find( ccb );
00397 //        std::map<int,DTCCBId>::const_iterator ccbIend = ccbMap.end();
00398         std::map<int,DTCCBId>::const_iterator ccbIter;
00399         // ----------- redundant check
00400 //        if ( ccbIter == ccbIend ) continue;
00401         if ( ( ccbIter = ccbMap.find( ccb ) ) == ccbMap.end() ) continue;
00402         const DTCCBId& chaId = ccbIter->second;
00403         // ----------- retrieve brick id list
00404 //        std::map<int,std::vector<int>*>::const_iterator brkIter =
00405 //                                                        brkMap.find( key );
00406 //        std::map<int,std::vector<int>*>::const_iterator brkIend =
00407 //                                                        brkMap.end();
00408 //        if ( brkIter == brkIend ) continue;
00409         std::map<int,std::vector<int>*>::const_iterator brkIter;
00410         if ( ( brkIter = brkMap.find( key ) ) == brkMap.end() ) continue;
00411         std::vector<int>* brkPtr = brkIter->second;
00412         // ----------- redundant check
00413         if ( brkPtr == 0 ) continue;
00414         // ----------- set brick id lists in payload
00415         std::vector<int> bkList;
00416         bkList.reserve( 20 );
00417 //        std::map<int,int>::const_iterator bktIter = bktMap.begin();
00418 //        std::map<int,int>::const_iterator bktIend = bktMap.end();
00419         std::map<int,int>::const_iterator bktIter;
00420         std::vector<int>::const_iterator bkiIter = brkPtr->begin();
00421         std::vector<int>::const_iterator bkiIend = brkPtr->end();
00422         while ( bkiIter != bkiIend ) {
00423           int brickId = *bkiIter++;
00424 //          bktIter = bktMap.find( brickId );
00425           // ----------- redundant check
00426           if ( ( bktIter = bktMap.find( brickId ) ) == bktMap.end() ) continue;
00427           if ( bktIter->second == cft ) bkList.push_back( brickId );
00428         }
00429         fullConf->appendConfigKey( chaId.wheelId,
00430                                    chaId.stationId,
00431                                    chaId.sectorId,
00432                                    bkList );
00433       }
00434     }
00435     cond::Time_t snc = runEntry.first;
00436     m_to_transfer.push_back( std::make_pair( fullConf, snc ) );
00437     std::cout << "writing payload : " << sizeof( *fullConf ) 
00438               << " ( " << ( fullConf->end() - fullConf->begin() )
00439               << " ) " << std::endl;
00440   }
00441 
00442   isession.transaction().commit();
00443   isession.close();
00444 
00445   return;
00446 
00447 }
00448 
00449 
00450 void DTKeyedConfigHandler::chkConfigList() {
00451 
00452   std::cout << "open POOL out db " << std::endl;
00453   edm::Service<cond::service::PoolDBOutputService> outdb;
00454 
00455   std::cout << "start queries " << std::endl;
00456   std::map<int,bool> activeConfigMap;
00457   coral::ITable& fullConfigTable =
00458     isession.nominalSchema().tableHandle( "CONFIGSETS" );
00459   std::auto_ptr<coral::IQuery>
00460     fullConfigQuery( fullConfigTable.newQuery() );
00461   fullConfigQuery->addToOutputList( "CONFKEY" );
00462   fullConfigQuery->addToOutputList( "NAME" );
00463   fullConfigQuery->addToOutputList( "RUN" );
00464   coral::ICursor& fullConfigCursor = fullConfigQuery->execute();
00465   while( fullConfigCursor.next() ) {
00466     const coral::AttributeList& row = fullConfigCursor.currentRow();
00467     int fullConfigId = row["CONFKEY"].data<int>();
00468     int fullConfigRN = row["RUN"    ].data<int>();
00469     if ( fullConfigRN ) activeConfigMap.insert(
00470                         std::pair<int,bool>( fullConfigId, true ) );
00471     else                activeConfigMap.insert(
00472                         std::pair<int,bool>( fullConfigId, false ) );
00473     std::string fullConfigName = row["NAME"].data<std::string>();
00474   }
00475 
00476 //  std::cout << " =============== CCB config list" << std::endl;
00477   std::map<int,bool> activeCCBCfgMap;
00478   coral::ITable& fullCCBCfgTable =
00479     isession.nominalSchema().tableHandle( "CCBRELATIONS" );
00480   std::auto_ptr<coral::IQuery>
00481     fullCCBCfgQuery( fullCCBCfgTable.newQuery() );
00482   fullCCBCfgQuery->addToOutputList( "CONFKEY" );
00483   fullCCBCfgQuery->addToOutputList( "CONFCCBKEY" );
00484   coral::ICursor& fullCCBCfgCursor = fullCCBCfgQuery->execute();
00485   while( fullCCBCfgCursor.next() ) {
00486     const coral::AttributeList& row = fullCCBCfgCursor.currentRow();
00487     int fullConfigId = row["CONFKEY"   ].data<int>();
00488     int fullCCBCfgId = row["CONFCCBKEY"].data<int>();
00489 //    std::map<int,bool>::const_iterator cfgIter =
00490 //                                       activeConfigMap.find( fullConfigId );
00491     std::map<int,bool>::const_iterator cfgIter;
00492     if ( ( cfgIter = activeConfigMap.find( fullConfigId ) )
00493                   == activeConfigMap.end() ) continue;
00494     if ( !( cfgIter->second ) ) continue;
00495     if ( activeCCBCfgMap.find( fullCCBCfgId ) ==
00496          activeCCBCfgMap.end() ) 
00497          activeCCBCfgMap.insert( std::pair<int,bool>( fullCCBCfgId, true ) );
00498   }
00499 
00500 //  std::cout << " =============== config brick list" << std::endl;
00501   std::map<int,bool> activeCfgBrkMap;
00502   coral::ITable& ccbConfBrkTable =
00503     isession.nominalSchema().tableHandle( "CFG2BRKREL" );
00504   std::auto_ptr<coral::IQuery>
00505     ccbConfBrickQuery( ccbConfBrkTable.newQuery() );
00506   ccbConfBrickQuery->addToOutputList( "CONFID" );
00507   ccbConfBrickQuery->addToOutputList( "BRKID" );
00508   coral::ICursor& ccbConfBrickCursor = ccbConfBrickQuery->execute();
00509   while( ccbConfBrickCursor.next() ) {
00510     const coral::AttributeList& row = ccbConfBrickCursor.currentRow();
00511     int fullCCBCfgId = row["CONFID"].data<int>();
00512     int ccbConfBrkId = row["BRKID" ].data<int>();
00513 //    std::map<int,bool>::const_iterator ccbIter =
00514 //                                       activeCCBCfgMap.find( fullCCBCfgId );
00515     std::map<int,bool>::const_iterator ccbIter;
00516     if ( ( ccbIter = activeCCBCfgMap.find( fullCCBCfgId ) )
00517                   == activeCCBCfgMap.end() ) continue;
00518     if ( !( ccbIter->second ) ) continue;
00519     if ( activeCfgBrkMap.find( ccbConfBrkId ) ==
00520          activeCfgBrkMap.end() )
00521          activeCfgBrkMap.insert( std::pair<int,bool>( ccbConfBrkId, true ) );
00522   }
00523 
00524 //  std::cout << " ===============" << std::endl;
00525 
00526   coral::ITable& brickConfigTable =
00527     isession.nominalSchema().tableHandle( "CFGBRICKS" );
00528   std::auto_ptr<coral::IQuery>
00529     brickConfigQuery( brickConfigTable.newQuery() );
00530   brickConfigQuery->addToOutputList( "BRKID" );
00531   brickConfigQuery->addToOutputList( "BRKNAME" );
00532   coral::ICursor& brickConfigCursor = brickConfigQuery->execute();
00533   DTKeyedConfig* brickData = 0;
00534   std::vector<int> missingList;
00535   std::vector<unsigned long long> checkedKeys;
00536   while( brickConfigCursor.next() ) {
00537     const coral::AttributeList& row = brickConfigCursor.currentRow();
00538     int brickConfigId = row["BRKID"].data<int>();
00539     if ( brickConfigId < minBrickId ) continue;
00540     if ( brickConfigId > maxBrickId ) continue;
00541 //    std::map<int,bool>::const_iterator brkIter =
00542 //                                       activeCfgBrkMap.find( brickConfigId );
00543     std::map<int,bool>::const_iterator brkIter;
00544     if ( ( brkIter = activeCfgBrkMap.find( brickConfigId ) )
00545                   == activeCfgBrkMap.end() ) continue;
00546     if ( !( brkIter->second ) ) continue;
00547     std::string brickConfigName = row["BRKNAME"].data<std::string>();
00548     std::cout << "brick " << brickConfigId
00549               << " : "    << brickConfigName << std::endl;
00550     checkedKeys.push_back( brickConfigId );
00551     bool brickFound = false;
00552     try {
00553       keyList->load( checkedKeys );
00554       const DTKeyedConfig* brickCheck = keyList->get<DTKeyedConfig>( 0 );
00555       if ( brickCheck != 0 ) brickFound =
00556                              ( brickCheck->getId() == brickConfigId );
00557     }
00558     catch ( std::exception e ) {
00559     }
00560     if ( !brickFound ) {
00561       std::cout << "brick " << brickConfigId << " missing, copy request"
00562                 << std::endl;
00563       missingList.push_back( brickConfigId );
00564     }
00565     checkedKeys.clear();
00566   }
00567   keyList->load( checkedKeys );
00568 
00569   std::vector<int>::const_iterator brickIter = missingList.begin();
00570   std::vector<int>::const_iterator brickIend = missingList.end();
00571   while ( brickIter != brickIend ) {
00572     int brickConfigId = *brickIter++;
00573 //    std::cout << "get data for brick: " << brickConfigId << std::endl;
00574     coral::AttributeList bindVariableList;
00575     bindVariableList.extend( "brickId", typeid(int) );
00576     bindVariableList["brickId"].data<int>() = brickConfigId;
00577     std::auto_ptr<coral::IQuery>
00578            brickDataQuery( isession.nominalSchema().newQuery() );
00579     brickDataQuery->addToTableList( "CFGRELATIONS" );
00580     brickDataQuery->addToTableList( "CONFIGCMDS" );
00581     std::string
00582     brickCondition  =      "CONFIGCMDS.CMDID=CFGRELATIONS.CMDID";
00583     brickCondition += " and CFGRELATIONS.BRKID=:brickId";
00584     brickDataQuery->addToOutputList( "CFGRELATIONS.BRKID" );
00585     brickDataQuery->addToOutputList( "CONFIGCMDS.CONFDATA" );
00586     brickDataQuery->setCondition( brickCondition, bindVariableList );
00587     coral::ICursor& brickDataCursor = brickDataQuery->execute();
00588     brickData = new DTKeyedConfig();
00589     brickData->setId( brickConfigId );
00590     while( brickDataCursor.next() ) {
00591       const coral::AttributeList& row = brickDataCursor.currentRow();
00592       brickData->add( row["CONFIGCMDS.CONFDATA"].data<std::string>() );
00593     }
00594     cond::KeyedElement k( brickData, brickConfigId );
00595     std::cout << "now writing brick: " << brickConfigId << std::endl;
00596     outdb->writeOne( k.m_obj, k.m_key, brickContainer );
00597   }
00598 
00599   return;
00600 
00601 }
00602 
00603 
00604 std::string DTKeyedConfigHandler::id() const {
00605   return dataTag;
00606 }
00607 
00608 
00609 bool DTKeyedConfigHandler::sameConfigList(
00610                            const std::vector<DTConfigKey>& cfgl,
00611                            const std::vector<DTConfigKey>& cfgr ) {
00612   if ( cfgl.size() != cfgr.size() ) return false;
00613   std::map<int,int> lmap;
00614   std::vector<DTConfigKey>::const_iterator lIter = cfgl.begin();
00615   std::vector<DTConfigKey>::const_iterator lIend = cfgl.end();
00616   while ( lIter != lIend ) {
00617     const DTConfigKey& entry = *lIter++;
00618     lmap.insert( std::pair<int,int>( entry.confType, entry.confKey ) );
00619  }
00620   std::map<int,int> rmap;
00621   std::vector<DTConfigKey>::const_iterator rIter = cfgr.begin();
00622   std::vector<DTConfigKey>::const_iterator rIend = cfgr.end();
00623   while ( rIter != rIend ) {
00624     const DTConfigKey& entry = *rIter++;
00625     rmap.insert( std::pair<int,int>( entry.confType, entry.confKey ) );
00626   }
00627   std::map<int,int>::const_iterator lmIter = lmap.begin();
00628   std::map<int,int>::const_iterator lmIend = lmap.end();
00629   std::map<int,int>::const_iterator rmIter = rmap.begin();
00630   std::map<int,int>::const_iterator rmIend = rmap.end();
00631   while ( ( lmIter != lmIend ) &&
00632           ( rmIter != rmIend ) ) {
00633     const std::pair<int,int>& lEntry = *lmIter++;
00634     const std::pair<int,int>& rEntry = *rmIter++;
00635     if ( lEntry.first  != rEntry.first  ) return false;
00636     if ( lEntry.second != rEntry.second ) return false;
00637   }
00638   return true;
00639 }
00640 
00641 
00642 void DTKeyedConfigHandler::setList( cond::KeyList* list ) {
00643   keyList = list;
00644 }
00645