Concept Cache holding TDbiResultSet s for a specific database table. More...
#include <TDbiCache.hxx>
Public Types | |
typedef std::list < TDbiResultSet * > | ResultList_t |
Public Member Functions | |
TDbiCache (TDbiTableProxy &qp, const string &tableName) | |
virtual | ~TDbiCache () |
UInt_t | GetMaxSize () const |
UInt_t | GetCurSize () const |
UInt_t | GetNumAdopted () const |
UInt_t | GetNumReused () const |
const TDbiResultSet * | Search (const TVldContext &vc, const TDbi::Task &task) const |
const TDbiResultSet * | Search (const string &sqlQualifiers) const |
const TDbiResultSet * | Search (const TDbiValidityRec &vr, const string &sqlQualifiers="") const |
Secondary search. | |
ostream & | ShowStatistics (ostream &msg) const |
void | Adopt (TDbiResultSet *res, bool generateKey=true) |
void | Purge () |
void | SetStale () |
Private Member Functions | |
TDbiCache (const TDbiCache &) | |
TDbiCache & | operator= (const TDbiCache &) |
const ResultList_t * | GetSubCache (Int_t aggNo) const |
void | Purge (ResultList_t &subCache, const TDbiResultSet *res=0) |
Private Attributes | |
TDbiTableProxy & | fTableProxy |
TableProxy owning cache. | |
const string & | fTableName |
Name of associated table. | |
std::map< Int_t, ResultList_t > | fCache |
UInt_t | fCurSize |
Current size. | |
UInt_t | fMaxSize |
Max (high water) size. | |
UInt_t | fNumAdopted |
Total number adopted. | |
UInt_t | fNumReused |
Number reused i.e. found. | |
Friends | |
class | TDbiValidate |
Concept Cache holding TDbiResultSet s for a specific database table.
Purpose A TDbiCache is an object that minimises database I/O by caching query results. Queries are always first sent to the cache and only if not present are they sent down to the database. Contact: A.Finch@lancaster.ac.uk
Definition at line 37 of file TDbiCache.hxx.
typedef std::list<TDbiResultSet*> TDbiCache::ResultList_t |
Definition at line 45 of file TDbiCache.hxx.
TDbiCache::TDbiCache | ( | TDbiTableProxy & | qp, | |
const string & | tableName | |||
) |
Purpose: Constructor Arguments: in qp Owning TDbiTableProxy. in tableName Name of associated table Return: n/a Contact: N. West Specification:- ============= o Create empty cache. Program Notes:- ============= None.
Definition at line 61 of file TDbiCache.cxx.
References SK_DBI_Trace.
00061 : 00062 fTableProxy(qp), 00063 fTableName(tableName), 00064 fCurSize(0), 00065 fMaxSize(0), 00066 fNumAdopted(0), 00067 fNumReused(0) 00068 { 00069 00070 00071 SK_DBI_Trace( "Creating TDbiCache" << " "); 00072 00073 }
TDbiCache::~TDbiCache | ( | ) | [virtual] |
Purpose: Destructor Arguments: None. Return: n/a Contact: N. West Specification:- ============= o Destroy cache and all owned TDbiResultSets. Program Notes:- ============= None.
Definition at line 98 of file TDbiCache.cxx.
References fCache, GetSubCache(), Purge(), and SK_DBI_Trace.
00098 { 00099 00100 00101 00102 SK_DBI_Trace( "Destroying TDbiCache" << " "); 00103 00104 // Purge the AggNo == -1 cache before deleting. For extended 00105 // context queries it can have TDbiResultSetAggs that are clients of 00106 // TDbiResultSetNonAggs in the same cache, so purging will remove clientless 00107 // TDbiResultSetAggs which should in turn make their TDbiResultSetNonAggs 00108 // clientless. 00109 if ( this->GetSubCache(-1) ) this->Purge(fCache[-1]); 00110 00111 for ( CacheItr_t itr = fCache.begin(); itr != fCache.end(); ++itr) { 00112 ResultList_t& subCache = itr->second; 00113 for ( SubCacheItr_t sitr = subCache.begin(); 00114 sitr != subCache.end(); 00115 ++sitr) delete *sitr; 00116 } 00117 00118 }
TDbiCache::TDbiCache | ( | const TDbiCache & | ) | [private] |
void TDbiCache::Adopt | ( | TDbiResultSet * | res, | |
bool | generateKey = true | |||
) |
Purpose: Adopt and own a TDbiResultSet Arguments: res in The TDbiResiult to be adopted. generateKey in If true generate key Return: None. Contact: N. West Specification:- ============= o Create new sub-cache for aggregate if necessary. o Purge sub-cache of unwanted data and adopt new result. Program Notes:- ============= New entries are added to the end of the sub-cache unwanted entries are always removed from the beginning so sub-cache is a FIFO.
Definition at line 146 of file TDbiCache.cxx.
References fCache, fCurSize, fMaxSize, fNumAdopted, TDbiResultSet::GenerateKey(), TDbiValidityRec::GetAggregateNo(), TDbiResultSet::GetKey(), GetSubCache(), TDbiResultSet::GetValidityRec(), TDbiResultSet::GetValidityRecGlobal(), Purge(), SK_DBI_Debug, SK_DBI_Info, and TDbiResultSet::TableName().
Referenced by TDbiTableProxy::Query(), and TDbiTableProxy::RestoreFromL2Cache().
00146 { 00147 00148 if ( ! res ) return; 00149 int aggNo = res->GetValidityRec().GetAggregateNo(); 00150 00151 // Prime sub-cache if necessary. 00152 if ( ! this->GetSubCache(aggNo) ) { 00153 ResultList_t emptyList; 00154 fCache[aggNo] = emptyList; 00155 } 00156 00157 // Purge expired entries and add new result to cache. 00158 ResultList_t& subCache = fCache[aggNo]; 00159 Purge(subCache, res); 00160 subCache.push_back(res); 00161 ++fCurSize; 00162 ++fNumAdopted; 00163 SK_DBI_Debug( "Adopting result for " << res->TableName() 00164 << " " << res->GetValidityRecGlobal() 00165 << "\nCache size now " << fCurSize << " "); 00166 if ( fCurSize > fMaxSize ) fMaxSize = fCurSize; 00167 // If required generate key. 00168 if ( generateKey ) { 00169 res->GenerateKey(); 00170 SK_DBI_Info( "Caching new results: ResultKey: " << *res->GetKey()); 00171 } 00172 }
UInt_t TDbiCache::GetCurSize | ( | ) | const [inline] |
UInt_t TDbiCache::GetMaxSize | ( | ) | const [inline] |
UInt_t TDbiCache::GetNumAdopted | ( | ) | const [inline] |
UInt_t TDbiCache::GetNumReused | ( | ) | const [inline] |
const ResultList_t * TDbiCache::GetSubCache | ( | Int_t | aggNo | ) | const [private] |
Purpose: Return sub-cache for aggregate or 0 if none..
Definition at line 179 of file TDbiCache.cxx.
References ConstCacheItr_t, and fCache.
Referenced by Adopt(), Search(), and ~TDbiCache().
00179 { 00180 00181 ConstCacheItr_t itr = fCache.find(aggNo); 00182 return ( itr == fCache.end() ) ? 0 : &itr->second; 00183 00184 }
void TDbiCache::Purge | ( | ResultList_t & | subCache, | |
const TDbiResultSet * | res = 0 | |||
) | [private] |
Purpose: Purge surplus sub-cache memebers. Arguments: subCache in/out The sub-cache to be purged res in Optional TDbiResultSet (default =0) Return: None. Contact: N. West Specification:- ============= o Purge surplus sub-cache members i.e. those without clients. o If a TDbiResultSet is supplied, only purge entries that have expired relative to it or are stale. Program Notes:- =============
Definition at line 242 of file TDbiCache.cxx.
References TDbiResultSet::CanDelete(), fCurSize, TDbiResultSet::GetNumClients(), TDbiResultSet::GetValidityRec(), SK_DBI_Debug, and TDbiResultSet::TableName().
00242 { 00243 00244 00245 // Passing a TDbiResultSet allows the sub-cache to hold entries 00246 // for different detector types, simulation masks and tasks. 00247 00248 for ( SubCacheItr_t itr = subCache.begin(); itr != subCache.end(); ) { 00249 TDbiResultSet* pRes = *itr; 00250 00251 if ( pRes->GetNumClients() == 0 00252 && ( ! res 00253 || pRes->CanDelete(res) ) ) { 00254 00255 SK_DBI_Debug( "Purging " << pRes->GetValidityRec() 00256 << " from " << pRes->TableName() 00257 << " cache. Cache size now " 00258 << fCurSize-1 << " "); 00259 delete pRes; 00260 // Erasing increments iterator. 00261 itr = subCache.erase(itr); 00262 --fCurSize; 00263 00264 } 00265 else { 00266 ++itr; 00267 } 00268 } 00269 00270 }
void TDbiCache::Purge | ( | ) |
Purpose: Purge all sub-caches. Arguments: None. Return: None. Contact: N. West Specification:- ============= o Purge all sub_caches. Program Notes:- ============= The first sub-cached to be purged must be sub-cache -1 as its members may be aggregated and consequently will be connected to members in other caches.
Definition at line 209 of file TDbiCache.cxx.
References fCache.
Referenced by Adopt(), TDbiDatabaseManager::PurgeCaches(), and ~TDbiCache().
00209 { 00210 00211 00212 00213 00214 for ( CacheItr_t itr = fCache.begin(); itr != fCache.end(); ++itr 00215 ) Purge(itr->second); 00216 00217 }
const TDbiResultSet * TDbiCache::Search | ( | const TDbiValidityRec & | vrec, | |
const string & | sqlQualifiers = "" | |||
) | const |
Secondary search.
Purpose: Search sub-cache for TDbiResultSet set matching a TDbiValidityRec. with an optional sqlQualifiers string. Return: Pointer to matching TDbiResultSet, or = 0 if none.
Definition at line 278 of file TDbiCache.cxx.
References fNumReused, fTableName, TDbiValidityRec::GetAggregateNo(), TDbiResultSet::GetNumAggregates(), TDbiResultSet::GetNumRows(), GetSubCache(), TDbiResultSet::Satisfies(), and SK_DBI_Trace.
00279 { 00280 00281 00282 Int_t aggNo = vrec.GetAggregateNo(); 00283 00284 SK_DBI_Trace( "Secondary cache search of table " << fTableName 00285 << " for " << vrec 00286 << (sqlQualifiers != "" ? sqlQualifiers : "" ) << " "); 00287 const ResultList_t* subCache = this->GetSubCache(aggNo); 00288 if ( ! subCache ) { 00289 SK_DBI_Trace( "Secondary cache search failed." << " "); 00290 return 0; 00291 } 00292 00293 ConstSubCacheItr_t itrEnd = subCache->end(); 00294 for ( ConstSubCacheItr_t itr = subCache->begin(); 00295 itr != itrEnd; 00296 ++itr) { 00297 TDbiResultSet* res = *itr; 00298 if ( res->Satisfies(vrec,sqlQualifiers) ) { 00299 fNumReused += res->GetNumAggregates(); 00300 SK_DBI_Trace( "Secondary cache search succeeded. Result set no. of rows: " 00301 << res->GetNumRows() << " "); 00302 return res; 00303 } 00304 } 00305 00306 SK_DBI_Trace( "Secondary cache search failed." << " "); 00307 return 0; 00308 }
const TDbiResultSet * TDbiCache::Search | ( | const string & | sqlQualifiers | ) | const |
Purpose: Search primary cache for TDbiResultSet set matching a new query. Arguments: sqlQualifiers in The SQL qualifiers (context-sql;data-sql;fill-options) Return: Pointer to matching TDbiResultSet, or = 0 if none.
Definition at line 379 of file TDbiCache.cxx.
References fNumReused, fTableName, TDbiResultSet::GetNumAggregates(), TDbiResultSet::GetNumRows(), GetSubCache(), TDbiResultSet::Satisfies(), and SK_DBI_Trace.
00379 { 00380 00381 SK_DBI_Trace( "Primary cache search of table " << fTableName 00382 << " for SQL " << sqlQualifiers << " "); 00383 const ResultList_t* subCache = this->GetSubCache(-1); 00384 if ( ! subCache ) { 00385 SK_DBI_Trace( "Primary cache search failed" << " "); 00386 return 0; 00387 } 00388 for ( ConstSubCacheItr_t itr = subCache->begin(); 00389 itr != subCache->end(); 00390 ++itr) { 00391 TDbiResultSet* res = *itr; 00392 if ( res->Satisfies(sqlQualifiers) ) { 00393 fNumReused += res->GetNumAggregates(); 00394 SK_DBI_Trace( "Primary cache search succeeded Result set no. of rows: " 00395 << res->GetNumRows() << " "); 00396 return res; 00397 } 00398 } 00399 SK_DBI_Trace( "Primary cache search failed" << " "); 00400 return 0; 00401 }
const TDbiResultSet * TDbiCache::Search | ( | const TVldContext & | vc, | |
const TDbi::Task & | task | |||
) | const |
Purpose: Search primary cache for TDbiResultSet set matching a new query. Arguments: vc in Context of new query task in Task of new query Return: Pointer to matching TDbiResultSet, or = 0 if none.
Definition at line 321 of file TDbiCache.cxx.
References DbiSimFlag::AsString(), fNumReused, fTableName, TDbiSimFlagAssociation::Get(), TVldContext::GetDetector(), TDbiResultSet::GetNumAggregates(), TDbiResultSet::GetNumRows(), TVldContext::GetSimFlag(), GetSubCache(), TVldContext::GetTimeStamp(), TDbiSimFlagAssociation::Instance(), TDbiResultSet::Satisfies(), SK_DBI_Debug, and SK_DBI_Trace.
Referenced by TDbiTableProxy::Query(), and TDbiTableProxy::RestoreFromL2Cache().
00322 { 00323 00324 SK_DBI_Trace( "Primary cache search of table " << fTableName 00325 << " for " << vc 00326 << " with task " << task << " "); 00327 const ResultList_t* subCache = this->GetSubCache(-1); 00328 if ( ! subCache ) { 00329 SK_DBI_Trace( "Primary cache search failed - sub-cache -1 is empty" << " "); 00330 return 0; 00331 } 00332 00333 // Loop over all possible SimFlag associations. 00334 00335 DbiDetector::Detector_t det(vc.GetDetector()); 00336 DbiSimFlag::SimFlag_t sim(vc.GetSimFlag()); 00337 TVldTimeStamp ts(vc.GetTimeStamp()); 00338 00339 TDbiSimFlagAssociation::SimList_t simList 00340 = TDbiSimFlagAssociation::Instance().Get(sim); 00341 00342 TDbiSimFlagAssociation::SimList_t::iterator listItr = simList.begin(); 00343 TDbiSimFlagAssociation::SimList_t::iterator listItrEnd = simList.end(); 00344 while ( listItr != listItrEnd ) { 00345 00346 DbiSimFlag::SimFlag_t simTry = *listItr; 00347 TVldContext vcTry(det,simTry,ts); 00348 00349 SK_DBI_Debug( " Searching cache with SimFlag: " 00350 << DbiSimFlag::AsString(simTry) << " "); 00351 for ( ConstSubCacheItr_t itr = subCache->begin(); 00352 itr != subCache->end(); 00353 ++itr) { 00354 TDbiResultSet* res = *itr; 00355 if ( res->Satisfies(vcTry,task) ) { 00356 fNumReused += res->GetNumAggregates(); 00357 SK_DBI_Trace( "Primary cache search succeeded. Result set no. of rows: " 00358 << res->GetNumRows() << " "); 00359 return res; 00360 } 00361 } 00362 00363 SK_DBI_Trace( "Primary cache search failed." << " "); 00364 ++listItr; 00365 } 00366 00367 return 0; 00368 }
void TDbiCache::SetStale | ( | ) |
Purpose: Set all entries in the cache as stale i.e. don't reuse. Arguments: None Return: n/a Contact: N. West Specification:- ============= o Set all entries in the cache as stale i.e. don't reuse. Program Notes:- ============= This member function can be used to effectively clear the cache. As existing TDbiResultSet objects currently in the cache may currently have clients, its not possible simply to delete them, so instead this function marks them as stale so they will not be reused and will eventually be dropped once all their clients have disconnected.
Definition at line 429 of file TDbiCache.cxx.
References fCache.
00429 { 00430 00431 for ( CacheItr_t cacheItr = fCache.begin(); 00432 cacheItr != fCache.end(); 00433 ++cacheItr 00434 ) { 00435 ResultList_t& subcache = cacheItr->second; 00436 00437 for ( SubCacheItr_t subcacheItr = subcache.begin(); 00438 subcacheItr != subcache.end(); 00439 ++subcacheItr ) (*subcacheItr)->SetCanReuse(kFALSE); 00440 } 00441 00442 }
ostream & TDbiCache::ShowStatistics | ( | ostream & | msg | ) | const |
Purpose: Display statistics for cache on supplied ostream. Arguments: msg in ostream to output on. Return: Updated ostream. Contact: N. West Specification:- ============= o Output : Current Size, Max size, Adopted and Resused as 4 10 character wide fields. Program Notes:- ============= None.
Definition at line 467 of file TDbiCache.cxx.
References fCurSize, fMaxSize, fNumAdopted, and fNumReused.
00467 { 00468 00469 MsgFormat ifmt("%10i"); 00470 00471 msg << ifmt(fCurSize) << ifmt(fMaxSize) 00472 << ifmt(fNumAdopted) << ifmt(fNumReused); 00473 return msg; 00474 00475 }
friend class TDbiValidate [friend] |
Definition at line 40 of file TDbiCache.hxx.
std::map<Int_t,ResultList_t> TDbiCache::fCache [private] |
Map of sub-caches indexed by aggregate number. Each sub-cache is a list of owned results for that aggregate.
Definition at line 101 of file TDbiCache.hxx.
Referenced by Adopt(), GetSubCache(), Purge(), SetStale(), and ~TDbiCache().
UInt_t TDbiCache::fCurSize [mutable, private] |
Current size.
Definition at line 104 of file TDbiCache.hxx.
Referenced by Adopt(), GetCurSize(), Purge(), and ShowStatistics().
UInt_t TDbiCache::fMaxSize [mutable, private] |
Max (high water) size.
Definition at line 107 of file TDbiCache.hxx.
Referenced by Adopt(), GetMaxSize(), and ShowStatistics().
UInt_t TDbiCache::fNumAdopted [mutable, private] |
Total number adopted.
Definition at line 110 of file TDbiCache.hxx.
Referenced by Adopt(), GetNumAdopted(), and ShowStatistics().
UInt_t TDbiCache::fNumReused [mutable, private] |
Number reused i.e. found.
Definition at line 113 of file TDbiCache.hxx.
Referenced by GetNumReused(), Search(), and ShowStatistics().
const string& TDbiCache::fTableName [private] |
TDbiTableProxy& TDbiCache::fTableProxy [private] |
TableProxy owning cache.
Definition at line 93 of file TDbiCache.hxx.