libdap++  Updated for version 3.11.7
DAPCache3.h
Go to the documentation of this file.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 // These typedefs are used to record information about the files in the cache.
48 // See DAPCache3.cc and look at the purge() method.
49 typedef struct {
50  string name;
51  unsigned long long size;
52  time_t time;
53 } cache_entry;
54 
55 typedef std::list<cache_entry> CacheFiles;
56 
80 class DAPCache3: public libdap::DapObj {
81 
82 private:
83  static DAPCache3 * d_instance;
84 
85  static const char DAP_CACHE_CHAR = '#';
86 
87  string d_cache_dir;
88  string d_prefix;
89 
91  unsigned long long d_max_cache_size_in_bytes;
92  // When we purge, how much should we throw away. Set in the ctor to 80% of the max size.
93  unsigned long long d_target_size;
94 #if 0
95  // This class implements a singleton, so the constructor is hidden.
96  BESCache3(BESKeys *keys, const string &cache_dir_key, const string &prefix_key, const string &size_key);
97 #endif
98  // Testing
99  DAPCache3(const string &cache_dir, const string &prefix, unsigned long long size);
100 
101  // Suppress the assignment operator and default copy ctor, ...
102  DAPCache3();
103  DAPCache3(const DAPCache3 &);
104  DAPCache3 &operator=(const DAPCache3 &rhs);
105 
106  void m_check_ctor_params();
107  void m_initialize_cache_info();
108 
109  unsigned long long m_collect_cache_dir_info(CacheFiles &contents);
110 
112  string d_cache_info;
113  int d_cache_info_fd;
114 
115  void m_record_descriptor(const string &file, int fd);
116  int m_get_descriptor(const string &file);
117 
118  // map that relates files to the descriptor used to obtain a lock
119  typedef std::map<string, int> FilesAndLockDescriptors;
120  FilesAndLockDescriptors d_locks;
121 
122 public:
123  virtual ~DAPCache3() { }
124 
125  string get_cache_file_name(const string &src, bool mangle = true);
126 
127  virtual bool create_and_lock(const string &target, int &fd);
128  virtual bool get_read_lock(const string &target, int &fd);
129  virtual void exclusive_to_shared_lock(int fd);
130  virtual void unlock_and_close(const string &target);
131  virtual void unlock_and_close(int fd);
132 
133  virtual void lock_cache_write();
134  virtual void lock_cache_read();
135  virtual void unlock_cache();
136 
137  virtual unsigned long long update_cache_info(const string &target);
138  virtual bool cache_too_big(unsigned long long current_size) const;
139  virtual unsigned long long get_cache_size();
140  virtual void update_and_purge(const string &new_file);
141  virtual void purge_file(const string &file);
142 
143 #if 0
144  static BESCache3 *get_instance(BESKeys *keys, const string &cache_dir_key, const string &prefix_key, const string &size_key);
145 #endif
146  static DAPCache3 *get_instance(const string &cache_dir, const string &prefix, unsigned long long size);
147  static DAPCache3 *get_instance();
148 
149  virtual void dump(ostream &strm) const ;
150 };
151 
152 #endif // DAPCache3_h_