12 #include "HDF5DiskCache.h"
15 #include "BESInternalError.h"
16 #include "TheBESKeys.h"
23 const string HDF5DiskCache::PATH_KEY =
"H5.DiskCacheDataPath";
24 const string HDF5DiskCache::PREFIX_KEY =
"H5.DiskCacheFilePrefix";
25 const string HDF5DiskCache::SIZE_KEY =
"H5.DiskCacheSize";
27 long 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);
42 string 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);
57 string 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);
73 HDF5DiskCache::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 == 0) {
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);
122 bool HDF5DiskCache::is_valid(
const string & cache_file_name,
const int expected_file_size)
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)
138 bool HDF5DiskCache::get_data_from_cache(
const string & cache_file_name,
const int 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;
146 get_read_lock(cache_file_name1,fd1);
148 cerr<<
"After get_read_lock "<<endl;
150 if (
false == get_read_lock(cache_file_name, fd))
152 else if (
false == is_valid(cache_file_name, expected_file_size)) {
153 unlock_and_close(cache_file_name);
154 purge_file(cache_file_name);
161 bool HDF5DiskCache::write_cached_data(
const string & cache_file_name,
const int expected_file_size,
162 const vector<double> &val)
165 BESDEBUG(
"cache",
"In HDF5DiskCache::write_cached_data()" << endl);
167 bool ret_value =
false;
170 if (create_and_lock(cache_file_name, fd)) {
175 ret_val = write(fd, &val[0], 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;
186 unsigned long long size = update_cache_info(cache_file_name);
187 if (cache_too_big(size)) update_and_purge(cache_file_name);
191 unlock_and_close(cache_file_name);
199 bool HDF5DiskCache::write_cached_data2(
const string & cache_file_name,
const int expected_file_size,
const void *buf)
202 BESDEBUG(
"cache",
"In HDF5DiskCache::write_cached_data()" << endl);
204 bool ret_value =
false;
207 if (create_and_lock(cache_file_name, fd)) {
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;
223 unsigned long long size = update_cache_info(cache_file_name);
224 if (cache_too_big(size)) update_and_purge(cache_file_name);
228 unlock_and_close(cache_file_name);
236 void HDF5DiskCache::dummy_test_func() {
238 cerr<<
"HDF5DiskCache function is fine "<<endl;
242 string HDF5DiskCache::get_cache_file_name_h4(
const string & src,
bool mangle) {