OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESUncompressManager.cc
Go to the documentation of this file.
1 // BESUncompressManager.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) 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 #include <sstream>
34 
35 using std::istringstream ;
36 
37 #include "BESUncompressManager.h"
38 #include "BESUncompressGZ.h"
39 #include "BESUncompressBZ2.h"
40 #include "BESUncompressZ.h"
41 #include "BESCache.h"
42 #include "BESInternalError.h"
43 #include "BESDebug.h"
44 #include "TheBESKeys.h"
45 
46 BESUncompressManager *BESUncompressManager::_instance = 0 ;
47 
58 {
62 
63  bool found = false ;
64  string key = "BES.Uncompress.Retry" ;
65  string val ;
66  TheBESKeys::TheKeys()->get_value( key, val, found ) ;
67  if( !found || val.empty() )
68  {
69  _retry = 2000 ;
70  }
71  else
72  {
73  istringstream is( val ) ;
74  is >> _retry ;
75  }
76 
77  key = "BES.Uncompress.NumTries" ;
78  val = "" ;
79  TheBESKeys::TheKeys()->get_value( key, val, found ) ;
80  if( !found || val.empty() )
81  {
82  _num_tries = 10 ;
83  }
84  else
85  {
86  istringstream is( val ) ;
87  is >> _num_tries ;
88  }
89 }
90 
100 bool
102  p_bes_uncompress method )
103 {
104  BESUncompressManager::UCIter i ;
105  i = _uncompress_list.find( name ) ;
106  if( i == _uncompress_list.end() )
107  {
108  _uncompress_list[name] = method ;
109  return true ;
110  }
111  return false ;
112 }
113 
122 bool
124 {
125  BESUncompressManager::UIter i ;
126  i = _uncompress_list.find( name ) ;
127  if( i != _uncompress_list.end() )
128  {
129  _uncompress_list.erase( i ) ;
130  return true ;
131  }
132  return false ;
133 }
134 
145 {
146  BESUncompressManager::UCIter i ;
147  i = _uncompress_list.find( name ) ;
148  if( i != _uncompress_list.end() )
149  {
150  return (*i).second ;
151  }
152  return 0 ;
153 }
154 
160 string
162 {
163  string ret ;
164  bool first_name = true ;
165  BESUncompressManager::UCIter i = _uncompress_list.begin() ;
166  for( ; i != _uncompress_list.end(); i++ )
167  {
168  if( !first_name )
169  ret += ", " ;
170  ret += (*i).first ;
171  first_name = false ;
172  }
173  return ret ;
174 }
175 
212 bool
213 BESUncompressManager::uncompress( const string &src, string &target,
214  BESCache &cache )
215 {
216  BESDEBUG( "bes", "BESUncompressManager::uncompress - src = " << src << endl ) ;
217  string::size_type dot = src.rfind( "." ) ;
218  if( dot != string::npos )
219  {
220  string ext = src.substr( dot+1, src.length() - dot ) ;
221  // Why fold the extension to lowercase? jhrg 5/9/07
222  // The extension (Z, gz, bz2, GZ, BZ2, z) is used to determine which
223  // uncompression engine to use. It is compared to the list, which is
224  // all lower case. pcw 2/22/08
225  for( int i = 0; i < static_cast<int>(ext.length()); i++ )
226  {
227  ext[i] = tolower( ext[i] ) ;
228  }
229 
230  // if we find the method for this file then use it. If we don't find
231  // it then assume that the file is not compressed and simply return
232  // the src file at the end of the method.
233  //
234  // Actually return false; return true if the file has been decompressed and return the
235  // cached (decompressed) file name in the value-result parameter 'target'. jhrg 3/21/12
236  p_bes_uncompress p = find_method( ext ) ;
237  if( p )
238  {
239  // the file is compressed so we either need to uncompress it or
240  // we need to tell if it is already cached. To do this, lock the
241  // cache so no one else can do anything
242  if( cache.lock( _retry, _num_tries ) )
243  {
244  try
245  {
246  // before calling uncompress on the file, see if the file
247  // has already been cached. If it has, then simply return
248  // the target, no need to cache.
249  BESDEBUG( "bes", "BESUncompressManager::uncompress - is cached? " << src << endl ) ;
250  if( cache.is_cached( src, target ) )
251  {
252  BESDEBUG( "bes", "BESUncompressManager::uncompress - " << "is cached " << target << endl ) ;
253  cache.unlock() ;
254  return true ;
255  }
256 
257  // the file is not cached, so we need to uncompress the
258  // file. First determine if there is enough space in
259  // the cache to uncompress the file
260  BESDEBUG( "bes", "BESUncompressManager::uncompress - " << "purging cache" << endl ) ;
261  cache.purge() ;
262 
263  // Now that we have some room ... uncompress the file
264  BESDEBUG( "bes", "BESUncompressManager::uncompress - "
265  << "uncompress to " << target
266  << " using " << ext << " uncompression"
267  << endl ) ;
268 
269  // we are now done in the cache, unlock it
270  cache.unlock() ;
271 
272  // MPJ: Is this safe to call after unlock?
273  // We just unlocked the cache before we
274  // decompress the file, so the get_read_lock call may fail
275  // for another call while this is occurring
276  // and spawn another decompress overwriting the other?
277  // Or will another coming along see the unfinished
278  // decompressed file and complain?
279  p( src, target ) ;
280  return true ;
281  }
282  catch( BESError & )
283  {
284  // a problem in the cache, unlock it and re-throw the
285  // exception
286  cache.unlock() ;
287  throw ;
288  }
289  catch( ... )
290  {
291  // an unknown problem in the cache, unlock it and throw a
292  // BES exception
293  cache.unlock() ;
294 #if 0
295  string err = (string)"Problem working with the cache, "
296  + "unknown error" ;
297 #endif
298  throw BESInternalError( "Problem working with the cache, unknown error", __FILE__,__LINE__);
299  }
300  }
301  else
302  {
303  string err = "Unable to lock the cache "
304  + cache.cache_dir() ;
305  throw BESInternalError( err, __FILE__, __LINE__ ) ;
306  }
307  }
308  else
309  {
310  BESDEBUG( "bes", "BESUncompressManager::uncompress - not compressed " << endl ) ;
311  }
312  }
313  else
314  {
315  BESDEBUG( "bes", "BESUncompressmanager::uncompress - not file extension" << endl ) ;
316 #if 0
317  // This could just mean that there is a README file here, so just
318  // return the src file name and let the system run its course.
319  string err = "Unable to determine type of file from "
320  + src ;
321  throw BESInternalError( err, __FILE__, __LINE__ ) ;
322 #endif
323  }
324 
325  return false ;
326 }
327 
335 void
336 BESUncompressManager::dump( ostream &strm ) const
337 {
338  strm << BESIndent::LMarg << "BESUncompressManager::dump - ("
339  << (void *)this << ")" << endl ;
341  if( _uncompress_list.size() )
342  {
343  strm << BESIndent::LMarg << "registered uncompression methods:" << endl;
345  BESUncompressManager::UCIter i = _uncompress_list.begin() ;
346  BESUncompressManager::UCIter ie = _uncompress_list.end() ;
347  for( ; i != ie; i++ )
348  {
349  strm << BESIndent::LMarg << (*i).first << endl ;
350  }
352  }
353  else
354  {
355  strm << BESIndent::LMarg << "registered uncompress methods: none" << endl ;
356  }
358 }
359 
362 {
363  if( _instance == 0 )
364  {
365  _instance = new BESUncompressManager ;
366  }
367  return _instance ;
368 }
static BESUncompressManager * TheManager()
static void uncompress(const string &src, const string &target)
uncompress a file with the .gz file extension
virtual bool remove_method(const string &name)
removes a uncompress method from the list
static void uncompress(const string &src, const string &target)
uncompress a file with the .bz2 file extension
exception thrown if inernal error encountered
virtual bool lock(unsigned int retry_ms, unsigned int num_tries)
lock the cache using a file lock
Definition: BESCache.cc:194
virtual void dump(ostream &strm) const
dumps information about this object
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
Implementation of a caching mechanism.
Definition: BESCache.h:58
static void Indent()
Definition: BESIndent.cc:38
List of all registered uncompress methods.
Abstract exception class for the BES with basic string message.
Definition: BESError.h:51
virtual bool uncompress(const string &src, string &target, BESCache &cache)
find the method that can uncompress the specified src and pass control to that method.
virtual string get_method_names()
returns the comma separated list of all uncompression methods currently registered.
static ostream & LMarg(ostream &strm)
Definition: BESIndent.cc:73
virtual bool add_method(const string &name, p_bes_uncompress method)
add a uncompress method to the list
virtual bool unlock()
unlock the cache
Definition: BESCache.cc:246
virtual p_bes_uncompress find_method(const string &name)
returns the uncompression method specified
static void uncompress(const string &src, const string &target)
uncompress a file with the .gz file extension
string cache_dir() const
Definition: BESCache.h:143
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
Definition: BESKeys.cc:453
virtual bool is_cached(const string &src, string &target)
Determine if the file specified by src is cached.
Definition: BESCache.cc:277
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
Definition: BESDebug.h:64
static void UnIndent()
Definition: BESIndent.cc:44
static BESKeys * TheKeys()
Definition: TheBESKeys.cc:48
void(* p_bes_uncompress)(const string &src, const string &target)
BESUncompressManager(void)
constructs an uncompression manager adding gz, z, and bz2 uncompression methods by default...