29#include "BESInternalError.h"
32#include "TheBESKeys.h"
33#include "BESUncompressCache.h"
39bool BESUncompressCache::d_enabled =
true;
41const string BESUncompressCache::DIR_KEY =
"BES.UncompressCache.dir";
42const string BESUncompressCache::PREFIX_KEY =
"BES.UncompressCache.prefix";
43const string BESUncompressCache::SIZE_KEY =
"BES.UncompressCache.size";
46#define prolog std::string("BESUncompressCache::").append(__func__).append("() - ")
48unsigned long BESUncompressCache::getCacheSizeFromConfig()
52 unsigned long size_in_megabytes = 0;
55 std::istringstream iss(size);
56 iss >> size_in_megabytes;
59 string msg = prolog+
"The BES Key " + SIZE_KEY
60 +
" is not set! It MUST be set to utilize the decompression cache. ";
61 BESDEBUG( MODULE, msg << endl);
62 throw BESInternalError(msg, __FILE__, __LINE__);
64 return size_in_megabytes;
67string BESUncompressCache::getCacheDirFromConfig()
74 string msg = prolog +
"The BES Key " + DIR_KEY
75 +
" is not set! It MUST be set to utilize the decompression cache. ";
76 BESDEBUG( MODULE, msg << endl);
77 throw BESInternalError(msg, __FILE__, __LINE__);
83string BESUncompressCache::getCachePrefixFromConfig()
92 string msg = prolog +
"The BES Key " + PREFIX_KEY
93 +
" is not set! It MUST be set to utilize the decompression cache. ";
94 BESDEBUG( MODULE, msg << endl);
95 throw BESInternalError(msg, __FILE__, __LINE__);
130 string cache_file_name = src;
133 string::size_type last_dot = cache_file_name.rfind(
'.');
134 if (last_dot != string::npos) {
135 cache_file_name = cache_file_name.substr(0, last_dot);
140 BESDEBUG( MODULE, prolog <<
"cache_file_name: '" << cache_file_name <<
"'" << endl);
142 return cache_file_name;
145BESUncompressCache::BESUncompressCache()
147 BESDEBUG( MODULE, prolog <<
"BEGIN" << endl);
150 d_dimCacheDir = getCacheDirFromConfig();
151 d_dimCacheFilePrefix = getCachePrefixFromConfig();
152 d_maxCacheSize = getCacheSizeFromConfig();
154 BESDEBUG( MODULE, prolog <<
"Cache configuration params: " << d_dimCacheDir <<
", " << d_dimCacheFilePrefix <<
", " << d_maxCacheSize << endl);
156 initialize(d_dimCacheDir, d_dimCacheFilePrefix, d_maxCacheSize);
158 BESDEBUG( MODULE, prolog <<
"END" << endl);
161BESUncompressCache::BESUncompressCache(
const string &data_root_dir,
const string &cache_dir,
const string &prefix,
162 unsigned long long size)
164 BESDEBUG( MODULE, prolog <<
"BEGIN" << endl);
167 d_dataRootDir = data_root_dir;
168 d_dimCacheDir = cache_dir;
169 d_dimCacheFilePrefix = prefix;
170 d_maxCacheSize = size;
172 initialize(d_dimCacheDir, d_dimCacheFilePrefix, d_maxCacheSize);
174 BESDEBUG( MODULE, prolog <<
"END" << endl);
179 unsigned long long max_cache_size)
181 if (d_enabled && d_instance == 0) {
183 d_instance =
new BESUncompressCache(data_root_dir, cache_dir, result_file_prefix, max_cache_size);
184 d_enabled = d_instance->cache_enabled();
188 BESDEBUG( MODULE, prolog <<
"Cache is DISABLED"<< endl);
192 atexit(delete_instance);
194 BESDEBUG( MODULE, prolog <<
"Cache is ENABLED"<< endl);
207 if (d_enabled && d_instance == 0) {
208 d_instance =
new BESUncompressCache();
209 d_enabled = d_instance->cache_enabled();
213 BESDEBUG( MODULE, prolog <<
"Cache is DISABLED"<< endl);
217 atexit(delete_instance);
219 BESDEBUG( MODULE, prolog <<
"Cache is ENABLED"<< endl);
226BESUncompressCache::~BESUncompressCache() {}
240bool BESUncompressCache::is_valid(
const string &cache_file_name,
const string &local_id)
246 off_t entry_size = 0;
247 time_t entry_time = 0;
249 if (stat(cache_file_name.c_str(), &buf) == 0) {
250 entry_size = buf.st_size;
251 entry_time = buf.st_mtime;
257 if (entry_size == 0)
return false;
259 time_t dataset_time = entry_time;
260 if (stat(datasetFileName.c_str(), &buf) == 0) {
261 dataset_time = buf.st_mtime;
271 if (dataset_time > entry_time)
return false;
void initialize(const std::string &cache_dir, const std::string &prefix, unsigned long long size)
Initialize an instance of FileLockingCache.
static bool dir_exists(const std::string &dir)
virtual std::string get_cache_file_name(const std::string &src, bool mangle=true)
static BESUncompressCache * get_instance()
std::string get_cache_file_name(const std::string &src, bool mangle=true) override
Build the name of file that will holds the uncompressed data from 'src' in the cache.
static std::string lowercase(const std::string &s)
static std::string assemblePath(const std::string &firstPart, const std::string &secondPart, bool leadingSlash=false, bool trailingSlash=false)
Assemble path fragments making sure that they are separated by a single '/' character.
void get_value(const std::string &s, std::string &val, bool &found)
Retrieve the value of a given key, if set.
static TheBESKeys * TheKeys()
Access to the singleton.