OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESCache.h
Go to the documentation of this file.
1 // BESCache.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) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #ifndef BESCache_h_
34 #define BESCache_h_ 1
35 
36 #include <algorithm>
37 #include <map>
38 #include <string>
39 #include <sstream>
40 
41 #include "BESObj.h"
42 
43 class BESKeys ;
44 
45 static const unsigned int MAX_LOCK_RETRY_MS = 5000; // in microseconds
46 static const unsigned int MAX_LOCK_TRIES = 16;
47 
58 class BESCache : public BESObj
59 {
60 public:
61 
63  struct cache_entry
64  {
65  string name ;
66  unsigned long long size ;
67  };
68 
71  typedef std::multimap<double, cache_entry, std::greater<double> > CacheFilesByAgeMap;
72 
74  struct CacheDirInfo
75  {
78  , _num_files_in_cache(0ULL)
79  , _contents()
80  {}
81 
83  {
84  clear();
85  }
86 
87  void clear()
88  {
90  _num_files_in_cache = 0ULL;
91  _contents.clear();
92  }
93 
94  unsigned long long get_avg_size() const
95  {
96  return ( (_num_files_in_cache > 0) ?
98  (0ULL) );
99  }
100 
101  std::string toString() const
102  {
103  std::ostringstream oss;
104  oss << "Numfiles: " << _num_files_in_cache << ""
105  " Total size: " << _total_cache_files_size;
106  return oss.str();
107  }
108 
109  unsigned long long _total_cache_files_size;
110  unsigned long long _num_files_in_cache;
112  }; // struct CacheDirInfo
113 
114 
115 private:
116  // slashes are replaced with this char to make cache file.
117  static const char BES_CACHE_CHAR = '#';
118 
119  string _cache_dir ;
120  string _prefix ;
121  unsigned long long _cache_size_in_megs ;
122  int _lock_fd ;
123 
124  void check_ctor_params();
125  BESCache() {}
126 public:
127  BESCache( const string &cache_dir,
128  const string &prefix,
129  unsigned long long sizeInMegs ) ;
130  BESCache( BESKeys &keys,
131  const string &cache_dir_key,
132  const string &prefix_key,
133  const string &size_key ) ;
134  virtual ~BESCache() {}
135 
136  virtual bool lock( unsigned int retry_ms,
137  unsigned int num_tries ) ;
138  virtual bool unlock() ;
139 
140  virtual bool is_cached( const string &src, string &target ) ;
141  virtual void purge( ) ;
142 
143  string cache_dir( ) const { return _cache_dir ; }
144  string match_prefix() const { return _prefix + BES_CACHE_CHAR; };
145  string prefix( ) const { return _prefix ; }
146  unsigned long long cache_size( ) const { return _cache_size_in_megs ; }
147 
149  BESCache::CacheDirInfo& cd_info //output
150  ) const;
151 
152  virtual void dump( ostream &strm ) const ;
153 };
154 
155 #endif // BESCache_h_
156 
unsigned long long cache_size() const
Definition: BESCache.h:146
virtual bool lock(unsigned int retry_ms, unsigned int num_tries)
lock the cache using a file lock
Definition: BESCache.cc:194
virtual void purge()
Check to see if the cache size exceeds the size specified in the constructor and purge older files un...
Definition: BESCache.cc:324
BESCache::CacheFilesByAgeMap _contents
Definition: BESCache.h:111
virtual void dump(ostream &strm) const
dumps information about this object
Definition: BESCache.cc:504
unsigned long long _total_cache_files_size
Definition: BESCache.h:109
std::multimap< double, cache_entry, std::greater< double > > CacheFilesByAgeMap
Sugar for the multimap of entries sorted with older files first.
Definition: BESCache.h:71
Implementation of a caching mechanism.
Definition: BESCache.h:58
for filename -> filesize map below
Definition: BESCache.h:63
Helper class for info on the cache directory.
Definition: BESCache.h:74
Base object for bes objects.
Definition: BESObj.h:52
unsigned long long get_avg_size() const
Definition: BESCache.h:94
void collect_cache_dir_info(BESCache::CacheDirInfo &cd_info) const
Definition: BESCache.cc:440
mapping of key/value pairs defining different behaviors of an application.
Definition: BESKeys.h:84
unsigned long long _num_files_in_cache
Definition: BESCache.h:110
virtual bool unlock()
unlock the cache
Definition: BESCache.cc:246
std::string toString() const
Definition: BESCache.h:101
string match_prefix() const
Definition: BESCache.h:144
virtual ~BESCache()
Definition: BESCache.h:134
string prefix() const
Definition: BESCache.h:145
string cache_dir() const
Definition: BESCache.h:143
virtual bool is_cached(const string &src, string &target)
Determine if the file specified by src is cached.
Definition: BESCache.cc:277
unsigned long long size
Definition: BESCache.h:66
string name
Definition: BESCache.h:65