EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FairDbTableProxyRegistry.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FairDbTableProxyRegistry.cxx
1 #include <vector>
2 
3 #include "TSystem.h"
4 #include "TString.h"
5 
6 #include "FairDbExceptionLog.h"
7 #include "FairDbBinaryFile.h"
8 #include "FairDbCache.h"
9 #include "FairDbMultConnector.h"
10 #include "FairDbConfigSet.h"
11 #include "FairDbServices.h"
13 #include "FairDbTableProxy.h"
14 #include "FairDbString.h"
15 
17 
18 
20 int FairDbTableProxyRegistry::Cleaner::fgCount = 0;
21 
22 
25  fMultConnector(NULL),
26  fSqlCondition(),
27  fTPmap(),
29  fSimFlagAss()
30 {
31 
32 // Create cascader for database access.
33 
35 
36 // Get any environment configuration.
37 
39 
40 // Create a FairDbRecord to record a summary of all FairDbResults in memory
41 // and register it as a service.
42 // FairDbServices::SetRecord(new FairDbRecord);
43 
44  cout << "Creating FairDbTableProxyRegistry"
45  << endl;
46 
47 
48 }
49 
51 {
52 
53  if ( FairDbExceptionLog::GetGELog().Size() ) {
54  cout << "Database Global Exception Log contains "
55  << FairDbExceptionLog::GetGELog().Size() << " entries:-" << endl;;
57  }
58 
59  int shutdown = 0;
60  if ( ! this->GetConfig().Get("Shutdown",shutdown)
61  || shutdown == 0 ) {
62  cout << " shutdown not requested" << endl;
63  return;
64  }
65 
66  cout << " shutting down..." << endl;
67 
68 // Destroy all owned objects.
69 
70  for ( std::map<std::string,FairDbTableProxy*>::iterator itr = fTPmap.begin();
71  itr != fTPmap.end();
72  ++itr) {
73  FairDbTableProxy* tp = (*itr).second;
74  delete tp;
75  }
76 
77  delete fMultConnector;
78  fMultConnector = 0;
79 
80  //delete FairDbServices::GetRecord();
82 
83 
84  cout << "Destroying FairDbTableProxyRegistry" << endl;
85 
86  cout << " shutdown complete." << endl;
88 
89 }
90 
91 
93 {
94 
95  std::map<std::string,FairDbTableProxy*>::const_iterator itr = fTPmap.begin();
96  std::map<std::string,FairDbTableProxy*>::const_iterator itrEnd = fTPmap.end();
97  for ( ; itr != itrEnd; ++itr) { this->ApplySqlCondition(itr->second); }
98 }
99 
100 
102 {
103 
104  string sqlFull = fSqlCondition;
105  const string tableName(proxy->GetTableName());
106  const string& date = fRollbackDates.GetDate(tableName);
107  if ( date.size() ) {
108  if ( sqlFull.size() ) { sqlFull += " and "; }
109  sqlFull += fRollbackDates.GetType(tableName);
110  sqlFull += " < \'";
111  sqlFull += date;
112  sqlFull += "\'";
113  }
114 
115  proxy->SetSqlCondition(sqlFull);
116 }
117 
118 
120 {
121 
123  this->ApplySqlCondition();
124 }
125 
126 
128 {
129 
130  fSimFlagAss.Clear();
131 }
132 
134 {
135 
136  FairRegistry& reg = this->GetConfig();
137 
138  //Load up SimFlag Associations and remove them from the FairRegistry.
139  fSimFlagAss.Set(reg);
140 
141  //Load up Rollback dates and remove them from the FairRegistry.
142  fRollbackDates.Set(reg);
143 
144  //Apply any rollback now in force.
145  this->ApplySqlCondition();
146 
147  // If Level 2 cache enabled establish working directory
148  // for FairDbBinaryFile.
149  const char* dir;
150  if ( reg.Get("Level2Cache",dir) ) {
151  // Expand any environmental variables.
152  TString tmp(dir);
153  // March 2004 ExpandPathName returns false even if it works, so test for failure
154  // by looking for an unexpanded symbol.
155  gSystem->ExpandPathName(tmp);
156  if ( tmp.Contains("$" ) ) {
157  dir = "./";
158  cout << "Directory name expansion failed, using "
159  << dir << " instead" << endl;
160  } else {
161  dir = tmp.Data();
162  }
163 
165  cout << "FairDbTableProxyRegistry: Setting L2 Cache to: " << dir << endl;
166  }
167 
168  // Check for request to make all cascade connections permanent
169  // and remove from the Registry.
170 
171  int connectionsPermanent = 0;
172  if ( reg.Get("MakeConnectionsPermanent",connectionsPermanent) ) {
173  reg.RemoveKey("MakeConnectionsPermanent");
174  Int_t dbNo =fMultConnector->GetNumDb();
175  if ( connectionsPermanent > 0 ) {
176  while ( --dbNo >= 0 ) { fMultConnector->SetPermanent(dbNo); }
177  cout << "Making all database connections permanent" << endl;
178  // Inform FairDbServices so that FairDbConnection can check when opening new connections.
180  } else {
181  while ( --dbNo >= 0 ) { fMultConnector->SetPermanent(dbNo,false); }
182  cout << "Forcing all connections, including ASCII DB, to be temporary" << endl;
183  // Inform FairDbServices so that FairDbConnection can check when opening new connections.
185  }
186  }
187 
188  // Check for request to order context queries and remove from the Registry.
189 
190  int OrderContextQuery = 0;
191  if ( reg.Get("OrderContextQuery",OrderContextQuery) ) {
192  reg.RemoveKey("OrderContextQuery");
193  if ( OrderContextQuery ) {
195  cout << "Forcing ordering of all context queries" << endl;
196  }
197  }
198 
199  // Abort if Registry contains any unknown keys
200 
201  const char* knownKeys[] = { "Level2Cache",
202  "Shutdown"
203  };
204  int numKnownKeys = sizeof(knownKeys)/sizeof(char*);
205  bool hasUnknownKeys = false;
206 
207  FairRegistry::FairRegistryKey keyItr(&this->GetConfig());
208  while ( const char* foundKey = keyItr() ) {
209  bool keyUnknown = true;
210  for (int keyNum = 0; keyNum < numKnownKeys; ++keyNum ) {
211  if ( ! strcmp(foundKey,knownKeys[keyNum]) ) { keyUnknown = false; }
212  }
213  if ( keyUnknown ) {
214 
215  cout << "Illegal registry item: " << foundKey << endl;
216  hasUnknownKeys = true;
217  }
218  }
219 
220  if ( hasUnknownKeys ) {
221  cout << "Aborting due to illegal registry items." << endl;
222  abort();
223  }
224 }
225 
226 
228 (const std::string& tableNameReq,
229  const FairDbTableRow* tableRow)
230 {
231 
232 // Assume upper case name if cannot find request.
233  std::string tableName = tableNameReq;
234  std::string vldSuffix = "Validity";
235  if ( ! fMultConnector->TableExists(tableName + vldSuffix ) ) {
236  tableName = FairUtilString::ToUpper(tableName);
237  vldSuffix = "VAL";
238  }
239  std::string proxyName = tableName;
240 
241  proxyName.append("::");
242  proxyName.append(tableRow->ClassName());
243  FairDbTableProxy* qpp = fTPmap[proxyName];
244  if ( ! qpp ) {
245  qpp = new FairDbTableProxy(fMultConnector,tableName,vldSuffix,tableRow);
246  this->ApplySqlCondition(qpp);
247  fTPmap[proxyName] = qpp;
248  }
249 
250  return *qpp;
251 
252 }
253 
254 
255 Bool_t FairDbTableProxyRegistry::HasRowCounter(const std::string& tableName)
256 {
257 
258  FairDbConfigSet cfs;
259  FairDbTableProxy& tp = this->GetTableProxy(tableName,&cfs);
260  return tp.GetMetaData().HasRowCounter();
261 
262 }
263 
265 {
266 
267  if ( ! fgInstance ) {
268 // Delete is handled by Cleaner class based on #include count
270  }
271  return *fgInstance;
272 
273 }
274 
275 
277 {
278 
279  for ( std::map<std::string,FairDbTableProxy*>::iterator itr = fTPmap.begin();
280  itr != fTPmap.end();
281  ++itr) {
282  FairDbTableProxy* tp = (*itr).second;
283  tp->GetCache()->Purge();
284  }
285 
286 }
287 
288 
289 void FairDbTableProxyRegistry::RefreshMetaData(const std::string& tableName)
290 {
291 
292  std::map<std::string,FairDbTableProxy*>::iterator itr = fTPmap.begin();
293  std::map<std::string,FairDbTableProxy*>::iterator itrEnd = fTPmap.end();
294  for ( ; itr != itrEnd; ++itr) {
295  FairDbTableProxy* table = (*itr).second;
296  if ( table && table->GetTableName() == tableName ) { table->RefreshMetaData(); }
297  }
298 
299 }
300 
302 {
303  const char* strENV_DBI = gSystem->Getenv("ENV_DB");
304  if ( strENV_DBI == 0 || strlen(strENV_DBI) == 0 ) { return; }
305 
306  cout << "\nConfiguring from the environmental "
307  << "variable ENV_DB:-\n " << strENV_DBI << endl;
308  std::vector<std::string> configRequests;
309  FairUtilString::StringTok(configRequests, strENV_DBI, ";");
310 
311  for (unsigned entry = 0; entry < configRequests.size(); ++entry ) {
312  this->Set(configRequests[entry].c_str());
313  }
314  this->Update();
315 }
316 
317 void FairDbTableProxyRegistry::SetSqlCondition(const std::string& sql)
318 {
319 
320  fSqlCondition = sql;
321  this->ApplySqlCondition();
322 }
323 
325 {
326 
327  cout << "\n\nCache statistics:-\n\n"
328  << "Table Name "
329  << " Current Maximum Total Total\n"
330  << " "
331  << " Size Size Adopted Reused" << endl;
332 
333 // Loop over all owned objects.
334 
335  for ( std::map<std::string,FairDbTableProxy*>::const_iterator itr = fTPmap.begin();
336  itr != fTPmap.end();
337  ++itr) {
338  const FairDbTableProxy* tp = (*itr).second;
339  std::string name = (*itr).first;
340  if ( name.size() < 40 ) { name.append(40-name.size(),' '); }
341  cout << name;
342 // Only want to look at cache so by-pass constness.
343  // const_cast<FairDbTableProxy*>(tp)->GetCache()->ShowStatistics();
344  cout << endl;
345  }
346  cout << "\n" << endl;
347 
348 // Only want to look at cascader so by-pass constness.
349 
350  cout << const_cast<FairDbTableProxyRegistry*>(this)->GetMultConnector();
351 
352 
353 }