CMS 3D CMS Logo

cond::SequenceManager Class Reference

#include <CondCore/DBCommon/interface/SequenceManager.h>

List of all members.

Public Member Functions

void clear ()
 Clears the internal state.
void createSequencesTable ()
 Creates the table holding the sequences.
bool existSequencesTable ()
 Whether sequence table exists.
unsigned long long incrementId (const std::string &reftableName)
 Increments and returns a new valid oid for a table.
 SequenceManager (cond::CoralTransaction &coraldb, const std::string &sequenceTableName)
 Constructor.
void updateId (const std::string &reftableName, unsigned long long lastId)
 Updates the last used id.
 ~SequenceManager ()
 Destructor.

Private Member Functions

void init ()
bool lockEntry (coral::ISchema &schema, const std::string &reftableName, unsigned long long &lastId)
 Locks the id entry in the ref table and returns the lastId value.

Private Attributes

cond::CoralTransactionm_coraldb
 The coraldb in use.
bool m_sequenceTableExists
 Flag indicating whether the sequence table exists.
std::string m_sequenceTableName
 Sequence table name.
std::string m_setClause
 The set clause for updating a sequence entry.
bool m_started
std::map< std::string,
unsigned long long > 
m_tableToId
 Map of ids used.
std::string m_whereClause
 The where clause pinning a sequence entry.
coral::AttributeList * m_whereData
 The data for the where clause.


Detailed Description

Definition at line 22 of file SequenceManager.h.


Constructor & Destructor Documentation

SequenceManager::SequenceManager ( cond::CoralTransaction coraldb,
const std::string &  sequenceTableName 
)

Constructor.

Definition at line 17 of file SequenceManager.cc.

References init(), and m_whereData.

00018                                                                           :
00019   m_coraldb(coraldb),
00020   m_sequenceTableName( sequenceTableName ),
00021   m_tableToId(),
00022   m_sequenceTableExists( false ),
00023   m_whereClause( std::string("REFTABLE_NAME")+" =:"+std::string("REFTABLE_NAME")), 
00024   m_whereData( 0 ),
00025   m_setClause( std::string("IDVALUE")+" = "+std::string("IDVALUE")+" + 1"),
00026   m_started( false )
00027 {
00028   m_whereData = new coral::AttributeList; 
00029   m_whereData->extend<std::string>(std::string("REFTABLE_NAME"));
00030   init();
00031 }
void

SequenceManager::~SequenceManager (  ) 

Destructor.

Definition at line 37 of file SequenceManager.cc.

References m_whereData.

00038 {  
00039   delete m_whereData;
00040 }


Member Function Documentation

void SequenceManager::clear ( void   ) 

Clears the internal state.

Definition at line 137 of file SequenceManager.cc.

References m_tableToId.

00138 {
00139   m_tableToId.clear();
00140 }

void SequenceManager::createSequencesTable (  ) 

Creates the table holding the sequences.

Definition at line 146 of file SequenceManager.cc.

References description, m_coraldb, m_sequenceTableExists, m_sequenceTableName, cond::CoralTransaction::nominalSchema(), and python::listobjects::schema.

Referenced by cond::Logger::createLogDBIfNonExist().

00147 {
00148   coral::ISchema& schema= m_coraldb.nominalSchema();
00149   coral::TableDescription description( "CONDSEQ" );
00150   description.setName(m_sequenceTableName);
00151   description.insertColumn(std::string("REFTABLE_NAME"),
00152                            coral::AttributeSpecification::typeNameForType<std::string>() );
00153   description.setNotNullConstraint(std::string("REFTABLE_NAME"));
00154   description.insertColumn(std::string("IDVALUE"),
00155                            coral::AttributeSpecification::typeNameForType<unsigned long long>() );
00156   description.setNotNullConstraint(std::string("IDVALUE"));
00157   description.setPrimaryKey( std::vector< std::string >( 1, std::string("REFTABLE_NAME")));
00158   schema.createTable( description ).privilegeManager().grantToPublic( coral::ITablePrivilegeManager::Select );
00159   m_sequenceTableExists=true;
00160 }

