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::Time_t& endOfValidity,
66  cond::Time_t& lastValidatedTime ){
67  Query< TIME_TYPE, OBJECT_TYPE, END_OF_VALIDITY, DESCRIPTION, LAST_VALIDATED_TIME > q( m_schema );
68  q.addCondition<NAME>( name );
69  for ( auto row : q ) std::tie( timeType, objectType, endOfValidity, description, lastValidatedTime ) = row;
70 
71  return q.retrievedRows();
72  }
73 
74  bool TAG::Table::getMetadata( const std::string& name,
75  std::string& description,
76  boost::posix_time::ptime& insertionTime,
77  boost::posix_time::ptime& modificationTime ){
78  Query< DESCRIPTION, INSERTION_TIME, MODIFICATION_TIME > q( m_schema );
79  q.addCondition<NAME>( name );
80  for ( auto row : q ) std::tie( description, insertionTime, modificationTime ) = row;
81  return q.retrievedRows();
82  }
83 
84  void TAG::Table::insert( const std::string& name,
85  cond::TimeType timeType,
86  const std::string& objectType,
87  cond::SynchronizationType synchronizationType,
88  cond::Time_t endOfValidity,
89  const std::string& description,
90  cond::Time_t lastValidatedTime,
91  const boost::posix_time::ptime& insertionTime ){
92  RowBuffer< NAME, TIME_TYPE, OBJECT_TYPE, SYNCHRONIZATION, END_OF_VALIDITY, DESCRIPTION, LAST_VALIDATED_TIME, INSERTION_TIME, MODIFICATION_TIME >
93  dataToInsert( std::tie( name, timeType, objectType, synchronizationType, endOfValidity, description, lastValidatedTime, insertionTime, insertionTime ) );
94  insertInTable( m_schema, tname, dataToInsert.get() );
95  }
96 
97  void TAG::Table::update( const std::string& name,
98  cond::Time_t& endOfValidity,
99  const std::string& description,
100  cond::Time_t lastValidatedTime,
101  const boost::posix_time::ptime& updateTime ){
102  UpdateBuffer buffer;
103  buffer.setColumnData< END_OF_VALIDITY, DESCRIPTION, LAST_VALIDATED_TIME, MODIFICATION_TIME >( std::tie( endOfValidity, description, lastValidatedTime, updateTime ) );
104  buffer.addWhereCondition<NAME>( name );
105  updateTable( m_schema, tname, buffer );
106  }
107 
108  void TAG::Table::updateValidity( const std::string& name,
109  cond::Time_t lastValidatedTime,
110  const boost::posix_time::ptime& updateTime ){
111  UpdateBuffer buffer;
112  buffer.setColumnData< LAST_VALIDATED_TIME, MODIFICATION_TIME >( std::tie( lastValidatedTime, updateTime ) );
113  buffer.addWhereCondition<NAME>( name );
114  updateTable( m_schema, tname, buffer );
115  }
116 
117  IOV::Table::Table( coral::ISchema& schema ):
118  m_schema( schema ){
119  }
120 
121  bool IOV::Table::exists(){
122  return existsTable( m_schema, tname );
123  }
124 
125  void IOV::Table::create(){
126  if( exists()){
127  throwException( "IOV table already exists in this schema.",
128  "IOV::Schema::create");
129  }
130 
131  TableDescription< TAG_NAME, SINCE, PAYLOAD_HASH, INSERTION_TIME > descr( tname );
132  descr.setPrimaryKey< TAG_NAME, SINCE, INSERTION_TIME >();
133  descr.setForeignKey< TAG_NAME, TAG::NAME >( "TAG_NAME_FK" );
134  descr.setForeignKey< PAYLOAD_HASH, PAYLOAD::HASH >( "PAYLOAD_HASH_FK" );
135  createTable( m_schema, descr.get() );
136  }
137 
138  size_t IOV::Table::selectGroups( const std::string& tag, std::vector<cond::Time_t>& groups ){
139  Query< SINCE_GROUP > q( m_schema, true );
140  q.addCondition<TAG_NAME>( tag );
141  q.addOrderClause<SINCE_GROUP>();
142  for( auto row : q ){
143  groups.push_back(std::get<0>(row));
144  }
145  return q.retrievedRows();
146  }
147 
148  size_t IOV::Table::selectSnapshotGroups( const std::string& tag, const boost::posix_time::ptime& snapshotTime, std::vector<cond::Time_t>& groups ){
149  Query< SINCE_GROUP > q( m_schema, true );
150  q.addCondition<TAG_NAME>( tag );
151  q.addCondition<INSERTION_TIME>( snapshotTime,"<=" );
152  q.addOrderClause<SINCE_GROUP>();
153  for( auto row : q ){
154  groups.push_back(std::get<0>(row));
155  }
156  return q.retrievedRows();
157  }
158 
159  size_t IOV::Table::selectLatestByGroup( const std::string& tag, cond::Time_t lowerSinceGroup, cond::Time_t upperSinceGroup ,
160  std::vector<std::tuple<cond::Time_t,cond::Hash> >& iovs ){
161  Query< SINCE, PAYLOAD_HASH > q( m_schema );
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  q.addOrderClause<SINCE>();
166  q.addOrderClause<INSERTION_TIME>( false );
167  size_t initialSize = iovs.size();
168  for( auto row : q ){
169  // starting from the second iov in the array, skip the rows with older timestamp
170  if( iovs.size()-initialSize && std::get<0>(iovs.back()) == std::get<0>(row) ) continue;
171  iovs.push_back( row );
172  }
173  return iovs.size()-initialSize;
174  }
175 
176  size_t IOV::Table::selectSnapshotByGroup( const std::string& tag, cond::Time_t lowerSinceGroup, cond::Time_t upperSinceGroup,
177  const boost::posix_time::ptime& snapshotTime,
178  std::vector<std::tuple<cond::Time_t,cond::Hash> >& iovs ){
179  Query< SINCE, PAYLOAD_HASH > q( m_schema );
180  q.addCondition<TAG_NAME>( tag );
181  if( lowerSinceGroup > 0 ) q.addCondition<SINCE>( lowerSinceGroup, ">=" );
182  if( upperSinceGroup < cond::time::MAX_VAL ) q.addCondition<SINCE>( upperSinceGroup, "<" );
183  q.addCondition<INSERTION_TIME>( snapshotTime,"<=" );
184  q.addOrderClause<SINCE>();
185  q.addOrderClause<INSERTION_TIME>( false );
186  size_t initialSize = iovs.size();
187  for ( auto row : q ) {
188  // starting from the second iov in the array, skip the rows with older timestamp
189  if( iovs.size()-initialSize && std::get<0>(iovs.back()) == std::get<0>(row) ) continue;
190  iovs.push_back( row );
191  }
192  return iovs.size()-initialSize;
193  }
194 
195  size_t IOV::Table::selectLatest( const std::string& tag,
196  std::vector<std::tuple<cond::Time_t,cond::Hash> >& iovs ){
197  Query< SINCE, PAYLOAD_HASH > q( m_schema );
198  q.addCondition<TAG_NAME>( tag );
199  q.addOrderClause<SINCE>();
200  q.addOrderClause<INSERTION_TIME>( false );
201  size_t initialSize = iovs.size();
202  for ( auto row : q ) {
203  // starting from the second iov in the array, skip the rows with older timestamp
204  if( iovs.size()-initialSize && std::get<0>(iovs.back()) == std::get<0>(row) ) continue;
205  iovs.push_back( row );
206  }
207  return iovs.size()-initialSize;
208  }
209 
210  bool IOV::Table::getLastIov( const std::string& tag, cond::Time_t& since, cond::Hash& hash ){
211  Query< SINCE, PAYLOAD_HASH > q( m_schema );
212  q.addCondition<TAG_NAME>( tag );
213  q.addOrderClause<SINCE>( false );
214  q.addOrderClause<INSERTION_TIME>( false );
215  for ( auto row : q ) {
216  since = std::get<0>(row);
217  hash = std::get<1>(row);
218  return true;
219  }
220  return false;
221  }
222 
223  bool IOV::Table::getSize( const std::string& tag, size_t& size ){
224  Query< SEQUENCE_SIZE > q( m_schema );
225  q.addCondition<TAG_NAME>( tag );
226  for ( auto row : q ) {
227  size = std::get<0>(row);
228  return true;
229  }
230  return false;
231  }
232 
233  bool IOV::Table::getSnapshotSize( const std::string& tag, const boost::posix_time::ptime& snapshotTime, size_t& size ){
234  Query< SEQUENCE_SIZE > q( m_schema );
235  q.addCondition<TAG_NAME>( tag );
236  q.addCondition<INSERTION_TIME>( snapshotTime,"<=" );
237  for ( auto row : q ) {
238  size = std::get<0>(row);
239  return true;
240  }
241  return false;
242  }
243 
244  void IOV::Table::insertOne( const std::string& tag,
245  cond::Time_t since,
246  cond::Hash payloadHash,
247  const boost::posix_time::ptime& insertTimeStamp ){
248  RowBuffer< TAG_NAME, SINCE, PAYLOAD_HASH, INSERTION_TIME > dataToInsert( std::tie( tag, since, payloadHash, insertTimeStamp ) );
249  insertInTable( m_schema, tname, dataToInsert.get() );
250  }
251 
252  void IOV::Table::insertMany( const std::string& tag,
253  const std::vector<std::tuple<cond::Time_t,cond::Hash,boost::posix_time::ptime> >& iovs ){
254  BulkInserter< TAG_NAME, SINCE, PAYLOAD_HASH, INSERTION_TIME > inserter( m_schema, tname );
255  for( auto row : iovs ) inserter.insert( std::tuple_cat( std::tie(tag),row ) );
256 
257  inserter.flush();
258  }
259 
260  PAYLOAD::Table::Table( coral::ISchema& schema ):
261  m_schema( schema ){
262  }
263 
264  bool PAYLOAD::Table::exists(){
265  return existsTable( m_schema, tname );
266  }
267 
268  void PAYLOAD::Table::create(){
269  if( exists()){
270  throwException( "Payload table already exists in this schema.",
271  "PAYLOAD::Schema::create");
272  }
273 
274  TableDescription< HASH, OBJECT_TYPE, DATA, STREAMER_INFO, VERSION, INSERTION_TIME > descr( tname );
275  descr.setPrimaryKey<HASH>();
276  createTable( m_schema, descr.get() );
277  }
278 
279  bool PAYLOAD::Table::select( const cond::Hash& payloadHash ){
280  Query< HASH > q( m_schema );
281  q.addCondition<HASH>( payloadHash );
282  for ( auto row : q ) {}
283 
284  return q.retrievedRows();
285  }
286 
287  bool PAYLOAD::Table::getType( const cond::Hash& payloadHash, std::string& objectType ){
288  Query< OBJECT_TYPE > q( m_schema );
289  q.addCondition<HASH>( payloadHash );
290  for ( auto row : q ) {
291  objectType = std::get<0>(row);
292  }
293 
294  return q.retrievedRows();
295  }
296 
297  bool PAYLOAD::Table::select( const cond::Hash& payloadHash,
298  std::string& objectType,
299  cond::Binary& payloadData ){
300  Query< DATA, OBJECT_TYPE > q( m_schema );
301  q.addCondition<HASH>( payloadHash );
302  for ( auto row : q ) {
303  std::tie( payloadData, objectType ) = row;
304  }
305  return q.retrievedRows();
306  }
307 
308  bool PAYLOAD::Table::insert( const cond::Hash& payloadHash,
309  const std::string& objectType,
310  const cond::Binary& payloadData,
311  const boost::posix_time::ptime& insertionTime ){
312  cond::Binary dummy;
313  std::string streamerType("ROOT5");
314  dummy.copy( streamerType );
315  std::string version("dummy");
316  RowBuffer< HASH, OBJECT_TYPE, DATA, STREAMER_INFO, VERSION, INSERTION_TIME > dataToInsert( std::tie( payloadHash, objectType, payloadData, dummy, version, insertionTime ) );
317  bool failOnDuplicate = false;
318  return insertInTable( m_schema, tname, dataToInsert.get(), failOnDuplicate );
319  }
320 
321  cond::Hash PAYLOAD::Table::insertIfNew( const std::string& payloadObjectType, const cond::Binary& payloadData,
322  const boost::posix_time::ptime& insertionTime ){
323  cond::Hash payloadHash = makeHash( payloadObjectType, payloadData );
324  // 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...
325  if( !select( payloadHash ) ){
326  insert( payloadHash, payloadObjectType, payloadData, insertionTime );
327  }
328  return payloadHash;
329  }
330 
331  TAG_MIGRATION::Table::Table( coral::ISchema& schema ):
332  m_schema( schema ){
333  }
334 
335  bool TAG_MIGRATION::Table::exists(){
336  return existsTable( m_schema, tname );
337  }
338 
340  if( exists() ){
341  throwException( "TAG_MIGRATIONtable already exists in this schema.",
342  "TAG::create");
343  }
344  TableDescription< SOURCE_ACCOUNT, SOURCE_TAG, TAG_NAME, INSERTION_TIME > descr( tname );
345  descr.setPrimaryKey<SOURCE_ACCOUNT, SOURCE_TAG>();
346  descr.setForeignKey< TAG_NAME, TAG::NAME >( "TAG_NAME_FK" );
347  createTable( m_schema, descr.get() );
348  }
349 
350  bool TAG_MIGRATION::Table::select( const std::string& sourceAccount, const std::string& sourceTag, std::string& tagName ){
351  Query< TAG_NAME > q( m_schema );
352  q.addCondition<SOURCE_ACCOUNT>( sourceAccount );
353  q.addCondition<SOURCE_TAG>( sourceTag );
354  for ( auto row : q ) {
355  std::tie( tagName ) = row;
356  }
357 
358  return q.retrievedRows();
359 
360  }
361 
362  void TAG_MIGRATION::Table::insert( const std::string& sourceAccount, const std::string& sourceTag, const std::string& tagName,
363  const boost::posix_time::ptime& insertionTime ){
364  RowBuffer< SOURCE_ACCOUNT, SOURCE_TAG, TAG_NAME, INSERTION_TIME >
365  dataToInsert( std::tie( sourceAccount, sourceTag, tagName, insertionTime ) );
366  insertInTable( m_schema, tname, dataToInsert.get() );
367  }
368 
369  IOVSchema::IOVSchema( coral::ISchema& schema ):
370  m_tagTable( schema ),
371  m_iovTable( schema ),
372  m_payloadTable( schema ),
373  m_tagMigrationTable( schema ){
374  }
375 
377  if( !m_tagTable.exists() ) return false;
378  if( !m_payloadTable.exists() ) return false;
379  if( !m_iovTable.exists() ) return false;
380  return true;
381  }
382 
384  bool created = false;
385  if( !exists() ){
386  m_tagTable.create();
387  m_payloadTable.create();
388  m_iovTable.create();
389  created = true;
390  }
391  return created;
392  }
393 
395  return m_tagTable;
396  }
397 
399  return m_iovTable;
400  }
401 
403  return m_payloadTable;
404  }
405 
407  return m_tagMigrationTable;
408  }
409 
410  }
411 }
412 
int i
Definition: DBlmapReader.cc:9
PAYLOAD::Table m_payloadTable
Definition: IOVSchema.h:171
TimeType
Definition: Time.h:21
size_t size() const
Definition: Binary.cc:61
unsigned long long Time_t
Definition: Time.h:16
TAG_MIGRATION::Table m_tagMigrationTable
Definition: IOVSchema.h:172
std::string Hash
Definition: Types.h:34
void copy(const std::string &source)
Definition: Binary.cc:46
IPayloadTable & payloadTable()
Definition: IOVSchema.cc:402
bool insert(Storage &iStorage, ItemType *iItem, const IdTag &iIdTag)
Definition: HCMethods.h:49
ITagMigrationTable & tagMigrationTable()
Definition: IOVSchema.cc:406
const void * data() const
Definition: Binary.cc:52
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:22
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