CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/CondCore/ORA/src/Sequences.cc

Go to the documentation of this file.
00001 #include "CondCore/ORA/interface/Exception.h"
00002 #include "Sequences.h"
00003 #include "IDatabaseSchema.h"
00004 
00005 ora::Sequences::Sequences( ora::IDatabaseSchema& schema ):
00006   m_lastIds(),
00007   m_table( schema.sequenceTable()){
00008 }
00009 
00010 ora::Sequences::Sequences( ora::ISequenceTable& table ):
00011   m_lastIds(),
00012   m_table( table ){
00013 }
00014 
00015 ora::Sequences::~Sequences(){
00016 }
00017 
00018 void ora::Sequences::create( const std::string& sequenceName ){
00019   m_table.add( sequenceName );
00020 }
00021 
00022 int ora::Sequences::getNextId( const std::string& sequenceName, bool sinchronize ){
00023   int next = 0;
00024   std::map<std::string,int>::iterator iS = m_lastIds.find( sequenceName );
00025   if( iS == m_lastIds.end() ){
00026     bool found = m_table.getLastId( sequenceName, next );
00027     if( ! found ) {
00028       throwException("Sequence \""+sequenceName+"\" does not exists.","Sequences::getNextId");
00029     } else {
00030       next += 1;
00031     }
00032     m_lastIds.insert( std::make_pair( sequenceName, next ));
00033   } else {
00034     next = ++iS->second;
00035   }
00036 
00037   if( sinchronize){
00038     m_table.sinchronize( sequenceName, next );
00039   }  
00040   return next;
00041 }
00042 
00043 void ora::Sequences::sinchronize( const std::string& sequenceName ){
00044   std::map<std::string,int>::iterator iS = m_lastIds.find( sequenceName );
00045   if( iS != m_lastIds.end() ){
00046     int lastOnDb = 0;
00047     m_table.getLastId( sequenceName, lastOnDb );
00048     if( lastOnDb < iS->second ) m_table.sinchronize( sequenceName, iS->second );
00049     m_lastIds.erase( sequenceName );
00050   }
00051 }
00052 
00053 void ora::Sequences::sinchronizeAll(){
00054   for( std::map<std::string,int>::iterator iS = m_lastIds.begin();
00055        iS != m_lastIds.end(); iS++ ){
00056     int lastOnDb = 0;
00057     m_table.getLastId( iS->first, lastOnDb );
00058     if( lastOnDb < iS->second ) m_table.sinchronize( iS->first, iS->second );    
00059   }
00060   clear();
00061 }
00062 
00063 void ora::Sequences::erase( const std::string& sequenceName ){
00064   m_table.erase( sequenceName );
00065 }
00066 
00067 void ora::Sequences::clear(){
00068   m_lastIds.clear();
00069 }
00070 
00071 ora::NamedSequence::NamedSequence( const std::string& sequenceName, ora::IDatabaseSchema& dbSchema ):
00072   m_name( sequenceName ),
00073   m_sequences( dbSchema ){
00074 }
00075 
00076 ora::NamedSequence::~NamedSequence(){
00077 }
00078 
00079 void ora::NamedSequence::create(){
00080   m_sequences.create( m_name );
00081 }
00082 
00083 int ora::NamedSequence::getNextId( bool sinchronize ){
00084   return m_sequences.getNextId( m_name, sinchronize );
00085 }
00086 
00087 void ora::NamedSequence::sinchronize(){
00088   m_sequences.sinchronize( m_name );
00089 }
00090 
00091 void ora::NamedSequence::erase(){
00092   m_sequences.erase( m_name );
00093 }
00094 
00095 void ora::NamedSequence::clear(){
00096   m_sequences.clear();
00097 }
00098