bool SequenceManager::existSequencesTable (  ) 

Whether sequence table exists.

Definition at line 142 of file SequenceManager.cc.

References m_sequenceTableExists.

Referenced by cond::Logger::createLogDBIfNonExist().

00142                                         {
00143   return m_sequenceTableExists;
00144 }

unsigned long long SequenceManager::incrementId ( const std::string &  reftableName  ) 

Increments and returns a new valid oid for a table.

Definition at line 42 of file SequenceManager.cc.

References exception, Exception, lockEntry(), m_coraldb, m_sequenceTableExists, m_sequenceTableName, m_setClause, m_tableToId, m_whereClause, m_whereData, cond::CoralTransaction::nominalSchema(), and python::listobjects::schema.

Referenced by cond::Logger::logFailedOperationNow(), and cond::Logger::logOperationNow().

00042                                                             {
00043   std::map< std::string, unsigned long long >::iterator iSequence = m_tableToId.find( tableName );
00044   coral::ISchema& schema=m_coraldb.nominalSchema();
00045   if ( iSequence == m_tableToId.end() ) {
00046     // Make sure that the sequence table exists.
00047     if ( ! m_sequenceTableExists ) {
00048       throw cond::Exception("SequenceManager::incrementId");
00049     }
00050     // Lock the entry in the table.
00051     unsigned long long lastIdUsed = 0;
00052     if ( ! ( this->lockEntry( schema, tableName, lastIdUsed ) ) ) {
00053       // Create the entry in the table if it does not exist.
00054       coral::AttributeList rowData;
00055       rowData.extend<std::string>("REFTABLE_NAME");
00056       rowData.extend<unsigned long long>("IDVALUE");
00057       coral::AttributeList::iterator iAttribute = rowData.begin();
00058       iAttribute->data< std::string >() = tableName;
00059       ++iAttribute;
00060       unsigned long long startingIdValue = lastIdUsed;
00061       iAttribute->data< unsigned long long >() = startingIdValue;
00062       try{
00063         schema.tableHandle( m_sequenceTableName ).dataEditor().insertRow( rowData );
00064         m_tableToId.insert( std::make_pair( tableName, startingIdValue ) );
00065         return startingIdValue;
00066       }catch(const coral::DataEditorException& er){
00067         //std::cout<<"cannot insert, probably already created by others, try lock again"<<std::endl;
00068         this->lockEntry( schema, tableName, lastIdUsed );
00069         ++lastIdUsed;
00070         iSequence = m_tableToId.insert( std::make_pair( tableName, lastIdUsed ) ).first;
00071         m_whereData->begin()->data<std::string>() = tableName;
00072         schema.tableHandle(m_sequenceTableName).dataEditor().updateRows(m_setClause,m_whereClause,*m_whereData );
00073         return lastIdUsed;
00074         //startingIdValue = lastIdUsed+1;
00075         //m_tableToId.insert( std::make_pair( tableName, startingIdValue ) );
00076       }catch(std::exception& er){
00077         //std::cout<<"real error "<<er.what()<<std::endl;
00078         throw cond::Exception(er.what());
00079       }
00080 
00081     }
00082     // Add the entry into the map.
00083     iSequence = m_tableToId.insert( std::make_pair( tableName, lastIdUsed ) ).first;
00084   }
00085   // Increment the oid transiently
00086   unsigned long long& oid = iSequence->second;
00087   this->lockEntry( schema, tableName, oid );
00088   ++oid;
00089   // Increment the oid in the database as well
00090   
00091   m_whereData->begin()->data<std::string>() = tableName;
00092   schema.tableHandle(m_sequenceTableName).dataEditor().updateRows(m_setClause,m_whereClause,*m_whereData );
00093   
00094   return oid;
00095 }

