Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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-2005, Jeroen T. Vermeulen <jtv@xs4all.nl>
00011  *
00012  * See COPYING for copyright license.  If you did not receive a file called
00013  * COPYING with this source code, please notify the distributor of this mistake,
00014  * or contact the author.
00015  *
00016  *-------------------------------------------------------------------------
00017  */
00018 #ifndef PQXX_CACHEDRESULT_H
00019 #define PQXX_CACHEDRESULT_H
00020 
00021 #include "pqxx/libcompiler.h"
00022 
00023 #include <map>
00024 
00025 #include "pqxx/cursor.h"
00026 #include "pqxx/result.h"
00027 
00028 namespace pqxx
00029 {
00030 
00055 class PQXX_LIBEXPORT cachedresult
00056 {
00057 public:
00058   typedef result::size_type size_type;
00059   typedef size_type blocknum;
00060 
00062   typedef result::tuple tuple;
00063 
00065   typedef tuple Tuple;
00066 
00078   template<typename TRANSACTION> explicit
00079     cachedresult(TRANSACTION &T,
00080                  const char Query[],
00081                  const PGSTD::string &BaseName="query",
00082                  size_type Granularity=100) :                           //[t40]
00083       m_Granularity(Granularity),
00084       m_Cache(),
00085       m_Cursor(T, Query, BaseName, Granularity),
00086       m_EmptyResult(),
00087       m_HaveEmpty(false)
00088   {
00089     // Trigger build error if T has insufficient isolation level
00090     error_permitted_isolation_level(PQXX_TYPENAME TRANSACTION::isolation_tag());
00091     init();
00092   }
00093 
00094 
00096 
00104   const tuple operator[](size_type i) const                             //[t41]
00105         { return GetBlock(BlockFor(i))[Offset(i)]; }
00106 
00108 
00119    const tuple at(size_type i) const                                    //[t40]
00120         { return GetBlock(BlockFor(i)).at(Offset(i)); }
00121 
00123   size_type size() const;                                               //[t40]
00124 
00126   bool empty() const;                                                   //[t47]
00127 
00128 private:
00129   typedef Cursor::pos pos;
00130 
00132 
00137   static void
00138     error_permitted_isolation_level(isolation_traits<serializable>) throw () {}
00139 
00140   void init();
00141 
00142   blocknum BlockFor(size_type Row) const throw ()
00143         { return Row / m_Granularity; }
00144   size_type Offset(size_type Row) const throw ()
00145         { return Row % m_Granularity; }
00146   Cursor::size_type FirstRowOf(blocknum Block) const throw ()
00147         { return Block*m_Granularity; }
00148 
00149   void MoveTo(blocknum) const;
00150 
00152   const result &Fetch() const;
00153 
00154   const result &GetBlock(blocknum b) const
00155   {
00156     CacheMap::const_iterator i = m_Cache.find(b);
00157     if (i != m_Cache.end()) return i->second;
00158 
00159     MoveTo(b);
00160     return Fetch();
00161   }
00162 
00164   size_type m_Granularity;
00165 
00166   typedef PGSTD::map<blocknum, result> CacheMap;
00167   mutable CacheMap m_Cache;
00168 
00169   mutable Cursor m_Cursor;
00170   mutable result m_EmptyResult;
00171   mutable bool   m_HaveEmpty;
00172 
00173   // Not allowed:
00174   cachedresult();
00175   cachedresult(const cachedresult &);
00176   cachedresult &operator=(const cachedresult &);
00177 };
00178 
00180 typedef cachedresult CachedResult;
00181 
00182 } // namespace pqxx
00183 
00184 #endif
00185 

Generated on Fri May 6 17:10:10 2005 for libpqxx by  doxygen 1.4.2