bes  Updated for version 3.20.6
BESFileLockingCache.h
1 // BESFileLockingCache.h
2 
3 // This file was originally part of bes, A C++ back-end server
4 // implementation framework for the OPeNDAP Data Access Protocol.
5 // Copied to libdap. This is used to cache responses built from
6 // functional CE expressions.
7 
8 // Copyright (c) 2012 OPeNDAP, Inc
9 // Author: James Gallagher <jgallagher@opendap.org>,
10 // Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
11 //
12 // This library is free software; you can redistribute it and/or
13 // modify it under the terms of the GNU Lesser General Public
14 // License as published by the Free Software Foundation; either
15 // version 2.1 of the License, or (at your option) any later version.
16 //
17 // This library is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 //
26 // You can contact University Corporation for Atmospheric Research at
27 // 3080 Center Green Drive, Boulder, CO 80301
28 
29 #ifndef BESFileLockingCache_h_
30 #define BESFileLockingCache_h_ 1
31 
32 #include <unistd.h>
33 
34 #include <map>
35 #include <string>
36 #include <list>
37 
38 #include "BESObj.h"
39 
40 #define USE_GET_SHARED_LOCK 1
41 
42 // These typedefs are used to record information about the files in the cache.
43 // See BESFileLockingCache.cc and look at the purge() method.
44 typedef struct {
45  std::string name;
46  unsigned long long size;
47  time_t time;
48 } cache_entry;
49 
50 typedef std::list<cache_entry> CacheFiles;
51 
85 class BESFileLockingCache: public BESObj {
86 
87  friend class cacheT;
88  friend class FileLockingCacheTest;
89 
90 private:
91  static const char DAP_CACHE_CHAR = '#';
92 
93  bool d_cache_enabled;
94 
95  // pathname of the cache directory
96  std::string d_cache_dir;
97 
98  // tack this on the front of each cache file name
99  std::string d_prefix;
100 
103  unsigned long long d_max_cache_size_in_bytes;
104 
105  // When we purge, how much should we throw away. Set in the ctor to 80% of the max size.
106  unsigned long long d_target_size;
107 
108  // Name of the file that tracks the size of the cache
109  std::string d_cache_info;
110  int d_cache_info_fd;
111 
112  // map that relates files to the descriptor used to obtain a lock
113  typedef std::multimap<std::string, int> FilesAndLockDescriptors;
114  FilesAndLockDescriptors d_locks;
115 
116  bool m_check_ctor_params();
117  bool m_initialize_cache_info();
118 
119  unsigned long long m_collect_cache_dir_info(CacheFiles &contents);
120 
121  void m_record_descriptor(const std::string &file, int fd);
122  int m_remove_descriptor(const std::string &file);
123 #if USE_GET_SHARED_LOCK
124  int m_find_descriptor(const std::string &file);
125 #endif
126  // Suppress the assignment operator and default copy ctor, ...
128  BESFileLockingCache &operator=(const BESFileLockingCache &rhs);
129 
130 public:
131  // TODO Should cache_enabled be false given that cache_dir is empty? jhrg 2/18/18
132  BESFileLockingCache(): d_cache_enabled(true), d_cache_dir(""), d_prefix(""), d_max_cache_size_in_bytes(0),
133  d_target_size(0), d_cache_info(""), d_cache_info_fd(-1) { }
134 
135  BESFileLockingCache(const std::string &cache_dir, const std::string &prefix, unsigned long long size);
136 
137  virtual ~BESFileLockingCache()
138  {
139  if (d_cache_info_fd != -1) {
140  close(d_cache_info_fd);
141  d_cache_info_fd = -1;
142  }
143  }
144 
145  void initialize(const std::string &cache_dir, const std::string &prefix, unsigned long long size);
146 
147  virtual std::string get_cache_file_name(const std::string &src, bool mangle = true);
148 
149  virtual bool create_and_lock(const std::string &target, int &fd);
150  virtual bool get_read_lock(const std::string &target, int &fd);
151  virtual void exclusive_to_shared_lock(int fd);
152  virtual void unlock_and_close(const std::string &target);
153 
154  virtual void lock_cache_write();
155  virtual void lock_cache_read();
156  virtual void unlock_cache();
157 
158  virtual unsigned long long update_cache_info(const std::string &target);
159  virtual bool cache_too_big(unsigned long long current_size) const;
160  virtual unsigned long long get_cache_size();
161 
162  virtual bool get_exclusive_lock_nb(const std::string &target, int &fd);
163  virtual bool get_exclusive_lock(const std::string &target, int &fd);
164 
165  virtual void update_and_purge(const std::string &new_file);
166  virtual void purge_file(const std::string &file);
167 
177  bool is_unlimited() const {
178  return d_max_cache_size_in_bytes == 0;
179  }
180 
182  const std::string get_cache_file_prefix()
183  {
184  return d_prefix;
185  }
186 
188  const std::string get_cache_directory()
189  {
190  return d_cache_dir;
191  }
192 
193  // This is a static method because it's often called from 'get_instance()'
194  // methods that are static.
195  static bool dir_exists(const std::string &dir);
196 
198  bool cache_enabled() const
199  {
200  return d_cache_enabled;
201  }
202 
204  void disable()
205  {
206  d_cache_enabled = false;
207  }
208 
210  void enable()
211  {
212  d_cache_enabled = true;
213  }
214 
215  virtual void dump(std::ostream &strm) const;
216 };
217 
218 #endif // BESFileLockingCache_h_
BESFileLockingCache::get_read_lock
virtual bool get_read_lock(const std::string &target, int &fd)
Get a read-only lock on the file if it exists.
Definition: BESFileLockingCache.cc:544
BESFileLockingCache::get_cache_size
virtual unsigned long long get_cache_size()
Get the cache size.
Definition: BESFileLockingCache.cc:792
BESFileLockingCache::cache_enabled
bool cache_enabled() const
Definition: BESFileLockingCache.h:198
BESFileLockingCache::create_and_lock
virtual bool create_and_lock(const std::string &target, int &fd)
Create a file in the cache and lock it for write access.
Definition: BESFileLockingCache.cc:599
BESFileLockingCache::lock_cache_write
virtual void lock_cache_write()
Definition: BESFileLockingCache.cc:654
BESFileLockingCache::is_unlimited
bool is_unlimited() const
Is this cache allowed to store as much as it wants?
Definition: BESFileLockingCache.h:177
BESFileLockingCache::dir_exists
static bool dir_exists(const std::string &dir)
Definition: BESFileLockingCache.cc:1136
BESFileLockingCache::get_cache_file_prefix
const std::string get_cache_file_prefix()
Definition: BESFileLockingCache.h:182
BESFileLockingCache::purge_file
virtual void purge_file(const std::string &file)
Purge a single file from the cache.
Definition: BESFileLockingCache.cc:1085
BESFileLockingCache::dump
virtual void dump(std::ostream &strm) const
dumps information about this object
Definition: BESFileLockingCache.cc:1151
BESFileLockingCache::unlock_and_close
virtual void unlock_and_close(const std::string &target)
Definition: BESFileLockingCache.cc:713
cache_entry
Definition: BESFileLockingCache.h:44
BESFileLockingCache::enable
void enable()
Enable the cache.
Definition: BESFileLockingCache.h:210
BESObj
top level BES object to house generic methods
Definition: BESObj.h:49
BESFileLockingCache::lock_cache_read
virtual void lock_cache_read()
Definition: BESFileLockingCache.cc:669
BESFileLockingCache::initialize
void initialize(const std::string &cache_dir, const std::string &prefix, unsigned long long size)
Initialize an instance of FileLockingCache.
Definition: BESFileLockingCache.cc:116
BESFileLockingCache::disable
void disable()
Disable the cache.
Definition: BESFileLockingCache.h:204
BESFileLockingCache
Implementation of a caching mechanism for compressed data.
Definition: BESFileLockingCache.h:85
BESFileLockingCache::update_cache_info
virtual unsigned long long update_cache_info(const std::string &target)
Update the cache info file to include 'target'.
Definition: BESFileLockingCache.cc:737
BESFileLockingCache::get_cache_file_name
virtual std::string get_cache_file_name(const std::string &src, bool mangle=true)
Definition: BESFileLockingCache.cc:451
BESFileLockingCache::update_and_purge
virtual void update_and_purge(const std::string &new_file)
Purge files from the cache.
Definition: BESFileLockingCache.cc:940
BESFileLockingCache::unlock_cache
virtual void unlock_cache()
Definition: BESFileLockingCache.cc:686
BESFileLockingCache::get_cache_directory
const std::string get_cache_directory()
Definition: BESFileLockingCache.h:188
BESFileLockingCache::cache_too_big
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.
Definition: BESFileLockingCache.cc:780
BESFileLockingCache::exclusive_to_shared_lock
virtual void exclusive_to_shared_lock(int fd)
Transfer from an exclusive lock to a shared lock.
Definition: BESFileLockingCache.cc:630