14#include "BESH4MCache.h"
17#include "BESInternalError.h"
18#include "TheBESKeys.h"
20#include "HDF4RequestHandler.h"
25bool BESH4Cache::d_enabled =
true;
27const string BESH4Cache::PATH_KEY =
"HDF4.Cache.latlon.path";
28const string BESH4Cache::PREFIX_KEY =
"HDF4.Cache.latlon.prefix";
29const string BESH4Cache::SIZE_KEY =
"HDF4.Cache.latlon.size";
31long BESH4Cache::getCacheSizeFromConfig()
33 if (HDF4RequestHandler::get_cache_latlon_size_exist() ==
true) {
35 "In BESH4Cache::getCacheSize(): Located BES key " << SIZE_KEY<<
"=" << HDF4RequestHandler::get_cache_latlon_size() << endl);
36 return HDF4RequestHandler::get_cache_latlon_size();
39 string msg =
"[ERROR] BESH4Cache::getCacheSize() - The BES Key " + SIZE_KEY
40 +
" is not set! It MUST be set to utilize the HDF4 cache. ";
41 BESDEBUG(
"cache", msg);
42 throw BESInternalError(msg, __FILE__, __LINE__);
46string BESH4Cache::getCachePrefixFromConfig()
48 if (HDF4RequestHandler::get_cache_latlon_prefix_exist() ==
true) {
50 "In BESH4Cache::getCachePrefix(): Located BES key " << PREFIX_KEY<<
"=" << HDF4RequestHandler::get_cache_latlon_prefix() << endl);
51 return HDF4RequestHandler::get_cache_latlon_prefix();
54 string msg =
"[ERROR] BESH4Cache::getCachePrefix() - The BES Key " + PREFIX_KEY
55 +
" is not set! It MUST be set to utilize the HDF4 cache. ";
56 BESDEBUG(
"cache", msg);
57 throw BESInternalError(msg, __FILE__, __LINE__);
61string BESH4Cache::getCacheDirFromConfig()
63 if (HDF4RequestHandler::get_cache_latlon_path_exist() ==
true) {
65 "In BESH4Cache::getCacheDirFromConfig(): Located BES key " << PATH_KEY<<
"=" << HDF4RequestHandler::get_cache_latlon_path() << endl);
66 return HDF4RequestHandler::get_cache_latlon_path();
69 string msg =
"[ERROR] BESH4Cache::getCachePrefix() - The BES Key " + PREFIX_KEY
70 +
" is not set! It MUST be set to utilize the HDF4 cache. ";
71 BESDEBUG(
"cache", msg);
72 throw BESInternalError(msg, __FILE__, __LINE__);
76BESH4Cache::BESH4Cache()
78 BESDEBUG(
"cache",
"In BESH4Cache::BESH4Cache()" << endl);
80 string cacheDir = getCacheDirFromConfig();
81 string prefix = getCachePrefixFromConfig();
82 long size_in_megabytes = getCacheSizeFromConfig();
85 "BESH4Cache() - Cache config params: " << cacheDir <<
", " << prefix <<
", " << size_in_megabytes << endl);
90 if (!cacheDir.empty() && size_in_megabytes > 0) {
91 BESDEBUG(
"cache",
"Before calling initialize function." << endl);
92 initialize(cacheDir, prefix, size_in_megabytes);
95 BESDEBUG(
"cache",
"Leaving BESH4Cache::BESH4Cache()" << endl);
104 if (d_enabled && d_instance ==
nullptr) {
106 string cache_dir = getCacheDirFromConfig();
107 if ((stat(cache_dir.c_str(), &buf) == 0) && (buf.st_mode & S_IFDIR)) {
109 d_instance =
new BESH4Cache();
111 d_enabled = d_instance->cache_enabled();
114 d_instance =
nullptr;
115 BESDEBUG(
"cache",
"BESH4Cache::"<<__func__ <<
"() - " <<
116 "Cache is DISABLED"<< endl);
120 atexit(delete_instance);
122 BESDEBUG(
"cache",
"BESH4Cache::" << __func__ <<
"() - " <<
123 "Cache is ENABLED"<< endl);
128 "BESH4Cache::get_instance(): Failed to obtain cache! msg: " << bie.
get_message() << endl);
137bool BESH4Cache::is_valid(
const string & cache_file_name,
const int expected_file_size)
const
141 int result = stat(cache_file_name.c_str(), &st);
143 string msg =
"Cannot check the cached file " + cache_file_name;
146 if (expected_file_size == st.st_size)
153bool BESH4Cache::get_data_from_cache(
const string & cache_file_name,
const int expected_file_size,
int &fd)
156 cerr<<
"coming to get_data_from_cache "<<endl;
157 BESDEBUG(
"cache",
"In BESH4Cache::get_data_from_cache()" << endl);
158 cerr<<
"cache_file_name is "<<cache_file_name <<endl;
160 string cache_file_name1 = cache_file_name;
163 cerr<<
"After get_read_lock "<<endl;
167 else if (
false == is_valid(cache_file_name, expected_file_size)) {
176bool BESH4Cache::write_cached_data(
const string & cache_file_name,
const int expected_file_size,
177 const vector<double> &val)
180 BESDEBUG(
"cache",
"In BESH4Cache::write_cached_data()" << endl);
182 bool ret_value =
false;
190 ret_val = write(fd, val.data(), expected_file_size);
193 if (ret_val != expected_file_size) {
194 if (unlink(cache_file_name.c_str()) != 0) {
195 string msg =
"Cannot remove the corrupt cached file " + cache_file_name;
196 throw BESInternalError(msg, __FILE__, __LINE__);
214bool BESH4Cache::write_cached_data2(
const string & cache_file_name,
const int expected_file_size,
const void *buf)
217 BESDEBUG(
"cache",
"In BESH4Cache::write_cached_data()" << endl);
219 bool ret_value =
false;
227 ret_val = write(fd, buf, expected_file_size);
230 if (ret_val != expected_file_size) {
231 if (unlink(cache_file_name.c_str()) != 0) {
232 string msg =
"Cannot remove the corrupt cached file " + cache_file_name;
233 throw BESInternalError(msg, __FILE__, __LINE__);
251void BESH4Cache::dummy_test_func() {
253 cerr<<
"BESH4Cache function is fine "<<endl;
257string BESH4Cache::get_cache_file_name_h4(
const string & src,
bool mangle) {
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.
static BESH4Cache * get_instance()
exception thrown if internal error encountered