OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESFileLockingCache.h
Go to the documentation of this file.
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 <map>
33 #include <string>
34 #include <list>
35 
36 #include "BESObj.h"
37 
38 // These typedefs are used to record information about the files in the cache.
39 // See BESFileLockingCache.cc and look at the purge() method.
40 typedef struct {
41  string name;
42  unsigned long long size;
43  time_t time;
44 } cache_entry;
45 
46 typedef std::list<cache_entry> CacheFiles;
47 
71 class BESFileLockingCache: public BESObj {
72 
73 private:
74 
75  static const char DAP_CACHE_CHAR = '#';
76 
77  string d_cache_dir;
78  string d_prefix;
79 
81  unsigned long long d_max_cache_size_in_bytes;
82  // When we purge, how much should we throw away. Set in the ctor to 80% of the max size.
83  unsigned long long d_target_size;
84  // Testing
85 
86  // Suppress the assignment operator and default copy ctor, ...
88  BESFileLockingCache &operator=(const BESFileLockingCache &rhs);
89 
90  void m_check_ctor_params();
91  void m_initialize_cache_info();
92 
93  unsigned long long m_collect_cache_dir_info(CacheFiles &contents);
94 
96  string d_cache_info;
97  int d_cache_info_fd;
98 
99  void m_record_descriptor(const string &file, int fd);
100  int m_get_descriptor(const string &file);
101 
102  // map that relates files to the descriptor used to obtain a lock
103  typedef std::multimap<string, int> FilesAndLockDescriptors;
104  FilesAndLockDescriptors d_locks;
105 
106 
107 protected:
108 
109  BESFileLockingCache(): d_cache_dir(""), d_prefix(""), d_max_cache_size_in_bytes(0), d_target_size(0), d_cache_info(""), d_cache_info_fd(0){};
110  void initialize(const string &cache_dir, const string &prefix, unsigned long long size);
111  BESFileLockingCache(const string &cache_dir, const string &prefix, unsigned long long size);
112  virtual ~BESFileLockingCache() { }
113 
114 public:
115 
116  virtual string get_cache_file_name(const string &src, bool mangle = true);
117 
118  virtual bool create_and_lock(const string &target, int &fd);
119  virtual bool get_read_lock(const string &target, int &fd);
120  virtual void exclusive_to_shared_lock(int fd);
121  virtual void unlock_and_close(const string &target);
122  virtual void unlock_and_close(int fd);
123 
124  virtual void lock_cache_write();
125  virtual void lock_cache_read();
126  virtual void unlock_cache();
127 
128  virtual unsigned long long update_cache_info(const string &target);
129  virtual bool cache_too_big(unsigned long long current_size) const;
130  virtual unsigned long long get_cache_size();
131  virtual void update_and_purge(const string &new_file);
132  virtual void purge_file(const string &file);
133 
134  const string getCacheFilePrefix();
135  const string getCacheDirectory();
136 
137  virtual void dump(ostream &strm) const ;
138 };
139 
140 #endif // BESFileLockingCache_h_
virtual void unlock_cache()
Unlock the cache info file.
std::list< cache_entry > CacheFiles
Definition: BESCache3.h:46
virtual bool create_and_lock(const string &target, int &fd)
Create a file in the cache and lock it for write access.
Definition: BESCache3.h:40
const string getCacheDirectory()
virtual unsigned long long get_cache_size()
Get the cache size.
Implementation of a caching mechanism for compressed data.
Base object for bes objects.
Definition: BESObj.h:52
void initialize(const string &cache_dir, const string &prefix, unsigned long long size)
virtual void dump(ostream &strm) const
dumps information about this object
virtual void purge_file(const string &file)
Purge a single file from the cache.
std::list< cache_entry > CacheFiles
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 lock_cache_write()
Get an exclusive lock on the 'cache info' file.
virtual string get_cache_file_name(const string &src, bool mangle=true)
Build the name of file that will holds the uncompressed data from 'src' in the cache.
const string getCacheFilePrefix()
virtual bool get_read_lock(const string &target, int &fd)
Get a read-only lock on the file if it exists.
virtual void update_and_purge(const string &new_file)
Purge files from the cache.
virtual unsigned long long update_cache_info(const string &target)
Update the cache info file to include 'target'.
virtual void lock_cache_read()
Get a shared lock on the 'cache info' file.
virtual void exclusive_to_shared_lock(int fd)
Transfer from an exclusive lock to a shared lock.
virtual void unlock_and_close(const string &target)
Unlock the named file.