00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef PQXX_CACHEDRESULT_H
00015 #define PQXX_CACHEDRESULT_H
00016
00017 #include <map>
00018
00019 #include "pqxx/cursor.h"
00020 #include "pqxx/result.h"
00021
00022 namespace pqxx
00023 {
00024
00025 class TransactionItf;
00026
00027
00028
00046 class PQXX_LIBEXPORT CachedResult
00047 {
00048 public:
00049 typedef Result::size_type size_type;
00050 typedef size_type blocknum;
00051 typedef Result::Tuple Tuple;
00052
00057 explicit CachedResult(pqxx::TransactionItf &,
00058 const char Query[],
00059 const PGSTD::string &BaseName="query",
00060 size_type Granularity=100);
00061
00062
00063
00064
00065
00066 const Tuple operator[](size_type i) const
00067 { return GetBlock(BlockFor(i))[Offset(i)]; }
00068
00069 const Tuple at(size_type i) const
00070 { return GetBlock(BlockFor(i)).at(Offset(i)); }
00071
00073 size_type size() const;
00074
00076 bool empty() const;
00077
00079 void clear();
00080
00082 class const_iterator
00083 {
00084 const CachedResult &m_Home;
00085 CachedResult::size_type m_Row;
00086 public:
00087 explicit const_iterator(const CachedResult &Home) : m_Home(Home), m_Row(0){}
00088
00089 private:
00090
00091 const_iterator();
00092 };
00093
00094 private:
00095
00096 typedef Cursor::pos pos;
00097
00098 class CacheEntry
00099 {
00100 int m_RefCount;
00101 Result m_Data;
00102
00103 public:
00104 CacheEntry() : m_RefCount(0), m_Data() {}
00105 explicit CacheEntry(const Result &R) : m_RefCount(0), m_Data(R) {}
00106
00107 const Result &Data() const { return m_Data; }
00108 int RefCount() const { return m_RefCount; }
00109 };
00110
00111
00112 blocknum BlockFor(size_type Row) const throw ()
00113 { return Row / m_Granularity; }
00114 size_type Offset(size_type Row) const throw ()
00115 { return Row % m_Granularity; }
00116 Cursor::size_type FirstRowOf(blocknum Block) const throw ()
00117 { return Block*m_Granularity; }
00118
00119 void MoveTo(blocknum) const;
00120
00122 Result Fetch() const;
00123
00124 Result GetBlock(blocknum b) const
00125 {
00126 CacheMap::const_iterator i = m_Cache.find(b);
00127 if (i != m_Cache.end()) return i->second.Data();
00128
00129 MoveTo(b);
00130 return Fetch();
00131 }
00132
00134 size_type m_Granularity;
00135
00136 typedef PGSTD::map<blocknum, CacheEntry> CacheMap;
00137 mutable CacheMap m_Cache;
00138
00139 mutable Cursor m_Cursor;
00140
00141
00142 CachedResult();
00143 CachedResult(const CachedResult &);
00144 CachedResult &operator=(const CachedResult &);
00145 };
00146
00147
00148 }
00149
00150 #endif
00151