OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESCache3.h
Go to the documentation of this file.
1 // BESCache3.h
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2012 OPeNDAP, Inc
7 // Author: James Gallagher <jgallagher@opendap.org>,
8 // Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 //
24 // You can contact University Corporation for Atmospheric Research at
25 // 3080 Center Green Drive, Boulder, CO 80301
26 
27 #ifndef BESCache3_h_
28 #define BESCache3_h_ 1
29 
30 // #include <algorithm>
31 #include <map>
32 #include <string>
33 // #include <sstream>
34 
35 #include "BESObj.h"
36 #include "BESDebug.h"
37 
38 class BESKeys;
39 
40 // These typedefs are used to record information about the files in the cache.
41 // See BESCache3.cc and look at the purge() method.
42 typedef struct {
43  string name;
44  unsigned long long size;
45  time_t time;
46 } cache_entry;
47 
48 typedef std::list<cache_entry> CacheFiles;
49 
73 class BESCache3: public BESObj {
74 
75 private:
76  static BESCache3 * d_instance;
77 
78  static const char BES_CACHE_CHAR = '#';
79 
80  string d_cache_dir;
81  string d_prefix;
82 
84  unsigned long long d_max_cache_size_in_bytes;
85  // When we purge, how much should we throw away. Set in the ctor to 80% of the max size.
86  unsigned long long d_target_size;
87 
88  // This class implements a singleton, so the constructor is hidden.
89  BESCache3(BESKeys *keys, const string &cache_dir_key, const string &prefix_key, const string &size_key);
90  // Testing
91  BESCache3(const string &cache_dir, const string &prefix, unsigned long size);
92 
93  // Suppress the assignment operator and default copy ctor, ...
94  BESCache3() { }
95  BESCache3(const BESCache3 &rhs) { }
96  BESCache3 &operator=(const BESCache3 &rhs) { }
97 
98  void m_check_ctor_params();
99  void m_initialize_cache_info();
100 
101  unsigned long long m_collect_cache_dir_info(CacheFiles &contents);
102 
104  string d_cache_info;
105  int d_cache_info_fd;
106 
107  void m_record_descriptor(const string &file, int fd);
108  int m_get_descriptor(const string &file);
109 
110  // map that relates files to the descriptor used to obtain a lock
111  typedef std::map<string, int> FilesAndLockDescriptors;
112  FilesAndLockDescriptors d_locks;
113 
114 public:
115  virtual ~BESCache3() { }
116 
117  string get_cache_file_name(const string &src);
118 
119  virtual bool create_and_lock(const string &target, int &fd);
120  virtual bool get_read_lock(const string &target, int &fd);
121  virtual void exclusive_to_shared_lock(int fd);
122  virtual void unlock_and_close(const string &target);
123  virtual void unlock_and_close(int fd);
124 
125  virtual void lock_cache_write();
126  virtual void lock_cache_read();
127  virtual void unlock_cache();
128 
129  virtual unsigned long long update_cache_info(const string &target);
130  virtual bool cache_too_big(unsigned long long current_size) const;
131  virtual unsigned long long get_cache_size();
132  virtual void update_and_purge(const string &new_file);
133 
134  static BESCache3 *get_instance(BESKeys *keys, const string &cache_dir_key, const string &prefix_key, const string &size_key);
135  static BESCache3 *get_instance(const string &cache_dir, const string &prefix, unsigned long size); // Testing
136  static BESCache3 *get_instance();
137 
138  virtual void dump(ostream &strm) const ;
139 };
140 
141 #endif // BESCache3_h_
virtual void exclusive_to_shared_lock(int fd)
Transfer from an exclusive lock to a shared lock.
Definition: BESCache3.cc:558
virtual void dump(ostream &strm) const
dumps information about this object
Definition: BESCache3.cc:878
std::list< cache_entry > CacheFiles
Definition: BESCache3.h:48
time_t time
Definition: BESCache3.h:45
virtual ~BESCache3()
Definition: BESCache3.h:115
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: BESCache3.cc:698
Definition: BESCache3.h:42
virtual void lock_cache_write()
Get an exclusive lock on the 'cache info' file.
Definition: BESCache3.cc:580
unsigned long long size
Definition: BESCache3.h:44
string name
Definition: BESCache3.h:43
virtual void unlock_and_close(const string &target)
Unlock the named file.
Definition: BESCache3.cc:626
Base object for bes objects.
Definition: BESObj.h:52
virtual void lock_cache_read()
Get a shared lock on the 'cache info' file.
Definition: BESCache3.cc:592
mapping of key/value pairs defining different behaviors of an application.
Definition: BESKeys.h:84
Implementation of a caching mechanism for compressed data.
Definition: BESCache3.h:73
virtual unsigned long long update_cache_info(const string &target)
Update the cache info file to include 'target'.
Definition: BESCache3.cc:657
virtual bool create_and_lock(const string &target, int &fd)
Create a file in the cache and lock it for write access.
Definition: BESCache3.cc:528
virtual bool get_read_lock(const string &target, int &fd)
Get a read-only lock on the file if it exists.
Definition: BESCache3.cc:500
string get_cache_file_name(const string &src)
Build the name of file that will holds the uncompressed data from 'src' in the cache.
Definition: BESCache3.cc:465
virtual void update_and_purge(const string &new_file)
Purge files from the cache.
Definition: BESCache3.cc:792
virtual void unlock_cache()
Unlock the cache info file.
Definition: BESCache3.cc:606
virtual unsigned long long get_cache_size()
Get the cache size.
Definition: BESCache3.cc:710
static BESCache3 * get_instance()
Get an instance of the BESCache3 object.
Definition: BESCache3.cc:98