4 #include <openssl/sha.h>
8 namespace persistency {
12 if( !SHA1_Init( &ctx ) ){
13 throwException(
"SHA1 initialization error.",
"IOVSchema::makeHash");
15 if( !SHA1_Update( &ctx, objectType.c_str(), objectType.size() ) ){
16 throwException(
"SHA1 processing error (1).",
"IOVSchema::makeHash");
18 if( !SHA1_Update( &ctx, data.
data(), data.
size() ) ){
19 throwException(
"SHA1 processing error (2).",
"IOVSchema::makeHash");
21 unsigned char hash[SHA_DIGEST_LENGTH];
22 if( !SHA1_Final(hash, &ctx) ){
26 char tmp[SHA_DIGEST_LENGTH*2+1];
28 for (
unsigned int i = 0;
i < 20;
i++) {
29 ::sprintf(&tmp[
i * 2],
"%02x", hash[i]);
35 TAG::Table::Table( coral::ISchema&
schema ):
39 bool TAG::Table::Table::exists(){
40 return existsTable( m_schema, tname );
46 "TAG::Table::create");
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() );
54 Query< NAME >
q( m_schema );
55 q.addCondition<NAME>(
name );
56 for (
auto row :
q ) {}
58 return q.retrievedRows();
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;
72 return q.retrievedRows();
75 bool TAG::Table::getMetadata(
const std::string& name,
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();
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() );
102 const boost::posix_time::ptime& updateTime ){
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 );
109 void TAG::Table::updateValidity(
const std::string& name,
111 const boost::posix_time::ptime& updateTime ){
113 buffer.setColumnData< LAST_VALIDATED_TIME, MODIFICATION_TIME >( std::tie( lastValidatedTime, updateTime ) );
114 buffer.addWhereCondition<NAME>(
name );
115 updateTable( m_schema, tname, buffer );
118 IOV::Table::Table( coral::ISchema&
schema ):
122 bool IOV::Table::exists(){
123 return existsTable( m_schema, tname );
129 "IOV::Schema::create");
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() );
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>();
144 groups.push_back(std::get<0>(row));
146 return q.retrievedRows();
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>();
155 groups.push_back(std::get<0>(row));
157 return q.retrievedRows();
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,
">=" );
166 q.addOrderClause<SINCE>();
167 q.addOrderClause<INSERTION_TIME>(
false );
168 size_t initialSize = iovs.size();
171 if( iovs.size()-initialSize && std::get<0>(iovs.back()) == std::get<0>(row) )
continue;
172 iovs.push_back( row );
174 return iovs.size()-initialSize;
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,
">=" );
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 ) {
190 if( iovs.size()-initialSize && std::get<0>(iovs.back()) == std::get<0>(row) )
continue;
191 iovs.push_back( row );
193 return iovs.size()-initialSize;
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 ) {
205 if( iovs.size()-initialSize && std::get<0>(iovs.back()) == std::get<0>(row) )
continue;
206 iovs.push_back( row );
208 return iovs.size()-initialSize;
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);
225 Query< SEQUENCE_SIZE >
q( m_schema );
226 q.addCondition<TAG_NAME>(
tag );
227 for (
auto row : q ) {
228 size = std::get<0>(row);
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);
245 void IOV::Table::insertOne(
const std::string& tag,
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() );
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 ) );
263 buffer.addWhereCondition<TAG_NAME>(
tag );
264 deleteFromTable( m_schema, tname, buffer );
267 PAYLOAD::Table::Table( coral::ISchema&
schema ):
271 bool PAYLOAD::Table::exists(){
272 return existsTable( m_schema, tname );
278 "PAYLOAD::Schema::create");
281 TableDescription< HASH, OBJECT_TYPE, DATA, STREAMER_INFO, VERSION, INSERTION_TIME > descr( tname );
282 descr.setPrimaryKey<
HASH>();
283 createTable( m_schema, descr.get() );
287 Query< HASH >
q( m_schema );
288 q.addCondition<
HASH>( payloadHash );
289 for (
auto row : q ) {}
291 return q.retrievedRows();
295 Query< OBJECT_TYPE >
q( m_schema );
296 q.addCondition<
HASH>( payloadHash );
297 for (
auto row : q ) {
298 objectType = std::get<0>(row);
301 return q.retrievedRows();
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;
313 return q.retrievedRows();
320 const boost::posix_time::ptime& insertionTime ){
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 );
332 const boost::posix_time::ptime& insertionTime ){
335 if( !
select( payloadHash ) ){
336 insert( payloadHash, payloadObjectType, payloadData, streamerInfoData, insertionTime );
341 TAG_MIGRATION::Table::Table( coral::ISchema&
schema ):
345 bool TAG_MIGRATION::Table::exists(){
346 return existsTable( m_schema, tname );
351 throwException(
"TAG_MIGRATIONtable already exists in this schema.",
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() );
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;
368 return q.retrievedRows();
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() );
379 void TAG_MIGRATION::Table::updateValidationCode(
const std::string& sourceAccount,
const std::string& sourceTag,
int statusCode ){
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 );
387 IOVSchema::IOVSchema( coral::ISchema&
schema ):
388 m_tagTable( schema ),
389 m_iovTable( schema ),
390 m_payloadTable( schema ),
391 m_tagMigrationTable( schema ){
402 bool created =
false;
PAYLOAD::Table m_payloadTable
unsigned long long Time_t
TAG_MIGRATION::Table m_tagMigrationTable
IPayloadTable & payloadTable()
bool insert(Storage &iStorage, ItemType *iItem, const IdTag &iIdTag)
ITagMigrationTable & tagMigrationTable()
const void * data() const
void throwException(std::string const &message, std::string const &methodName)
std::vector< std::vector< double > > tmp
char data[epos_bytes_allocation]
cond::Hash makeHash(const std::string &objectType, const cond::Binary &data)
volatile std::atomic< bool > shutdown_flag false
const Time_t MAX_VAL(std::numeric_limits< Time_t >::max())
tuple size
Write out results.
void throwException(const std::string &message, const std::string &methodName)