EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FairDbCache.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FairDbCache.cxx
1 
2 #include "FairDbCache.h"
3 #include "FairDbResult.h"
4 #include "FairDbValidityRec.h"
6 #include "ValContext.h"
7 
9 
10 
11 typedef FairDbCache::ResultList_t ResultList_t;
12 typedef map<Int_t,ResultList_t>::const_iterator ConstCacheItr_t;
13 typedef map<Int_t,ResultList_t>::iterator CacheItr_t;
14 typedef ResultList_t::const_iterator ConstSubCacheItr_t;
15 typedef ResultList_t::iterator SubCacheItr_t;
16 
17 
18 FairDbCache::FairDbCache(FairDbTableProxy& qp,const string& tableName) :
19  fTableProxy(qp),
20  fTableName(tableName),
21  fCache(),
22  fCurSize(0),
23  fMaxSize(0),
24  fNumAdopted(0),
25  fNumReused(0)
26 {
27 
28 }
29 
30 //.....................................................................
31 
33 {
34 
35  // Purge the AggNo == -1 cache before deleting. For extended
36  // context queries it can have FairDbResultAggs that are clients of
37  // FairDbResultNonAggs in the same cache, so purging will remove clientless
38  // FairDbResultAggs which should in turn make their FairDbResultNonAggs
39  // clientless.
40  if ( this->GetSubCache(-1) ) { this->Purge(fCache[-1]); }
41 
42  for ( CacheItr_t itr = fCache.begin(); itr != fCache.end(); ++itr) {
43  ResultList_t& subCache = itr->second;
44  for ( SubCacheItr_t sitr = subCache.begin();
45  sitr != subCache.end();
46  ++sitr) { delete *sitr; }
47  }
48 
49 }
50 
51 void FairDbCache::Adopt(FairDbResult* res,bool registerKey)
52 {
53  if ( ! res ) { return; }
54  int aggNo = res->GetValidityRec().GetAggregateNo();
55 
56 // Prime sub-cache if necessary.
57  if ( ! this->GetSubCache(aggNo) ) {
58  ResultList_t emptyList;
59  fCache[aggNo] = emptyList;
60  }
61 
62 // Purge expired entries and add new result to cache.
63  ResultList_t& subCache = fCache[aggNo];
64  Purge(subCache, res);
65  subCache.push_back(res);
66  ++fCurSize;
67  ++fNumAdopted;
68  cout << "Adopting result for " << res->TableName()
69  << " " << res->GetValidityRecGlobal()
70  << "\nCache size now " << fCurSize << endl;
71  if ( fCurSize > fMaxSize ) { fMaxSize = fCurSize; }
72  // If required register key with FairDbRecord
73  if ( registerKey ) {
74  res->RegisterKey();
75  cout << "Caching new results: ResultKey: " << *res->GetKey();
76  }
77 }
78 
79 //.....................................................................
80 
81 const ResultList_t* FairDbCache::GetSubCache(Int_t aggNo) const
82 {
83  ConstCacheItr_t itr = fCache.find(aggNo);
84  return ( itr == fCache.end() ) ? 0 : &itr->second;
85 
86 }
87 
89 {
90 
91  for ( CacheItr_t itr = fCache.begin(); itr != fCache.end(); ++itr
92  ) { Purge(itr->second); }
93 
94 }
95 
96 void FairDbCache::Purge(ResultList_t& subCache, const FairDbResult* res)
97 {
98 
99  for ( SubCacheItr_t itr = subCache.begin(); itr != subCache.end(); ) {
100  FairDbResult* pRes = *itr;
101 
102  if ( pRes->GetNumClients() == 0
103  && ( ! res
104  || pRes->CanDelete(res) ) ) {
105 
106  cout << "Purging " << pRes->GetValidityRec()
107  << " from " << pRes->TableName()
108  << " cache. Cache size now "
109  << fCurSize-1 << endl;
110  delete pRes;
111 // Erasing increments iterator.
112  itr = subCache.erase(itr);
113  --fCurSize;
114 
115  } else {
116  ++itr;
117  }
118  }
119 
120 }
121 
123  const string& sqlQualifiers) const
124 {
125 
126  Int_t aggNo = vrec.GetAggregateNo();
127 
128  cout << "Secondary cache search of table " << fTableName
129  << " for " << vrec
130  << (sqlQualifiers != "" ? sqlQualifiers : "" ) << endl;
131  const ResultList_t* subCache = this->GetSubCache(aggNo);
132  if ( ! subCache ) {
133  cout << "Secondary cache search failed." << endl;
134  return 0;
135  }
136 
137  ConstSubCacheItr_t itrEnd = subCache->end();
138  for ( ConstSubCacheItr_t itr = subCache->begin();
139  itr != itrEnd;
140  ++itr) {
141  FairDbResult* res = *itr;
142  if ( res->Satisfies(vrec,sqlQualifiers) ) {
143  fNumReused += res->GetNumAggregates();
144  cout << "Secondary cache search succeeded. Result set no. of rows: "
145  << res->GetNumRows() << endl;
146  return res;
147  }
148  }
149 
150  cout << "Secondary cache search failed." << endl;
151  return 0;
152 }
153 
154 
156  const FairDb::Task& task ) const
157 {
158 
159  cout << "Primary cache search of table " << fTableName
160  << " for " << vc
161  << " with task " << task << endl;
162  const ResultList_t* subCache = this->GetSubCache(-1);
163  if ( ! subCache ) {
164  cout << "Primary cache search failed - sub-cache -1 is empty" << endl;
165  return 0;
166  }
167 
168  // Loop over all possible SimFlag associations.
169 
171  SimFlag::SimFlag_t sim(vc.GetSimFlag());
172  ValTimeStamp ts(vc.GetTimeStamp());
173 
176 
177  FairDbSimFlagAssociation::SimList_t::iterator listItr = simList.begin();
178  FairDbSimFlagAssociation::SimList_t::iterator listItrEnd = simList.end();
179  while ( listItr != listItrEnd ) {
180 
181  SimFlag::SimFlag_t simTry = *listItr;
182  ValContext vcTry(det,simTry,ts);
183 
184  cout << " Searching cache with SimFlag: "
185  << SimFlag::AsString(simTry) << endl;
186  for ( ConstSubCacheItr_t itr = subCache->begin();
187  itr != subCache->end();
188  ++itr) {
189  FairDbResult* res = *itr;
190  if ( res->Satisfies(vcTry,task) ) {
191  fNumReused += res->GetNumAggregates();
192  cout << "Primary cache search succeeded. Result set no. of rows: "
193  << res->GetNumRows() << endl;
194  return res;
195  }
196  }
197 
198  cout << "Primary cache search failed." << endl;
199  ++listItr;
200  }
201 
202  return 0;
203 }
204 
205 const FairDbResult* FairDbCache::Search(const string& sqlQualifiers) const
206 {
207 
208  cout << "Primary cache search of table " << fTableName
209  << " for SQL " << sqlQualifiers << endl;
210  const ResultList_t* subCache = this->GetSubCache(-1);
211  if ( ! subCache ) {
212  cout << "Primary cache search failed" << endl;
213  return 0;
214  }
215  for ( ConstSubCacheItr_t itr = subCache->begin();
216  itr != subCache->end();
217  ++itr) {
218  FairDbResult* res = *itr;
219  if ( res->Satisfies(sqlQualifiers) ) {
220  fNumReused += res->GetNumAggregates();
221  cout << "Primary cache search succeeded Result set no. of rows: "
222  << res->GetNumRows() << endl;
223  return res;
224  }
225  }
226  cout << "Primary cache search failed" << endl;
227  return 0;
228 }
229 
231 {
232 
233  for ( CacheItr_t cacheItr = fCache.begin();
234  cacheItr != fCache.end();
235  ++cacheItr
236  ) {
237  ResultList_t& subcache = cacheItr->second;
238 
239  for ( SubCacheItr_t subcacheItr = subcache.begin();
240  subcacheItr != subcache.end();
241  ++subcacheItr ) { (*subcacheItr)->SetCanReuse(kFALSE); }
242  }
243 
244 }
245 
246 
247 //MsgStream& FairDbCache::ShowStatistics(MsgStream& msg) const {
248 //
249 //}