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 );
144  for( auto row : q ){
145  groups.push_back(std::get<0>(row));
146  }
147  return q.retrievedRows();
148  }
149 
150  size_t IOV::Table::selectSnapshotGroups( const std::string& tag, const boost::posix_time::ptime& snapshotTime, std::vector<cond::Time_t>& groups ){
151  Query< SINCE_GROUP > q( m_schema, true );
152  q.addCondition<TAG_NAME>( tag );
153  q.addCondition<INSERTION_TIME>( snapshotTime,"<=" );
156  for( auto row : q ){
157  groups.push_back(std::get<0>(row));
158  }
159  return q.retrievedRows();
160  }
161 
162  size_t IOV::Table::selectLatestByGroup( const std::string& tag, cond::Time_t lowerSinceGroup, cond::Time_t upperSinceGroup ,
163  std::vector<std::tuple<cond::Time_t,cond::Hash> >& iovs ){
164  Query< SINCE, PAYLOAD_HASH > q( m_schema );
165  q.addCondition<TAG_NAME>( tag );
166  if( lowerSinceGroup > 0 ) q.addCondition<SINCE>( lowerSinceGroup, ">=" );
167  if( upperSinceGroup < cond::time::MAX_VAL ) q.addCondition<SINCE>( upperSinceGroup, "<" );
168  q.addOrderClause<SINCE>();
170  size_t initialSize = iovs.size();
171  for( auto row : q ){
172  // starting from the second iov in the array, skip the rows with older timestamp
173  if( iovs.size()-initialSize && std::get<0>(iovs.back()) == std::get<0>(row) ) continue;
174  iovs.push_back( row );
175  }
176  return iovs.size()-initialSize;
177  }
178 
179  size_t IOV::Table::selectSnapshotByGroup( const std::string& tag, cond::Time_t lowerSinceGroup, cond::Time_t upperSinceGroup,
180  const boost::posix_time::ptime& snapshotTime,
181  std::vector<std::tuple<cond::Time_t,cond::Hash> >& iovs ){
182  Query< SINCE, PAYLOAD_HASH > q( m_schema );
183  q.addCondition<TAG_NAME>( tag );
184  if( lowerSinceGroup > 0 ) q.addCondition<SINCE>( lowerSinceGroup, ">=" );
185  if( upperSinceGroup < cond::time::MAX_VAL ) q.addCondition<SINCE>( upperSinceGroup, "<" );
186  q.addCondition<INSERTION_TIME>( snapshotTime,"<=" );
187  q.addOrderClause<SINCE>();
189  size_t initialSize = iovs.size();
190  for ( auto row : q ) {
191  // starting from the second iov in the array, skip the rows with older timestamp
192  if( iovs.size()-initialSize && std::get<0>(iovs.back()) == std::get<0>(row) ) continue;
193  iovs.push_back( row );
194  }
195  return iovs.size()-initialSize;
196  }
197 
199  std::vector<std::tuple<cond::Time_t,cond::Hash> >& iovs ){
200  Query< SINCE, PAYLOAD_HASH > q( m_schema );
201  q.addCondition<TAG_NAME>( tag );
202  q.addOrderClause<SINCE>();
204  size_t initialSize = iovs.size();
205  for ( auto row : q ) {
206  // starting from the second iov in the array, skip the rows with older timestamp
207  if( iovs.size()-initialSize && std::get<0>(iovs.back()) == std::get<0>(row) ) continue;
208  iovs.push_back( row );
209  }
210  return iovs.size()-initialSize;
211  }
212 
214  const boost::posix_time::ptime& snapshotTime,
215  std::vector<std::tuple<cond::Time_t,cond::Hash> >& iovs){
216  Query< SINCE, PAYLOAD_HASH > q( m_schema );
217  q.addCondition<TAG_NAME>( tag );
218  q.addCondition<INSERTION_TIME>( snapshotTime,"<=" );
219  q.addOrderClause<SINCE>();
221  size_t initialSize = iovs.size();
222  for ( auto row : q ) {
223  // starting from the second iov in the array, skip the rows with older timestamp
224  if( iovs.size()-initialSize && std::get<0>(iovs.back()) == std::get<0>(row) ) continue;
225  iovs.push_back( row );
226  }
227  return iovs.size()-initialSize;
228  }
229 
231  Query< SINCE, PAYLOAD_HASH > q( m_schema );
232  q.addCondition<TAG_NAME>( tag );
233  q.addOrderClause<SINCE>( false );
235  for ( auto row : q ) {
236  since = std::get<0>(row);
237  hash = std::get<1>(row);
238  return true;
239  }
240  return false;
241  }
242 
243  bool IOV::Table::getSnapshotLastIov( const std::string& tag, const boost::posix_time::ptime& snapshotTime, cond::Time_t& since, cond::Hash& hash ){
244  Query< SINCE, PAYLOAD_HASH > q( m_schema );
245  q.addCondition<TAG_NAME>( tag );
246  q.addCondition<INSERTION_TIME>( snapshotTime,"<=" );
247  q.addOrderClause<SINCE>( false );
249  for ( auto row : q ) {
250  since = std::get<0>(row);
251  hash = std::get<1>(row);
252  return true;
253  }
254  return false;
255  }
256 
257  bool IOV::Table::getSize( const std::string& tag, size_t& size ){
258  Query< SEQUENCE_SIZE > q( m_schema );
259  q.addCondition<TAG_NAME>( tag );
260  for ( auto row : q ) {
261  size = std::get<0>(row);
262  return true;
263  }
264  return false;
265  }
266 
267  bool IOV::Table::getSnapshotSize( const std::string& tag, const boost::posix_time::ptime& snapshotTime, size_t& size ){
268  Query< SEQUENCE_SIZE > q( m_schema );
269  q.addCondition<TAG_NAME>( tag );
270  q.addCondition<INSERTION_TIME>( snapshotTime,"<=" );
271  for ( auto row : q ) {
272  size = std::get<0>(row);
273  return true;
274  }
275  return false;
276  }
277 
279  cond::Time_t since,
280  cond::Hash payloadHash,
281  const boost::posix_time::ptime& insertTimeStamp ){
282  RowBuffer< TAG_NAME, SINCE, PAYLOAD_HASH, INSERTION_TIME > dataToInsert( std::tie( tag, since, payloadHash, insertTimeStamp ) );
283  insertInTable( m_schema, tname, dataToInsert.get() );
284  }
285 
287  const std::vector<std::tuple<cond::Time_t,cond::Hash,boost::posix_time::ptime> >& iovs ){
289  for( auto row : iovs ) inserter.insert( std::tuple_cat( std::tie(tag),row ) );
290 
291  inserter.flush();
292  }
293 
295  DeleteBuffer buffer;
296  buffer.addWhereCondition<TAG_NAME>( tag );
297  deleteFromTable( m_schema, tname, buffer );
298  }
299 
300  TAG_LOG::Table::Table( coral::ISchema& schema ):
301  m_schema( schema ){
302  }
303 
305  return existsTable( m_schema, tname );
306  }
307 
309  if( exists() ){
310  throwException( "TAG_LOG table already exists in this schema.",
311  "TAG_LOG::create");
312  }
315  descr.setForeignKey< TAG_NAME, TAG::NAME >( "TAG_NAME_FK" );
316  createTable( m_schema, descr.get() );
317  }
318 
320  const boost::posix_time::ptime& eventTime,
321  const std::string& userName,
322  const std::string& hostName,
323  const std::string& command,
324  const std::string& action,
325  const std::string& userText){
327  dataToInsert( std::tie( tag, eventTime, userName, hostName, command, action, userText ) );
328  insertInTable( m_schema, tname, dataToInsert.get() );
329  }
330 
331  PAYLOAD::Table::Table( coral::ISchema& schema ):
332  m_schema( schema ){
333  }
334 
336  return existsTable( m_schema, tname );
337  }
338 
340  if( exists()){
341  throwException( "Payload table already exists in this schema.",
342  "PAYLOAD::Schema::create");
343  }
344 
346  descr.setPrimaryKey<HASH>();
347  createTable( m_schema, descr.get() );
348  }
349 
350  bool PAYLOAD::Table::select( const cond::Hash& payloadHash ){
351  Query< HASH > q( m_schema );
352  q.addCondition<HASH>( payloadHash );
353  for ( auto row : q ) {}
354 
355  return q.retrievedRows();
356  }
357 
358  bool PAYLOAD::Table::getType( const cond::Hash& payloadHash, std::string& objectType ){
359  Query< OBJECT_TYPE > q( m_schema );
360  q.addCondition<HASH>( payloadHash );
361  for ( auto row : q ) {
362  objectType = std::get<0>(row);
363  }
364 
365  return q.retrievedRows();
366  }
367 
368  bool PAYLOAD::Table::select( const cond::Hash& payloadHash,
369  std::string& objectType,
370  cond::Binary& payloadData,
371  cond::Binary& streamerInfoData ){
373  q.addCondition<HASH>( payloadHash );
374  for ( auto row : q ) {
375  std::tie( payloadData, streamerInfoData, objectType ) = row;
376  }
377  return q.retrievedRows();
378  }
379 
380  bool PAYLOAD::Table::insert( const cond::Hash& payloadHash,
381  const std::string& objectType,
382  const cond::Binary& payloadData,
383  const cond::Binary& streamerInfoData,
384  const boost::posix_time::ptime& insertionTime ){
385  std::string version("dummy");
386  cond::Binary sinfoData( streamerInfoData );
387  if( !sinfoData.size() ) sinfoData.copy( std::string("0") );
388  RowBuffer< HASH, OBJECT_TYPE, DATA, STREAMER_INFO, VERSION, INSERTION_TIME > dataToInsert( std::tie( payloadHash, objectType, payloadData, sinfoData, version, insertionTime ) );
389  bool failOnDuplicate = false;
390  return insertInTable( m_schema, tname, dataToInsert.get(), failOnDuplicate );
391  }
392 
394  const cond::Binary& payloadData,
395  const cond::Binary& streamerInfoData,
396  const boost::posix_time::ptime& insertionTime ){
397  cond::Hash payloadHash = makeHash( payloadObjectType, payloadData );
398  // 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...
399  if( !select( payloadHash ) ){
400  insert( payloadHash, payloadObjectType, payloadData, streamerInfoData, insertionTime );
401  }
402  return payloadHash;
403  }
404 
405  IOVSchema::IOVSchema( coral::ISchema& schema ):
406  m_tagTable( schema ),
407  m_iovTable( schema ),
408  m_tagLogTable( schema ),
409  m_payloadTable( schema ){
410  }
411 
413  if( !m_tagTable.exists() ) return false;
414  if( !m_payloadTable.exists() ) return false;
415  if( !m_iovTable.exists() ) return false;
416  return true;
417  }
418 
420  bool created = false;
421  if( !exists() ){
422  m_tagTable.create();
424  m_iovTable.create();
426  created = true;
427  }
428  return created;
429  }
430 
432  return m_tagTable;
433  }
434 
436  return m_iovTable;
437  }
438 
440  return m_tagLogTable;
441  }
442 
444  return m_payloadTable;
445  }
446 
447  }
448 }
449 
int i
Definition: DBlmapReader.cc:9
bool getSnapshotLastIov(const std::string &tag, const boost::posix_time::ptime &snapshotTime, cond::Time_t &since, cond::Hash &hash)
Definition: IOVSchema.cc:243
bool getSnapshotSize(const std::string &tag, const boost::posix_time::ptime &snapshotTime, size_t &size)
Definition: IOVSchema.cc:267
static char const * tname
Definition: IOVSchema.h:138
static std::string group()
Definition: IOVSchema.h:93
ITagLogTable & tagLogTable()
Definition: IOVSchema.cc:439
tuple schema
Definition: dataDML.py:2334
bool getSize(const std::string &tag, size_t &size)
Definition: IOVSchema.cc:257
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:588
Table(coral::ISchema &schema)
Definition: IOVSchema.cc:300
size_t selectLatest(const std::string &tag, std::vector< std::tuple< cond::Time_t, cond::Hash > > &iovs)
Definition: IOVSchema.cc:198
bool getLastIov(const std::string &tag, cond::Time_t &since, cond::Hash &hash)
Definition: IOVSchema.cc:230
PAYLOAD::Table m_payloadTable
Definition: IOVSchema.h:175
TimeType
Definition: Time.h:21
size_t size() const
Definition: Binary.cc:54
void insertMany(const std::string &tag, const std::vector< std::tuple< cond::Time_t, cond::Hash, boost::posix_time::ptime > > &iovs)
Definition: IOVSchema.cc:286
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:350
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
const coral::AttributeList & get() const
Definition: DbCore.h:171
void addOrderClause(bool ascending=true)
Definition: DbCore.h:439
std::string Hash
Definition: Types.h:45
void erase(const std::string &tag)
Definition: IOVSchema.cc:294
void copy(const std::string &source)
Definition: Binary.cc:40
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:443
bool insert(Storage &iStorage, ItemType *iItem, const IdTag &iIdTag)
Definition: HCMethods.h:49
bool select(const std::string &name)
Definition: IOVSchema.cc:53
bool getType(const cond::Hash &payloadHash, std::string &objectType)
Definition: IOVSchema.cc:358
const void * data() const
Definition: Binary.cc:45
TAG_LOG::Table m_tagLogTable
Definition: IOVSchema.h:174
tuple description
Definition: idDealer.py:66
void setColumnData(const Params &params)
Definition: DbCore.h:512
void insert(const std::string &tag, const boost::posix_time::ptime &eventTime, const std::string &userName, const std::string &hostName, const std::string &command, const std::string &action, const std::string &userText)
Definition: IOVSchema.cc:319
Table(coral::ISchema &schema)
Definition: IOVSchema.cc:331
IOVSchema(coral::ISchema &schema)
Definition: IOVSchema.cc:405
cond::Hash insertIfNew(const std::string &objectType, const cond::Binary &payloadData, const cond::Binary &streamerInfoData, const boost::posix_time::ptime &insertionTime)
Definition: IOVSchema.cc:393
string command
Definition: mps_check.py:26
string action
Definition: mps_fire.py:28
size_t selectSnapshot(const std::string &tag, const boost::posix_time::ptime &snapshotTime, std::vector< std::tuple< cond::Time_t, cond::Hash > > &iovs)
Definition: IOVSchema.cc:213
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
void groupBy(const std::string &expression)
Definition: DbCore.h:445
cond::Hash makeHash(const std::string &objectType, const cond::Binary &data)
Definition: IOVSchema.cc:10
SynchronizationType
Definition: Types.h:29
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:179
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:380
volatile std::atomic< bool > shutdown_flag false
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:521
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:278
tuple size
Write out results.
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:14
size_t selectSnapshotGroups(const std::string &tag, const boost::posix_time::ptime &snapshotTime, std::vector< cond::Time_t > &groups)
Definition: IOVSchema.cc:150
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:162
void addWhereCondition(const P &param, const std::string condition="=")
Definition: DbCore.h:555