CMS 3D CMS Logo

Public Member Functions | Private Member Functions | Private Attributes

cond::SequenceManager Class Reference

#include <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::DbSession &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::DbSession m_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 23 of file SequenceManager.h.


Constructor & Destructor Documentation

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

Constructor.

Definition at line 15 of file SequenceManager.cc.

References init(), m_whereData, and AlCaHLTBitMon_QueryRunRegistry::string.

                                                                          :
  m_coraldb(coraldb),
  m_sequenceTableName( sequenceTableName ),
  m_tableToId(),
  m_sequenceTableExists( false ),
  m_whereClause( std::string("REFTABLE_NAME")+" =:"+std::string("REFTABLE_NAME")), 
  m_whereData( 0 ),
  m_setClause( std::string("IDVALUE")+" = "+std::string("IDVALUE")+" + 1"),
  m_started( false )
{
  m_whereData = new coral::AttributeList; 
  m_whereData->extend<std::string>(std::string("REFTABLE_NAME"));
  init();
}
SequenceManager::~SequenceManager ( )

Destructor.

Definition at line 35 of file SequenceManager.cc.

{  
  delete m_whereData;
}

Member Function Documentation

void SequenceManager::clear ( void  )

Clears the internal state.

Definition at line 133 of file SequenceManager.cc.

{
  m_tableToId.clear();
}
void SequenceManager::createSequencesTable ( )

Creates the table holding the sequences.

Definition at line 142 of file SequenceManager.cc.

References idDealer::description, python::IdGenerator::schema, and AlCaHLTBitMon_QueryRunRegistry::string.

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

{
  coral::ISchema& schema= m_coraldb.nominalSchema();
  coral::TableDescription description( "CONDSEQ" );
  description.setName(m_sequenceTableName);
  description.insertColumn(std::string("REFTABLE_NAME"),
                           coral::AttributeSpecification::typeNameForType<std::string>() );
  description.setNotNullConstraint(std::string("REFTABLE_NAME"));
  description.insertColumn(std::string("IDVALUE"),
                           coral::AttributeSpecification::typeNameForType<unsigned long long>() );
  description.setNotNullConstraint(std::string("IDVALUE"));
  description.setPrimaryKey( std::vector< std::string >( 1, std::string("REFTABLE_NAME")));
  schema.createTable( description ).privilegeManager().grantToPublic( coral::ITablePrivilegeManager::Select );
  m_sequenceTableExists=true;
}
bool SequenceManager::existSequencesTable ( )

Whether sequence table exists.

Definition at line 138 of file SequenceManager.cc.

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

                                        {
  return m_sequenceTableExists;
}
unsigned long long SequenceManager::incrementId ( const std::string &  reftableName)

Increments and returns a new valid oid for a table.

Definition at line 40 of file SequenceManager.cc.

References exception, Exception, python::IdGenerator::schema, and AlCaHLTBitMon_QueryRunRegistry::string.

                                                            {
  std::map< std::string, unsigned long long >::iterator iSequence = m_tableToId.find( tableName );
  coral::ISchema& schema=m_coraldb.nominalSchema();
  if ( iSequence == m_tableToId.end() ) {
    // Make sure that the sequence table exists.
    if ( ! m_sequenceTableExists ) {
      throw cond::Exception("SequenceManager::incrementId");
    }
    // Lock the entry in the table.
    unsigned long long lastIdUsed = 0;
    if ( ! ( this->lockEntry( schema, tableName, lastIdUsed ) ) ) {
      // Create the entry in the table if it does not exist.
      coral::AttributeList rowData;
      rowData.extend<std::string>("REFTABLE_NAME");
      rowData.extend<unsigned long long>("IDVALUE");
      coral::AttributeList::iterator iAttribute = rowData.begin();
      iAttribute->data< std::string >() = tableName;
      ++iAttribute;
      unsigned long long startingIdValue = lastIdUsed;
      iAttribute->data< unsigned long long >() = startingIdValue;
      try{
        schema.tableHandle( m_sequenceTableName ).dataEditor().insertRow( rowData );
        m_tableToId.insert( std::make_pair( tableName, startingIdValue ) );
        return startingIdValue;
      }catch(const coral::DataEditorException& er){
        this->lockEntry( schema, tableName, lastIdUsed );
        ++lastIdUsed;
        iSequence = m_tableToId.insert( std::make_pair( tableName, lastIdUsed ) ).first;
        m_whereData->begin()->data<std::string>() = tableName;
        schema.tableHandle(m_sequenceTableName).dataEditor().updateRows(m_setClause,m_whereClause,*m_whereData );
        return lastIdUsed;
        //startingIdValue = lastIdUsed+1;
        //m_tableToId.insert( std::make_pair( tableName, startingIdValue ) );
      }catch(std::exception& er){
        throw cond::Exception(er.what());
      }

    }
    // Add the entry into the map.
    iSequence = m_tableToId.insert( std::make_pair( tableName, lastIdUsed ) ).first;
  }
  // Increment the oid transiently
  unsigned long long& oid = iSequence->second;
  this->lockEntry( schema, tableName, oid );
  ++oid;
  // Increment the oid in the database as well
  
  m_whereData->begin()->data<std::string>() = tableName;
  schema.tableHandle(m_sequenceTableName).dataEditor().updateRows(m_setClause,m_whereClause,*m_whereData );
  
  return oid;
}
void SequenceManager::init ( void  ) [private]

Definition at line 31 of file SequenceManager.cc.

Referenced by SequenceManager().

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 159 of file SequenceManager.cc.

References o2o::query, and AlCaHLTBitMon_QueryRunRegistry::string.

                                                              {
  std::auto_ptr< coral::IQuery > query( schema.tableHandle(m_sequenceTableName).newQuery());
  query->limitReturnedRows( 1, 0 );
  query->addToOutputList( std::string("IDVALUE") );
  query->defineOutputType( std::string("IDVALUE"), coral::AttributeSpecification::typeNameForType<unsigned long long>() );
  query->setForUpdate();
  m_whereData->begin()->data< std::string >() = sequenceName;
  query->setCondition( m_whereClause, *m_whereData );
  coral::ICursor& cursor = query->execute();
  if ( cursor.next() ) {
    lastId = cursor.currentRow().begin()->data< unsigned long long >();
    return true;
  }else {
    cursor.close();
    return false;
  }
}
void cond::SequenceManager::updateId ( const std::string &  reftableName,
unsigned long long  lastId 
)

Updates the last used id.


Member Data Documentation

The coraldb in use.

Definition at line 57 of file SequenceManager.h.

Flag indicating whether the sequence table exists.

Definition at line 66 of file SequenceManager.h.

Sequence table name.

Definition at line 60 of file SequenceManager.h.

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

The set clause for updating a sequence entry.

Definition at line 75 of file SequenceManager.h.

Definition at line 76 of file SequenceManager.h.

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

Map of ids used.

Definition at line 63 of file SequenceManager.h.

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

The where clause pinning a sequence entry.

Definition at line 69 of file SequenceManager.h.

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

The data for the where clause.

Definition at line 72 of file SequenceManager.h.

Referenced by SequenceManager().