bes  Updated for version 3.20.10
BESUncompressManager3.cc
1 // BESUncompressManager3.cc
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
9 // Jose Garcia <jgarcia@ucar.edu>
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 //
25 // You can contact University Corporation for Atmospheric Research at
26 // 3080 Center Green Drive, Boulder, CO 80301
27 
28 #include "config.h"
29 
30 #include <mutex>
31 
32 #include <sstream>
33 
34 using std::istringstream;
35 using std::endl;
36 using std::ostream;
37 using std::string;
38 
39 #include "BESUncompressManager3.h"
40 #include "BESUncompress3GZ.h"
41 #include "BESUncompress3BZ2.h"
42 #include "BESUncompress3Z.h"
43 
44 #include "BESFileLockingCache.h"
45 
46 #include "BESInternalError.h"
47 #include "BESDebug.h"
48 
49 #include "TheBESKeys.h"
50 
51 BESUncompressManager3 *BESUncompressManager3::d_instance = nullptr;
52 static std::once_flag d_euc_init_once;
53 
64 {
68 }
69 
70 BESUncompressManager3::~BESUncompressManager3() {}
71 
81 bool BESUncompressManager3::add_method(const string &name, p_bes_uncompress method)
82 {
83  std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
84 
85  BESUncompressManager3::UCIter i;
86  i = _uncompress_list.find(name);
87  if (i == _uncompress_list.end()) {
88  _uncompress_list[name] = method;
89  return true;
90  }
91  return false;
92 }
93 
102 p_bes_uncompress BESUncompressManager3::find_method(const string &name)
103 {
104  std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
105 
106  BESUncompressManager3::UCIter i;
107  i = _uncompress_list.find(name);
108  if (i != _uncompress_list.end()) {
109  return (*i).second;
110  }
111  return 0;
112 }
113 
146 bool BESUncompressManager3::uncompress(const string &src, string &cache_file, BESFileLockingCache *cache)
147 {
148  std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
149 
150  BESDEBUG( "uncompress2", "BESUncompressManager3::uncompress() - src: " << src << endl );
151 
162  if (cache == NULL) {
163  std::ostringstream oss;
164  oss << "BESUncompressManager3::" << __func__ << "() - ";
165  oss << "The supplied Cache object is NULL. Decompression Requires An Operational Cache.";
166  throw BESInternalError(oss.str(), __FILE__, __LINE__);
167  }
168 
169  // All compressed files have a 'dot extension'.
170  string::size_type dot = src.rfind(".");
171  if (dot == string::npos) {
172  BESDEBUG( "uncompress2", "BESUncompressManager3::uncompress() - no file extension" << endl );
173  return false;
174  }
175 
176  string ext = src.substr(dot + 1, src.length() - dot);
177 
178  // If there's no match for the extension, the file is not compressed and we return false.
179  // Otherwise, 'p' points to a function that uncompresses the data.
180  p_bes_uncompress p = find_method(ext);
181  if (!p) {
182  BESDEBUG( "uncompress2", "BESUncompressManager3::uncompress() - not compressed " << endl );
183  return false;
184  }
185 
186  // Get the name of the file in the cache (either the code finds this file or
187  // or it makes it).
188  cache_file = cache->get_cache_file_name(src);
189 
190  try {
191  BESDEBUG( "uncompress2", "BESUncompressManager3::uncompress() - is cached? " << src << endl );
192 
193  int fd;
194  if (cache->get_read_lock(cache_file, fd)) {
195  BESDEBUG( "uncompress", "BESUncompressManager3::uncompress() - cached hit: " << cache_file << endl );
196  return true;
197  }
198 
199  // Now we actually try to uncompress the file, given that there's not a decomp'd version
200  // in the cache. First make an empty file and get an exclusive lock on it.
201  if (cache->create_and_lock(cache_file, fd)) {
202  BESDEBUG( "uncompress", "BESUncompressManager3::uncompress() - caching " << cache_file << endl );
203 
204  // uncompress. Make sure that the decompression function does not close
205  // the file descriptor.
206  p(src, fd);
207 
208  // Change the exclusive lock on the new file to a shared lock. This keeps
209  // other processes from purging the new file and ensures that the reading
210  // process can use it.
211  cache->exclusive_to_shared_lock(fd);
212 
213  // Now update the total cache size info and purge if needed. The new file's
214  // name is passed into the purge method because this process cannot detect its
215  // own lock on the file.
216  unsigned long long size = cache->update_cache_info(cache_file);
217  if (cache->cache_too_big(size))
218  cache->update_and_purge(cache_file);
219 
220  return true;
221  }
222  else {
223  if (cache->get_read_lock(cache_file, fd)) {
224  BESDEBUG( "uncompress", "BESUncompressManager3::uncompress() - cached hit: " << cache_file << endl );
225  return true;
226  }
227  }
228 
229  return false;
230  }
231  catch (...) {
232  BESDEBUG( "uncompress", "BESUncompressManager3::uncompress() - caught exception, unlocking cache and re-throw." << endl );
233  cache->unlock_cache();
234  throw;
235  }
236 
237  return false; // gcc warns without this
238 }
239 
247 void BESUncompressManager3::dump(ostream &strm) const
248 {
249  std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
250 
251  strm << BESIndent::LMarg << "BESUncompressManager3::dump - (" << (void *) this << ")" << endl;
252  BESIndent::Indent();
253  if (_uncompress_list.size()) {
254  strm << BESIndent::LMarg << "registered uncompression methods:" << endl;
255  BESIndent::Indent();
256  BESUncompressManager3::UCIter i = _uncompress_list.begin();
257  BESUncompressManager3::UCIter ie = _uncompress_list.end();
258  for (; i != ie; i++) {
259  strm << BESIndent::LMarg << (*i).first << endl;
260  }
261  BESIndent::UnIndent();
262  }
263  else {
264  strm << BESIndent::LMarg << "registered uncompress methods: none" << endl;
265  }
266  BESIndent::UnIndent();
267 }
268 
270 BESUncompressManager3::TheManager()
271 {
272  std::call_once(d_euc_init_once,BESUncompressManager3::initialize_instance);
273  return d_instance;
274 }
275 
276 void BESUncompressManager3::initialize_instance() {
277  d_instance = new BESUncompressManager3;
278 #ifdef HAVE_ATEXIT
279  atexit(delete_instance);
280 #endif
281 }
282 
283 void BESUncompressManager3::delete_instance() {
284  delete d_instance;
285  d_instance = 0;
286 }
Implementation of a caching mechanism for compressed data.
virtual unsigned long long update_cache_info(const std::string &target)
Update the cache info file to include 'target'.
virtual bool create_and_lock(const std::string &target, int &fd)
Create a file in the cache and lock it for write access.
virtual void exclusive_to_shared_lock(int fd)
Transfer from an exclusive lock to a shared lock.
virtual bool get_read_lock(const std::string &target, int &fd)
Get a read-only lock on the file if it exists.
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.
virtual void update_and_purge(const std::string &new_file)
Purge files from the cache.
virtual std::string get_cache_file_name(const std::string &src, bool mangle=true)
exception thrown if internal error encountered
static void uncompress(const std::string &src, int fd)
uncompress a file with the .bz2 file extension
static void uncompress(const std::string &src, int dest_fd)
uncompress a file with the .gz file extension
static void uncompress(const std::string &src, int fd)
uncompress a file with the .gz file extension
List of all registered decompression methods.
BESUncompressManager3()
constructs an uncompression manager adding gz, z, and bz2 uncompression methods by default.
virtual bool add_method(const std::string &name, p_bes_uncompress method)
create_and_lock a uncompress method to the list
virtual p_bes_uncompress find_method(const std::string &name)
returns the uncompression method specified
virtual bool uncompress(const std::string &src, std::string &target, BESFileLockingCache *cache)
If the file 'src' should be uncompressed, do so and return a new file name on the value-result param ...
virtual void dump(std::ostream &strm) const
dumps information about this object