12#include "HDF5DiskCache.h"
15#include "BESInternalError.h"
16#include "TheBESKeys.h"
23const string HDF5DiskCache::PATH_KEY =
"H5.DiskCacheDataPath";
24const string HDF5DiskCache::PREFIX_KEY =
"H5.DiskCacheFilePrefix";
25const string HDF5DiskCache::SIZE_KEY =
"H5.DiskCacheSize";
27long HDF5DiskCache::getCacheSizeFromConfig(
const long cache_size)
31 "In HDF5DiskCache::getCacheSizeFromConfig(): Located BES key " << SIZE_KEY<<
"=" << cache_size << endl);
35 string msg =
"[ERROR] HDF5DiskCache::getCacheSize() - The BES Key " + SIZE_KEY
36 +
" is either not set or the size is not a positive integer! It MUST be set and the size must be greater than 0 to use the HDF5 Disk cache. ";
37 BESDEBUG(
"cache", msg);
38 throw BESInternalError(msg, __FILE__, __LINE__);
42string HDF5DiskCache::getCachePrefixFromConfig(
const string& cache_prefix)
44 if (cache_prefix!=
"") {
46 "In HDF5DiskCache::getCachePrefixFromConfig(): Located BES key " << PATH_KEY<<
"=" << cache_prefix << endl);
50 string msg =
"[ERROR] HDF5DiskCache::getCachePrefixFromConfig() - The BES Key " + PREFIX_KEY
51 +
" is either not set or the value is an empty string! It MUST be set to be a valid string to utilize the HDF5 Disk cache. ";
52 BESDEBUG(
"cache", msg);
53 throw BESInternalError(msg, __FILE__, __LINE__);
57string HDF5DiskCache::getCacheDirFromConfig(
const string& cache_dir)
61 "In HDF5DiskCache::getCacheDirFromConfig(): Located BES key " << PATH_KEY<<
"=" << cache_dir << endl);
65 string msg =
"[ERROR] HDF5DiskCache::getCacheDirFromConfig() - The BES Key " + PREFIX_KEY
66 +
" is either not set or the value is an empty string! It MUST be set to be a valid path to utilize the HDF5 Disk cache. ";
67 BESDEBUG(
"cache", msg);
68 throw BESInternalError(msg, __FILE__, __LINE__);
73HDF5DiskCache::HDF5DiskCache(
const unsigned long long _cache_size,
const string &_cache_dir,
const string &_cache_prefix)
75 BESDEBUG(
"cache",
"In HDF5DiskCache::HDF5DiskCache()" << endl);
77 string cacheDir = getCacheDirFromConfig(_cache_dir);
78 string prefix = getCachePrefixFromConfig(_cache_prefix);
79 unsigned long long size_in_megabytes = getCacheSizeFromConfig(_cache_size);
82 "HDF5DiskCache() - Cache config params: " << cacheDir <<
", " << prefix <<
", " << size_in_megabytes << endl);
87 if (!cacheDir.empty() && size_in_megabytes > 0) {
88 BESDEBUG(
"cache",
"Before calling initialize function." << endl);
89 initialize(cacheDir, prefix, size_in_megabytes);
92 BESDEBUG(
"cache",
"Leaving HDF5DiskCache::HDF5DiskCache()" << endl);
101 if (d_instance ==
nullptr) {
103 string cache_dir = getCacheDirFromConfig(_cache_dir);
104 if ((stat(cache_dir.c_str(), &buf) == 0) && (buf.st_mode & S_IFDIR)) {
106 d_instance =
new HDF5DiskCache(_cache_size,_cache_dir,_cache_prefix);
108 atexit(delete_instance);
113 "HDF5DiskCache::get_instance(): Failed to obtain cache! msg: " << bie.
get_message() << endl);
122bool HDF5DiskCache::is_valid(
const string & cache_file_name, int64_t expected_file_size)
const
126 int result = stat(cache_file_name.c_str(), &st);
128 string msg =
"Cannot check the cached file " + cache_file_name;
131 if (expected_file_size == st.st_size)
138bool HDF5DiskCache::get_data_from_cache(
const string & cache_file_name, int64_t expected_file_size,
int &fd)
141 cerr<<
"coming to get_data_from_cache "<<endl;
142 BESDEBUG(
"cache",
"In HDF5DiskCache::get_data_from_cache()" << endl);
143 cerr<<
"cache_file_name is "<<cache_file_name <<endl;
145 string cache_file_name1 = cache_file_name;
148 cerr<<
"After get_read_lock "<<endl;
152 else if (
false == is_valid(cache_file_name, expected_file_size)) {
161bool HDF5DiskCache::write_cached_data(
const string & cache_file_name, int64_t expected_file_size,
162 const vector<double> &val)
165 BESDEBUG(
"cache",
"In HDF5DiskCache::write_cached_data()" << endl);
167 bool ret_value =
false;
175 ret_val = write(fd, val.data(), expected_file_size);
178 if (ret_val != expected_file_size) {
179 if (unlink(cache_file_name.c_str()) != 0) {
180 string msg =
"Cannot remove the corrupt cached file " + cache_file_name;
181 throw BESInternalError(msg, __FILE__, __LINE__);
199bool HDF5DiskCache::write_cached_data2(
const string & cache_file_name, int64_t expected_file_size,
const void *buf)
202 BESDEBUG(
"cache",
"In HDF5DiskCache::write_cached_data()" << endl);
204 bool ret_value =
false;
212 ret_val = write(fd, buf, expected_file_size);
215 if (ret_val != expected_file_size) {
216 if (unlink(cache_file_name.c_str()) != 0) {
217 string msg =
"Cannot remove the corrupt cached file " + cache_file_name;
218 throw BESInternalError(msg, __FILE__, __LINE__);
236void HDF5DiskCache::dummy_test_func() {
238 cerr<<
"HDF5DiskCache function is fine "<<endl;
242string HDF5DiskCache::get_cache_file_name_h4(
const string & src,
bool mangle) {
include the entry functions to execute the handlers
std::string get_message() const
get the error message for this exception
void initialize(const std::string &cache_dir, const std::string &prefix, unsigned long long size)
Initialize an instance of FileLockingCache.
virtual void unlock_and_close(const std::string &target)
virtual unsigned long long update_cache_info(const std::string &target)
Update the cache info file to include 'target'.
virtual bool create_and_lock(const std::string &target, int &fd)
Create a file in the cache and lock it for write access.
virtual bool get_read_lock(const std::string &target, int &fd)
Get a read-only lock on the file if it exists.
virtual void purge_file(const std::string &file)
Purge a single file from the cache.
virtual bool cache_too_big(unsigned long long current_size) const
look at the cache size; is it too large? Look at the cache size and see if it is too big.
virtual void update_and_purge(const std::string &new_file)
Purge files from the cache.
exception thrown if internal error encountered
static HDF5DiskCache * get_instance(const long, const std::string &, const std::string &)