OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESCache.h
Go to the documentation of this file.
00001 // BESCache.h
00002 
00003 // This file is part of bes, A C++ back-end server implementation framework
00004 // for the OPeNDAP Data Access Protocol.
00005 
00006 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
00007 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
00008 //
00009 // This library is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU Lesser General Public
00011 // License as published by the Free Software Foundation; either
00012 // version 2.1 of the License, or (at your option) any later version.
00013 // 
00014 // This library is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 // Lesser General Public License for more details.
00018 // 
00019 // You should have received a copy of the GNU Lesser General Public
00020 // License along with this library; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // You can contact University Corporation for Atmospheric Research at
00024 // 3080 Center Green Drive, Boulder, CO 80301
00025  
00026 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
00027 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
00028 //
00029 // Authors:
00030 //      pwest       Patrick West <pwest@ucar.edu>
00031 //      jgarcia     Jose Garcia <jgarcia@ucar.edu>
00032 
00033 #ifndef BESCache_h_
00034 #define BESCache_h_ 1
00035 
00036 #include <algorithm>
00037 #include <map>
00038 #include <string>
00039 #include <sstream>
00040 
00041 #include "BESObj.h"
00042 
00043 class BESKeys ;
00044 
00045 static const unsigned int MAX_LOCK_RETRY_MS = 5000;     // in microseconds
00046 static const unsigned int MAX_LOCK_TRIES = 16;
00047 
00058 class BESCache : public BESObj
00059 {
00060 public:
00061 
00063   struct cache_entry
00064   {
00065       string name ;
00066       unsigned long long size ;
00067   };
00068 
00071   typedef std::multimap<double, cache_entry, std::greater<double> > CacheFilesByAgeMap;
00072 
00074     struct CacheDirInfo
00075     {
00076       CacheDirInfo()
00077       : _total_cache_files_size(0ULL)
00078       , _num_files_in_cache(0ULL)
00079       , _contents()
00080       {}
00081 
00082       ~CacheDirInfo()
00083       {
00084         clear();
00085       }
00086 
00087       void clear()
00088       {
00089         _total_cache_files_size = 0ULL;
00090         _num_files_in_cache = 0ULL;
00091         _contents.clear();
00092       }
00093 
00094       unsigned long long get_avg_size() const
00095       {
00096         return ( (_num_files_in_cache > 0) ?
00097             ( _total_cache_files_size / _num_files_in_cache) :
00098             (0ULL) );
00099       }
00100 
00101       std::string toString() const
00102       {
00103         std::ostringstream oss;
00104         oss << "Numfiles: " << _num_files_in_cache << ""
00105             " Total size: " << _total_cache_files_size;
00106         return oss.str();
00107       }
00108 
00109       unsigned long long _total_cache_files_size;
00110       unsigned long long _num_files_in_cache;
00111       BESCache::CacheFilesByAgeMap _contents;
00112     };  // struct CacheDirInfo
00113 
00114 
00115 private:
00116     // slashes are replaced with this char to make cache file.
00117     static const char           BES_CACHE_CHAR = '#';
00118 
00119     string                      _cache_dir ;
00120     string                      _prefix ;
00121     unsigned long long          _cache_size_in_megs ;
00122     int                         _lock_fd ;
00123 
00124     void                        check_ctor_params();
00125                                 BESCache() {}
00126 public:
00127                                 BESCache( const string &cache_dir,
00128                                           const string &prefix,
00129                                           unsigned long long sizeInMegs ) ;
00130                                 BESCache( BESKeys &keys,
00131                                           const string &cache_dir_key,
00132                                           const string &prefix_key,
00133                                           const string &size_key ) ;
00134     virtual                     ~BESCache() {}
00135 
00136     virtual bool                lock( unsigned int retry_ms,
00137                                       unsigned int num_tries ) ;
00138     virtual bool                unlock() ;
00139 
00140     virtual bool                is_cached( const string &src, string &target ) ;
00141     virtual void                purge( ) ;
00142 
00143     string                      cache_dir( ) const { return _cache_dir ; }
00144     string                      match_prefix() const { return _prefix + BES_CACHE_CHAR; };
00145     string                      prefix( ) const { return _prefix ; }
00146     unsigned long long          cache_size( ) const  { return _cache_size_in_megs ; }
00147 
00148     void                        collect_cache_dir_info(
00149                                   BESCache::CacheDirInfo& cd_info //output
00150                                   ) const;
00151 
00152     virtual void                dump( ostream &strm ) const ;
00153 };
00154 
00155 #endif // BESCache_h_
00156