CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SequenceManager.cc
Go to the documentation of this file.
3 #include "RelationalAccess/ISchema.h"
4 #include "RelationalAccess/TableDescription.h"
5 #include "RelationalAccess/ITable.h"
6 #include "RelationalAccess/ITableDataEditor.h"
7 #include "RelationalAccess/ITablePrivilegeManager.h"
8 #include "RelationalAccess/IQuery.h"
9 #include "RelationalAccess/ICursor.h"
10 #include "CoralBase/Attribute.h"
11 #include "CoralBase/AttributeList.h"
12 #include "CoralBase/AttributeSpecification.h"
13 #include "RelationalAccess/SchemaException.h"
14 #include <memory>
16  const std::string& sequenceTableName):
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 }
30 void
32  m_sequenceTableExists=m_coraldb.nominalSchema().existsTable(m_sequenceTableName) ;
33  m_started=true;
34 }
36 {
37  delete m_whereData;
38 }
39 unsigned long long
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 }
92 /*
93 void
94 cond::SequenceManager::updateId( const std::string& tableName,
95  unsigned long long lastId ){
96  // Make sure that the sequence table exists.
97  if ( ! m_sequenceTableExists ) {
98  throw;
99  }
100  bool update = false;
101  coral::IQuery* query = m_coraldb.nominalSchema().tableHandle( m_sequenceTableName ).newQuery();
102  query->limitReturnedRows( 1, 0 );
103  query->addToOutputList( std::string("IDVALUE") );
104  query->defineOutputType( std::string("IDVALUE"),coral::AttributeSpecification::typeNameForType<unsigned long long>() );
105  m_whereData->begin()->data< std::string >() = tableName;
106  query->setCondition( m_whereClause, *m_whereData );
107  coral::ICursor& cursor = query->execute();
108  if ( cursor.next() ) {
109  update = true;
110  }
111  delete query;
112 
113  coral::AttributeList rowData;
114  rowData.extend<unsigned long long>( std::string("IDVALUE") );
115  rowData.extend<std::string>( std::string("REFTABLE_NAME") );
116  coral::AttributeList::iterator iAttribute = rowData.begin();
117  iAttribute->data< unsigned long long >() = lastId;
118  ++iAttribute;
119  iAttribute->data< std::string >() = tableName;
120  coral::ISchema& schema= m_coraldb.nominalSchema();
121  if ( update ) {
122  // Update the entry in the table
123  std::string setClause(std::string("IDVALUE")+" =: "+std::string("IDVALUE"));
124  schema.tableHandle( m_sequenceTableName ).dataEditor().updateRows( setClause,m_whereClause,rowData );
125  m_tableToId.erase( tableName );
126  } else {
127  schema.tableHandle( m_sequenceTableName ).dataEditor().insertRow( rowData );
128  }
129  m_tableToId.insert( std::make_pair( tableName, lastId ) );
130 }
131 */
132 void
134 {
135  m_tableToId.clear();
136 }
137 bool
139  return m_sequenceTableExists;
140 }
141 void
143 {
144  coral::ISchema& schema= m_coraldb.nominalSchema();
145  coral::TableDescription description( "CONDSEQ" );
146  description.setName(m_sequenceTableName);
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 );
155  m_sequenceTableExists=true;
156 }
157 
158 bool
160  const std::string& sequenceName,
161  unsigned long long& lastId ){
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 }
~SequenceManager()
Destructor.
bool existSequencesTable()
Whether sequence table exists.
void createSequencesTable()
Creates the table holding the sequences.
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.
SequenceManager(cond::DbSession &coraldb, const std::string &sequenceTableName)
Constructor.
tuple description
Definition: idDealer.py:66
void clear()
Clears the internal state.
tuple query
Definition: o2o.py:269
coral::AttributeList * m_whereData
The data for the where clause.
unsigned long long incrementId(const std::string &reftableName)
Increments and returns a new valid oid for a table.