CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Private Member Functions | Private Attributes
cond::SequenceManager Class Reference

#include <SequenceManager.h>

Public Member Functions

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

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. More...
 

Private Attributes

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

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.

16  :
17  m_coraldb(coraldb),
18  m_sequenceTableName( sequenceTableName ),
19  m_tableToId(),
20  m_sequenceTableExists( false ),
21  m_whereClause( std::string("REFTABLE_NAME")+" =:"+std::string("REFTABLE_NAME")),
22  m_whereData( 0 ),
23  m_setClause( std::string("IDVALUE")+" = "+std::string("IDVALUE")+" + 1"),
24  m_started( false )
25 {
26  m_whereData = new coral::AttributeList;
27  m_whereData->extend<std::string>(std::string("REFTABLE_NAME"));
28  init();
29 }
std::map< std::string, unsigned long long > m_tableToId
Map of ids used.
std::string m_whereClause
The where clause pinning a sequence entry.
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.
cond::DbSession m_coraldb
The coraldb in use.
coral::AttributeList * m_whereData
The data for the where clause.
SequenceManager::~SequenceManager ( )

Destructor.

Definition at line 35 of file SequenceManager.cc.

36 {
37  delete m_whereData;
38 }
coral::AttributeList * m_whereData
The data for the where clause.

Member Function Documentation

void SequenceManager::clear ( void  )
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().

143 {
144  coral::ISchema& schema= m_coraldb.nominalSchema();
145  coral::TableDescription description( "CONDSEQ" );
147  description.insertColumn(std::string("REFTABLE_NAME"),
148  coral::AttributeSpecification::typeNameForType<std::string>() );
149  description.setNotNullConstraint(std::string("REFTABLE_NAME"));
150  description.insertColumn(std::string("IDVALUE"),
151  coral::AttributeSpecification::typeNameForType<unsigned long long>() );
152  description.setNotNullConstraint(std::string("IDVALUE"));
153  description.setPrimaryKey( std::vector< std::string >( 1, std::string("REFTABLE_NAME")));
154  schema.createTable( description ).privilegeManager().grantToPublic( coral::ITablePrivilegeManager::Select );
156 }
bool m_sequenceTableExists
Flag indicating whether the sequence table exists.
std::string m_sequenceTableName
Sequence table name.
tuple description
Definition: idDealer.py:66
coral::ISchema & nominalSchema()
Definition: DbSession.cc:228
cond::DbSession m_coraldb
The coraldb in use.
bool SequenceManager::existSequencesTable ( )

Whether sequence table exists.

Definition at line 138 of file SequenceManager.cc.

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

138  {
139  return m_sequenceTableExists;
140 }
bool m_sequenceTableExists
Flag indicating whether the sequence table exists.
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 cppFunctionSkipper::exception, edm::hlt::Exception, python.IdGenerator::schema, and AlCaHLTBitMon_QueryRunRegistry::string.