void SequenceManager::init ( void   )  [private]

Definition at line 33 of file SequenceManager.cc.

References m_coraldb, m_sequenceTableExists, m_sequenceTableName, m_started, and cond::CoralTransaction::nominalSchema().

Referenced by SequenceManager().

00033                          { 
00034   m_sequenceTableExists=m_coraldb.nominalSchema().existsTable(m_sequenceTableName) ;
00035   m_started=true;
00036 }

bool SequenceManager::lockEntry ( coral::ISchema &  schema,
const std::string &  reftableName,
unsigned long long &  lastId 
) [private]

Locks the id entry in the ref table and returns the lastId value.

Definition at line 163 of file SequenceManager.cc.

References m_sequenceTableName, m_whereClause, and m_whereData.

Referenced by incrementId().

00165                                                               {
00166   std::auto_ptr< coral::IQuery > query( schema.tableHandle(m_sequenceTableName).newQuery());
00167   query->limitReturnedRows( 1, 0 );
00168   query->addToOutputList( std::string("IDVALUE") );
00169   query->defineOutputType( std::string("IDVALUE"), coral::AttributeSpecification::typeNameForType<unsigned long long>() );
00170   query->setForUpdate();
00171   m_whereData->begin()->data< std::string >() = sequenceName;
00172   query->setCondition( m_whereClause, *m_whereData );
00173   coral::ICursor& cursor = query->execute();
00174   if ( cursor.next() ) {
00175     lastId = cursor.currentRow().begin()->data< unsigned long long >();
00176     //std::cout<<"sequence table lastId "<<lastId<<std::endl;
00177     //cursor.close();
00178     //std::cout<<"SequenceManager::lockEntry return true"<<std::endl;
00179     return true;
00180   }else {
00181     cursor.close();
00182     //std::cout<<"SequenceManager::lockEntry return false"<<std::endl;
00183     return false;
00184   }
00185 }

void cond::SequenceManager::updateId ( const std::string &  reftableName,
unsigned long long  lastId 
)

Updates the last used id.


Member Data Documentation

cond::CoralTransaction& cond::SequenceManager::m_coraldb [private]

The coraldb in use.

Definition at line 56 of file SequenceManager.h.

Referenced by createSequencesTable(), incrementId(), and init().

bool cond::SequenceManager::m_sequenceTableExists [private]

Flag indicating whether the sequence table exists.

Definition at line 65 of file SequenceManager.h.

Referenced by createSequencesTable(), existSequencesTable(), incrementId(), and init().

std::string cond::SequenceManager::m_sequenceTableName [private]

Sequence table name.

Definition at line 59 of file SequenceManager.h.

Referenced by createSequencesTable(), incrementId(), init(), and lockEntry().

std::string cond::SequenceManager::m_setClause [private]

The set clause for updating a sequence entry.

Definition at line 74 of file SequenceManager.h.

Referenced by incrementId().

bool cond::SequenceManager::m_started [private]

Definition at line 75 of file SequenceManager.h.

Referenced by init().

std::map< std::string, unsigned long long > cond::SequenceManager::m_tableToId [private]

Map of ids used.

Definition at line 62 of file SequenceManager.h.

Referenced by clear(), and incrementId().

std::string cond::SequenceManager::m_whereClause [private]

The where clause pinning a sequence entry.

Definition at line 68 of file SequenceManager.h.

Referenced by incrementId(), and lockEntry().

coral::AttributeList* cond::SequenceManager::m_whereData [private]

The data for the where clause.

Definition at line 71 of file SequenceManager.h.

Referenced by incrementId(), lockEntry(), SequenceManager(), and ~SequenceManager().


The documentation for this class was generated from the following files:
Generated on Tue Jun 9 18:36:49 2009 for CMSSW by  doxygen 1.5.4