8 #include <boost/filesystem.hpp>
9 #include <boost/regex.hpp>
10 #include <boost/bind.hpp>
14 namespace persistency {
22 static const boost::regex
popcon_many(
"PopCon[[:print:]]+?first payload Since ([[:alnum:]]+?),.*");
23 static const boost::regex
duplicate(
"duplicateIOV[[:print:]]+?[S|s]ince[=| ]([[:alnum:]]+?);.*");
24 static const boost::regex
popcon_one(
"PopCon[[:print:]]+?;Since ([[:alnum:]]+?);.*");
25 static const boost::regex
exportIOV(
"exportIOV[[:print:]]+?;since=([[:alnum:]]+?),[[:print:]]+?; *copied=([[:alnum:]]+?);.*");
38 if( log && !forValidation )
std::cout <<
" Loading source iov..."<<std::endl;
41 if( log )
std::cout <<
" Tag contains 0 iovs."<<std::endl;
53 exists = destSession.
existsIov( destTag );
58 }
else if( policy ==
NEW ){
62 editor = destSession.
editIov( destTag );
73 std::set<cond::Hash> pids;
74 std::set<cond::Time_t>
sinces;
77 if( sinces.find(
iov.since ) != sinces.end() ){
78 if( log && !forValidation )
std::cout <<
" WARNING. Skipping duplicated since="<<
iov.since<<std::endl;
81 sinces.insert(
iov.since );
83 auto usedIov = p.getInterval(
iov.since );
84 std::pair<std::string,boost::shared_ptr<void> > readBackPayload =
fetch( usedIov.payloadId, sourceSession );
85 cond::Hash ph =
import( sourceSession, usedIov.payloadId, readBackPayload.first, readBackPayload.second.get(), destSession );
89 if( log && !forValidation && niovs && (niovs%1000==0) )
std::cout <<
" Total of iov inserted: "<<niovs<<
" payloads: "<<pids.size()<<std::endl;
91 if( log && !forValidation)
std::cout <<
" Total of iov inserted: "<<niovs<<
" payloads: "<<pids.size()<<std::endl;
92 if( log && !forValidation)
std::cout <<
" Flushing changes..."<<std::endl;
101 std::vector<std::pair<cond::Time_t,boost::posix_time::ptime> >& loggedInsertions ){
103 std::set<cond::Time_t> loggedSinces;
106 for (
auto row :
q ) {
107 if( std::get<1>( row ).empty() )
continue;
112 if (boost::regex_match(std::get<1>( row ), matches,
popcon_many )){
113 since_t = boost::lexical_cast<
unsigned long long>(matches[1]);
115 }
else if (boost::regex_match(std::get<1>( row ), matches,
popcon_one )){
116 since_t = boost::lexical_cast<
unsigned long long>(matches[1]);
118 }
else if (boost::regex_match(std::get<1>( row ), matches,
exportIOV )){
119 since_t = boost::lexical_cast<
unsigned long long>(matches[1]);
120 copied_n = boost::lexical_cast<
unsigned long long>(matches[2]);
121 }
else if( boost::regex_match(std::get<1>( row ), matches,
duplicate )){
122 since_t = boost::lexical_cast<
unsigned long long>(matches[1]);
126 std::cout <<
"ERROR: tag "<<tag<<
": could not parse the PopConLog info entry: \""<<std::get<1>( row )<<
"\""<<std::endl;
127 loggedSinces.clear();
131 if( loggedSinces.find( since_t ) == loggedSinces.end() && copied_n>0 ){
134 if(sscanf( s_t.c_str(),
"%d-%d-%d-%d:%d:%d",&Y,&M,&
D,&
h,&
m,&
s) != 6 ){
135 throwException(
"Tag "+tag+
": time information can't be parsed.",
"migrateTag");
137 char parsable_s_t[23];
138 sprintf( parsable_s_t,
"%04d-%02d-%02d %02d:%02d:%02d.000",Y,M,D,h,m,s );
140 boost::posix_time::ptime insertionTime = boost::posix_time::time_from_string(
std::string( parsable_s_t ) );
141 loggedInsertions.push_back( std::make_pair( since_t, insertionTime ) );
142 loggedSinces.insert( since_t );
148 loggedInsertions.end(),
149 boost::bind( std::less<cond::Time_t>(),
153 return loggedInsertions.size()>0;
158 bool operator()(
const cond::Time_t& x,
const std::pair<cond::Time_t,boost::posix_time::ptime>& y ){
return ( x < y.first ); }
162 template <
typename T>
typename std::vector<T>::const_iterator
search(
const cond::Time_t& val,
const std::vector<T>& container ){
163 if( !container.size() )
return container.end();
164 auto p = std::upper_bound( container.begin(), container.end(), val, IOVComp() );
165 return (
p!= container.begin()) ?
p-1 : container.end();
176 std::cout <<
" Loading source iov..."<<std::endl;
179 std::cout <<
" Tag contains 0 iovs."<<std::endl;
183 std::vector<std::pair<cond::Time_t,boost::posix_time::ptime> > loggedInsertions;
184 std::tuple<std::string, boost::posix_time::ptime, boost::posix_time::ptime > metadata = p.
getMetadata();
185 boost::posix_time::ptime creationTime = boost::posix_time::time_from_string(
time_0 );
187 creationTime = std::get<2>(metadata);
191 if( !loggedInsertions.empty() ) creationTime = loggedInsertions[0].
second;
203 exists = destSession.
existsIov( destTag );
208 }
else if( policy ==
NEW ){
212 editor = destSession.
editIov( destTag );
218 std::set<cond::Hash> pids;
219 std::set<cond::Time_t>
sinces;
222 if( sinces.find(
iov.since ) != sinces.end() ){
223 std::cout <<
" WARNING. Skipping duplicated since="<<
iov.since<<std::endl;
226 sinces.insert(
iov.since );
228 boost::posix_time::ptime insertionTime = creationTime;
229 if( !loggedInsertions.empty() ){
230 auto iL =
search(
iov.since, loggedInsertions );
231 if( iL != loggedInsertions.end() ) insertionTime = iL->second;
235 auto usedIov = p.getInterval(
iov.since );
236 std::pair<std::string,boost::shared_ptr<void> > readBackPayload =
fetch( usedIov.payloadId, sourceSession );
237 cond::Hash ph =
import( sourceSession, usedIov.payloadId, readBackPayload.first, readBackPayload.second.get(), destSession );
239 editor.
insert(
iov.since, ph, insertionTime );
242 if( niovs && (niovs%1000==0) ) {
243 std::cout <<
" Total of iov inserted: "<<niovs<<
" payloads: "<<pids.size()<<std::endl;
244 std::cout <<
" Last since imported: "<<
iov.since<<std::endl;
247 std::cout <<
" Total of iov inserted: "<<niovs<<
" payloads: "<<pids.size()<<std::endl;
248 std::cout <<
" Flushing changes..."<<std::endl;
266 if( log )
std::cout <<
" Loading source iov..."<<std::endl;
269 if( log )
std::cout <<
" Tag contains 0 iovs."<<std::endl;
274 if( (*p.
begin()).since > begin ) begin = (*p.
begin()).since;
276 if( log )
std::cout <<
" No Iov in the selected range."<<std::endl;
286 exists = destSession.
existsIov( destTag );
289 editor = destSession.
editIov( destTag );
291 throwException(
"TimeType of the destination tag does not match with the source tag timeType.",
"importIovs");
293 throwException(
"PayloadType of the destination tag does not match with the source tag payloadType.",
"importIovs");
300 std::set<cond::Hash> pids;
301 std::set<cond::Time_t>
sinces;
302 auto iiov = p.
find( begin );
304 while( iiov != p.
end() ){
306 if( sinces.find( newSince ) != sinces.end() ){
307 if( log )
std::cout <<
" WARNING. Skipping duplicated since="<<newSince<<std::endl;
310 sinces.insert( newSince );
313 std::pair<std::string,boost::shared_ptr<void> > readBackPayload =
fetch( usedIov.payloadId, sourceSession );
314 cond::Hash ph =
import( sourceSession, usedIov.payloadId, readBackPayload.first, readBackPayload.second.get(), destSession );
315 editor.
insert( newSince, ph );
318 if( log && niovs && (niovs%1000==0) )
std::cout <<
" Total of iov inserted: "<<niovs<<
" payloads: "<<pids.size()<<std::endl;
320 if( iiov == p.
end() || (*iiov).since >
end ){
323 newSince = (*iiov).since;
326 if( log )
std::cout <<
" Total of iov inserted: "<<niovs<<
" payloads: "<<pids.size()<<std::endl;
327 if( log )
std::cout <<
" Flushing changes..."<<std::endl;
343 if( log )
std::cout <<
" Loading source iov..."<<std::endl;
346 if( log )
std::cout <<
" Tag contains 0 iovs."<<std::endl;
352 auto iiov = p.
find( sourceSince );
353 if( iiov == p.
end() ){
354 if( log )
std::cout <<
"ERROR: No Iov valid found for target time "<<sourceSince<<std::endl;
360 editor = session.
editIov( destTag );
362 throwException(
"TimeType of the destination tag does not match with the source tag timeType.",
"importIovs");
364 throwException(
"PayloadType of the destination tag does not match with the source tag payloadType.",
"importIovs");
367 if( description.empty() ) editor.
setDescription(
"Created copying iovs from tag "+sourceTag );
371 editor.
insert( destSince, (*iiov).payloadId );
373 if( log )
std::cout <<
" Flushing changes..."<<std::endl;
382 size_t ret =
copyTag( tag, session, destTag, writeSession,
NEW,
false,
true );
392 size_t n1 =
exportTagToFile( firstTag, firstTag, firstSession, firstFileName );
394 std::cout <<
"Can't compare empty tag "<<firstTag<<std::endl;
397 size_t n2 =
exportTagToFile( secondTag, firstTag, secondSession, secondFileName );
399 std::cout <<
"Can't compare empty tag "<<secondTag<<std::endl;
407 FILE*
file1 = fopen( firstFileName.c_str(),
"r" );
411 FILE* file2 = fopen( secondFileName.c_str(),
"r" );
413 throwException(
"Can't open file "+secondFileName,
"compareTags" );
422 size_t r1 = fread( buf1, 1, N, file1 );
423 size_t r2 = fread( buf2, 1, N, file2 );
425 if( r1 != r2 || memcmp( buf1, buf2, r1)) {
430 }
while(!feof(file2) || !feof(file2));
432 std::cout <<
" "<<totSize<<
" bytes compared."<<std::endl;
456 bool ret =
compareTags( refTag, refSession, refFile, candTag, candSession, candFile );
cond::SynchronizationType synchronizationType() const
void clearIov(const std::string &tag)
DbTransaction & transaction()
static const boost::regex popcon_one("PopCon[[:print:]]+?;Since ([[:alnum:]]+?);.*")
size_t importIovs(const std::string &sourceTag, Session &sourceSession, const std::string &destTag, Session &destSession, cond::Time_t begin, cond::Time_t end, const std::string &description, bool log)
int commit()
commit transaction.
void setDescription(const std::string &description)
size_t migrateTag(const std::string &sourceTag, Session &sourceSession, const std::string &destTag, Session &destSession, UpdatePolicy policy, cond::DbSession &logDbSession)
std::string payloadObjectType() const
Transaction & transaction()
U second(std::pair< T, U > const &p)
std::tuple< std::string, boost::posix_time::ptime, boost::posix_time::ptime > getMetadata() const
int start(bool readOnly=false)
start transaction
std::tuple< std::string, std::string, std::string > parseConnectionString(const std::string &connectionString)
IOVProxy readIov(const std::string &tag, bool full=false)
cond::TimeType timeType() const
unsigned long long Time_t
static const boost::regex duplicate("duplicateIOV[[:print:]]+?[S|s]ince[=| ]([[:alnum:]]+?);.*")
tuple path
else: Piece not in the list, fine.
Session createSession(const std::string &connectionString, bool writeCapable=false, BackendType backType=DEFAULT_DB)
size_t copyTag(const std::string &sourceTag, Session &sourceSession, const std::string &destTag, Session &destSession, UpdatePolicy policy, bool log, bool forValidation)
Iterator find(cond::Time_t time)
size_t exportTagToFile(const std::string &tag, const std::string &destTag, Session &session, const std::string fileName)
void start(bool readOnly=true)
static const boost::regex exportIOV("exportIOV[[:print:]]+?;since=([[:alnum:]]+?),[[:print:]]+?; *copied=([[:alnum:]]+?);.*")
cond::Iov_t getInterval(cond::Time_t time)
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
std::pair< std::string, boost::shared_ptr< void > > fetch(const cond::Hash &payloadId, Session &session)
bool getInsertionLogs(const std::string &tag, cond::DbSession &logDbSession, std::vector< std::pair< cond::Time_t, boost::posix_time::ptime > > &loggedInsertions)
std::string connectionString()
bool copyIov(Session &session, const std::string &sourceTag, const std::string &destTag, cond::Time_t souceSince, cond::Time_t destSince, const std::string &description, bool log)
IOVEditor editIov(const std::string &tag)
void insert(cond::Time_t since, const cond::Hash &payloadHash, bool checkType=false)
DecomposeProduct< arg, typename Div::arg > D
static const std::string time_0("2008-01-01 00:00:42.000")
coral::ISchema & nominalSchema()
static const boost::regex popcon_many("PopCon[[:print:]]+?first payload Since ([[:alnum:]]+?),.*")
bool existsIov(const std::string &tag)
cond::TimeType timeType() const
bool validateTag(const std::string &refTag, Session &refSession, const std::string &candTag, Session &candSession)
IOVEditor createIov(const std::string &tag, cond::TimeType timeType, cond::SynchronizationType synchronizationType=cond::OFFLINE)
bool operator()(const cond::Time_t &x, const std::pair< cond::Time_t, boost::posix_time::ptime > &y)
std::string payloadType() const
bool compareTags(const std::string &firstTag, Session &firstSession, const std::string &firstFileName, const std::string &secondTag, Session &secondSession, const std::string &secondFileName)
void throwException(const std::string &message, const std::string &methodName)
std::vector< T >::const_iterator search(const cond::Time_t &val, const std::vector< T > &container)