36 #include "PicoSHA2/picosha2.h"
38 #include <BESInternalError.h>
41 #include <TheBESKeys.h>
43 #include "RemoteHttpResourceCache.h"
44 #include "HttpdCatalogNames.h"
47 #define AT_EXIT(x) atexit((x))
54 namespace httpd_catalog {
56 RemoteHttpResourceCache *RemoteHttpResourceCache::d_instance = 0;
57 bool RemoteHttpResourceCache::d_enabled =
true;
59 const string RemoteHttpResourceCache::DIR_KEY =
"HttpResourceCache.dir";
60 const string RemoteHttpResourceCache::PREFIX_KEY =
"HttpResourceCache.prefix";
61 const string RemoteHttpResourceCache::SIZE_KEY =
"HttpResourceCache.size";
63 unsigned long RemoteHttpResourceCache::getCacheSizeFromConfig()
67 unsigned long size_in_megabytes = 0;
71 std::istringstream iss(size);
72 iss >> size_in_megabytes;
75 string msg =
"HttpdCatalogCache - The BES Key " + SIZE_KEY +
" is not set.";
76 BESDEBUG(MODULE, msg << endl);
80 return size_in_megabytes;
83 string RemoteHttpResourceCache::getCacheDirFromConfig()
90 string msg =
"HttpdCatalogCache - The BES Key " + DIR_KEY +
" is not set.";
91 BESDEBUG(MODULE, msg << endl);
98 string RemoteHttpResourceCache::getCachePrefixFromConfig()
108 string msg =
"HttpdCatalogCache - The BES Key " + PREFIX_KEY +
" is not set.";
109 BESDEBUG(MODULE, msg << endl);
116 RemoteHttpResourceCache::RemoteHttpResourceCache()
118 BESDEBUG(MODULE,
"HttpdCatalogCache::HttpdCatalogCache() - BEGIN" << endl);
120 string cacheDir = getCacheDirFromConfig();
121 string cachePrefix = getCachePrefixFromConfig();
122 unsigned long cacheSizeMbytes = getCacheSizeFromConfig();
124 BESDEBUG(MODULE,
"HttpdCatalogCache() - Cache configuration params: " << cacheDir <<
", " << cachePrefix <<
", " << cacheSizeMbytes << endl);
126 initialize(cacheDir, cachePrefix, cacheSizeMbytes);
128 BESDEBUG(MODULE,
"HttpdCatalogCache::HttpdCatalogCache() - END" << endl);
131 RemoteHttpResourceCache::RemoteHttpResourceCache(
const string &cache_dir,
const string &prefix,
unsigned long long size)
133 BESDEBUG(MODULE,
"HttpdCatalogCache::HttpdCatalogCache() - BEGIN" << endl);
137 BESDEBUG(MODULE,
"HttpdCatalogCache::HttpdCatalogCache() - END" << endl);
140 RemoteHttpResourceCache *
143 if (d_enabled && d_instance == 0) {
145 d_instance =
new RemoteHttpResourceCache(cache_dir, cache_file_prefix, max_cache_size);
150 BESDEBUG(MODULE,
"HttpdCatalogCache::"<<__func__ <<
"() - " <<
"Cache is DISABLED"<< endl);
153 AT_EXIT(delete_instance);
155 BESDEBUG(MODULE,
"HttpdCatalogCache::"<<__func__ <<
"() - " <<
"Cache is ENABLED"<< endl);
166 RemoteHttpResourceCache *
169 if (d_enabled && d_instance == 0) {
176 BESDEBUG(MODULE,
"HttpdCatalogCache::"<<__func__ <<
"() - " <<
"Cache is DISABLED"<< endl);
179 AT_EXIT(delete_instance);
181 BESDEBUG(MODULE,
"HttpdCatalogCache::" << __func__ <<
"() - " <<
"Cache is ENABLED"<< endl);
185 BESDEBUG(MODULE,
"[ERROR] HttpdCatalogCache::get_instance(): Failed to obtain cache! msg: " << bie.
get_message() << endl);
200 if (name.empty())
throw BESInternalError(
"Empty name passed to the Metadata Store.", __FILE__, __LINE__);
201 return picosha2::hash256_hex_string(name[0] ==
'/' ? name :
"/" + name);