bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
ObjMemCache.h
1
2// This file is part of bes, A C++ back-end server implementation framework
3// for the OPeNDAP Data Access Protocol.
4
5// Copyright (c) 2016 OPeNDAP
6// Author: James Gallagher <jgallagher@opendap.org>
7//
8// This library is free software; you can redistribute it and/or
9// modify it under the terms of the GNU Lesser General Public
10// License as published by the Free Software Foundation; either
11// version 2.1 of the License, or (at your option) any later version.
12//
13// This library is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16// Lesser General Public License for more details.
17//
18// You should have received a copy of the GNU Lesser General Public
19// License along with this library; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
22/*
23 * ObjMemCache.h
24 *
25 * Created on: May 18, 2016
26 * Author: jimg
27 */
28
29#ifndef DAP_OBJMEMCACHE_H_
30#define DAP_OBJMEMCACHE_H_
31
32#include <cassert>
33
34#include <string>
35#include <map>
36
37#include "BESIndent.h"
38
39namespace libdap {
40 class DapObj;
41}
42
43//namespace bes {
44
84 // TODO Make this a template or make a new version of this that is a
85 // template so it's typesafe. jhrg 9/6/23
86private:
87 struct Entry {
88 libdap::DapObj *d_obj;
89 const std::string d_name;
90
91 // We need the string so that we can erase the index entry easily
92 Entry(libdap::DapObj *o, const std::string &n): d_obj(o), d_name(n) { }
93 // deleting an Entry deletes the thing it references
94 ~Entry() { delete d_obj; d_obj = 0;}
95 };
96
97 // TODO Since d_age is used for both the cache and the index, as the 'int'
98 // it should be an unsigned int or those should be u long longs. It's
99 // unlikely we will ever run a process long enough to need 64-bits. jhrg 9/6/23
100
101 unsigned long long d_age; // When obj was added or last accessed
102 unsigned int d_entries_threshold; // no more than this num of entries
103 float d_purge_threshold; // free up this fraction of the cache
104
105 typedef std::pair<unsigned int, Entry*> cache_pair_t; // used by map::insert()
106 typedef std::map<unsigned int, Entry*> cache_t;
107 cache_t cache;
108
109 typedef std::pair<const std::string, unsigned int> index_pair_t;
110 // efficiency improvement - use an unordered_map when C++-11 is adopted
111 typedef std::map<const std::string, unsigned int> index_t;
112 index_t index;
113
114 friend class DDSMemCacheTest;
115
116public:
125 ObjMemCache(): d_age(0), d_entries_threshold(0), d_purge_threshold(0.2) { }
126
137 ObjMemCache(unsigned int entries_threshold, float purge_threshold): d_age(0),
138 d_entries_threshold(entries_threshold), d_purge_threshold(purge_threshold) {
139 // d_entries_threshold = entries_threshold >> 1; // * 2
140 }
141
142 virtual ~ObjMemCache();
143
144 virtual void add(libdap::DapObj *obj, const std::string &key);
145
146 virtual void remove(const std::string &key);
147
148 virtual libdap::DapObj *get(const std::string &key);
149
154 virtual unsigned int size() const {
155 assert(cache.size() == index.size());
156 return cache.size();
157 }
158
159 virtual void purge(float fraction);
160
165 virtual void dump(ostream &os) {
166 os << "ObjMemCache" << std::endl;
167 os << "Length of index: " << index.size() << std::endl;
168 for(index_t::const_iterator it = index.begin(); it != index.end(); ++it) {
169 os << it->first << " --> " << it->second << std::endl;
170 }
171
172 os << "Length of cache: " << cache.size() << std::endl;
173 for(cache_t::const_iterator it = cache.begin(); it != cache.end(); ++it) {
174 os << it->first << " --> " << it->second->d_name << std::endl;
175 }
176 }
177};
178
179// } namespace bes
180
181#endif /* DAP_OBJMEMCACHE_H_ */
An in-memory cache for DapObj (DAS, DDS, ...) objects.
Definition ObjMemCache.h:83
virtual unsigned int size() const
How many items are in the cache.
ObjMemCache()
Initialize the DapObj cache This constructor builds a cache that will require the caller manage the p...
virtual void add(libdap::DapObj *obj, const std::string &key)
Add an object to the cache and associate it with a key.
virtual libdap::DapObj * get(const std::string &key)
Get the cached pointer.
ObjMemCache(unsigned int entries_threshold, float purge_threshold)
Initialize the DapObj cache to use an item count threshold.
virtual void remove(const std::string &key)
Remove the object associated with a key.
virtual void dump(ostream &os)
What is in the cache.
virtual void purge(float fraction)
Purge the oldest elements.
STL class.