bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
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.
44typedef struct {
45 std::string name;
46 unsigned long long size;
47 time_t time;
49
50typedef std::list<cache_entry> CacheFiles;
51
52
86class BESFileLockingCache: public BESObj {
87
88 private:
89 const char DAP_CACHE_CHAR = '#';
90
91 // TODO Should cache_enabled be false given that cache_dir is empty? jhrg 2/18/18
92 bool d_cache_enabled = true;
93
94 // pathname of the cache directory
95 std::string d_cache_dir;
96
97 // tack this on the front of each cache file name
98 std::string d_prefix;
99
102 unsigned long long d_max_cache_size_in_bytes = 0;
103
104 // When we purge, how much should we throw away. Set in the ctor to 80% of the max size.
105 unsigned long long d_target_size = 0;
106
107 // Name of the file that tracks the size of the cache
108 std::string d_cache_info;
109 int d_cache_info_fd = -1;
110
111 // map that relates files to the descriptor used to obtain a lock
112 typedef std::multimap<std::string, int> FilesAndLockDescriptors;
113 FilesAndLockDescriptors d_locks;
114
115 bool m_check_ctor_params();
116 bool m_initialize_cache_info();
117
118 unsigned long long m_collect_cache_dir_info(CacheFiles &contents);
119
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);
124#endif
125
126#if 0
127 virtual void lock_cache_write();
128 virtual void lock_cache_read();
129 virtual void unlock_cache();
130#endif
131
132 friend class cacheT;
133 friend class FileLockingCacheTest; // This is in dispatch/tests
134 friend class BESFileLockingCacheTest;
135
136public:
137 BESFileLockingCache() = default;
138 BESFileLockingCache(const BESFileLockingCache &) = delete;
139 BESFileLockingCache &operator=(const BESFileLockingCache &rhs) = delete;
140
141 BESFileLockingCache(std::string cache_dir, std::string prefix, unsigned long long size);
142
143 ~BESFileLockingCache() override {
144 if (d_cache_info_fd != -1) {
145 close(d_cache_info_fd);
146 }
147 }
148
149 void initialize(const std::string &cache_dir, const std::string &prefix, unsigned long long size);
150
151 virtual std::string get_cache_file_name(const std::string &src, bool mangle = true);
152
153 virtual bool create_and_lock(const std::string &target, int &fd);
154 virtual bool get_read_lock(const std::string &target, int &fd);
155 virtual void exclusive_to_shared_lock(int fd);
156 virtual void unlock_and_close(const std::string &target);
157
158#if 0
159 virtual void unlock_cache();
160#endif
161
162 virtual unsigned long long update_cache_info(const std::string &target);
163 virtual bool cache_too_big(unsigned long long current_size) const;
164 virtual unsigned long long get_cache_size();
165
166 virtual bool get_exclusive_lock_nb(const std::string &target, int &fd);
167 virtual bool get_exclusive_lock(const std::string &target, int &fd);
168
169 virtual void update_and_purge(const std::string &new_file);
170 virtual void purge_file(const std::string &file);
171
181 bool is_unlimited() const {
182 return d_max_cache_size_in_bytes == 0;
183 }
184
186 std::string get_cache_file_prefix() const {
187 return d_prefix;
188 }
189
191 std::string get_cache_directory() const {
192 return d_cache_dir;
193 }
194
195 // This is a static method because it's often called from 'get_instance()'
196 // methods that are static.
197 static bool dir_exists(const std::string &dir);
198
200 bool cache_enabled() const {
201 return d_cache_enabled;
202 }
203
205 void disable() {
206 d_cache_enabled = false;
207 }
208
210 void enable() {
211 d_cache_enabled = true;
212 }
213
214 void dump(std::ostream &strm) const override;
215};
216
217#endif // BESFileLockingCache_h_
virtual unsigned long long get_cache_size()
Get the cache size.
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
Definition BESObj.h:54