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 
144  size_t IOV::Table::getGroups( const std::string& tag, const boost::posix_time::ptime& snapshotTime, std::vector<cond::Time_t>& groups ){
146  q.addCondition<TAG_NAME>( tag );
147  if( !snapshotTime.is_not_a_date_time() ){
149  }
152  for( auto row : q ){
153  groups.push_back(std::get<0>(row));
154  }
155  return q.retrievedRows();
156  }
157 
158  size_t IOV::Table::select( const std::string& tag, cond::Time_t lowerSinceGroup, cond::Time_t upperSinceGroup,
159  const boost::posix_time::ptime& snapshotTime,
160  std::vector<std::tuple<cond::Time_t,cond::Hash> >& iovs ){
162  q.addCondition<TAG_NAME>( tag );
163  if( lowerSinceGroup > 0 ) q.addCondition<SINCE>( lowerSinceGroup, ">=" );
164  if( upperSinceGroup < cond::time::MAX_VAL ) q.addCondition<SINCE>( upperSinceGroup, "<" );
165  if( !snapshotTime.is_not_a_date_time() ){
167  }
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  bool IOV::Table::getLastIov( const std::string& tag, const boost::posix_time::ptime& snapshotTime, cond::Time_t& since, cond::Hash& hash ){
181  q.addCondition<TAG_NAME>( tag );
182  if( !snapshotTime.is_not_a_date_time() ){
184  }
185  q.addOrderClause<SINCE>( false );
187  for ( auto row : q ) {
188  since = std::get<0>(row);
189  hash = std::get<1>(row);
190  return true;
191  }
192  return false;
193  }
194 
195  bool IOV::Table::getSize( const std::string& tag, const boost::posix_time::ptime& snapshotTime, size_t& size ){
197  q.addCondition<TAG_NAME>( tag );
198  if( !snapshotTime.is_not_a_date_time() ){
200  }
201  for ( auto row : q ) {
202  size = std::get<0>(row);
203  return true;
204  }
205  return false;
206  }
207 
209  const boost::posix_time::ptime& snapshotTime,
210  std::vector<std::tuple<cond::Time_t,cond::Hash> >& iovs ){
212  q0.addCondition<TAG_NAME>( tag );
213  q0.addCondition<SINCE>( begin, "<=");
214  if( !snapshotTime.is_not_a_date_time() ){
216  }
217  cond::Time_t lower_since = 0;
218  for( auto row: q0 ){
219  lower_since = std::get<0>( row );
220  }
221  if ( !lower_since ) return false;
223  q1.addCondition<TAG_NAME>( tag );
224  q1.addCondition<SINCE>( lower_since, ">=");
225  if( !snapshotTime.is_not_a_date_time() ){
227  }
228  q1.addCondition<SINCE>( end, "<=");
229  q1.addOrderClause<SINCE>();
231  size_t initialSize = iovs.size();
232  for( auto row: q1 ){
233  // starting from the second iov in the array, skip the rows with older timestamp
234  if( iovs.size()-initialSize && std::get<0>(iovs.back()) == std::get<0>(row) ) continue;
235  iovs.push_back( row );
236  }
237  return iovs.size()-initialSize;
238  }
239 
242  cond::Hash payloadHash,
243  const boost::posix_time::ptime& insertTimeStamp ){
244  RowBuffer< TAG_NAME, SINCE, PAYLOAD_HASH, INSERTION_TIME > dataToInsert( std::tie( tag, since, payloadHash, insertTimeStamp ) );
245  insertInTable( m_schema, tname, dataToInsert.get() );
246  }
247 
249  const std::vector<std::tuple<cond::Time_t,cond::Hash,boost::posix_time::ptime> >& iovs ){
251  for( auto row : iovs ) inserter.insert( std::tuple_cat( std::tie(tag),row ) );
252 
253  inserter.flush();
254  }
255 
258  buffer.addWhereCondition<TAG_NAME>( tag );
259  deleteFromTable( m_schema, tname, buffer );
260  }
261 
262  TAG_LOG::Table::Table( coral::ISchema& schema ):
263  m_schema( schema ){
264  }
265 
267  return existsTable( m_schema, tname );
268  }
269 
271  if( exists() ){
272  throwException( "TAG_LOG table already exists in this schema.",
273  "TAG_LOG::create");
274  }
277  descr.setForeignKey< TAG_NAME, TAG::NAME >( "TAG_NAME_FK" );
278  createTable( m_schema, descr.get() );
279  }
280 
282  const boost::posix_time::ptime& eventTime,
283  const std::string& userName,
284  const std::string& hostName,
285  const std::string& command,
286  const std::string& action,
287  const std::string& userText){
289  dataToInsert( std::tie( tag, eventTime, userName, hostName, command, action, userText ) );
290  insertInTable( m_schema, tname, dataToInsert.get() );
291  }
292 
293  PAYLOAD::Table::Table( coral::ISchema& schema ):
294  m_schema( schema ){
295  }
296 
298  return existsTable( m_schema, tname );
299  }
300 
302  if( exists()){
303  throwException( "Payload table already exists in this schema.",
304  "PAYLOAD::Schema::create");
305  }
306 
308  descr.setPrimaryKey<HASH>();
309  createTable( m_schema, descr.get() );
310  }
311 
312  bool PAYLOAD::Table::select( const cond::Hash& payloadHash ){
314  q.addCondition<HASH>( payloadHash );
315  for ( auto row : q ) {}
316 
317  return q.retrievedRows();
318  }
319 
320  bool PAYLOAD::Table::getType( const cond::Hash& payloadHash, std::string& objectType ){
322  q.addCondition<HASH>( payloadHash );
323  for ( auto row : q ) {
324  objectType = std::get<0>(row);
325  }
326 
327  return q.retrievedRows();
328  }
329 
330  bool PAYLOAD::Table::select( const cond::Hash& payloadHash,
331  std::string& objectType,
332  cond::Binary& payloadData,
333  cond::Binary& streamerInfoData ){
335  q.addCondition<HASH>( payloadHash );
336  for ( auto row : q ) {
337  std::tie( payloadData, streamerInfoData, objectType ) = row;
338  }
339  return q.retrievedRows();
340  }
341 
342  bool PAYLOAD::Table::insert( const cond::Hash& payloadHash,
343  const std::string& objectType,
344  const cond::Binary& payloadData,
345  const cond::Binary& streamerInfoData,
346  const boost::posix_time::ptime& insertionTime ){
347  std::string version("dummy");
348  cond::Binary sinfoData( streamerInfoData );
349  if( !sinfoData.size() ) sinfoData.copy( std::string("0") );
350  RowBuffer< HASH, OBJECT_TYPE, DATA, STREAMER_INFO, VERSION, INSERTION_TIME > dataToInsert( std::tie( payloadHash, objectType, payloadData, sinfoData, version, insertionTime ) );
351  bool failOnDuplicate = false;
352  return insertInTable( m_schema, tname, dataToInsert.get(), failOnDuplicate );
353  }
354 
356  const cond::Binary& payloadData,
357  const cond::Binary& streamerInfoData,
358  const boost::posix_time::ptime& insertionTime ){
359  cond::Hash payloadHash = makeHash( payloadObjectType, payloadData );
360  // 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...
361  if( !select( payloadHash ) ){
362  insert( payloadHash, payloadObjectType, payloadData, streamerInfoData, insertionTime );
363  }
364  return payloadHash;
365  }
366 
367  IOVSchema::IOVSchema( coral::ISchema& schema ):
368  m_tagTable( schema ),
369  m_iovTable( schema ),
370  m_tagLogTable( schema ),
371  m_payloadTable( schema ){
372  }
373 
375  if( !m_tagTable.exists() ) return false;
376  if( !m_payloadTable.exists() ) return false;
377  if( !m_iovTable.exists() ) return false;
378  return true;
379  }
380 
382  bool created = false;
383  if( !exists() ){
384  m_tagTable.create();
386  m_iovTable.create();
388  created = true;
389  }
390  return created;
391  }
392 
394  return m_tagTable;
395  }
396 
398  return m_iovTable;
399  }
400 
402  return m_tagLogTable;
403  }
404 
406  return m_payloadTable;
407  }
408 
409  }
410 }
411 
size
Write out results.
static char const * tname
Definition: IOVSchema.h:149
static std::string group()
Definition: IOVSchema.h:93
ITagLogTable & tagLogTable()
Definition: IOVSchema.cc:401
const coral::TableDescription & get()
Definition: DbCore.h:262
void updateValidity(const std::string &name, cond::Time_t lastValidatedTime, const boost::posix_time::ptime &updateTime)
Definition: IOVSchema.cc:114
void insert(const P &params)
Definition: DbCore.h:590
Table(coral::ISchema &schema)
Definition: IOVSchema.cc:262
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)
Definition: IOVSchema.cc:98
bool getLastIov(const std::string &tag, const boost::posix_time::ptime &snapshotTime, cond::Time_t &since, cond::Hash &hash)
Definition: IOVSchema.cc:179
size_t getGroups(const std::string &tag, const boost::posix_time::ptime &snapshotTime, std::vector< cond::Time_t > &groups)
Definition: IOVSchema.cc:144
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
bool getSize(const std::string &tag, const boost::posix_time::ptime &snapshotTime, size_t &size)
Definition: IOVSchema.cc:195
void insertMany(const std::string &tag, const std::vector< std::tuple< cond::Time_t, cond::Hash, boost::posix_time::ptime > > &iovs)
Definition: IOVSchema.cc:248
static char const * tname
Definition: IOVSchema.h:48
coral::ISchema & m_schema
Definition: IOVSchema.h:145
void setForeignKey(const std::string &name)
Definition: DbCore.h:257
bool select(const cond::Hash &payloadHash)
Definition: IOVSchema.cc:312
unsigned long long Time_t
Definition: Time.h:16
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)
Definition: IOVSchema.cc:158
static char const * tname
Definition: IOVSchema.h:13
Query & groupBy(const std::string &expression)
Definition: DbCore.h:445
const coral::AttributeList & get() const
Definition: DbCore.h:167
Query & addCondition(const T &value, const std::string condition="=")
Definition: DbCore.h:425
std::string Hash
Definition: Types.h:45
void erase(const std::string &tag)
Definition: IOVSchema.cc:256
void copy(const std::string &source)
Definition: Binary.cc:40
#define end
Definition: vmac.h:37
IPayloadTable & payloadTable()
Definition: IOVSchema.cc:405
Query & addOrderClause(bool ascending=true)
Definition: DbCore.h:438
bool select(const std::string &name)
Definition: IOVSchema.cc:53
bool getType(const cond::Hash &payloadHash, std::string &objectType)
Definition: IOVSchema.cc:320
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
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:281
Table(coral::ISchema &schema)
Definition: IOVSchema.cc:293
IOVSchema(coral::ISchema &schema)
Definition: IOVSchema.cc:367
double q1[4]
Definition: TauolaWrapper.h:87
cond::Hash insertIfNew(const std::string &objectType, const cond::Binary &payloadData, const cond::Binary &streamerInfoData, const boost::posix_time::ptime &insertionTime)
Definition: IOVSchema.cc:355
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
Definition: plugin.cc:24
#define begin
Definition: vmac.h:30
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:29
Table(coral::ISchema &schema)
Definition: IOVSchema.cc:35
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:342
Table(coral::ISchema &schema)
Definition: IOVSchema.cc:123
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)
Definition: IOVSchema.cc:208
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:523
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:240
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