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 
43  void TAG::Table::create(){
44  if( exists() ){
45  throwException( "TAG table already exists in this schema.",
46  "TAG::Table::create");
47  }
48  TableDescription< NAME, TIME_TYPE, OBJECT_TYPE, SYNCHRONIZATION, END_OF_VALIDITY, DESCRIPTION, LAST_VALIDATED_TIME, INSERTION_TIME, MODIFICATION_TIME > descr( tname );
49  descr.setPrimaryKey<NAME>();
50  createTable( m_schema, descr.get() );
51  }
52 
53  bool TAG::Table::select( const std::string& name ){
54  Query< NAME > q( m_schema );
55  q.addCondition<NAME>( name );
56  for ( auto row : q ) {}
57 
58  return q.retrievedRows();
59  }
60 
61  bool TAG::Table::select( const std::string& name,
62  cond::TimeType& timeType,
63  std::string& objectType,
64  cond::SynchronizationType& synchronizationType,
65  cond::Time_t& endOfValidity,
67  cond::Time_t& lastValidatedTime ){
68  Query< TIME_TYPE, OBJECT_TYPE, SYNCHRONIZATION, END_OF_VALIDITY, DESCRIPTION, LAST_VALIDATED_TIME > q( m_schema );
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 
75  bool TAG::Table::getMetadata( const std::string& name,
76  std::string& description,
77  boost::posix_time::ptime& insertionTime,
78  boost::posix_time::ptime& modificationTime ){
79  Query< DESCRIPTION, INSERTION_TIME, MODIFICATION_TIME > q( m_schema );
80  q.addCondition<NAME>( name );
81  for ( auto row : q ) std::tie( description, insertionTime, modificationTime ) = row;
82  return q.retrievedRows();
83  }
84 
85  void TAG::Table::insert( const std::string& name,
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 ){
93  RowBuffer< NAME, TIME_TYPE, OBJECT_TYPE, SYNCHRONIZATION, END_OF_VALIDITY, DESCRIPTION, LAST_VALIDATED_TIME, INSERTION_TIME, MODIFICATION_TIME >
94  dataToInsert( std::tie( name, timeType, objectType, synchronizationType, endOfValidity, description, lastValidatedTime, insertionTime, insertionTime ) );
95  insertInTable( m_schema, tname, dataToInsert.get() );
96  }
97 
98  void TAG::Table::update( const std::string& name,
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 
109  void TAG::Table::updateValidity( const std::string& name,
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 
122  bool IOV::Table::exists(){
123  return existsTable( m_schema, tname );
124  }
125 
126  void IOV::Table::create(){
127  if( exists()){
128  throwException( "IOV table already exists in this schema.",
129  "IOV::Schema::create");
130  }
131 
132  TableDescription< TAG_NAME, SINCE, PAYLOAD_HASH, INSERTION_TIME > descr( tname );
133  descr.setPrimaryKey< TAG_NAME, SINCE, INSERTION_TIME >();
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 );
142  q.addOrderClause<SINCE_GROUP>();
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,"<=" );
153  q.addOrderClause<SINCE_GROUP>();
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>();
167  q.addOrderClause<INSERTION_TIME>( false );
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>();
186  q.addOrderClause<INSERTION_TIME>( false );
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 
196  size_t IOV::Table::selectLatest( const std::string& tag,
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>();
201  q.addOrderClause<INSERTION_TIME>( false );
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 
211  bool IOV::Table::getLastIov( const std::string& tag, cond::Time_t& since, cond::Hash& hash ){
212  Query< SINCE, PAYLOAD_HASH > q( m_schema );
213  q.addCondition<TAG_NAME>( tag );
214  q.addOrderClause<SINCE>( false );
215  q.addOrderClause<INSERTION_TIME>( 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 
245  void IOV::Table::insertOne( const std::string& tag,
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 
253  void IOV::Table::insertMany( const std::string& tag,
254  const std::vector<std::tuple<cond::Time_t,cond::Hash,boost::posix_time::ptime> >& iovs ){
255  BulkInserter< TAG_NAME, SINCE, PAYLOAD_HASH, INSERTION_TIME > inserter( m_schema, tname );
256  for( auto row : iovs ) inserter.insert( std::tuple_cat( std::tie(tag),row ) );
257 
258  inserter.flush();
259  }
260 
261  void IOV::Table::erase( const std::string& tag ){
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 
271  bool PAYLOAD::Table::exists(){
272  return existsTable( m_schema, tname );
273  }
274 
275  void PAYLOAD::Table::create(){
276  if( exists()){
277  throwException( "Payload table already exists in this schema.",
278  "PAYLOAD::Schema::create");
279  }
280 
281  TableDescription< HASH, OBJECT_TYPE, DATA, STREAMER_INFO, VERSION, INSERTION_TIME > descr( tname );
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 ){
308  Query< DATA, STREAMER_INFO, OBJECT_TYPE > q( m_schema );
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 
329  cond::Hash PAYLOAD::Table::insertIfNew( const std::string& payloadObjectType,
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 
341  TAG_MIGRATION::Table::Table( coral::ISchema& schema ):
342  m_schema( schema ){
343  }
344 
345  bool TAG_MIGRATION::Table::exists(){
346  return existsTable( m_schema, tname );
347  }
348 
350  if( exists() ){
351  throwException( "TAG_MIGRATIONtable already exists in this schema.",
352  "TAG::create");
353  }
354  TableDescription< SOURCE_ACCOUNT, SOURCE_TAG, TAG_NAME, STATUS_CODE, INSERTION_TIME > descr( tname );
355  descr.setPrimaryKey<SOURCE_ACCOUNT, SOURCE_TAG>();
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 ){
374  RowBuffer< SOURCE_ACCOUNT, SOURCE_TAG, TAG_NAME, STATUS_CODE, INSERTION_TIME >
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();
405  m_payloadTable.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 
int i
Definition: DBlmapReader.cc:9
PAYLOAD::Table m_payloadTable
Definition: IOVSchema.h:176
TimeType
Definition: Time.h:21
size_t size() const
Definition: Binary.cc:57
unsigned long long Time_t
Definition: Time.h:16
TAG_MIGRATION::Table m_tagMigrationTable
Definition: IOVSchema.h:177
std::string Hash
Definition: Types.h:43
IPayloadTable & payloadTable()
Definition: IOVSchema.cc:420
bool insert(Storage &iStorage, ItemType *iItem, const IdTag &iIdTag)
Definition: HCMethods.h:49
ITagMigrationTable & tagMigrationTable()
Definition: IOVSchema.cc:424
const void * data() const
Definition: Binary.cc:48
tuple description
Definition: idDealer.py:66
void throwException(std::string const &message, std::string const &methodName)
Definition: Exception.cc:17
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
#define update(a, b)
cond::Hash makeHash(const std::string &objectType, const cond::Binary &data)
Definition: IOVSchema.cc:10
SynchronizationType
Definition: Types.h:31
volatile std::atomic< bool > shutdown_flag false
const Time_t MAX_VAL(std::numeric_limits< Time_t >::max())
SurfaceDeformation * create(int type, const std::vector< double > &params)
tuple size
Write out results.
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:11