bes  Updated for version 3.20.8
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 <sstream>
31 
32 using std::istringstream;
33 using std::endl;
34 using std::ostream;
35 using std::string;
36 
37 #include "BESUncompressManager3.h"
38 #include "BESUncompress3GZ.h"
39 #include "BESUncompress3BZ2.h"
40 #include "BESUncompress3Z.h"
41 
42 #include "BESFileLockingCache.h"
43 
44 #include "BESInternalError.h"
45 #include "BESDebug.h"
46 
47 #include "TheBESKeys.h"
48 
49 BESUncompressManager3 *BESUncompressManager3::_instance = 0;
50 
60 BESUncompressManager3::BESUncompressManager3()
61 {
65 }
66 
76 bool BESUncompressManager3::add_method(const string &name, p_bes_uncompress method)
77 {
78  BESUncompressManager3::UCIter i;
79  i = _uncompress_list.find(name);
80  if (i == _uncompress_list.end()) {
81  _uncompress_list[name] = method;
82  return true;
83  }
84  return false;
85 }
86 
95 p_bes_uncompress BESUncompressManager3::find_method(const string &name)
96 {
97  BESUncompressManager3::UCIter i;
98  i = _uncompress_list.find(name);
99  if (i != _uncompress_list.end()) {
100  return (*i).second;
101  }
102  return 0;
103 }
104 
137 bool BESUncompressManager3::uncompress(const string &src, string &cache_file, BESFileLockingCache *cache)
138 {
139  BESDEBUG( "uncompress2", "BESUncompressManager3::uncompress() - src: " << src << endl );
140 
151  if (cache == NULL) {
152  std::ostringstream oss;
153  oss << "BESUncompressManager3::" << __func__ << "() - ";
154  oss << "The supplied Cache object is NULL. Decompression Requires An Operational Cache.";
155  throw BESInternalError(oss.str(), __FILE__, __LINE__);
156  }
157 
158  // All compressed files have a 'dot extension'.
159  string::size_type dot = src.rfind(".");
160  if (dot == string::npos) {
161  BESDEBUG( "uncompress2", "BESUncompressManager3::uncompress() - no file extension" << endl );
162  return false;
163  }
164 
165  string ext = src.substr(dot + 1, src.length() - dot);
166 
167  // If there's no match for the extension, the file is not compressed and we return false.
168  // Otherwise, 'p' points to a function that uncompresses the data.
169  p_bes_uncompress p = find_method(ext);
170  if (!p) {
171  BESDEBUG( "uncompress2", "BESUncompressManager3::uncompress() - not compressed " << endl );
172  return false;
173  }
174 
175  // Get the name of the file in the cache (either the code finds this file or
176  // or it makes it).
177  cache_file = cache->get_cache_file_name(src);
178 
179  try {
180  BESDEBUG( "uncompress2", "BESUncompressManager3::uncompress() - is cached? " << src << endl );
181 
182  int fd;
183  if (cache->get_read_lock(cache_file, fd)) {
184  BESDEBUG( "uncompress", "BESUncompressManager3::uncompress() - cached hit: " << cache_file << endl );
185  return true;
186  }
187 
188  // Now we actually try to uncompress the file, given that there's not a decomp'd version
189  // in the cache. First make an empty file and get an exclusive lock on it.
190  if (cache->create_and_lock(cache_file, fd)) {
191  BESDEBUG( "uncompress", "BESUncompressManager3::uncompress() - caching " << cache_file << endl );
192 
193  // uncompress. Make sure that the decompression function does not close
194  // the file descriptor.
195  p(src, fd);
196 
197  // Change the exclusive lock on the new file to a shared lock. This keeps
198  // other processes from purging the new file and ensures that the reading
199  // process can use it.
200  cache->exclusive_to_shared_lock(fd);
201 
202  // Now update the total cache size info and purge if needed. The new file's
203  // name is passed into the purge method because this process cannot detect its
204  // own lock on the file.
205  unsigned long long size = cache->update_cache_info(cache_file);
206  if (cache->cache_too_big(size))
207  cache->update_and_purge(cache_file);
208 
209  return true;
210  }
211  else {
212  if (cache->get_read_lock(cache_file, fd)) {
213  BESDEBUG( "uncompress", "BESUncompressManager3::uncompress() - cached hit: " << cache_file << endl );
214  return true;
215  }
216  }
217 
218  return false;
219  }
220  catch (...) {
221  BESDEBUG( "uncompress", "BESUncompressManager3::uncompress() - caught exception, unlocking cache and re-throw." << endl );
222  cache->unlock_cache();
223  throw;
224  }
225 
226  return false; // gcc warns without this
227 }
228 
236 void BESUncompressManager3::dump(ostream &strm) const
237 {
238  strm << BESIndent::LMarg << "BESUncompressManager3::dump - (" << (void *) this << ")" << endl;
239  BESIndent::Indent();
240  if (_uncompress_list.size()) {
241  strm << BESIndent::LMarg << "registered uncompression methods:" << endl;
242  BESIndent::Indent();
243  BESUncompressManager3::UCIter i = _uncompress_list.begin();
244  BESUncompressManager3::UCIter ie = _uncompress_list.end();
245  for (; i != ie; i++) {
246  strm << BESIndent::LMarg << (*i).first << endl;
247  }
248  BESIndent::UnIndent();
249  }
250  else {
251  strm << BESIndent::LMarg << "registered uncompress methods: none" << endl;
252  }
253  BESIndent::UnIndent();
254 }
255 
257 BESUncompressManager3::TheManager()
258 {
259  if (_instance == 0) {
260  _instance = new BESUncompressManager3;
261  }
262  return _instance;
263 }
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.
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