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();
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;
71 return q.retrievedRows();
74 bool TAG::Table::getMetadata(
const std::string& name,
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();
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() );
101 const boost::posix_time::ptime& updateTime ){
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 );
108 void TAG::Table::updateValidity(
const std::string& name,
110 const boost::posix_time::ptime& updateTime ){
112 buffer.setColumnData< LAST_VALIDATED_TIME, MODIFICATION_TIME >( std::tie( lastValidatedTime, updateTime ) );
113 buffer.addWhereCondition<NAME>(
name );
114 updateTable( m_schema, tname, buffer );
117 IOV::Table::Table( coral::ISchema&
schema ):
121 bool IOV::Table::exists(){
122 return existsTable( m_schema, tname );
128 "IOV::Schema::create");
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() );
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>();
143 groups.push_back(std::get<0>(row));
145 return q.retrievedRows();
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>();
154 groups.push_back(std::get<0>(row));
156 return q.retrievedRows();
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,
">=" );
165 q.addOrderClause<SINCE>();
166 q.addOrderClause<INSERTION_TIME>(
false );
167 size_t initialSize = iovs.size();
170 if( iovs.size()-initialSize && std::get<0>(iovs.back()) == std::get<0>(row) )
continue;
171 iovs.push_back( row );
173 return iovs.size()-initialSize;
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,
">=" );
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 ) {
189 if( iovs.size()-initialSize && std::get<0>(iovs.back()) == std::get<0>(row) )
continue;
190 iovs.push_back( row );
192 return iovs.size()-initialSize;
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 ) {
204 if( iovs.size()-initialSize && std::get<0>(iovs.back()) == std::get<0>(row) )
continue;
205 iovs.push_back( row );
207 return iovs.size()-initialSize;
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);
224 Query< SEQUENCE_SIZE >
q( m_schema );
225 q.addCondition<TAG_NAME>(
tag );
226 for (
auto row : q ) {
227 size = std::get<0>(row);
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);
244 void IOV::Table::insertOne(
const std::string& tag,
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() );
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 ) );
260 PAYLOAD::Table::Table( coral::ISchema&
schema ):
264 bool PAYLOAD::Table::exists(){
265 return existsTable( m_schema, tname );
271 "PAYLOAD::Schema::create");
274 TableDescription< HASH, OBJECT_TYPE, DATA, STREAMER_INFO, VERSION, INSERTION_TIME > descr( tname );
275 descr.setPrimaryKey<
HASH>();
276 createTable( m_schema, descr.get() );
280 Query< HASH >
q( m_schema );
281 q.addCondition<
HASH>( payloadHash );
282 for (
auto row : q ) {}
284 return q.retrievedRows();
288 Query< OBJECT_TYPE >
q( m_schema );
289 q.addCondition<
HASH>( payloadHash );
290 for (
auto row : q ) {
291 objectType = std::get<0>(row);
294 return q.retrievedRows();
300 Query< DATA, OBJECT_TYPE >
q( m_schema );
301 q.addCondition<
HASH>( payloadHash );
302 for (
auto row : q ) {
303 std::tie( payloadData, objectType ) = row;
305 return q.retrievedRows();
311 const boost::posix_time::ptime& insertionTime ){
314 dummy.
copy( streamerType );
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 );
322 const boost::posix_time::ptime& insertionTime ){
325 if( !
select( payloadHash ) ){
326 insert( payloadHash, payloadObjectType, payloadData, insertionTime );
331 TAG_MIGRATION::Table::Table( coral::ISchema&
schema ):
335 bool TAG_MIGRATION::Table::exists(){
336 return existsTable( m_schema, tname );
341 throwException(
"TAG_MIGRATIONtable already exists in this schema.",
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() );
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;
358 return q.retrievedRows();
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() );
369 IOVSchema::IOVSchema( coral::ISchema&
schema ):
370 m_tagTable( schema ),
371 m_iovTable( schema ),
372 m_payloadTable( schema ),
373 m_tagMigrationTable( schema ){
384 bool created =
false;
PAYLOAD::Table m_payloadTable
unsigned long long Time_t
TAG_MIGRATION::Table m_tagMigrationTable
void copy(const std::string &source)
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)