bes  Updated for version 3.17.4
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 // These typedefs are used to record information about the files in the cache.
41 // See BESFileLockingCache.cc and look at the purge() method.
42 typedef struct {
43  string name;
44  unsigned long long size;
45  time_t time;
46 } cache_entry;
47 
48 typedef std::list<cache_entry> CacheFiles;
49 
73 class BESFileLockingCache: public BESObj {
74 private:
75  static const char DAP_CACHE_CHAR = '#';
76 
77 protected:
78 
79  // pathname of the cache directory
80  string d_cache_dir;
81 
82  // tack this on the front of each cache file name
83  string d_prefix;
84 
86  unsigned long long d_max_cache_size_in_bytes;
87 
88  // When we purge, how much should we throw away. Set in the ctor to 80% of the max size.
89  unsigned long long d_target_size;
90 
91 private:
92 
93  // Suppress the assignment operator and default copy ctor, ...
95  BESFileLockingCache &operator=(const BESFileLockingCache &rhs);
96 
97  void m_check_ctor_params();
98  void m_initialize_cache_info();
99 
100  unsigned long long m_collect_cache_dir_info(CacheFiles &contents);
101 
103  string d_cache_info;
104  int d_cache_info_fd;
105 
106  void m_record_descriptor(const string &file, int fd);
107  int m_get_descriptor(const string &file);
108 
109  // map that relates files to the descriptor used to obtain a lock
110  typedef std::multimap<string, int> FilesAndLockDescriptors;
111  FilesAndLockDescriptors d_locks;
112 
113  // Removed; see comment in .cc file. void unlock_and_close(int fd);
114 
115 protected:
116 
117  BESFileLockingCache(): d_cache_dir(""), d_prefix(""), d_max_cache_size_in_bytes(0), d_target_size(0), d_cache_info(""), d_cache_info_fd(-1){};
118  void initialize(const string &cache_dir, const string &prefix, unsigned long long size);
119  BESFileLockingCache(const string &cache_dir, const string &prefix, unsigned long long size);
120  virtual ~BESFileLockingCache() { if (d_cache_info_fd != -1){ close(d_cache_info_fd); d_cache_info_fd=-1;} }
121 
122 public:
123 
124  virtual string get_cache_file_name(const string &src, bool mangle = true);
125 
126  virtual bool create_and_lock(const string &target, int &fd);
127  virtual bool get_read_lock(const string &target, int &fd);
128  virtual void exclusive_to_shared_lock(int fd);
129  virtual void unlock_and_close(const string &target);
130  // Removed. See comments in the .cc file virtual void unlock_and_close(int fd);
131 
132  virtual bool getExclusiveLock(string file_name, int &ref_fd);
133 
134  virtual void lock_cache_write();
135  virtual void lock_cache_read();
136  virtual void unlock_cache();
137 
138  virtual unsigned long long update_cache_info(const string &target);
139  virtual bool cache_too_big(unsigned long long current_size) const;
140  virtual unsigned long long get_cache_size();
141  virtual void update_and_purge(const string &new_file);
142  virtual void purge_file(const string &file);
143 
144  const string getCacheFilePrefix();
145  const string getCacheDirectory();
146 
147  static bool dir_exists(const string &dir);
148  // static string assemblePath(const string &firstPart, const string &secondPart, bool addLeadingSlash = false);
149 
150 
151  virtual void dump(ostream &strm) const ;
152 };
153 
154 #endif // BESFileLockingCache_h_
Implementation of a caching mechanism for compressed data. This cache uses simple advisory locking fo...
Base object for bes objects.
Definition: BESObj.h:52
unsigned long long d_max_cache_size_in_bytes
How many bytes can the cache hold before we have to purge.