CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
IOVSchema.cc
Go to the documentation of this file.
2 #include "IOVSchema.h"
3 //
4 #include <openssl/sha.h>
5 
6 namespace cond {
7 
8  namespace persistency {
9 
10  cond::Hash makeHash( const std::string& objectType, const cond::Binary& data ){
11  SHA_CTX ctx;
12  if( !SHA1_Init( &ctx ) ){
13  throwException( "SHA1 initialization error.","IOVSchema::makeHash");
14  }
15  if( !SHA1_Update( &ctx, objectType.c_str(), objectType.size() ) ){
16  throwException( "SHA1 processing error (1).","IOVSchema::makeHash");
17  }
18  if( !SHA1_Update( &ctx, data.data(), data.size() ) ){
19  throwException( "SHA1 processing error (2).","IOVSchema::makeHash");
20  }
21  unsigned char hash[SHA_DIGEST_LENGTH];
22  if( !SHA1_Final(hash, &ctx) ){
23  throwException( "SHA1 finalization error.","IOVSchema::makeHash");
24  }
25 
26  char tmp[SHA_DIGEST_LENGTH*2+1];
27  // re-write bytes in hex
28  for (unsigned int i = 0; i < 20; i++) {
29  ::sprintf(&tmp[i * 2], "%02x", hash[i]);
30  }
31  tmp[20*2] = 0;
32  return tmp;
33  }
34 
35  TAG::Table::Table( coral::ISchema& schema ):
36  m_schema( schema ){
37  }
38 
39  bool TAG::Table::Table::exists(){
40  return existsTable( m_schema, tname );
41  }
42 
44  if( exists() ){
45  throwException( "TAG table already exists in this schema.",
46  "TAG::Table::create");
47  }
49  descr.setPrimaryKey<NAME>();
50  createTable( m_schema, descr.get() );
51  }
52 
54  Query< NAME > q( m_schema );
55  q.addCondition<NAME>( name );
56  for ( auto row : q ) {}
57 
58  return q.retrievedRows();
59  }
60 
62  cond::TimeType& timeType,
63  std::string& objectType,
64  cond::SynchronizationType& synchronizationType,
65  cond::Time_t& endOfValidity,
67  cond::Time_t& lastValidatedTime ){
69  q.addCondition<NAME>( name );
70  for ( auto row : q ) std::tie( timeType, objectType, synchronizationType, endOfValidity, description, lastValidatedTime ) = row;
71 
72  return q.retrievedRows();
73  }
74 
77  boost::posix_time::ptime& insertionTime,
78  boost::posix_time::ptime& modificationTime ){
80  q.addCondition<NAME>( name );
81  for ( auto row : q ) std::tie( description, insertionTime, modificationTime ) = row;
82  return q.retrievedRows();
83  }
84 
86  cond::TimeType timeType,
87  const std::string& objectType,
88  cond::SynchronizationType synchronizationType,
89  cond::Time_t endOfValidity,
90  const std::string& description,
91  cond::Time_t lastValidatedTime,
92  const boost::posix_time::ptime& insertionTime ){
94  dataToInsert( std::tie( name, timeType, objectType, synchronizationType, endOfValidity, description, lastValidatedTime, insertionTime, insertionTime ) );
95  insertInTable( m_schema, tname, dataToInsert.get() );
96  }
97 
99  cond::Time_t& endOfValidity,
100  const std::string& description,
101  cond::Time_t lastValidatedTime,
102  const boost::posix_time::ptime& updateTime ){
103  UpdateBuffer buffer;
104  buffer.setColumnData< END_OF_VALIDITY, DESCRIPTION, LAST_VALIDATED_TIME, MODIFICATION_TIME >( std::tie( endOfValidity, description, lastValidatedTime, updateTime ) );
105  buffer.addWhereCondition<NAME>( name );
106  updateTable( m_schema, tname, buffer );
107  }
108 
110  cond::Time_t lastValidatedTime,
111  const boost::posix_time::ptime& updateTime ){
112  UpdateBuffer buffer;
113  buffer.setColumnData< LAST_VALIDATED_TIME, MODIFICATION_TIME >( std::tie( lastValidatedTime, updateTime ) );
114  buffer.addWhereCondition<NAME>( name );
115  updateTable( m_schema, tname, buffer );
116  }
117 
118  IOV::Table::Table( coral::ISchema& schema ):
119  m_schema( schema ){
120  }
121 
123  return existsTable( m_schema, tname );
124  }
125 
127  if( exists()){
128  throwException( "IOV table already exists in this schema.",
129  "IOV::Schema::create");
130  }
131 
134  descr.setForeignKey< TAG_NAME, TAG::NAME >( "TAG_NAME_FK" );
135  descr.setForeignKey< PAYLOAD_HASH, PAYLOAD::HASH >( "PAYLOAD_HASH_FK" );
136  createTable( m_schema, descr.get() );
137  }
138 
139  size_t IOV::Table::selectGroups( const std::string& tag, std::vector<cond::Time_t>& groups ){
140  Query< SINCE_GROUP > q( m_schema, true );
141  q.addCondition<TAG_NAME>( tag );
143  for( auto row : q ){
144  groups.push_back(std::get<0>(row));
145  }
146  return q.retrievedRows();
147  }
148 
149  size_t IOV::Table::selectSnapshotGroups( const std::string& tag, const boost::posix_time::ptime& snapshotTime, std::vector<cond::Time_t>& groups ){
150  Query< SINCE_GROUP > q( m_schema, true );
151  q.addCondition<TAG_NAME>( tag );
152  q.addCondition<INSERTION_TIME>( snapshotTime,"<=" );
154  for( auto row : q ){
155  groups.push_back(std::get<0>(row));
156  }
157  return q.retrievedRows();
158  }
159 
160  size_t IOV::Table::selectLatestByGroup( const std::string& tag, cond::Time_t lowerSinceGroup, cond::Time_t upperSinceGroup ,
161  std::vector<std::tuple<cond::Time_t,cond::Hash> >& iovs ){
162  Query< SINCE, PAYLOAD_HASH > q( m_schema );
163  q.addCondition<TAG_NAME>( tag );
164  if( lowerSinceGroup > 0 ) q.addCondition<SINCE>( lowerSinceGroup, ">=" );
165  if( upperSinceGroup < cond::time::MAX_VAL ) q.addCondition<SINCE>( upperSinceGroup, "<" );
166  q.addOrderClause<SINCE>();
168  size_t initialSize = iovs.size();
169  for( auto row : q ){
170  // starting from the second iov in the array, skip the rows with older timestamp
171  if( iovs.size()-initialSize && std::get<0>(iovs.back()) == std::get<0>(row) ) continue;
172  iovs.push_back( row );
173  }
174  return iovs.size()-initialSize;
175  }
176 
177  size_t IOV::Table::selectSnapshotByGroup( const std::string& tag, cond::Time_t lowerSinceGroup, cond::Time_t upperSinceGroup,
178  const boost::posix_time::ptime& snapshotTime,
179  std::vector<std::tuple<cond::Time_t,cond::Hash> >& iovs ){
180  Query< SINCE, PAYLOAD_HASH > q( m_schema );
181  q.addCondition<TAG_NAME>( tag );
182  if( lowerSinceGroup > 0 ) q.addCondition<SINCE>( lowerSinceGroup, ">=" );
183  if( upperSinceGroup < cond::time::MAX_VAL ) q.addCondition<SINCE>( upperSinceGroup, "<" );
184  q.addCondition<INSERTION_TIME>( snapshotTime,"<=" );
185  q.addOrderClause<SINCE>();
187  size_t initialSize = iovs.size();
188  for ( auto row : q ) {
189  // starting from the second iov in the array, skip the rows with older timestamp
190  if( iovs.size()-initialSize && std::get<0>(iovs.back()) == std::get<0>(row) ) continue;
191  iovs.push_back( row );
192  }
193  return iovs.size()-initialSize;
194  }
195 
197  std::vector<std::tuple<cond::Time_t,cond::Hash> >& iovs ){
198  Query< SINCE, PAYLOAD_HASH > q( m_schema );
199  q.addCondition<TAG_NAME>( tag );
200  q.addOrderClause<SINCE>();
202  size_t initialSize = iovs.size();
203  for ( auto row : q ) {
204  // starting from the second iov in the array, skip the rows with older timestamp
205  if( iovs.size()-initialSize && std::get<0>(iovs.back()) == std::get<0>(row) ) continue;
206  iovs.push_back( row );
207  }
208  return iovs.size()-initialSize;
209  }
210 
212  Query< SINCE, PAYLOAD_HASH > q( m_schema );
213  q.addCondition<TAG_NAME>( tag );
214  q.addOrderClause<SINCE>( false );
216  for ( auto row : q ) {
217  since = std::get<0>(row);
218  hash = std::get<1>(row);
219  return true;
220  }
221  return false;
222  }
223 
224  bool IOV::Table::getSize( const std::string& tag, size_t& size ){
225  Query< SEQUENCE_SIZE > q( m_schema );
226  q.addCondition<TAG_NAME>( tag );
227  for ( auto row : q ) {
228  size = std::get<0>(row);
229  return true;
230  }
231  return false;
232  }
233 
234  bool IOV::Table::getSnapshotSize( const std::string& tag, const boost::posix_time::ptime& snapshotTime, size_t& size ){
235  Query< SEQUENCE_SIZE > q( m_schema );
236  q.addCondition<TAG_NAME>( tag );
237  q.addCondition<INSERTION_TIME>( snapshotTime,"<=" );
238  for ( auto row : q ) {
239  size = std::get<0>(row);
240  return true;
241  }
242  return false;
243  }
244 
246  cond::Time_t since,
247  cond::Hash payloadHash,
248  const boost::posix_time::ptime& insertTimeStamp ){
249  RowBuffer< TAG_NAME, SINCE, PAYLOAD_HASH, INSERTION_TIME > dataToInsert( std::tie( tag, since, payloadHash, insertTimeStamp ) );
250  insertInTable( m_schema, tname, dataToInsert.get() );
251  }
252 
254  const std::vector<std::tuple<cond::Time_t,cond::Hash,boost::posix_time::ptime> >& iovs ){
256  for( auto row : iovs ) inserter.insert( std::tuple_cat( std::tie(tag),row ) );
257 
258  inserter.flush();
259  }
260 
262  DeleteBuffer buffer;
263  buffer.addWhereCondition<TAG_NAME>( tag );
264  deleteFromTable( m_schema, tname, buffer );
265  }
266 
267  PAYLOAD::Table::Table( coral::ISchema& schema ):
268  m_schema( schema ){
269  }
270 
272  return existsTable( m_schema, tname );
273  }
274 
276  if( exists()){
277  throwException( "Payload table already exists in this schema.",
278  "PAYLOAD::Schema::create");
279  }
280 
282  descr.setPrimaryKey<HASH>();
283  createTable( m_schema, descr.get() );
284  }
285 
286  bool PAYLOAD::Table::select( const cond::Hash& payloadHash ){
287  Query< HASH > q( m_schema );
288  q.addCondition<HASH>( payloadHash );
289  for ( auto row : q ) {}
290 
291  return q.retrievedRows();
292  }
293 
294  bool PAYLOAD::Table::getType( const cond::Hash& payloadHash, std::string& objectType ){
295  Query< OBJECT_TYPE > q( m_schema );
296  q.addCondition<HASH>( payloadHash );
297  for ( auto row : q ) {
298  objectType = std::get<0>(row);
299  }
300 
301  return q.retrievedRows();
302  }
303 
304  bool PAYLOAD::Table::select( const cond::Hash& payloadHash,
305  std::string& objectType,
306  cond::Binary& payloadData,
307  cond::Binary& streamerInfoData ){
309  q.addCondition<HASH>( payloadHash );
310  for ( auto row : q ) {
311  std::tie( payloadData, streamerInfoData, objectType ) = row;
312  }
313  return q.retrievedRows();
314  }
315 
316  bool PAYLOAD::Table::insert( const cond::Hash& payloadHash,
317  const std::string& objectType,
318  const cond::Binary& payloadData,
319  const cond::Binary& streamerInfoData,
320  const boost::posix_time::ptime& insertionTime ){
321  std::string version("dummy");
322  cond::Binary sinfoData( streamerInfoData );
323  if( !sinfoData.size() ) sinfoData.copy( std::string("0") );
324  RowBuffer< HASH, OBJECT_TYPE, DATA, STREAMER_INFO, VERSION, INSERTION_TIME > dataToInsert( std::tie( payloadHash, objectType, payloadData, sinfoData, version, insertionTime ) );
325  bool failOnDuplicate = false;
326  return insertInTable( m_schema, tname, dataToInsert.get(), failOnDuplicate );
327  }
328 
330  const cond::Binary& payloadData,
331  const cond::Binary& streamerInfoData,
332  const boost::posix_time::ptime& insertionTime ){
333  cond::Hash payloadHash = makeHash( payloadObjectType, payloadData );
334  // the check on the hash existance is only required to avoid the error message printing in SQLite! once this is removed, this check is useless...
335  if( !select( payloadHash ) ){
336  insert( payloadHash, payloadObjectType, payloadData, streamerInfoData, insertionTime );
337  }
338  return payloadHash;
339  }
340 
342  m_schema( schema ){
343  }
344 
346  return existsTable( m_schema, tname );
347  }
348 
350  if( exists() ){
351  throwException( "TAG_MIGRATIONtable already exists in this schema.",
352  "TAG::create");
353  }
356  descr.setForeignKey< TAG_NAME, TAG::NAME >( "TAG_NAME_FK" );
357  createTable( m_schema, descr.get() );
358  }
359 
360  bool TAG_MIGRATION::Table::select( const std::string& sourceAccount, const std::string& sourceTag, std::string& tagName, int& statusCode ){
361  Query< TAG_NAME, STATUS_CODE > q( m_schema );
362  q.addCondition<SOURCE_ACCOUNT>( sourceAccount );
363  q.addCondition<SOURCE_TAG>( sourceTag );
364  for ( auto row : q ) {
365  std::tie( tagName, statusCode ) = row;
366  }
367 
368  return q.retrievedRows();
369 
370  }
371 
372  void TAG_MIGRATION::Table::insert( const std::string& sourceAccount, const std::string& sourceTag, const std::string& tagName,
373  int statusCode, const boost::posix_time::ptime& insertionTime ){
375  dataToInsert( std::tie( sourceAccount, sourceTag, tagName, statusCode, insertionTime ) );
376  insertInTable( m_schema, tname, dataToInsert.get() );
377  }
378 
379  void TAG_MIGRATION::Table::updateValidationCode( const std::string& sourceAccount, const std::string& sourceTag, int statusCode ){
380  UpdateBuffer buffer;
381  buffer.setColumnData< STATUS_CODE >( std::tie( statusCode ) );
382  buffer.addWhereCondition<SOURCE_ACCOUNT>( sourceAccount );
383  buffer.addWhereCondition<SOURCE_TAG>( sourceTag );
384  updateTable( m_schema, tname, buffer );
385  }
386 
387  IOVSchema::IOVSchema( coral::ISchema& schema ):
388  m_tagTable( schema ),
389  m_iovTable( schema ),
390  m_payloadTable( schema ),
391  m_tagMigrationTable( schema ){
392  }
393 
395  if( !m_tagTable.exists() ) return false;
396  if( !m_payloadTable.exists() ) return false;
397  if( !m_iovTable.exists() ) return false;
398  return true;
399  }
400 
402  bool created = false;
403  if( !exists() ){
404  m_tagTable.create();
406  m_iovTable.create();
407  created = true;
408  }
409  return created;
410  }
411 
413  return m_tagTable;
414  }
415 
417  return m_iovTable;
418  }
419 
421  return m_payloadTable;
422  }
423 
425  return m_tagMigrationTable;
426  }
427 
428  }
429 }
430 
void updateValidationCode(const std::string &sourceAccount, const std::string &sourceTag, int statusCode)
Definition: IOVSchema.cc:379
int i
Definition: DBlmapReader.cc:9
bool getSnapshotSize(const std::string &tag, const boost::posix_time::ptime &snapshotTime, size_t &size)
Definition: IOVSchema.cc:234
static char const * tname
Definition: IOVSchema.h:140
bool getSize(const std::string &tag, size_t &size)
Definition: IOVSchema.cc:224
const coral::TableDescription & get()
Definition: DbCore.h:266
void updateValidity(const std::string &name, cond::Time_t lastValidatedTime, const boost::posix_time::ptime &updateTime)
Definition: IOVSchema.cc:109
void insert(const P &params)
Definition: DbCore.h:584
size_t selectLatest(const std::string &tag, std::vector< std::tuple< cond::Time_t, cond::Hash > > &iovs)
Definition: IOVSchema.cc:196
bool getLastIov(const std::string &tag, cond::Time_t &since, cond::Hash &hash)
Definition: IOVSchema.cc:211
PAYLOAD::Table m_payloadTable
Definition: IOVSchema.h:176
TimeType
Definition: Time.h:21
size_t size() const
Definition: Binary.cc:57
void insertMany(const std::string &tag, const std::vector< std::tuple< cond::Time_t, cond::Hash, boost::posix_time::ptime > > &iovs)
Definition: IOVSchema.cc:253
size_t selectGroups(const std::string &tag, std::vector< cond::Time_t > &groups)
Definition: IOVSchema.cc:139
static char const * tname
Definition: IOVSchema.h:48
void setForeignKey(const std::string &name)
Definition: DbCore.h:261
bool select(const cond::Hash &payloadHash)
Definition: IOVSchema.cc:286
unsigned long long Time_t
Definition: Time.h:16
void addCondition(const T &value, const std::string condition="=")
Definition: DbCore.h:428
static char const * tname
Definition: IOVSchema.h:13
bool select(const std::string &sourceAccount, const std::string &sourceTag, std::string &tagName, int &statusCode)
Definition: IOVSchema.cc:360
const coral::AttributeList & get() const
Definition: DbCore.h:171
void addOrderClause(bool ascending=true)
Definition: DbCore.h:439
TAG_MIGRATION::Table m_tagMigrationTable
Definition: IOVSchema.h:177
std::string Hash
Definition: Types.h:43
void erase(const std::string &tag)
Definition: IOVSchema.cc:261
void copy(const std::string &source)
Definition: Binary.cc:42
void update(const std::string &name, cond::Time_t &endOfValidity, const std::string &description, cond::Time_t lastValidatedTime, const boost::posix_time::ptime &updateTime)
Definition: IOVSchema.cc:98
IPayloadTable & payloadTable()
Definition: IOVSchema.cc:420
bool insert(Storage &iStorage, ItemType *iItem, const IdTag &iIdTag)
Definition: HCMethods.h:49
bool select(const std::string &name)
Definition: IOVSchema.cc:53
ITagMigrationTable & tagMigrationTable()
Definition: IOVSchema.cc:424
bool getType(const cond::Hash &payloadHash, std::string &objectType)
Definition: IOVSchema.cc:294
const void * data() const
Definition: Binary.cc:48
tuple description
Definition: idDealer.py:66
void setColumnData(const Params &params)
Definition: DbCore.h:508
Table(coral::ISchema &schema)
Definition: IOVSchema.cc:267
IOVSchema(coral::ISchema &schema)
Definition: IOVSchema.cc:387
cond::Hash insertIfNew(const std::string &objectType, const cond::Binary &payloadData, const cond::Binary &streamerInfoData, const boost::posix_time::ptime &insertionTime)
Definition: IOVSchema.cc:329
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
cond::Hash makeHash(const std::string &objectType, const cond::Binary &data)
Definition: IOVSchema.cc:10
SynchronizationType
Definition: Types.h:31
Table(coral::ISchema &schema)
Definition: IOVSchema.cc:35
size_t selectSnapshotByGroup(const std::string &tag, cond::Time_t lowerGroup, cond::Time_t upperGroup, const boost::posix_time::ptime &snapshotTime, std::vector< std::tuple< cond::Time_t, cond::Hash > > &iovs)
Definition: IOVSchema.cc:177
bool insert(const cond::Hash &payloadHash, const std::string &objectType, const cond::Binary &payloadData, const cond::Binary &streamerInfoData, const boost::posix_time::ptime &insertionTime)
Definition: IOVSchema.cc:316
volatile std::atomic< bool > shutdown_flag false
void insert(const std::string &sourceAccount, const std::string &sourceTag, const std::string &tagName, int statusCode, const boost::posix_time::ptime &insertionTime)
Definition: IOVSchema.cc:372
Table(coral::ISchema &schema)
Definition: IOVSchema.cc:118
static char const * tname
Definition: IOVSchema.h:79
void insert(const std::string &name, cond::TimeType timeType, const std::string &objectType, cond::SynchronizationType synchronizationType, cond::Time_t endOfValidity, const std::string &description, cond::Time_t lastValidatedTime, const boost::posix_time::ptime &insertionTime)
Definition: IOVSchema.cc:85
void addWhereCondition(const P &param, const std::string condition="=")
Definition: DbCore.h:517
bool getMetadata(const std::string &name, std::string &description, boost::posix_time::ptime &insertionTime, boost::posix_time::ptime &modificationTime)
Definition: IOVSchema.cc:75
const Time_t MAX_VAL(std::numeric_limits< Time_t >::max())
void insertOne(const std::string &tag, cond::Time_t since, cond::Hash payloadHash, const boost::posix_time::ptime &insertTime)
Definition: IOVSchema.cc:245
tuple size
Write out results.
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:11
size_t selectSnapshotGroups(const std::string &tag, const boost::posix_time::ptime &snapshotTime, std::vector< cond::Time_t > &groups)
Definition: IOVSchema.cc:149
size_t selectLatestByGroup(const std::string &tag, cond::Time_t lowerGroup, cond::Time_t upperGroup, std::vector< std::tuple< cond::Time_t, cond::Hash > > &iovs)
Definition: IOVSchema.cc:160
void addWhereCondition(const P &param, const std::string condition="=")
Definition: DbCore.h:551