CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch2/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() : iov(0){}
00089     IterHelp( IOVProxyData & in);
00090     
00091     result_type const & operator()(int i) const {
00092       if (iov) elem.set(*iov,i);
00093       return elem;
00094     }
00095     
00096   private:
00097     IOVSequence const * iov;
00098     mutable IOVElementProxy elem;
00099   };
00100 
00101   typedef boost::transform_iterator<IterHelp,boost::counting_iterator<int> > iov_range_iterator;
00102 
00103   class IOVRange {
00104   public:
00105     
00106     typedef iov_range_iterator const_iterator;
00107 
00108   public:
00109     IOVRange();
00110     
00111     IOVRange( const boost::shared_ptr<IOVProxyData>& iov, int low, int high );
00112     
00113     IOVRange( const IOVRange& rhs );
00114     
00115     IOVRange& operator=( const IOVRange& rhs );
00116     
00117     const_iterator begin() const {
00118       return  boost::make_transform_iterator(boost::counting_iterator<int>(m_low),
00119                                              IterHelp(*m_iov));
00120     }
00121     
00122     const_iterator end() const {
00123       // returns: 0 if not inizialized (m_low=m_high=-1)
00124       //          m_high + 1 = m_low + size elsewhere (since size = m_high - m_low + 1)
00125       int index = m_high + 1;
00126       return  boost::make_transform_iterator(boost::counting_iterator<int>(index),
00127                                              IterHelp(*m_iov));
00128     }
00129     
00130     const_iterator find(cond::Time_t time) const;
00131 
00132     IOVElementProxy front() const;
00133 
00134     IOVElementProxy back() const;
00135 
00136     size_t size() const;
00137 
00138   private:
00139     boost::shared_ptr<IOVProxyData> m_iov;
00140     int m_low;
00141     int m_high;
00142   };
00143 
00144   /* IOV as the user wants it
00145    */
00146   class IOVProxy {
00147   public:
00148     typedef iov_range_iterator const_iterator;
00149 
00150   public:  
00151     IOVProxy();
00152 
00153     ~IOVProxy();
00154     
00155     explicit IOVProxy(cond::DbSession& dbSession);
00156 
00157     IOVProxy(cond::DbSession& dbSession, const std::string & token );
00158 
00159     IOVProxy( boost::shared_ptr<IOVProxyData>& data );
00160 
00161     IOVProxy( const IOVProxy& rhs );
00162 
00163     IOVProxy& operator=( const IOVProxy& rhs );
00164 
00165     void load( const std::string & token );
00166 
00167     bool refresh();
00168     bool refresh( cond::DbSession& newSession );
00169 
00170     const std::string& token(); 
00171     
00172     bool isValid( cond::Time_t currenttime );
00173 
00174     std::pair<cond::Time_t, cond::Time_t> validity( cond::Time_t currenttime );
00175     
00176     const_iterator begin() const {
00177       return  boost::make_transform_iterator(boost::counting_iterator<int>(0),
00178                                              IterHelp(*m_iov));
00179     }
00180     
00181     const_iterator end() const {
00182       int index = size();
00183       return  boost::make_transform_iterator(boost::counting_iterator<int>(index),
00184                                              IterHelp(*m_iov));
00185     }
00186 
00187     // find 
00188     const_iterator find(cond::Time_t time) const;
00189 
00190     // limit to the first n 
00191     IOVRange head(int n) const;
00192 
00193     // limit to the last n
00194     IOVRange tail(int n) const;
00195 
00196     IOVRange range(cond::Time_t since, cond::Time_t  till) const;
00197 
00198     IOVRange rangeHead(cond::Time_t since, cond::Time_t  till, int n) const;
00199 
00200     IOVRange rangeTail(cond::Time_t since, cond::Time_t  till, int n) const;
00201 
00202     int size() const;
00203 
00204     IOVSequence const & iov() const;
00205     
00206     TimeType timetype() const;
00207 
00208     cond::Time_t firstSince() const;
00209 
00210     cond::Time_t lastTill() const;
00211 
00212     std::set<std::string> const& payloadClasses() const;
00213 
00214     std::string comment() const;
00215 
00216     int revision() const;
00217 
00218     cond::Time_t timestamp() const;
00219 
00220     SequenceState state() const {
00221       return SequenceState(iov());
00222     }
00223 
00224     DbSession& db() const;
00225 
00226   private:
00227     boost::shared_ptr<IOVProxyData> m_iov;
00228     
00229   };
00230 }
00231 
00232 #endif // CondCore_IOVService_IOVProxy_h