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  p_bes_uncompress p = find_method( ext ) ;
234  if( p )
235  {
236  // the file is compressed so we either need to uncompress it or
237  // we need to tell if it is already cached. To do this, lock the
238  // cache so no one else can do anything
239  if( cache.lock( _retry, _num_tries ) )
240  {
241  try
242  {
243  // before calling uncompress on the file, see if the file
244  // has already been cached. If it has, then simply return
245  // the target, no need to cache.
246  BESDEBUG( "bes", "BESUncompressManager::uncompress - is cached? " << src << endl ) ;
247  if( cache.is_cached( src, target ) )
248  {
249  BESDEBUG( "bes", "BESUncompressManager::uncompress - " << "is cached " << target << endl ) ;
250  cache.unlock() ;
251  return true ;
252  }
253 
254  // the file is not cached, so we need to uncompress the
255  // file. First determine if there is enough space in
256  // the cache to uncompress the file
257  BESDEBUG( "bes", "BESUncompressManager::uncompress - " << "purging cache" << endl ) ;
258  cache.purge() ;
259 
260  // Now that we have some room ... uncompress the file
261  BESDEBUG( "bes", "BESUncompressManager::uncompress - "
262  << "uncompress to " << target
263  << " using " << ext << " uncompression"
264  << endl ) ;
265 
266  // we are now done in the cache, unlock it
267  cache.unlock() ;
268 
269  // MPJ: Is this safe to call after unlock?
270  // We just unlocked the cache before we
271  // decompress the file, so the is_cached call may fail
272  // for another call while this is occurring
273  // and spawn another decompress overwriting the other?
274  // Or will another coming along see the unfinished
275  // decompressed file and complain?
276  p( src, target ) ;
277  return true ;
278  }
279  catch( BESError & )
280  {
281  // a problem in the cache, unlock it and re-throw the
282  // exception
283  cache.unlock() ;
284  throw ;
285  }
286  catch( ... )
287  {
288  // an unknown problem in the cache, unlock it and throw a
289  // BES exception
290  cache.unlock() ;
291  string err = (string)"Problem working with the cache, "
292  + "unknown error" ;
293  throw BESInternalError( err, __FILE__,__LINE__);
294  }
295  }
296  else
297  {
298  string err = "Unable to lock the cache "
299  + cache.cache_dir() ;
300  throw BESInternalError( err, __FILE__, __LINE__ ) ;
301  }
302  }
303  else
304  {
305  BESDEBUG( "bes", "BESUncompressManager::uncompress - not compressed " << endl ) ;
306  }
307  }
308  else
309  {
310  BESDEBUG( "bes", "BESUncompressmanager::uncompress - not file extension" << endl ) ;
311 #if 0
312  // This could just mean that there is a README file here, so just
313  // return the src file name and let the system run its course.
314  string err = "Unable to determine type of file from "
315  + src ;
316  throw BESInternalError( err, __FILE__, __LINE__ ) ;
317 #endif
318  }
319 
320  return false ;
321 }
322 
330 void
331 BESUncompressManager::dump( ostream &strm ) const
332 {
333  strm << BESIndent::LMarg << "BESUncompressManager::dump - ("
334  << (void *)this << ")" << endl ;
336  if( _uncompress_list.size() )
337  {
338  strm << BESIndent::LMarg << "registered uncompression methods:" << endl;
340  BESUncompressManager::UCIter i = _uncompress_list.begin() ;
341  BESUncompressManager::UCIter ie = _uncompress_list.end() ;
342  for( ; i != ie; i++ )
343  {
344  strm << BESIndent::LMarg << (*i).first << endl ;
345  }
347  }
348  else
349  {
350  strm << BESIndent::LMarg << "registered uncompress methods: none" << endl ;
351  }
353 }
354 
357 {
358  if( _instance == 0 )
359  {
360  _instance = new BESUncompressManager ;
361  }
362  return _instance ;
363 }