libdap Updated for version 3.21.1
libdap4 is an implementation of OPeNDAP's DAP protocol.
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., 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 <list>
34#include <map>
35#include <string>
36// #include <sstream>
37
38#include "DapObj.h"
39
40#if 0
41#include "BESDebug.h"
42#include "BESObj.h"
43
44class BESKeys;
45#endif
46
47namespace 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.
51typedef struct {
52 string name;
53 unsigned long long size;
54 time_t time;
56
57typedef std::list<cache_entry> CacheFiles;
58
82class DAPCache3 : public libdap::DapObj {
83
84private:
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
128public:
129 static DAPCache3 *get_instance(const string &cache_dir, const string &prefix, unsigned long long size);
130 static DAPCache3 *get_instance();
131
132 string get_cache_file_name(const string &src, bool mangle = true);
133
134 virtual bool create_and_lock(const string &target, int &fd);
135 virtual bool get_read_lock(const string &target, int &fd);
136 virtual void exclusive_to_shared_lock(int fd);
137 virtual void unlock_and_close(const string &target);
138 virtual void unlock_and_close(int fd);
139
140 virtual void lock_cache_write();
141 virtual void lock_cache_read();
142 virtual void unlock_cache();
143
144 virtual unsigned long long update_cache_info(const string &target);
145 virtual bool cache_too_big(unsigned long long current_size) const;
146 virtual unsigned long long get_cache_size();
147 virtual void update_and_purge(const string &new_file);
148 virtual void purge_file(const string &file);
149
150#if 0
151 static BESCache3 *get_instance(BESKeys *keys, const string &cache_dir_key, const string &prefix_key, const string &size_key);
152#endif
153
154 virtual void dump(ostream &strm) const;
155};
156
157} // namespace libdap
158
159#endif // DAPCache3_h_
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:746
string get_cache_file_name(const string &src, bool mangle=true)
Definition DAPCache3.cc:506
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:735
virtual void purge_file(const string &file)
Purge a single file from the cache.
Definition DAPCache3.cc:911
virtual bool get_read_lock(const string &target, int &fd)
Get a read-only lock on the file if it exists.
Definition DAPCache3.cc:546
virtual void unlock_cache()
Definition DAPCache3.cc:646
virtual void dump(ostream &strm) const
dumps information about this object
Definition DAPCache3.cc:958
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:601
virtual void lock_cache_read()
Definition DAPCache3.cc:633
virtual void unlock_and_close(const string &target)
virtual unsigned long long update_cache_info(const string &target)
Update the cache info file to include 'target'.
Definition DAPCache3.cc:695
static DAPCache3 * get_instance()
Definition DAPCache3.cc:148
virtual void update_and_purge(const string &new_file)
Purge files from the cache.
Definition DAPCache3.cc:821
virtual void lock_cache_write()
Definition DAPCache3.cc:622
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,...
Definition DAPCache3.cc:573
libdap base object for common functionality of libdap objects
Definition DapObj.h:49
STL class.
top level DAP object to house generic methods
Definition AISConnect.cc:30
std::list< cache_entry > CacheFiles
Definition DAPCache3.h:57
Definition DAPCache3.h:51
unsigned long long size
Definition DAPCache3.h:53
string name
Definition DAPCache3.h:52
time_t time
Definition DAPCache3.h:54