Cache.cxx
Go to the documentation of this file.
1 //____________________________________________________________________________
2 /*
3  Copyright (c) 2003-2020, The GENIE Collaboration
4  For the full text of the license visit http://copyright.genie-mc.org
5 
6  Costas Andreopoulos <constantinos.andreopoulos \at cern.ch>
7  University of Liverpool & STFC Rutherford Appleton Laboratory
8 */
9 //____________________________________________________________________________
10 
11 #include <sstream>
12 #include <iostream>
13 
14 #include <TSystem.h>
15 #include <TDirectory.h>
16 #include <TList.h>
17 #include <TObjString.h>
18 
20 #include "Framework/Utils/Cache.h"
22 
23 using std::ostringstream;
24 using std::endl;
25 
26 namespace genie {
27 
28 //____________________________________________________________________________
29 ostream & operator << (ostream & stream, const Cache & cache)
30 {
31  cache.Print(stream);
32  return stream;
33 }
34 //____________________________________________________________________________
36 //____________________________________________________________________________
38 {
39  fInstance = 0;
40  fCacheMap = 0;
41  fCacheFile = 0;
42 }
43 //____________________________________________________________________________
45 {
46  this->Save();
47 
48  if(fCacheMap) {
50  for(citer = fCacheMap->begin(); citer != fCacheMap->end(); ++citer) {
51  CacheBranchI * branch = citer->second;
52  if(branch) {
53  delete branch;
54  branch = 0;
55  }
56  }
57  fCacheMap->clear();
58  delete fCacheMap;
59  }
60  if(fCacheFile) {
61  fCacheFile->Close();
62  delete fCacheFile;
63  }
64  fInstance = 0;
65 }
66 //____________________________________________________________________________
68 {
69  if(fInstance == 0) {
70  static Cache::Cleaner cleaner;
72 
73  fInstance = new Cache;
74 
75  fInstance->fCacheMap = new map<string, CacheBranchI * >;
76  }
77  return fInstance;
78 }
79 //____________________________________________________________________________
81 {
83 
84  if (map_iter == fCacheMap->end()) return 0;
85  return map_iter->second;
86 }
87 //____________________________________________________________________________
88 void Cache::AddCacheBranch(string key, CacheBranchI * branch)
89 {
90  fCacheMap->insert( map<string, CacheBranchI *>::value_type(key,branch) );
91 }
92 //____________________________________________________________________________
93 string Cache::CacheBranchKey(string k0, string k1, string k2) const
94 {
95  ostringstream key;
96 
97  key << k0;
98  if(k1.size()>0) key << "/" << k1;
99  if(k2.size()>0) key << "/" << k2;
100 
101  return key.str();
102 }
103 //____________________________________________________________________________
105 {
106  LOG("Cache", pNOTICE) << "Removing cache branch: " << key;
107 
108 }
109 //____________________________________________________________________________
111 {
112  LOG("Cache", pNOTICE) << "Removing cache branches";
113 
114  if(fCacheMap) {
116  for(citer = fCacheMap->begin(); citer != fCacheMap->end(); ++citer) {
117  CacheBranchI * branch = citer->second;
118  if(branch) {
119  delete branch;
120  branch = 0;
121  }
122  }
123  fCacheMap->clear();
124  }
125 }
126 //____________________________________________________________________________
127 void Cache::RmMatchedCacheBranches(string key_substring)
128 {
129  LOG("Cache", pNOTICE) << "Removing cache branches: *"<< key_substring<< "*";
130 
131 }
132 //____________________________________________________________________________
133 void Cache::Load(void)
134 {
135  LOG("Cache", pNOTICE) << "Loading cache";
136 
137  if(!fCacheFile) return;
138  TList * keys = (TList*) fCacheFile->Get("key_list");
139  TIter kiter(keys);
140  TObjString * keyobj = 0;
141  int ib=0;
142  while ((keyobj = (TObjString *)kiter.Next())) {
143  string key = string(keyobj->GetString().Data());
144  ostringstream bname;
145  bname << "buffer_" << ib++;
146  CacheBranchI * buffer = (CacheBranchI*) fCacheFile->Get(bname.str().c_str());
147  if(buffer) {
148  fCacheMap->insert( map<string, CacheBranchI *>::value_type(key,buffer) );
149  }
150  }
151  LOG("Cache", pNOTICE) << "Cache loaded...";
152  LOG("Cache", pNOTICE) << *this;
153 }
154 //____________________________________________________________________________
155 void Cache::Save(void)
156 {
157  if(!fCacheFile) {
158  return;
159  }
160  fCacheFile->cd();
161 
162  int ib=0;
163  TList * keys = new TList;
164  keys->SetOwner(true);
165 
167  for(citer = fCacheMap->begin(); citer != fCacheMap->end(); ++citer) {
168  string key = citer->first;
169  CacheBranchI * branch = citer->second;
170  if(branch) {
171  ostringstream bname;
172  bname << "buffer_" << ib++;
173  keys->Add(new TObjString(key.c_str()));
174  branch->Write(bname.str().c_str(), TObject::kOverwrite);
175  }
176  }
177  keys->Write("key_list", TObject::kSingleKey | TObject::kOverwrite );
178 
179  keys->Clear();
180  delete keys;
181 }
182 //____________________________________________________________________________
184 {
185  if(filename.size() == 0) return;
186 
187  if(fCacheFile) {
188  if(fCacheFile->IsOpen()) {
189  delete fCacheFile;
190  fCacheFile = 0;
191  }
192  }
193 
194  LOG("Cache", pNOTICE) << "Using cache file: " << filename;
195 
196  fCacheFile = new TFile(filename.c_str(),"update");
197  if(!fCacheFile->IsOpen()) {
198  delete fCacheFile;
199  fCacheFile = 0;
200  LOG("Cache", pWARN) << "Could not open cache file: " << filename;
201  }
202 
203  this->Load();
204 }
205 //____________________________________________________________________________
206 void Cache::Print(ostream & stream) const
207 {
208  stream << "\n [-] GENIE Cache Buffers:";
209  stream << "\n |";
211  for(citer = fCacheMap->begin(); citer != fCacheMap->end(); ++citer) {
212  string key = citer->first;
213  CacheBranchI * branch = citer->second;
214  stream << "\n |--o " << key;
215  if(!branch) {
216  stream << " *** NULL *** ";
217  }
218  }
219  stream << "\n";
220 }
221 //___________________________________________________________________________
222 
223 } // genie namespace
intermediate_table::iterator iterator
THE MAIN GENIE PROJECT NAMESPACE
Definition: AlgCmp.h:25
std::string string
Definition: nybbler.cc:12
void Load(void)
load/save
Definition: Cache.cxx:133
map< string, CacheBranchI * > * fCacheMap
map of cache buffers & cache file
Definition: Cache.h:71
intermediate_table::const_iterator const_iterator
string filename
Definition: train.py:213
void RmAllCacheBranches(void)
Definition: Cache.cxx:110
void AddCacheBranch(string key, CacheBranchI *branch)
Definition: Cache.cxx:88
void DummyMethodAndSilentCompiler()
Definition: Cache.h:81
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:96
def key(type, name=None)
Definition: graph.py:13
void OpenCacheFile(string filename)
cache file
Definition: Cache.cxx:183
void Print(ostream &stream) const
print cache buffers
Definition: Cache.cxx:206
string CacheBranchKey(string k0, string k1="", string k2="") const
Definition: Cache.cxx:93
void Save(void)
Definition: Cache.cxx:155
#define pWARN
Definition: Messenger.h:60
CacheBranchI * FindCacheBranch(string key)
finding/adding cache branches
Definition: Cache.cxx:80
GENIE Cache Memory.
Definition: Cache.h:38
virtual ~Cache()
Definition: Cache.cxx:44
ostream & operator<<(ostream &stream, const AlgConfigPool &config_pool)
static Cache * fInstance
singleton instance
Definition: Cache.h:68
Cache()
singleton class: constructors are private
Definition: Cache.cxx:37
TFile * fCacheFile
Definition: Cache.h:72
proper de-allocation of the singleton object
Definition: Cache.h:80
void RmMatchedCacheBranches(string key_substring)
Definition: Cache.cxx:127
#define pNOTICE
Definition: Messenger.h:61
static Cache * Instance(void)
Definition: Cache.cxx:67
QTextStream & endl(QTextStream &s)
The TObject at the root of concrete cache branches.
Definition: CacheBranchI.h:25
void RmCacheBranch(string key)
removing cache branches
Definition: Cache.cxx:104