CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/CondCore/IOVService/interface/IOVProxy.h

Go to the documentation of this file.
00001 #ifndef CondCore_IOVService_IOVProxy_h
00002 #define CondCore_IOVService_IOVProxy_h
00003 
00004 #include "CondCore/DBCommon/interface/DbSession.h"
00005 
00006 #include "CondFormats/Common/interface/IOVSequence.h"
00007 #include "CondFormats/Common/interface/SequenceState.h"
00008 #include "CondFormats/Common/interface/Time.h"
00009 
00010 //
00011 #include <boost/shared_ptr.hpp>
00012 #include <boost/iterator/transform_iterator.hpp>
00013 #include <boost/iterator/counting_iterator.hpp>
00014 
00015 namespace cond {
00016   
00017   class IOVSequence;
00018 
00019   struct IOVProxyData {
00020     explicit IOVProxyData( cond::DbSession& dbs ) : 
00021       dbSession(dbs),
00022       data(), 
00023       token(""){
00024     }
00025       
00026     IOVProxyData(cond::DbSession& dbs,
00027                  const std::string & tok) : 
00028       dbSession(dbs),
00029       data(), 
00030       token(tok){
00031       refresh();
00032     }
00033 
00034     ~IOVProxyData(){  
00035     }
00036       
00037     void refresh(); 
00038     void refresh( cond::DbSession& dbs ); 
00039      
00040     std::pair<int,int> range( cond::Time_t since, cond::Time_t  till );
00041 
00042     cond::DbSession dbSession;
00043     boost::shared_ptr<cond::IOVSequence> data;
00044     std::string token;
00045   };
00046   
00047   class IOVElementProxy {
00048   public:
00049     IOVElementProxy() : 
00050       m_since(cond::invalidTime),
00051       m_till(cond::invalidTime),
00052       m_token("") {}
00053     
00054     IOVElementProxy(cond::Time_t is, cond::Time_t it, std::string const& itoken):
00055       m_since(is), 
00056       m_till(it), 
00057       m_token(itoken) {}
00058     
00059     void set(cond::Time_t is, cond::Time_t it, std::string const& itoken) {
00060       m_since=is; 
00061       m_till=it; 
00062       m_token=itoken;
00063     }
00064     
00065     void set(IOVSequence const & v, int i);
00066     
00067     cond::Time_t since() const {
00068       return m_since;
00069     }
00070     
00071     cond::Time_t till() const {
00072       return m_till;
00073     }
00074     
00075     std::string const & token() const {
00076       return m_token;
00077     }
00078  
00079   private:
00080     cond::Time_t m_since;
00081     cond::Time_t m_till;
00082     std::string m_token;
00083   };
00084     
00085   class IterHelp {
00086   public:
00087     typedef IOVElementProxy result_type;
00088     IterHelp() : lowBound(0),iov(0){}
00089     IterHelp( IOVProxyData & in );
00090     IterHelp( IOVProxyData & in, cond::Time_t lowBound );
00091     
00092     result_type const & operator()(int i) const {
00093       if (iov) {
00094         IOVElementProxy tmp;
00095         tmp.set(*iov,i);
00096         cond::Time_t since = tmp.since();
00097         if( since < lowBound ) since = lowBound;
00098         elem.set( since, tmp.till(), tmp.token() ); 
00099       }
00100       return elem;
00101     }
00102     
00103   private:
00104     cond::Time_t lowBound;
00105     IOVSequence const * iov;
00106     mutable IOVElementProxy elem;
00107   };
00108 
00109   typedef boost::transform_iterator<IterHelp,boost::counting_iterator<int> > iov_range_iterator;
00110 
00111   class IOVRange {
00112   public:
00113     
00114     typedef iov_range_iterator const_iterator;
00115 
00116   public:
00117     IOVRange();
00118     
00119     // selection = 0 for the full sample, +n for the head(n), -n for the tail(n)
00120     IOVRange( const boost::shared_ptr<IOVProxyData>& iov, cond::Time_t since, cond::Time_t till, int selection=0 );
00121     // selection = 0 for the full sample, +n for the head(n), -n for the tail(n)
00122     IOVRange( const boost::shared_ptr<IOVProxyData>& iov, int selection );
00123     
00124     IOVRange( const IOVRange& rhs );
00125     
00126     IOVRange& operator=( const IOVRange& rhs );
00127     
00128     const_iterator begin() const {
00129       return  boost::make_transform_iterator(boost::counting_iterator<int>(m_low),
00130                                              IterHelp(*m_iov, m_lowBound));
00131     }
00132     
00133     const_iterator end() const {
00134       // returns: 0 if not inizialized (m_low=m_high=-1)
00135       //          m_high + 1 = m_low + size elsewhere (since size = m_high - m_low + 1)
00136       int index = m_high + 1;
00137       return  boost::make_transform_iterator(boost::counting_iterator<int>(index),
00138                                              IterHelp(*m_iov, m_lowBound));
00139     }
00140     
00141     const_iterator find(cond::Time_t time) const;
00142 
00143     IOVElementProxy front() const;
00144 
00145     IOVElementProxy back() const;
00146 
00147     size_t size() const;
00148 
00149   private:
00150     boost::shared_ptr<IOVProxyData> m_iov;
00151     int m_low;
00152     int m_high;
00153     cond::Time_t m_lowBound;
00154   };
00155 
00156   /* IOV as the user wants it
00157    */
00158   class IOVProxy {
00159   public:
00160     typedef iov_range_iterator const_iterator;
00161 
00162   public:  
00163     IOVProxy();
00164 
00165     ~IOVProxy();
00166     
00167     explicit IOVProxy(cond::DbSession& dbSession);
00168 
00169     IOVProxy(cond::DbSession& dbSession, const std::string & token );
00170 
00171     IOVProxy( boost::shared_ptr<IOVProxyData>& data );
00172 
00173     IOVProxy( const IOVProxy& rhs );
00174 
00175     IOVProxy& operator=( const IOVProxy& rhs );
00176 
00177     void load( const std::string & token );
00178 
00179     bool refresh();
00180     bool refresh( cond::DbSession& newSession );
00181 
00182     const std::string& token(); 
00183     
00184     bool isValid( cond::Time_t currenttime );
00185 
00186     std::pair<cond::Time_t, cond::Time_t> validity( cond::Time_t currenttime );
00187     
00188     const_iterator begin() const {
00189       return  boost::make_transform_iterator(boost::counting_iterator<int>(0),
00190                                              IterHelp(*m_iov));
00191     }
00192     
00193     const_iterator end() const {
00194       int index = size();
00195       return  boost::make_transform_iterator(boost::counting_iterator<int>(index),
00196                                              IterHelp(*m_iov));
00197     }
00198 
00199     // find 
00200     const_iterator find(cond::Time_t time) const;
00201 
00202     // limit to the first n 
00203     IOVRange head(int n) const;
00204 
00205     // limit to the last n
00206     IOVRange tail(int n) const;
00207 
00208     IOVRange range(cond::Time_t since, cond::Time_t  till) const;
00209 
00210     IOVRange rangeHead(cond::Time_t since, cond::Time_t  till, int n) const;
00211 
00212     IOVRange rangeTail(cond::Time_t since, cond::Time_t  till, int n) const;
00213 
00214     int size() const;
00215 
00216     IOVSequence const & iov() const;
00217     
00218     TimeType timetype() const;
00219 
00220     cond::Time_t firstSince() const;
00221 
00222     cond::Time_t lastTill() const;
00223 
00224     std::set<std::string> const& payloadClasses() const;
00225 
00226     std::string comment() const;
00227 
00228     int revision() const;
00229 
00230     cond::Time_t timestamp() const;
00231 
00232     SequenceState state() const {
00233       return SequenceState(iov());
00234     }
00235 
00236     DbSession& db() const;
00237 
00238   private:
00239     boost::shared_ptr<IOVProxyData> m_iov;
00240     
00241   };
00242 }
00243 
00244 #endif // CondCore_IOVService_IOVProxy_h