42 #include "PicoSHA2/picosha2.h" 44 #include "BESInternalError.h" 47 #include "TheBESKeys.h" 53 #define AT_EXIT(x) atexit((x)) 62 bool CmrCache::d_enabled =
true;
64 const string CmrCache::DIR_KEY =
"CMR.Cache.dir";
65 const string CmrCache::PREFIX_KEY =
"CMR.Cache.prefix";
66 const string CmrCache::SIZE_KEY =
"CMR.Cache.size";
68 unsigned long CmrCache::getCacheSizeFromConfig()
73 unsigned long size_in_megabytes = 0;
76 std::istringstream iss(size);
77 iss >> size_in_megabytes;
80 string msg =
"CmrCache - The BES Key " + SIZE_KEY +
" is not set.";
81 BESDEBUG(MODULE, msg << endl);
84 return size_in_megabytes;
87 string CmrCache::getCacheDirFromConfig()
94 string msg =
"CmrCache - The BES Key " + DIR_KEY +
" is not set.";
95 BESDEBUG(MODULE, msg << endl);
102 string CmrCache::getCachePrefixFromConfig()
111 string msg =
"CmrCache - The BES Key " + PREFIX_KEY +
" is not set.";
112 BESDEBUG(MODULE, msg << endl);
121 BESDEBUG(MODULE,
"CmrCache::CmrCache() - BEGIN" << endl);
123 string cacheDir = getCacheDirFromConfig();
124 string cachePrefix = getCachePrefixFromConfig();
125 unsigned long cacheSizeMbytes = getCacheSizeFromConfig();
128 "CmrCache() - Cache configuration params: " << cacheDir <<
", " << cachePrefix <<
", " << cacheSizeMbytes << endl);
130 initialize(cacheDir, cachePrefix, cacheSizeMbytes);
132 BESDEBUG(MODULE,
"CmrCache::CmrCache() - END" << endl);
135 CmrCache::CmrCache(
const string &cache_dir,
const string &prefix,
unsigned long long size)
137 BESDEBUG(MODULE,
"CmrCache::CmrCache() - BEGIN" << endl);
141 BESDEBUG(MODULE,
"CmrCache::CmrCache() - END" << endl);
145 CmrCache::get_instance(
const string &cache_dir,
const string &cache_file_prefix,
unsigned long long max_cache_size)
147 if (d_enabled && d_instance == 0) {
149 d_instance =
new CmrCache(cache_dir, cache_file_prefix, max_cache_size);
154 BESDEBUG(MODULE,
"CmrCache::"<<__func__ <<
"() - " <<
"Cache is DISABLED"<< endl);
157 AT_EXIT(delete_instance);
159 BESDEBUG(MODULE,
"CmrCache::"<<__func__ <<
"() - " <<
"Cache is ENABLED"<< endl);
173 if (d_enabled && d_instance == 0) {
180 BESDEBUG(MODULE,
"CmrCache::"<<__func__ <<
"() - " <<
"Cache is DISABLED"<< endl);
183 AT_EXIT(delete_instance);
185 BESDEBUG(MODULE,
"CmrCache::" << __func__ <<
"() - " <<
"Cache is ENABLED"<< endl);
190 "[ERROR] CmrCache::get_instance(): Failed to obtain cache! msg: " << bie.
get_message() << endl);
208 throw BESInternalError(
"Empty name passed to the Metadata Store.", __FILE__, __LINE__);
209 return picosha2::hash256_hex_string(name[0] ==
'/' ? name :
"/" + name);
exception thrown if inernal error encountered
const std::string get_cache_file_prefix()
static string lowercase(const string &s)
virtual string get_cache_file_name(const string &src, bool mangle=true)
virtual std::string get_message()
get the error message for this exception
bool cache_enabled() const
static bool dir_exists(const std::string &dir)
void get_value(const std::string &s, std::string &val, bool &found)
Retrieve the value of a given key, if set.
static TheBESKeys * TheKeys()
const std::string get_cache_directory()
void initialize(const std::string &cache_dir, const std::string &prefix, unsigned long long size)
Initialize an instance of FileLockingCache.
static CmrCache * get_instance()
string get_hash(const string &name)
static string assemblePath(const string &firstPart, const string &secondPart, bool leadingSlash=false, bool trailingSlash=false)
Assemble path fragments making sure that they are separated by a single '/' character.
A cache for content accessed via the CMR.