00001 #ifndef DBICACHE_H 00002 #define DBICACHE_H 00003 00004 /** 00005 * 00006 * 00007 * \class TDbiCache 00008 * 00009 * 00010 * \brief 00011 * <b>Concept</b> Cache holding TDbiResultSet s for a specific database table. 00012 * 00013 * \brief 00014 * <b>Purpose</b> A TDbiCache is an object that minimises database I/O 00015 * by caching query results. Queries are always first sent to the 00016 * cache and only if not present are they sent down to the database. 00017 * 00018 * Contact: A.Finch@lancaster.ac.uk 00019 * 00020 * 00021 */ 00022 00023 #include <list> 00024 #include <map> 00025 #include <string> 00026 using std::string; 00027 00028 #include "TDbi.hxx" 00029 00030 class TVldContext; 00031 class TDbiResultSet; 00032 class TDbiDatabaseManager; 00033 class TDbiTableProxy; 00034 class TDbiValidityRec; 00035 //class ostream; 00036 00037 class TDbiCache 00038 { 00039 00040 friend class TDbiValidate; // To allow access to purge. 00041 public: 00042 00043 // Typedefs 00044 00045 typedef std::list<TDbiResultSet*> ResultList_t; 00046 00047 // Constructors and destructors. 00048 00049 TDbiCache(TDbiTableProxy& qp, 00050 const string& tableName); 00051 virtual ~TDbiCache(); 00052 00053 00054 // State testing member functions 00055 UInt_t GetMaxSize() const { return fMaxSize; } 00056 UInt_t GetCurSize() const { return fCurSize; } 00057 UInt_t GetNumAdopted() const { return fNumAdopted; } 00058 UInt_t GetNumReused() const { return fNumReused; } 00059 // Primary searches. 00060 const TDbiResultSet* Search(const TVldContext& vc, 00061 const TDbi::Task& task) const; 00062 const TDbiResultSet* Search(const string& sqlQualifiers) const; 00063 00064 /// Secondary search. 00065 const TDbiResultSet* Search(const TDbiValidityRec& vr, 00066 const string& sqlQualifiers = "") const; 00067 ostream& ShowStatistics(ostream& msg) const; 00068 00069 // State changing member functions 00070 void Adopt(TDbiResultSet* res,bool generateKey = true); 00071 void Purge(); 00072 void SetStale(); 00073 00074 protected: 00075 00076 // State testing member functions 00077 00078 // State changing member functions 00079 00080 private: 00081 00082 // Disabled (not implemented) copy constructor and asignment. 00083 TDbiCache(const TDbiCache&); 00084 TDbiCache& operator=(const TDbiCache&); 00085 00086 const ResultList_t* GetSubCache(Int_t aggNo) const; 00087 void Purge(ResultList_t& subCache, const TDbiResultSet* res=0); 00088 00089 // Data members 00090 00091 00092 /// TableProxy owning cache. 00093 TDbiTableProxy& fTableProxy; 00094 00095 /// Name of associated table. 00096 const string& fTableName; 00097 00098 /// Map of sub-caches indexed by aggregate number. 00099 /// Each sub-cache is a list of owned results for 00100 /// that aggregate. 00101 std::map<Int_t,ResultList_t> fCache; 00102 00103 /// Current size 00104 mutable UInt_t fCurSize; 00105 00106 /// Max (high water) size 00107 mutable UInt_t fMaxSize; 00108 00109 /// Total number adopted 00110 mutable UInt_t fNumAdopted; 00111 00112 /// Number reused i.e. found. 00113 mutable UInt_t fNumReused; 00114 00115 00116 ClassDef(TDbiCache,0) //Query result cache for specific database table. 00117 00118 }; 00119 00120 00121 00122 #endif // DBICACHE_H 00123