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

References init(), and m_whereData.

17  :
18  m_coraldb(coraldb),
19  m_sequenceTableName( sequenceTableName ),
20  m_tableToId(),
21  m_sequenceTableExists( false ),
22  m_whereClause( std::string("REFTABLE_NAME")+" =:"+std::string("REFTABLE_NAME")),
23  m_whereData( 0 ),
24  m_setClause( std::string("IDVALUE")+" = "+std::string("IDVALUE")+" + 1"),
25  m_started( false )
26 {
27  m_whereData = new coral::AttributeList;
28  m_whereData->extend<std::string>(std::string("REFTABLE_NAME"));
29  init();
30 }
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 36 of file SequenceManager.cc.

37 {
38  delete m_whereData;
39 }
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 145 of file SequenceManager.cc.

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

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

146 {
147  coral::ISchema& schema= m_coraldb.nominalSchema();
148  coral::TableDescription description( "CONDSEQ" );
150  description.insertColumn(std::string("REFTABLE_NAME"),
151  coral::AttributeSpecification::typeNameForType<std::string>() );
152  description.setNotNullConstraint(std::string("REFTABLE_NAME"));
153  description.insertColumn(std::string("IDVALUE"),
154  coral::AttributeSpecification::typeNameForType<unsigned long long>() );
155  description.setNotNullConstraint(std::string("IDVALUE"));
156  description.setPrimaryKey( std::vector< std::string >( 1, std::string("REFTABLE_NAME")));
157  schema.createTable( description ).privilegeManager().grantToPublic( coral::ITablePrivilegeManager::Select );
159 }
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:175
cond::DbSession m_coraldb
The coraldb in use.
bool SequenceManager::existSequencesTable ( )

Whether sequence table exists.

Definition at line 141 of file SequenceManager.cc.

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

141  {
142  return m_sequenceTableExists;
143 }
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 41 of file SequenceManager.cc.

References cmsCodeRules.cppFunctionSkipper::exception, edm::hlt::Exception, and python.IdGenerator::schema.

41  {
42  std::map< std::string, unsigned long long >::iterator iSequence = m_tableToId.find( tableName );
43  coral::ISchema& schema=m_coraldb.nominalSchema();
44  if ( iSequence == m_tableToId.end() ) {
45  // Make sure that the sequence table exists.
46  if ( ! m_sequenceTableExists ) {
47  throw cond::Exception("SequenceManager::incrementId");
48  }
49  // Lock the entry in the table.
50  unsigned long long lastIdUsed = 0;
51  if ( ! ( this->lockEntry( schema, tableName, lastIdUsed ) ) ) {
52  // Create the entry in the table if it does not exist.
53  coral::AttributeList rowData;
54  rowData.extend<std::string>("REFTABLE_NAME");
55  rowData.extend<unsigned long long>("IDVALUE");
56  coral::AttributeList::iterator iAttribute = rowData.begin();
57  iAttribute->data< std::string >() = tableName;
58  ++iAttribute;
59  unsigned long long startingIdValue = lastIdUsed;
60  iAttribute->data< unsigned long long >() = startingIdValue;
61  try{
62  schema.tableHandle( m_sequenceTableName ).dataEditor().insertRow( rowData );
63  m_tableToId.insert( std::make_pair( tableName, startingIdValue ) );
64  return startingIdValue;
65  }catch(const coral::DataEditorException& er){
66  //std::cout<<"cannot insert, probably already created by others, try lock again"<<std::endl;
67  this->lockEntry( schema, tableName, lastIdUsed );
68  ++lastIdUsed;
69  iSequence = m_tableToId.insert( std::make_pair( tableName, lastIdUsed ) ).first;
70  m_whereData->begin()->data<std::string>() = tableName;
71  schema.tableHandle(m_sequenceTableName).dataEditor().updateRows(m_setClause,m_whereClause,*m_whereData );
72  return lastIdUsed;
73  //startingIdValue = lastIdUsed+1;
74  //m_tableToId.insert( std::make_pair( tableName, startingIdValue ) );
75  }catch(std::exception& er){
76  //std::cout<<"real error "<<er.what()<<std::endl;
77  throw cond::Exception(er.what());
78  }
79 
80  }
81  // Add the entry into the map.
82  iSequence = m_tableToId.insert( std::make_pair( tableName, lastIdUsed ) ).first;
83  }
84  // Increment the oid transiently
85  unsigned long long& oid = iSequence->second;
86  this->lockEntry( schema, tableName, oid );
87  ++oid;
88  // Increment the oid in the database as well
89 
90  m_whereData->begin()->data<std::string>() = tableName;
91  schema.tableHandle(m_sequenceTableName).dataEditor().updateRows(m_setClause,m_whereClause,*m_whereData );
92 
93  return oid;
94 }
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:175
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 32 of file SequenceManager.cc.

Referenced by SequenceManager().

32  {
34  m_started=true;
35 }
bool m_sequenceTableExists
Flag indicating whether the sequence table exists.
std::string m_sequenceTableName
Sequence table name.
coral::ISchema & nominalSchema()
Definition: DbSession.cc:175
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 162 of file SequenceManager.cc.

References o2o::query.

164  {
165  std::auto_ptr< coral::IQuery > query( schema.tableHandle(m_sequenceTableName).newQuery());
166  query->limitReturnedRows( 1, 0 );
167  query->addToOutputList( std::string("IDVALUE") );
168  query->defineOutputType( std::string("IDVALUE"), coral::AttributeSpecification::typeNameForType<unsigned long long>() );
169  query->setForUpdate();
170  m_whereData->begin()->data< std::string >() = sequenceName;
171  query->setCondition( m_whereClause, *m_whereData );
172  coral::ICursor& cursor = query->execute();
173  if ( cursor.next() ) {
174  lastId = cursor.currentRow().begin()->data< unsigned long long >();
175  //std::cout<<"sequence table lastId "<<lastId<<std::endl;
176  //cursor.close();
177  //std::cout<<"SequenceManager::lockEntry return true"<<std::endl;
178  return true;
179  }else {
180  cursor.close();
181  //std::cout<<"SequenceManager::lockEntry return false"<<std::endl;
182  return false;
183  }
184 }
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().