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))
52 namespace httpd_catalog {
54 RemoteHttpResourceCache *RemoteHttpResourceCache::d_instance = 0;
55 bool RemoteHttpResourceCache::d_enabled =
true;
57 const string RemoteHttpResourceCache::DIR_KEY =
"HttpResourceCache.dir";
58 const string RemoteHttpResourceCache::PREFIX_KEY =
"HttpResourceCache.prefix";
59 const string RemoteHttpResourceCache::SIZE_KEY =
"HttpResourceCache.size";
61 unsigned long RemoteHttpResourceCache::getCacheSizeFromConfig()
65 unsigned long size_in_megabytes = 0;
69 std::istringstream iss(size);
70 iss >> size_in_megabytes;
73 string msg =
"HttpdCatalogCache - The BES Key " + SIZE_KEY +
" is not set.";
74 BESDEBUG(MODULE, msg << endl);
78 return size_in_megabytes;
81 string RemoteHttpResourceCache::getCacheDirFromConfig()
88 string msg =
"HttpdCatalogCache - The BES Key " + DIR_KEY +
" is not set.";
89 BESDEBUG(MODULE, msg << endl);
96 string RemoteHttpResourceCache::getCachePrefixFromConfig()
106 string msg =
"HttpdCatalogCache - The BES Key " + PREFIX_KEY +
" is not set.";
107 BESDEBUG(MODULE, msg << endl);
114 RemoteHttpResourceCache::RemoteHttpResourceCache()
116 BESDEBUG(MODULE,
"HttpdCatalogCache::HttpdCatalogCache() - BEGIN" << endl);
118 string cacheDir = getCacheDirFromConfig();
119 string cachePrefix = getCachePrefixFromConfig();
120 unsigned long cacheSizeMbytes = getCacheSizeFromConfig();
122 BESDEBUG(MODULE,
"HttpdCatalogCache() - Cache configuration params: " << cacheDir <<
", " << cachePrefix <<
", " << cacheSizeMbytes << endl);
124 initialize(cacheDir, cachePrefix, cacheSizeMbytes);
126 BESDEBUG(MODULE,
"HttpdCatalogCache::HttpdCatalogCache() - END" << endl);
129 RemoteHttpResourceCache::RemoteHttpResourceCache(
const string &cache_dir,
const string &prefix,
unsigned long long size)
131 BESDEBUG(MODULE,
"HttpdCatalogCache::HttpdCatalogCache() - BEGIN" << endl);
135 BESDEBUG(MODULE,
"HttpdCatalogCache::HttpdCatalogCache() - END" << endl);
138 RemoteHttpResourceCache *
141 if (d_enabled && d_instance == 0) {
143 d_instance =
new RemoteHttpResourceCache(cache_dir, cache_file_prefix, max_cache_size);
148 BESDEBUG(MODULE,
"HttpdCatalogCache::"<<__func__ <<
"() - " <<
"Cache is DISABLED"<< endl);
151 AT_EXIT(delete_instance);
153 BESDEBUG(MODULE,
"HttpdCatalogCache::"<<__func__ <<
"() - " <<
"Cache is ENABLED"<< endl);
164 RemoteHttpResourceCache *
167 if (d_enabled && d_instance == 0) {
174 BESDEBUG(MODULE,
"HttpdCatalogCache::"<<__func__ <<
"() - " <<
"Cache is DISABLED"<< endl);
177 AT_EXIT(delete_instance);
179 BESDEBUG(MODULE,
"HttpdCatalogCache::" << __func__ <<
"() - " <<
"Cache is ENABLED"<< endl);
183 BESDEBUG(MODULE,
"[ERROR] HttpdCatalogCache::get_instance(): Failed to obtain cache! msg: " << bie.
get_message() << endl);
198 if (name.empty())
throw BESInternalError(
"Empty name passed to the Metadata Store.", __FILE__, __LINE__);
199 return picosha2::hash256_hex_string(name[0] ==
'/' ? name :
"/" + name);