40  {
41  std::map< std::string, unsigned long long >::iterator iSequence = m_tableToId.find( tableName );
42  coral::ISchema& schema=m_coraldb.nominalSchema();
43  if ( iSequence == m_tableToId.end() ) {
44  // Make sure that the sequence table exists.
45  if ( ! m_sequenceTableExists ) {
46  throw cond::Exception("SequenceManager::incrementId");
47  }
48  // Lock the entry in the table.
49  unsigned long long lastIdUsed = 0;
50  if ( ! ( this->lockEntry( schema, tableName, lastIdUsed ) ) ) {
51  // Create the entry in the table if it does not exist.
52  coral::AttributeList rowData;
53  rowData.extend<std::string>("REFTABLE_NAME");
54  rowData.extend<unsigned long long>("IDVALUE");
55  coral::AttributeList::iterator iAttribute = rowData.begin();
56  iAttribute->data< std::string >() = tableName;
57  ++iAttribute;
58  unsigned long long startingIdValue = lastIdUsed;
59  iAttribute->data< unsigned long long >() = startingIdValue;
60  try{
61  schema.tableHandle( m_sequenceTableName ).dataEditor().insertRow( rowData );
62  m_tableToId.insert( std::make_pair( tableName, startingIdValue ) );
63  return startingIdValue;
64  }catch(const coral::DataEditorException& er){
65  this->lockEntry( schema, tableName, lastIdUsed );
66  ++lastIdUsed;
67  iSequence = m_tableToId.insert( std::make_pair( tableName, lastIdUsed ) ).first;
68  m_whereData->begin()->data<std::string>() = tableName;
69  schema.tableHandle(m_sequenceTableName).dataEditor().updateRows(m_setClause,m_whereClause,*m_whereData );
70  return lastIdUsed;
71  //startingIdValue = lastIdUsed+1;
72  //m_tableToId.insert( std::make_pair( tableName, startingIdValue ) );
73  }catch(std::exception& er){
74  throw cond::Exception(er.what());
75  }
76 
77  }
78  // Add the entry into the map.
79  iSequence = m_tableToId.insert( std::make_pair( tableName, lastIdUsed ) ).first;
80  }
81  // Increment the oid transiently
82  unsigned long long& oid = iSequence->second;
83  this->lockEntry( schema, tableName, oid );
84  ++oid;
85  // Increment the oid in the database as well
86 
87  m_whereData->begin()->data<std::string>() = tableName;
88  schema.tableHandle(m_sequenceTableName).dataEditor().updateRows(m_setClause,m_whereClause,*m_whereData );
89 
90  return oid;
91 }
std::map< std::string, unsigned long long > m_tableToId
Map of ids used.
std::string m_whereClause
The where clause pinning a sequence entry.
bool m_sequenceTableExists
Flag indicating whether the sequence table exists.
std::string m_sequenceTableName
Sequence table name.
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.
std::string m_setClause
The set clause for updating a sequence entry.
coral::ISchema & nominalSchema()
Definition: DbSession.cc:228
cond::DbSession m_coraldb
The coraldb in use.
coral::AttributeList * m_whereData
The data for the where clause.
void SequenceManager::init ( void  )
private

Definition at line 31 of file SequenceManager.cc.

Referenced by SequenceManager().

31  {
33  m_started=true;
34 }
bool m_sequenceTableExists
Flag indicating whether the sequence table exists.
std::string m_sequenceTableName
Sequence table name.
coral::ISchema & nominalSchema()
Definition: DbSession.cc:228
cond::DbSession m_coraldb
The coraldb in use.
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.

161  {
162  std::auto_ptr< coral::IQuery > query( schema.tableHandle(m_sequenceTableName).newQuery());
163  query->limitReturnedRows( 1, 0 );
164  query->addToOutputList( std::string("IDVALUE") );
165  query->defineOutputType( std::string("IDVALUE"), coral::AttributeSpecification::typeNameForType<unsigned long long>() );
166  query->setForUpdate();
167  m_whereData->begin()->data< std::string >() = sequenceName;
168  query->setCondition( m_whereClause, *m_whereData );
169  coral::ICursor& cursor = query->execute();
170  if ( cursor.next() ) {
171  lastId = cursor.currentRow().begin()->data< unsigned long long >();
172  return true;
173  }else {
174  cursor.close();
175  return false;
176  }
177 }
std::string m_whereClause
The where clause pinning a sequence entry.
std::string m_sequenceTableName
Sequence table name.
tuple query
Definition: o2o.py:269
coral::AttributeList * m_whereData
The data for the where clause.
void cond::SequenceManager::updateId ( const std::string &  reftableName,
unsigned long long  lastId 
)

Updates the last used id.

Member Data Documentation

cond::DbSession cond::SequenceManager::m_coraldb
private

The coraldb in use.

Definition at line 57 of file SequenceManager.h.

bool cond::SequenceManager::m_sequenceTableExists
private

Flag indicating whether the sequence table exists.

Definition at line 66 of file SequenceManager.h.

std::string cond::SequenceManager::m_sequenceTableName
private

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.

bool cond::SequenceManager::m_started
private

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().