Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

cachedresult.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  *   FILE
00004  *      pqxx/cachedresult.h
00005  *
00006  *   DESCRIPTION
00007  *      definitions for the pqxx::CachedResult class and support classes.
00008  *   pqxx::CachedResult is a lazy-fetching, transparently-cached result set
00009  *
00010  * Copyright (c) 2001-2002, Jeroen T. Vermeulen <jtv@xs4all.nl>
00011  *
00012  *-------------------------------------------------------------------------
00013  */
00014 #ifndef PQXX_CACHEDRESULT_H
00015 #define PQXX_CACHEDRESULT_H
00016 
00017 #include "pqxx/result.h"
00018 
00019 namespace pqxx
00020 {
00021 
00022 class PQXX_LIBEXPORT CachedResult
00023 {
00024 public:
00025   typedef Result::size_type size_type;
00026 
00027   // TODO: Add name?
00028   explicit CachedResult(pqxx::TransactionItf &Trans,
00029                         std::string Query,
00030                         size_type Granularity=100,
00031                         size_type MaxBlocks=100) :
00032     m_Granularity(Granularity),
00033     m_MaxBlocks(MaxBlocks),
00034     m_Cache()
00035   {
00036   }
00037 
00038   ~CachedResult();                                                      //[]
00039 
00040   class PQXX_LIBEXPORT const_iterator
00041   {
00042   public:
00043     typedef Result::Tuple::size_type size_type;
00044 
00045     const_iterator(const CachedResult *r, CachedResult::size_type i) :
00046       m_Home(r),
00047       m_Block(r->BlocKFor(i)),
00048       m_Entry(r->BlockEntry(i))
00049     {
00050       m_Home->GrabBlock(m_Block);
00051     }
00052 
00053     ~const_iterator() { m_Home->ReleaseBlock(m_Block); }
00054 
00055     const Result::Tuple operator*() const
00056     {
00057       return m_Home->m_Cache[m_Home->BlockFor(i)];
00058     }
00059 
00060     const Result::Field operator[](size_type i) const
00061     {
00062       return operator*().second[m_Home->BlockEntry(i)];
00063     }
00064 
00065   private:
00066     const CachedResult *m_Home;
00067     CachedResult::size_type m_Block, m_Entry;
00068   };
00069 
00070   const const_iterator operator[](size_type) const;                     //[]
00071   const const_iterator at(size_type) const;                             //[]
00072 
00073   void clear();                                                         //[]
00074 
00075 private:
00076   size_type BlockFor(size_type Row) const { return Row / m_Granularity; }
00077   size_type BlockEntry(size_type Row) const { return Row % m_Granularity; }
00078 
00079   void GrabBlock(size_type Block);
00080   void ReleaseBlock(size_type Block);
00081 
00082   size_type m_Granularity;
00083   size_type m_MaxBlocks;
00084   std::map<size_type, std::pair<int, Result> > m_Cache;
00085 
00086   // Not allowed:
00087   CachedResult(const CachedResult &);
00088   CachedResult &operator=(const CachedResult &);
00089 };
00090 
00091 
00092 } // namespace pqxx
00093 
00094 #endif
00095 

Generated on Wed Oct 23 15:57:05 2002 for libpqxx by doxygen1.2.18