libdap  Updated for version 3.20.6
libdap4 is an implementation of OPeNDAP's DAP protocol.
DAPCache3.h
1 // DAPCache3.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 DAPCache3_h_
30 #define DAPCache3_h_ 1
31 
32 // #include <algorithm>
33 #include <map>
34 #include <string>
35 #include <list>
36 // #include <sstream>
37 
38 #include "DapObj.h"
39 
40 #if 0
41 #include "BESObj.h"
42 #include "BESDebug.h"
43 
44 class BESKeys;
45 #endif
46 
47 namespace libdap {
48 
49 // These typedefs are used to record information about the files in the cache.
50 // See DAPCache3.cc and look at the purge() method.
51 typedef struct {
52  string name;
53  unsigned long long size;
54  time_t time;
55 } cache_entry;
56 
57 typedef std::list<cache_entry> CacheFiles;
58 
82 class DAPCache3: public libdap::DapObj {
83 
84 private:
85  static DAPCache3 * d_instance;
86 
87  static const char DAP_CACHE_CHAR = '#';
88 
89  string d_cache_dir;
90  string d_prefix;
91 
93  unsigned long long d_max_cache_size_in_bytes;
94  // When we purge, how much should we throw away. Set in the ctor to 80% of the max size.
95  unsigned long long d_target_size;
96 #if 0
97  // This class implements a singleton, so the constructor is hidden.
98  BESCache3(BESKeys *keys, const string &cache_dir_key, const string &prefix_key, const string &size_key);
99 #endif
100  // Testing
101  DAPCache3(const string &cache_dir, const string &prefix, unsigned long long size);
102 
103  // Suppress the assignment operator and default copy ctor, ...
104  DAPCache3();
105  DAPCache3(const DAPCache3 &);
106  DAPCache3 &operator=(const DAPCache3 &rhs);
107 
108  void m_check_ctor_params();
109  void m_initialize_cache_info();
110 
111  unsigned long long m_collect_cache_dir_info(CacheFiles &contents);
112 
114  string d_cache_info;
115  int d_cache_info_fd;
116 
117  void m_record_descriptor(const string &file, int fd);
118  int m_get_descriptor(const string &file);
119 
120  // map that relates files to the descriptor used to obtain a lock
121  typedef std::map<string, int> FilesAndLockDescriptors;
122  FilesAndLockDescriptors d_locks;
123 
124  // Life-cycle control
125  virtual ~DAPCache3() { }
126  static void delete_instance();
127 
128 public:
129  static DAPCache3 *get_instance(const string &cache_dir, const string &prefix, unsigned long long size);
130  static DAPCache3 *get_instance();
131 
132 
133  string get_cache_file_name(const string &src, bool mangle = true);
134 
135  virtual bool create_and_lock(const string &target, int &fd);
136  virtual bool get_read_lock(const string &target, int &fd);
137  virtual void exclusive_to_shared_lock(int fd);
138  virtual void unlock_and_close(const string &target);
139  virtual void unlock_and_close(int fd);
140 
141  virtual void lock_cache_write();
142  virtual void lock_cache_read();
143  virtual void unlock_cache();
144 
145  virtual unsigned long long update_cache_info(const string &target);
146  virtual bool cache_too_big(unsigned long long current_size) const;
147  virtual unsigned long long get_cache_size();
148  virtual void update_and_purge(const string &new_file);
149  virtual void purge_file(const string &file);
150 
151 #if 0
152  static BESCache3 *get_instance(BESKeys *keys, const string &cache_dir_key, const string &prefix_key, const string &size_key);
153 #endif
154 
155  virtual void dump(ostream &strm) const ;
156 };
157 
158 } // namespace libdap
159 
160 #endif // DAPCache3_h_
virtual unsigned long long update_cache_info(const string &target)
Update the cache info file to include &#39;target&#39;.
Definition: DAPCache3.cc:719
string get_cache_file_name(const string &src, bool mangle=true)
Definition: DAPCache3.cc:521
virtual void dump(ostream &strm) const
dumps information about this object
Definition: DAPCache3.cc:993
virtual unsigned long long get_cache_size()
Get the cache size. Read the size information from the cache info file and return it...
Definition: DAPCache3.cc:772
top level DAP object to house generic methods
Definition: AISConnect.cc:30
virtual bool create_and_lock(const string &target, int &fd)
Create a file in the cache and lock it for write access. If the file does not exist, make it, open it for read-write access and get an exclusive lock on it. The locking operation blocks, although that should never happen.
Definition: DAPCache3.cc:590
virtual void lock_cache_write()
Definition: DAPCache3.cc:642
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: DAPCache3.cc:760
virtual void exclusive_to_shared_lock(int fd)
Transfer from an exclusive lock to a shared lock. If the file has an exclusive write lock on it...
Definition: DAPCache3.cc:620
virtual bool get_read_lock(const string &target, int &fd)
Get a read-only lock on the file if it exists.
Definition: DAPCache3.cc:562
virtual void lock_cache_read()
Definition: DAPCache3.cc:654
virtual void update_and_purge(const string &new_file)
Purge files from the cache.
Definition: DAPCache3.cc:854
Definition: DAPCache3.h:51
libdap base object for common functionality of libdap objects
Definition: DapObj.h:50
static DAPCache3 * get_instance()
Definition: DAPCache3.cc:153
Implementation of a caching mechanism for compressed data. This cache uses simple advisory locking fo...
Definition: DAPCache3.h:82
virtual void purge_file(const string &file)
Purge a single file from the cache.
Definition: DAPCache3.cc:944
virtual void unlock_cache()
Definition: DAPCache3.cc:668