00001 #include "CondCore/IOVService/interface/IOVProxy.h"
00002
00003 #include "CondFormats/Common/interface/TimeConversions.h"
00004
00005 void cond::IOVProxyData::refresh(){
00006 data = dbSession.getTypedObject<cond::IOVSequence>( token );
00007
00008 data->loadAll();
00009
00010 if( dbSession.isOldSchema() ){
00011 PoolTokenParser parser( dbSession.storage() );
00012 data->swapTokens( parser );
00013 }
00014 }
00015
00016 std::pair<int,int> cond::IOVProxyData::range( cond::Time_t since,
00017 cond::Time_t till ){
00018 int low = (since<data->iovs().front().sinceTime()) ? 0 :
00019 data->find(since)-data->iovs().begin();
00020 int high = data->find(till) - data->iovs().begin();
00021 high = std::min( high+1, (int)data->iovs().size());
00022 return std::make_pair( low, high );
00023 }
00024
00025 void cond::IOVElementProxy::set(IOVSequence const & v, int ii) {
00026 size_t i =ii;
00027 if (i>=v.iovs().size()) {
00028 set(cond::invalidTime, cond::invalidTime,"");
00029 return;
00030 }
00031 m_token = v.iovs()[i].token();
00032 m_since = v.iovs()[i].sinceTime();
00033 if(i+1==v.iovs().size()) {
00034 m_till = v.lastTill();
00035 return;
00036 }
00037 cond::UnpackedTime unpackedTime;
00038 cond::Time_t totalNanoseconds;
00039 cond::Time_t totalSecondsInNanoseconds;
00040 switch (v.timeType()) {
00041 case timestamp:
00042
00043 unpackedTime = cond::time::unpack(v.iovs()[i+1].sinceTime());
00044
00045 totalSecondsInNanoseconds = ((cond::Time_t)unpackedTime.first)*1000000000;
00046
00047 totalNanoseconds = totalSecondsInNanoseconds + ((cond::Time_t)(unpackedTime.second));
00048
00049 totalNanoseconds--;
00050
00051 unpackedTime.first = (unsigned int) (totalNanoseconds/1000000000);
00052 unpackedTime.second = (unsigned int)(totalNanoseconds - (cond::Time_t)unpackedTime.first*1000000000);
00053 m_till = cond::time::pack(unpackedTime);
00054 break;
00055 default:
00056 m_till = v.iovs()[i+1].sinceTime()-1;
00057 }
00058 }
00059
00060 cond::IterHelp::IterHelp(IOVProxyData& impl) :
00061 iov(&(*impl.data)),
00062 elem(){
00063 }
00064
00065 cond::IOVRange::IOVRange():
00066 m_iov(),
00067 m_low(-1),
00068 m_high(-1){
00069 }
00070
00071 cond::IOVRange::IOVRange( const boost::shared_ptr<IOVProxyData>& iov,
00072 int low,
00073 int high ):
00074 m_iov( iov ),
00075 m_low( low ),
00076 m_high( high ){
00077 }
00078
00079 cond::IOVRange::IOVRange( const IOVRange& rhs ):
00080 m_iov( rhs.m_iov ),
00081 m_low( rhs.m_low ),
00082 m_high( rhs.m_high ){
00083 }
00084
00085 cond::IOVRange& cond::IOVRange::operator=( const IOVRange& rhs ){
00086 m_iov = rhs.m_iov;
00087 m_low = rhs.m_low;
00088 m_high = rhs.m_high;
00089 return *this;
00090 }
00091
00092 cond::IOVRange::const_iterator cond::IOVRange::find(cond::Time_t time) const {
00093 int n = m_iov->data->find(time)-m_iov->data->iovs().begin();
00094 return (n<m_low || m_high<n ) ?
00095 end() :
00096 boost::make_transform_iterator(boost::counting_iterator<int>(n),
00097 IterHelp(*m_iov));
00098 }
00099
00100 cond::IOVElementProxy cond::IOVRange::front() const {
00101 IterHelp helper(*m_iov);
00102 return helper( m_low );
00103 }
00104
00105 cond::IOVElementProxy cond::IOVRange::back() const {
00106 IterHelp helper(*m_iov);
00107 return helper( m_high );
00108 }
00109
00110 size_t cond::IOVRange::size() const {
00111 return m_high-m_low;
00112 }
00113
00114 cond::IOVProxy::IOVProxy() :
00115 m_iov(){
00116 }
00117
00118 cond::IOVProxy::~IOVProxy() {
00119 }
00120
00121 cond::IOVProxy::IOVProxy(cond::DbSession& dbSession):
00122 m_iov(new IOVProxyData(dbSession)){
00123 }
00124
00125 cond::IOVProxy::IOVProxy(cond::DbSession& dbSession,
00126 const std::string & token):
00127 m_iov(new IOVProxyData(dbSession,token)){
00128 }
00129
00130 cond::IOVProxy::IOVProxy( boost::shared_ptr<IOVProxyData>& data ):
00131 m_iov( data ){
00132 }
00133
00134 cond::IOVProxy::IOVProxy( const IOVProxy& rhs ):
00135 m_iov( rhs.m_iov ){
00136 }
00137
00138 cond::IOVProxy& cond::IOVProxy::operator=( const IOVProxy& rhs ){
00139 m_iov = rhs.m_iov;
00140 return *this;
00141 }
00142
00143 void cond::IOVProxy::load( const std::string & token){
00144 m_iov->token = token;
00145 m_iov->refresh();
00146 }
00147
00148 bool cond::IOVProxy::refresh(){
00149 int oldsize = size();
00150 m_iov->refresh();
00151 return oldsize<size();
00152 }
00153
00154 const std::string& cond::IOVProxy::token(){
00155 return m_iov->token;
00156 }
00157
00158 bool cond::IOVProxy::isValid( cond::Time_t currenttime ){
00159 return ( currenttime >= iov().firstSince() &&
00160 currenttime <= iov().lastTill() );
00161 }
00162
00163 std::pair<cond::Time_t, cond::Time_t> cond::IOVProxy::validity( cond::Time_t currenttime ){
00164 cond::Time_t since=iov().firstSince();
00165 cond::Time_t till=iov().lastTill();
00166 IOVSequence::const_iterator iter=iov().find(currenttime);
00167 if (iter!=iov().iovs().end()) {
00168 since=iter->sinceTime();
00169 iter++;
00170 if (iter!=iov().iovs().end())
00171 till = iter->sinceTime()-1;
00172 }
00173 else {
00174 since=iov().lastTill();
00175 }
00176 return std::pair<cond::Time_t, cond::Time_t>(since,till);
00177 }
00178
00179 cond::IOVRange cond::IOVProxy::range(cond::Time_t since,
00180 cond::Time_t till) const {
00181 std::pair<int,int> rg = m_iov->range( since, till );
00182 return IOVRange( m_iov, rg.first, rg.second );
00183 }
00184
00185 cond::IOVRange cond::IOVProxy::rangeHead(cond::Time_t since,
00186 cond::Time_t till,
00187 int n) const {
00188 std::pair<int,int> rg = m_iov->range( since, till );
00189 return IOVRange( m_iov, rg.first, std::min( rg.first+n, rg.second ) );
00190 }
00191
00192 cond::IOVRange cond::IOVProxy::rangeTail(cond::Time_t since,
00193 cond::Time_t till,
00194 int n) const {
00195 std::pair<int,int> rg = m_iov->range( since, till );
00196 return IOVRange( m_iov, std::max( rg.second-n,rg.first), rg.second );
00197 }
00198
00199 cond::IOVRange cond::IOVProxy::head(int n) const {
00200 int high = std::min( n, size() );
00201 return IOVRange( m_iov, 0, high );
00202 }
00203
00204 cond::IOVRange cond::IOVProxy::tail(int n) const {
00205 int low = std::max(size()-n, 0);
00206 return IOVRange( m_iov, low, size() );
00207 }
00208
00209 cond::IOVProxy::const_iterator cond::IOVProxy::find(cond::Time_t time) const {
00210 int n = iov().find(time)-iov().iovs().begin();
00211 return ( n<0 || n>size() ) ?
00212 end() :
00213 boost::make_transform_iterator(boost::counting_iterator<int>(n),
00214 IterHelp(*m_iov));
00215 }
00216
00217 int cond::IOVProxy::size() const {
00218 return iov().iovs().size();
00219 }
00220
00221 cond::IOVSequence const & cond::IOVProxy::iov() const {
00222 return *(m_iov->data);
00223 }
00224
00225 cond::TimeType cond::IOVProxy::timetype() const {
00226 return iov().timeType();
00227 }
00228
00229 cond::Time_t cond::IOVProxy::firstSince() const {
00230 return iov().firstSince();
00231 }
00232
00233 cond::Time_t cond::IOVProxy::lastTill() const {
00234 return iov().lastTill();
00235 }
00236
00237 std::set<std::string> const&
00238 cond::IOVProxy::payloadClasses() const{
00239 return iov().payloadClasses();
00240 }
00241
00242 std::string
00243 cond::IOVProxy::comment() const{
00244 return iov().comment();
00245 }
00246
00247 int
00248 cond::IOVProxy::revision() const{
00249 return iov().revision();
00250 }
00251
00252 cond::Time_t cond::IOVProxy::timestamp() const {
00253 return iov().timestamp();
00254 }
00255
00256 cond::DbSession& cond::IOVProxy::db() const {
00257 return m_iov->dbSession;
00258 }
00259
00260
00261