29#ifndef BESFileLockingCache_h_
30#define BESFileLockingCache_h_ 1
40#define USE_GET_SHARED_LOCK 1
46 unsigned long long size;
50typedef std::list<cache_entry> CacheFiles;
86class BESFileLockingCache:
public BESObj {
89 const char DAP_CACHE_CHAR =
'#';
92 bool d_cache_enabled =
true;
95 std::string d_cache_dir;
102 unsigned long long d_max_cache_size_in_bytes = 0;
105 unsigned long long d_target_size = 0;
108 std::string d_cache_info;
109 int d_cache_info_fd = -1;
112 typedef std::multimap<std::string, int> FilesAndLockDescriptors;
113 FilesAndLockDescriptors d_locks;
115 bool m_check_ctor_params();
116 bool m_initialize_cache_info();
118 unsigned long long m_collect_cache_dir_info(CacheFiles &contents);
120 void m_record_descriptor(
const std::string &file,
int fd);
121 int m_remove_descriptor(
const std::string &file);
122#if USE_GET_SHARED_LOCK
123 int m_find_descriptor(
const std::string &file);
127 virtual void lock_cache_write();
128 virtual void lock_cache_read();
129 virtual void unlock_cache();
133 friend class FileLockingCacheTest;
134 friend class BESFileLockingCacheTest;
137 BESFileLockingCache() =
default;
138 BESFileLockingCache(
const BESFileLockingCache &) =
delete;
139 BESFileLockingCache &operator=(
const BESFileLockingCache &rhs) =
delete;
141 BESFileLockingCache(std::string cache_dir, std::string prefix,
unsigned long long size);
143 ~BESFileLockingCache()
override {
144 if (d_cache_info_fd != -1) {
145 close(d_cache_info_fd);
149 void initialize(
const std::string &cache_dir,
const std::string &prefix,
unsigned long long size);
154 virtual bool get_read_lock(
const std::string &target,
int &fd);
159 virtual void unlock_cache();
163 virtual bool cache_too_big(
unsigned long long current_size)
const;
170 virtual void purge_file(
const std::string &file);
182 return d_max_cache_size_in_bytes == 0;
197 static bool dir_exists(
const std::string &dir);
201 return d_cache_enabled;
206 d_cache_enabled =
false;
211 d_cache_enabled =
true;
214 void dump(std::ostream &strm)
const override;
virtual unsigned long long get_cache_size()
Get the cache size.
bool cache_enabled() const
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)
std::string get_cache_directory() const
bool is_unlimited() const
Is this cache allowed to store as much as it wants?
virtual unsigned long long update_cache_info(const std::string &target)
Update the cache info file to include 'target'.
void enable()
Enable the cache.
virtual bool create_and_lock(const std::string &target, int &fd)
Create a file in the cache and lock it for write access.
virtual void exclusive_to_shared_lock(int fd)
Transfer from an exclusive lock to a shared lock.
virtual bool get_read_lock(const std::string &target, int &fd)
Get a read-only lock on the file if it exists.
static bool dir_exists(const std::string &dir)
virtual bool get_exclusive_lock(const std::string &target, int &fd)
void disable()
Disable the cache.
virtual bool get_exclusive_lock_nb(const std::string &target, int &fd)
std::string get_cache_file_prefix() const
void dump(std::ostream &strm) const override
dumps information about this object
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.
virtual std::string get_cache_file_name(const std::string &src, bool mangle=true)
top level BES object to house generic methods