CMS 3D CMS Logo

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 
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::SynchronizationType synchronizationType,
100  cond::Time_t& endOfValidity,
101  const std::string& description,
102  cond::Time_t lastValidatedTime,
103  const boost::posix_time::ptime& updateTime ){
106  endOfValidity,
107  description,
108  lastValidatedTime,
109  updateTime ) );
110  buffer.addWhereCondition<NAME>( name );
111  updateTable( m_schema, tname, buffer );
112  }
113 
115  cond::Time_t lastValidatedTime,
116  const boost::posix_time::ptime& updateTime ){
118  buffer.setColumnData< LAST_VALIDATED_TIME, MODIFICATION_TIME >( std::tie( lastValidatedTime, updateTime ) );
119  buffer.addWhereCondition<NAME>( name );
120  updateTable( m_schema, tname, buffer );
121  }
122 
123  IOV::Table::Table( coral::ISchema& schema ):
124  m_schema( schema ){
125  }
126 
128  return existsTable( m_schema, tname );
129  }
130 
132  if( exists()){
133  throwException( "IOV table already exists in this schema.",
134  "IOV::Schema::create");
135  }
136 
139  descr.setForeignKey< TAG_NAME, TAG::NAME >( "TAG_NAME_FK" );
140  descr.setForeignKey< PAYLOAD_HASH, PAYLOAD::HASH >( "PAYLOAD_HASH_FK" );
141  createTable( m_schema, descr.get() );
142  }
143 
145  const boost::posix_time::ptime& snapshotTime,
146  unsigned long long gSize,
147  std::vector<cond::Time_t>& groups ){
149  q.addCondition<TAG_NAME>( tag );
150  if( !snapshotTime.is_not_a_date_time() ){
152  }
153  q.groupBy(SINCE_GROUP::group(gSize));
155  for( auto row : q ){
156  groups.push_back(std::get<0>(row));
157  }
158  return q.retrievedRows();
159  }
160 
161  size_t IOV::Table::select( const std::string& tag, cond::Time_t lowerSinceGroup, cond::Time_t upperSinceGroup,
162  const boost::posix_time::ptime& snapshotTime,
163  std::vector<std::tuple<cond::Time_t,cond::Hash> >& iovs ){
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  if( !snapshotTime.is_not_a_date_time() ){
170  }
171  q.addOrderClause<SINCE>();
173  size_t initialSize = iovs.size();
174  for ( auto row : q ) {
175  // starting from the second iov in the array, skip the rows with older timestamp
176  if( iovs.size()-initialSize && std::get<0>(iovs.back()) == std::get<0>(row) ) continue;
177  iovs.push_back( row );
178  }
179  return iovs.size()-initialSize;
180  }
181 
182  bool IOV::Table::getLastIov( const std::string& tag, const boost::posix_time::ptime& snapshotTime, cond::Time_t& since, cond::Hash& hash ){
184  q.addCondition<TAG_NAME>( tag );
185  if( !snapshotTime.is_not_a_date_time() ){
187  }
188  q.addOrderClause<SINCE>( false );
190  for ( auto row : q ) {
191  since = std::get<0>(row);
192  hash = std::get<1>(row);
193  return true;
194  }
195  return false;
196  }
197 
198  bool IOV::Table::getSize( const std::string& tag, const boost::posix_time::ptime& snapshotTime, size_t& size ){
200  q.addCondition<TAG_NAME>( tag );
201  if( !snapshotTime.is_not_a_date_time() ){
203  }
204  for ( auto row : q ) {
205  size = std::get<0>(row);
206  return true;
207  }
208  return false;
209  }
210 
212  const boost::posix_time::ptime& snapshotTime,
213  std::vector<std::tuple<cond::Time_t,cond::Hash> >& iovs ){
215  q0.addCondition<TAG_NAME>( tag );
216  q0.addCondition<SINCE>( begin, "<=");
217  if( !snapshotTime.is_not_a_date_time() ){
219  }
220  cond::Time_t lower_since = 0;
221  for( auto row: q0 ){
222  lower_since = std::get<0>( row );
223  }
224  if ( !lower_since ) return false;
226  q1.addCondition<TAG_NAME>( tag );
227  q1.addCondition<SINCE>( lower_since, ">=");
228  if( !snapshotTime.is_not_a_date_time() ){
230  }
231  q1.addCondition<SINCE>( end, "<=");
232  q1.addOrderClause<SINCE>();
234  size_t initialSize = iovs.size();
235  for( auto row: q1 ){
236  // starting from the second iov in the array, skip the rows with older timestamp
237  if( iovs.size()-initialSize && std::get<0>(iovs.back()) == std::get<0>(row) ) continue;
238  iovs.push_back( row );
239  }
240  return iovs.size()-initialSize;
241  }
242 
245  cond::Hash payloadHash,
246  const boost::posix_time::ptime& insertTimeStamp ){
247  RowBuffer< TAG_NAME, SINCE, PAYLOAD_HASH, INSERTION_TIME > dataToInsert( std::tie( tag, since, payloadHash, insertTimeStamp ) );
248  insertInTable( m_schema, tname, dataToInsert.get() );
249  }
250 
252  const std::vector<std::tuple<cond::Time_t,cond::Hash,boost::posix_time::ptime> >& iovs ){
254  for( auto row : iovs ) inserter.insert( std::tuple_cat( std::tie(tag),row ) );
255 
256  inserter.flush();
257  }
258 
261  buffer.addWhereCondition<TAG_NAME>( tag );
262  deleteFromTable( m_schema, tname, buffer );
263  }
264 
265  TAG_LOG::Table::Table( coral::ISchema& schema ):
266  m_schema( schema ){
267  }
268 
270  return existsTable( m_schema, tname );
271  }
272 
274  if( exists() ){
275  throwException( "TAG_LOG table already exists in this schema.",
276  "TAG_LOG::create");
277  }
280  descr.setForeignKey< TAG_NAME, TAG::NAME >( "TAG_NAME_FK" );
281  createTable( m_schema, descr.get() );
282  }
283 
285  const boost::posix_time::ptime& eventTime,
286  const std::string& userName,
287  const std::string& hostName,
288  const std::string& command,
289  const std::string& action,
290  const std::string& userText){
292  dataToInsert( std::tie( tag, eventTime, userName, hostName, command, action, userText ) );
293  insertInTable( m_schema, tname, dataToInsert.get() );
294  }
295 
296  PAYLOAD::Table::Table( coral::ISchema& schema ):
297  m_schema( schema ){
298  }
299 
301  return existsTable( m_schema, tname );
302  }
303 
305  if( exists()){
306  throwException( "Payload table already exists in this schema.",
307  "PAYLOAD::Schema::create");
308  }
309 
311  descr.setPrimaryKey<HASH>();
312  createTable( m_schema, descr.get() );
313  }
314 
315  bool PAYLOAD::Table::select( const cond::Hash& payloadHash ){
317  q.addCondition<HASH>( payloadHash );
318  for ( auto row : q ) {}
319 
320  return q.retrievedRows();
321  }
322 
323  bool PAYLOAD::Table::getType( const cond::Hash& payloadHash, std::string& objectType ){
325  q.addCondition<HASH>( payloadHash );
326  for ( auto row : q ) {
327  objectType = std::get<0>(row);
328  }
329 
330  return q.retrievedRows();
331  }
332 
333  bool PAYLOAD::Table::select( const cond::Hash& payloadHash,
334  std::string& objectType,
335  cond::Binary& payloadData,
336  cond::Binary& streamerInfoData ){
338  q.addCondition<HASH>( payloadHash );
339  for ( auto row : q ) {
340  std::tie( payloadData, streamerInfoData, objectType ) = row;
341  }
342  return q.retrievedRows();
343  }
344 
345  bool PAYLOAD::Table::insert( const cond::Hash& payloadHash,
346  const std::string& objectType,
347  const cond::Binary& payloadData,
348  const cond::Binary& streamerInfoData,
349  const boost::posix_time::ptime& insertionTime ){
350  std::string version("dummy");
351  cond::Binary sinfoData( streamerInfoData );
352  if( !sinfoData.size() ) sinfoData.copy( std::string("0") );
353  RowBuffer< HASH, OBJECT_TYPE, DATA, STREAMER_INFO, VERSION, INSERTION_TIME > dataToInsert( std::tie( payloadHash, objectType, payloadData, sinfoData, version, insertionTime ) );
354  bool failOnDuplicate = false;
355  return insertInTable( m_schema, tname, dataToInsert.get(), failOnDuplicate );
356  }
357 
359  const cond::Binary& payloadData,
360  const cond::Binary& streamerInfoData,
361  const boost::posix_time::ptime& insertionTime ){
362  cond::Hash payloadHash = makeHash( payloadObjectType, payloadData );
363  // 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...
364  if( !select( payloadHash ) ){
365  insert( payloadHash, payloadObjectType, payloadData, streamerInfoData, insertionTime );
366  }
367  return payloadHash;
368  }
369 
370  IOVSchema::IOVSchema( coral::ISchema& schema ):
371  m_tagTable( schema ),
372  m_iovTable( schema ),
373  m_tagLogTable( schema ),
374  m_payloadTable( schema ){
375  }
376 
378  if( !m_tagTable.exists() ) return false;
379  if( !m_payloadTable.exists() ) return false;
380  if( !m_iovTable.exists() ) return false;
381  return true;
382  }
383 
385  bool created = false;
386  if( !exists() ){
387  m_tagTable.create();
389  m_iovTable.create();
391  created = true;
392  }
393  return created;
394  }
395 
397  return m_tagTable;
398  }
399 
401  return m_iovTable;
402  }
403 
405  return m_tagLogTable;
406  }
407 
409  return m_payloadTable;
410  }
411 
412  }
413 }
414 
size
Write out results.
bool getLastIov(const std::string &tag, const boost::posix_time::ptime &snapshotTime, cond::Time_t &since, cond::Hash &hash) override
Definition: IOVSchema.cc:182
bool select(const cond::Hash &payloadHash)
Definition: IOVSchema.cc:315
static char const * tname
Definition: IOVSchema.h:149
Table(coral::ISchema &schema)
Definition: IOVSchema.cc:35
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) override
Definition: IOVSchema.cc:284
const coral::TableDescription & get()
Definition: DbCore.h:262
void insert(const P &params)
Definition: DbCore.h:590
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) override
Definition: IOVSchema.cc:85
PAYLOAD::Table m_payloadTable
Definition: IOVSchema.h:186
coral::ISchema & m_schema
Definition: IOVSchema.h:44
TimeType
Definition: Time.h:21
size_t size() const
Definition: Binary.cc:54
Table(coral::ISchema &schema)
Definition: IOVSchema.cc:123
static std::string group(unsigned long long groupSize)
Definition: IOVSchema.h:93
static char const * tname
Definition: IOVSchema.h:48
IPayloadTable & payloadTable() override
Definition: IOVSchema.cc:408
cond::Hash insertIfNew(const std::string &objectType, const cond::Binary &payloadData, const cond::Binary &streamerInfoData, const boost::posix_time::ptime &insertionTime) override
Definition: IOVSchema.cc:358
coral::ISchema & m_schema
Definition: IOVSchema.h:145
void setForeignKey(const std::string &name)
Definition: DbCore.h:257
void update(const std::string &name, cond::SynchronizationType synchronizationType, cond::Time_t &endOfValidity, const std::string &description, cond::Time_t lastValidatedTime, const boost::posix_time::ptime &updateTime) override
Definition: IOVSchema.cc:98
unsigned long long Time_t
Definition: Time.h:16
static char const * tname
Definition: IOVSchema.h:13
Query & groupBy(const std::string &expression)
Definition: DbCore.h:445
bool getMetadata(const std::string &name, std::string &description, boost::posix_time::ptime &insertionTime, boost::posix_time::ptime &modificationTime) override
Definition: IOVSchema.cc:75
const coral::AttributeList & get() const
Definition: DbCore.h:167
bool select(const std::string &name) override
Definition: IOVSchema.cc:53
Query & addCondition(const T &value, const std::string condition="=")
Definition: DbCore.h:425
void insertOne(const std::string &tag, cond::Time_t since, cond::Hash payloadHash, const boost::posix_time::ptime &insertTime) override
Definition: IOVSchema.cc:243
void erase(const std::string &tag) override
Definition: IOVSchema.cc:259
std::string Hash
Definition: Types.h:45
void copy(const std::string &source)
Definition: Binary.cc:40
#define end
Definition: vmac.h:39
bool getRange(const std::string &tag, cond::Time_t begin, cond::Time_t end, const boost::posix_time::ptime &snapshotTime, std::vector< std::tuple< cond::Time_t, cond::Hash > > &iovs) override
Definition: IOVSchema.cc:211
ITagLogTable & tagLogTable() override
Definition: IOVSchema.cc:404
size_t select(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) override
Definition: IOVSchema.cc:161
Query & addOrderClause(bool ascending=true)
Definition: DbCore.h:438
Table(coral::ISchema &schema)
Definition: IOVSchema.cc:296
const void * data() const
Definition: Binary.cc:45
TAG_LOG::Table m_tagLogTable
Definition: IOVSchema.h:185
void setColumnData(const Params &params)
Definition: DbCore.h:514
Table(coral::ISchema &schema)
Definition: IOVSchema.cc:265
IOVSchema(coral::ISchema &schema)
Definition: IOVSchema.cc:370
void updateValidity(const std::string &name, cond::Time_t lastValidatedTime, const boost::posix_time::ptime &updateTime) override
Definition: IOVSchema.cc:114
double q1[4]
Definition: TauolaWrapper.h:87
bool getSize(const std::string &tag, const boost::posix_time::ptime &snapshotTime, size_t &size) override
Definition: IOVSchema.cc:198
void insertMany(const std::string &tag, const std::vector< std::tuple< cond::Time_t, cond::Hash, boost::posix_time::ptime > > &iovs) override
Definition: IOVSchema.cc:251
list command
Definition: mps_check.py:25
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
Definition: plugin.cc:24
#define begin
Definition: vmac.h:32
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
bool getType(const cond::Hash &payloadHash, std::string &objectType) override
Definition: IOVSchema.cc:323
cond::Hash makeHash(const std::string &objectType, const cond::Binary &data)
Definition: IOVSchema.cc:10
size_t getGroups(const std::string &tag, const boost::posix_time::ptime &snapshotTime, unsigned long long groupSize, std::vector< cond::Time_t > &groups) override
Definition: IOVSchema.cc:144
SynchronizationType
Definition: Types.h:29
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:345
IIOVTable & iovTable() override
Definition: IOVSchema.cc:400
static char const * tname
Definition: IOVSchema.h:79
void addWhereCondition(const P &param, const std::string condition="=")
Definition: DbCore.h:523
const Time_t MAX_VAL(std::numeric_limits< Time_t >::max())
ITagTable & tagTable() override
Definition: IOVSchema.cc:396
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:14
void addWhereCondition(const P &param, const std::string condition="=")
Definition: DbCore.h:557