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