openModeller  Version 1.5.0
CacheManager.cpp
Go to the documentation of this file.
1 
28 #include <openmodeller/Log.hh>
30 #include <openmodeller/Settings.hh>
32 extern "C" {
34 }
35 
36 #include <stdlib.h>
37 #include <string.h>
38 #include <iostream>
39 #include <sstream>
40 #include <stdio.h>
41 
42 using namespace std;
43 
44 /**************************************************************************
45  *
46  * Implementation of CacheManager class
47  *
48  *************************************************************************/
49 
50 /*******************/
51 /*** constructor ***/
52 
54  _cacheDir()
55 {
56 }
57 
58 /******************/
59 /*** destructor ***/
60 
62 {
63 }
64 
65 /**************************/
66 /*** Singleton accessor ***/
69 {
70  static CacheManager theInstance;
71  return theInstance;
72 }
73 
74 /******************/
75 /*** initialize ***/
76 void
77 CacheManager::initialize( const std::string dir )
78 {
79  CacheManager& cm = _getInstance();
80 
81  bool use_default = true;
82 
83  if ( ! dir.empty() ) {
84 
85  Log::instance()->debug( "Using specified directory as cache: %s\n", dir.c_str() );
86  cm._cacheDir = dir;
87  use_default = false;
88  }
89  else {
90 
91  if ( Settings::count( "CACHE_DIRECTORY" ) == 1 ) {
92 
93  cm._cacheDir = Settings::get( "CACHE_DIRECTORY" );
94  Log::instance()->debug( "Using cache directory from configuration file: %s\n", cm._cacheDir.c_str() );
95  use_default = false;
96  }
97  else {
98 
99  char *env = getenv( "OM_CACHE_PATH" );
100 
101  if ( env != 0 ) {
102 
103  string om_cache_path = (char const *)env;
104 
105  if ( ! om_cache_path.empty() ) {
106 
107  Log::instance()->debug( "Using cache directory defined in OM_CACHE_PATH: %s\n", om_cache_path.c_str() );
108  cm._cacheDir = om_cache_path;
109  use_default = false;
110  }
111  }
112  }
113  }
114 
115  if ( use_default ) {
116 
117  Log::instance()->debug( "Using default cache directory under the current path\n" );
118  cm._cacheDir = getWorkingPath();
119  cm._cacheDir.append("/cache");
120  }
121 
122  if ( ! pathExists( cm._cacheDir ) ) {
123 
124  createPath( cm._cacheDir );
125  }
126 }
127 
128 /*****************/
129 /*** is cached ***/
130 void
132 {
133  CacheManager& cm = _getInstance();
134 
135  if ( cm._cacheDir.empty() ) {
136 
137  cm.initialize();
138  }
139 }
140 
141 /*****************/
142 /*** is cached ***/
143 bool
144 CacheManager::isCached( const std::string id, const std::string subdir )
145 {
147 
148  CacheManager& cm = _getInstance();
149 
150  std::string path = cm._cacheDir;
151 
152  if ( ! subdir.empty() ) {
153 
154  path.append("/");
155  path.append(subdir);
156  }
157 
158  createPath( path );
159 
160  path.append("/");
161  path.append(id);
162 
163  ifstream ifile(path.c_str(), std::ios::in);
164 
165  if ( ifile ) {
166 
167  return true;
168  }
169 
170  return false;
171 }
172 
173 /*********************/
174 /*** is cached md5 ***/
175 bool
176 CacheManager::isCachedMd5( const std::string id, const std::string subdir )
177 {
178  return isCached( getContentIdMd5( id ), subdir );
179 }
180 
181 /*************/
182 /*** cache ***/
183 void
184 CacheManager::cache( const std::string id, const std::ostringstream& content, const std::string subdir )
185 {
187 
188  CacheManager& cm = _getInstance();
189 
190  std::string path = cm._cacheDir;
191 
192  if ( ! subdir.empty() ) {
193 
194  path.append("/");
195  path.append(subdir);
196  }
197 
198  createPath( path );
199 
200  path.append("/");
201  path.append(id);
202 
203  ofstream ofile( path.c_str() );
204 
205  if ( ofile ) {
206 
207  ofile << content.str();
208  }
209  else {
210 
211  Log::instance()->error( "Could not cache content for %s\n", id.c_str() );
212  throw FileIOException( "Could not cache content\n", id );
213  }
214 }
215 
216 /*****************/
217 /*** cache md5 ***/
218 void
219 CacheManager::cacheMd5( const std::string id, const std::ostringstream& content, const std::string subdir )
220 {
221  cache( getContentIdMd5( id ), content, subdir );
222 }
223 
224 /****************************/
225 /*** get Content Location ***/
226 std::string
227 CacheManager::getContentLocation( const std::string id, const std::string subdir )
228 {
230 
231  CacheManager& cm = _getInstance();
232 
233  std::string path = cm._cacheDir;
234 
235  if ( ! subdir.empty() ) {
236 
237  path.append("/");
238  path.append(subdir);
239  }
240 
241  path.append("/");
242  path.append(id);
243 
244  return path;
245 }
246 
247 /****************************/
248 /*** get Content Location ***/
249 std::string
250 CacheManager::getContentLocationMd5( const std::string id, const std::string subdir )
251 {
252  return getContentLocation( getContentIdMd5( id ), subdir );
253 }
254 
255 /*************************/
256 /*** get Friendly Hash ***/
257 std::string
258 CacheManager::getContentIdMd5( const std::string id )
259 {
260  const size_t MYSIZE = 16;
261 
262  unsigned char digest[MYSIZE];
263 
264  MD5_CTX md5;
265  MD5_Init( &md5 );
266  MD5_Update( &md5, const_cast<char *>( id.c_str() ), id.size() );
267  MD5_Final( digest, &md5 );
268 
269  char buffer[(MYSIZE*2)+1];
270 
271  for (unsigned int i = 0; i < MYSIZE; ++i ) {
272 
273  sprintf(&buffer[i*2], "%02x", (unsigned int)digest[i]);
274  }
275 
276  std::string md5_id( buffer );
277 
278  return md5_id;
279 }
280 
281 
282 /*******************/
283 /*** erase Cache ***/
284 int
285 CacheManager::eraseCache( const std::string id, const std::string subdir )
286 {
287  std::string path = getContentLocation( id, subdir );
288 
289  return remove(path.c_str());
290 }
291 
292 /***********************/
293 /*** erase Cache md5 ***/
294 int
295 CacheManager::eraseCacheMd5( const std::string id, const std::string subdir )
296 {
297  return eraseCache( getContentIdMd5( id ), subdir );
298 }
void MD5_Final(unsigned char *result, MD5_CTX *ctx)
Definition: md5.c:244
static std::string getContentIdMd5(const std::string id)
static bool isCached(const std::string id, const std::string subdir="")
static void cache(const std::string id, const std::ostringstream &content, const std::string subdir="")
static void cacheMd5(const std::string id, const std::ostringstream &content, const std::string subdir="")
std::string _cacheDir
bool createPath(const std::string path)
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Definition: Log.cpp:45
static std::string getContentLocation(const std::string id, const std::string subdir="")
static void initialize(const std::string dir="")
static std::string get(const std::string &key)
Definition: Settings.cpp:112
static int eraseCache(const std::string id, const std::string subdir="")
void error(const char *format,...)
'Error' level.
Definition: Log.cpp:290
std::string getWorkingPath()
static bool isCachedMd5(const std::string id, const std::string subdir="")
bool pathExists(const std::string path)
void MD5_Update(MD5_CTX *ctx, void *data, unsigned long size)
Definition: md5.c:210
static void _ensureInitialized()
void MD5_Init(MD5_CTX *ctx)
Definition: md5.c:199
static int count(const std::string &key)
Definition: Settings.cpp:127
static CacheManager & _getInstance()
Definition: md5.h:34
static std::string getContentLocationMd5(const std::string id, const std::string subdir="")
static int eraseCacheMd5(const std::string id, const std::string subdir="")
void debug(const char *format,...)
'Debug' level.
Definition: Log.cpp:237