30 unsigned long size_in_megabytes = 0;
33 BESDEBUG(
"cache",
"In BESH4Cache::getDefaultCacheSize(): Located BES key " <<
34 SIZE_KEY<<
"=" << size << endl);
35 istringstream iss(size);
36 iss >> size_in_megabytes;
39 string msg =
"[ERROR] BESH4Cache::getCacheSize() - The BES Key " + SIZE_KEY +
" is not set! It MUST be set to utilize the HDF4 cache. ";
43 return size_in_megabytes;
52 BESDEBUG(
"cache",
"In BESH4Cache::getDefaultCachePrefix(): Located BES key " <<
53 PREFIX_KEY<<
"=" << prefix << endl);
57 string msg =
"[ERROR] BESH4Cache::getCachePrefix() - The BES Key " + PREFIX_KEY +
" is not set! It MUST be set to utilize the HDF4 cache. ";
73 BESDEBUG(
"cache",
"In BESH4Cache::getCachePrefix(): Located BES key " <<
74 PATH_KEY<<
"=" << cacheDir << endl);
78 string msg =
"[ERROR] BESH4Cache::getCacheDir() - The BES Key " + PATH_KEY +
" is not set! It MUST be set to utilize the HDF4 cache. ";
85 BESH4Cache::BESH4Cache(){
87 BESDEBUG(
"cache",
"In BESH4Cache::BESH4Cache()" << endl);
89 string cacheDir = getCacheDirFromConfig();
90 string prefix = getCachePrefixFromConfig();
91 unsigned long size_in_megabytes = getCacheSizeFromConfig();
93 BESDEBUG(
"cache",
"BESH4Cache() - Cache config params: " << cacheDir <<
", " << prefix <<
", " << size_in_megabytes << endl);
100 if (!cacheDir.empty() && size_in_megabytes > 0) {
101 BESDEBUG(
"cache",
"Before calling initialize function." << endl);
102 initialize(cacheDir, prefix, size_in_megabytes);
105 BESDEBUG(
"cache",
"Leaving BESH4Cache::BESH4Cache()" << endl);
123 BESH4Cache::BESH4Cache(
const string &cache_dir,
const string &prefix,
unsigned long long size):
BESFileLockingCache(cache_dir,prefix,size) {
140 if (d_instance == 0){
141 if(dir_exists(cache_dir)){
143 d_instance =
new BESH4Cache(cache_dir, prefix, size);
146 BESDEBUG(
"cache",
"BESH4Cache::get_instance(): Failed to obtain cache! msg: " << bie.
get_message() << endl);
160 if (d_instance == 0) {
163 if((stat(cache_dir.c_str(),&buf)==0) && (buf.st_mode & S_IFDIR)){
168 BESDEBUG(
"cache",
"BESH4Cache::get_instance(): Failed to obtain cache! msg: " << bie.
get_message() << endl);
177 BESDEBUG(
"cache",
"BESH4Cache::delete_instance() - Deleting singleton BESH4Cache instance." << endl);
178 cerr <<
"BESH4Cache::delete_instance() - Deleting singleton BESH4Cache instance. d_instance="<< d_instance << endl;
187 int result = stat(cache_file_name.c_str(),&st);
189 string msg =
"Cannot check the cached file " + cache_file_name;
192 if(expected_file_size == st.st_size)
201 cerr<<
"coming to get_data_from_cache "<<endl;
202 BESDEBUG(
"cache",
"In BESH4Cache::get_data_from_cache()" << endl);
203 cerr<<
"cache_file_name is "<<cache_file_name <<endl;
205 string cache_file_name1 = cache_file_name;
208 cerr<<
"After get_read_lock "<<endl;
212 else if(
false ==
is_valid(cache_file_name,expected_file_size)) {
224 BESDEBUG(
"cache",
"In BESH4Cache::write_cached_data()" << endl);
226 bool ret_value =
false;
234 ret_val = write(fd,&val[0],expected_file_size);
238 if(ret_val != expected_file_size) {
239 if(unlink(cache_file_name.c_str())!=0){
240 string msg =
"Cannot remove the corrupt cached file " + cache_file_name;
263 void BESH4Cache::dummy_test_func() {
265 cerr<<
"BESH4Cache function is fine "<<endl;
270 string BESH4Cache::get_cache_file_name_h4(
const string & src,
bool mangle) {
exception thrown if inernal error encountered
virtual bool create_and_lock(const string &target, int &fd)
Create a file in the cache and lock it for write access.
static string lowercase(const string &s)
Convert a string to all lower case.
bool get_data_from_cache(const string &cache_file_name, const int expected_file_size, int &fd)
bool is_valid(const string &cache_file_name, const int expected_file_size)
static string getCachePrefixFromConfig()
static const string SIZE_KEY
Implementation of a caching mechanism for compressed data.
static unsigned long getCacheSizeFromConfig()
virtual string get_message()
get the error message for this exception
static void delete_instance()
bool write_cached_data(const string &cache_file_name, const int expected_file_size, const std::vector< double > &val)
virtual void purge_file(const 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...
static BESH4Cache * get_instance()
Get the default instance of the BESH4Cache object.
virtual bool get_read_lock(const string &target, int &fd)
Get a read-only lock on the file if it exists.
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
virtual void update_and_purge(const string &new_file)
Purge files from the cache.
static const string PREFIX_KEY
virtual unsigned long long update_cache_info(const string &target)
Update the cache info file to include 'target'.
static const string PATH_KEY
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
static string getCacheDirFromConfig()
static BESKeys * TheKeys()
virtual void unlock_and_close(const string &target)
Unlock the named file.