TDbiCache.cxx
Go to the documentation of this file.00001
00002
00003 #include "TDbiCache.hxx"
00004 #include "TDbiResultSet.hxx"
00005 #include "TDbiResultKey.hxx"
00006 #include "TDbiResultSetNonAgg.hxx"
00007 #include "TDbiSimFlagAssociation.hxx"
00008 #include "TDbiValidityRec.hxx"
00009 #include <MsgFormat.h>
00010 #include <TSK_DBI_Log.hxx>
00011 #include <MsgFormat.h>
00012 using std::endl;
00013 #include "TVldContext.hxx"
00014
00015 ClassImp(TDbiCache)
00016
00017
00018
00019 typedef TDbiCache::ResultList_t ResultList_t;
00020 typedef map<Int_t,ResultList_t>::const_iterator ConstCacheItr_t;
00021 typedef map<Int_t,ResultList_t>::iterator CacheItr_t;
00022 typedef ResultList_t::const_iterator ConstSubCacheItr_t;
00023 typedef ResultList_t::iterator SubCacheItr_t;
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 TDbiCache::TDbiCache(TDbiTableProxy& qp,const string& tableName) :
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 }
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 TDbiCache::~TDbiCache() {
00099
00100
00101
00102 SK_DBI_Trace( "Destroying TDbiCache" << " ");
00103
00104
00105
00106
00107
00108
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 }
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 void TDbiCache::Adopt(TDbiResultSet* res,bool generateKey) {
00147
00148 if ( ! res ) return;
00149 int aggNo = res->GetValidityRec().GetAggregateNo();
00150
00151
00152 if ( ! this->GetSubCache(aggNo) ) {
00153 ResultList_t emptyList;
00154 fCache[aggNo] = emptyList;
00155 }
00156
00157
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
00168 if ( generateKey ) {
00169 res->GenerateKey();
00170 SK_DBI_Info( "Caching new results: ResultKey: " << *res->GetKey());
00171 }
00172 }
00173
00174
00175
00176
00177
00178
00179 const ResultList_t* TDbiCache::GetSubCache(Int_t aggNo) const {
00180
00181 ConstCacheItr_t itr = fCache.find(aggNo);
00182 return ( itr == fCache.end() ) ? 0 : &itr->second;
00183
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209 void TDbiCache::Purge() {
00210
00211
00212
00213
00214 for ( CacheItr_t itr = fCache.begin(); itr != fCache.end(); ++itr
00215 ) Purge(itr->second);
00216
00217 }
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242 void TDbiCache::Purge(ResultList_t& subCache, const TDbiResultSet* res) {
00243
00244
00245
00246
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
00261 itr = subCache.erase(itr);
00262 --fCurSize;
00263
00264 }
00265 else {
00266 ++itr;
00267 }
00268 }
00269
00270 }
00271
00272
00273
00274
00275
00276
00277
00278 const TDbiResultSet* TDbiCache::Search(const TDbiValidityRec& vrec,
00279 const string& sqlQualifiers) const {
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 }
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321 const TDbiResultSet* TDbiCache::Search(const TVldContext& vc,
00322 const TDbi::Task& task ) const {
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
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 }
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379 const TDbiResultSet* TDbiCache::Search(const string& sqlQualifiers) const {
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 }
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429 void TDbiCache::SetStale() {
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 }
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467 ostream& TDbiCache::ShowStatistics(ostream& msg) const {
00468
00469 MsgFormat ifmt("%10i");
00470
00471 msg << ifmt(fCurSize) << ifmt(fMaxSize)
00472 << ifmt(fNumAdopted) << ifmt(fNumReused);
00473 return msg;
00474
00475 }
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509