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  for( auto row: q1 ){
230  iovs.push_back( row );
231  }
232  return iovs.size();
233  }
234 
237  cond::Hash payloadHash,
238  const boost::posix_time::ptime& insertTimeStamp ){
239  RowBuffer< TAG_NAME, SINCE, PAYLOAD_HASH, INSERTION_TIME > dataToInsert( std::tie( tag, since, payloadHash, insertTimeStamp ) );
240  insertInTable( m_schema, tname, dataToInsert.get() );
241  }
242 
244  const std::vector<std::tuple<cond::Time_t,cond::Hash,boost::posix_time::ptime> >& iovs ){
246  for( auto row : iovs ) inserter.insert( std::tuple_cat( std::tie(tag),row ) );
247 
248  inserter.flush();
249  }
250 
253  buffer.addWhereCondition<TAG_NAME>( tag );
254  deleteFromTable( m_schema, tname, buffer );
255  }
256 
257  TAG_LOG::Table::Table( coral::ISchema& schema ):
258  m_schema( schema ){
259  }
260 
262  return existsTable( m_schema, tname );
263  }
264 
266  if( exists() ){
267  throwException( "TAG_LOG table already exists in this schema.",
268  "TAG_LOG::create");
269  }
272  descr.setForeignKey< TAG_NAME, TAG::NAME >( "TAG_NAME_FK" );
273  createTable( m_schema, descr.get() );
274  }
275 
277  const boost::posix_time::ptime& eventTime,
278  const std::string& userName,
279  const std::string& hostName,
280  const std::string& command,
281  const std::string& action,
282  const std::string& userText){
284  dataToInsert( std::tie( tag, eventTime, userName, hostName, command, action, userText ) );
285  insertInTable( m_schema, tname, dataToInsert.get() );
286  }
287 
288  PAYLOAD::Table::Table( coral::ISchema& schema ):
289  m_schema( schema ){
290  }
291 
293  return existsTable( m_schema, tname );
294  }
295 
297  if( exists()){
298  throwException( "Payload table already exists in this schema.",
299  "PAYLOAD::Schema::create");
300  }
301 
303  descr.setPrimaryKey<HASH>();
304  createTable( m_schema, descr.get() );
305  }
306 
307  bool PAYLOAD::Table::select( const cond::Hash& payloadHash ){
309  q.addCondition<HASH>( payloadHash );
310  for ( auto row : q ) {}
311 
312  return q.retrievedRows();
313  }
314 
315  bool PAYLOAD::Table::getType( const cond::Hash& payloadHash, std::string& objectType ){
317  q.addCondition<HASH>( payloadHash );
318  for ( auto row : q ) {
319  objectType = std::get<0>(row);
320  }
321 
322  return q.retrievedRows();
323  }
324 
325  bool PAYLOAD::Table::select( const cond::Hash& payloadHash,
326  std::string& objectType,
327  cond::Binary& payloadData,
328  cond::Binary& streamerInfoData ){
330  q.addCondition<HASH>( payloadHash );
331  for ( auto row : q ) {
332  std::tie( payloadData, streamerInfoData, objectType ) = row;
333  }
334  return q.retrievedRows();
335  }
336 
337  bool PAYLOAD::Table::insert( const cond::Hash& payloadHash,
338  const std::string& objectType,
339  const cond::Binary& payloadData,
340  const cond::Binary& streamerInfoData,
341  const boost::posix_time::ptime& insertionTime ){
342  std::string version("dummy");
343  cond::Binary sinfoData( streamerInfoData );
344  if( !sinfoData.size() ) sinfoData.copy( std::string("0") );
345  RowBuffer< HASH, OBJECT_TYPE, DATA, STREAMER_INFO, VERSION, INSERTION_TIME > dataToInsert( std::tie( payloadHash, objectType, payloadData, sinfoData, version, insertionTime ) );
346  bool failOnDuplicate = false;
347  return insertInTable( m_schema, tname, dataToInsert.get(), failOnDuplicate );
348  }
349 
351  const cond::Binary& payloadData,
352  const cond::Binary& streamerInfoData,
353  const boost::posix_time::ptime& insertionTime ){
354  cond::Hash payloadHash = makeHash( payloadObjectType, payloadData );
355  // 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...
356  if( !select( payloadHash ) ){
357  insert( payloadHash, payloadObjectType, payloadData, streamerInfoData, insertionTime );
358  }
359  return payloadHash;
360  }
361 
362  IOVSchema::IOVSchema( coral::ISchema& schema ):
363  m_tagTable( schema ),
364  m_iovTable( schema ),
365  m_tagLogTable( schema ),
366  m_payloadTable( schema ){
367  }
368 
370  if( !m_tagTable.exists() ) return false;
371  if( !m_payloadTable.exists() ) return false;
372  if( !m_iovTable.exists() ) return false;
373  return true;
374  }
375 
377  bool created = false;
378  if( !exists() ){
379  m_tagTable.create();
381  m_iovTable.create();
383  created = true;
384  }
385  return created;
386  }
387 
389  return m_tagTable;
390  }
391 
393  return m_iovTable;
394  }
395 
397  return m_tagLogTable;
398  }
399 
401  return m_payloadTable;
402  }
403 
404  }
405 }
406 
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:396
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: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)
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:243
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:307
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:251
void copy(const std::string &source)
Definition: Binary.cc:40
#define end
Definition: vmac.h:37
IPayloadTable & payloadTable()
Definition: IOVSchema.cc:400
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:315
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:276
Table(coral::ISchema &schema)
Definition: IOVSchema.cc:288
IOVSchema(coral::ISchema &schema)
Definition: IOVSchema.cc:362
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:350
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:337
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:235
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