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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 <map>
31 #include <string>
32 
33 #include "BESObj.h"
34 #include "BESDebug.h"
35 
36 class BESKeys;
37 
38 // These typedefs are used to record information about the files in the cache.
39 // See BESCache3.cc and look at the purge() method.
40 typedef struct {
41  string name;
42  unsigned long long size;
43  time_t time;
44 } cache_entry;
45 
46 typedef std::list<cache_entry> CacheFiles;
47 
71 class BESCache3: public BESObj {
72 
73 private:
74  static BESCache3 * d_instance;
75 
76  static const char BES_CACHE_CHAR = '#';
77 
78  string d_cache_dir;
79  string d_prefix;
80 
82  unsigned long long d_max_cache_size_in_bytes;
83  // When we purge, how much should we throw away. Set in the ctor to 80% of the max size.
84  unsigned long long d_target_size;
85 
86  // This class implements a singleton, so the constructor is hidden.
87  BESCache3(BESKeys *keys, const string &cache_dir_key, const string &prefix_key, const string &size_key);
88  // Testing
89  BESCache3(const string &cache_dir, const string &prefix, unsigned long size);
90 
91  // Suppress the assignment operator and default copy ctor, ...
92  BESCache3() { }
93  BESCache3(const BESCache3 &) :BESObj() { }
94  BESCache3 &operator=(const BESCache3 &) { return *this; }
95 
96  void m_check_ctor_params();
97  void m_initialize_cache_info();
98 
99  unsigned long long m_collect_cache_dir_info(CacheFiles &contents);
100 
102  string d_cache_info;
103  int d_cache_info_fd;
104 
105  void m_record_descriptor(const string &file, int fd);
106  int m_get_descriptor(const string &file);
107 
108  // map that relates files to the descriptor used to obtain a lock
109  typedef std::multimap<string, int > FilesAndLockDescriptors;
110  FilesAndLockDescriptors d_locks;
111 
112 public:
113  virtual ~BESCache3() { }
114 
115  string get_cache_file_name(const string &src);
116 
117  virtual bool create_and_lock(const string &target, int &fd);
118  virtual bool get_read_lock(const string &target, int &fd);
119  virtual void exclusive_to_shared_lock(int fd);
120  virtual void unlock_and_close(const string &target);
121  virtual void unlock_and_close(int fd);
122 
123  virtual void lock_cache_write();
124  virtual void lock_cache_read();
125  virtual void unlock_cache();
126 
127  virtual unsigned long long update_cache_info(const string &target);
128  virtual bool cache_too_big(unsigned long long current_size) const;
129  virtual unsigned long long get_cache_size();
130  virtual void update_and_purge(const string &new_file);
131 
132  static BESCache3 *get_instance(BESKeys *keys, const string &cache_dir_key, const string &prefix_key, const string &size_key);
133  static BESCache3 *get_instance(const string &cache_dir, const string &prefix, unsigned long size); // Testing
134  static BESCache3 *get_instance();
135 
136  virtual void dump(ostream &strm) const ;
137 };
138 
139 #endif // BESCache3_h_
virtual void exclusive_to_shared_lock(int fd)
Transfer from an exclusive lock to a shared lock.
Definition: BESCache3.cc:565
virtual void dump(ostream &strm) const
dumps information about this object
Definition: BESCache3.cc:897
std::list< cache_entry > CacheFiles
Definition: BESCache3.h:46
time_t time
Definition: BESCache3.h:43
virtual ~BESCache3()
Definition: BESCache3.h:113
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:713
Definition: BESCache3.h:40
virtual void lock_cache_write()
Get an exclusive lock on the 'cache info' file.
Definition: BESCache3.cc:587
unsigned long long size
Definition: BESCache3.h:42
string name
Definition: BESCache3.h:41
virtual void unlock_and_close(const string &target)
Unlock the named file.
Definition: BESCache3.cc:633
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:599
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:71
virtual unsigned long long update_cache_info(const string &target)
Update the cache info file to include 'target'.
Definition: BESCache3.cc:668
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:535
virtual bool get_read_lock(const string &target, int &fd)
Get a read-only lock on the file if it exists.
Definition: BESCache3.cc:507
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:472
virtual void update_and_purge(const string &new_file)
Purge files from the cache.
Definition: BESCache3.cc:811
virtual void unlock_cache()
Unlock the cache info file.
Definition: BESCache3.cc:613
virtual unsigned long long get_cache_size()
Get the cache size.
Definition: BESCache3.cc:725
static BESCache3 * get_instance()
Get an instance of the BESCache3 object.
Definition: BESCache3.cc:98