OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESCache3.cc
Go to the documentation of this file.
1 // BESCache3.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 Jose Garcia <jgarcia@ucar.edu>
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 //
24 // You can contact University Corporation for Atmospheric Research at
25 // 3080 Center Green Drive, Boulder, CO 80301
26 
27 #include "config.h"
28 
29 #include <string>
30 #include <sstream>
31 
32 #include "BESCache3.h"
33 
34 #include "BESSyntaxUserError.h"
35 #include "BESInternalError.h"
36 
37 #include "TheBESKeys.h"
38 #include "BESDebug.h"
39 #include "BESLog.h"
40 
41 using namespace std;
42 
43 BESCache3 *BESCache3::d_instance = 0;
44 
45 // The BESCache3 code is a singleton that assumes it's running in the absence of threads but that
46 // the cache is shared by several processes, each of which have their own instance of BESCache3.
58 BESCache3 *
59 BESCache3::get_instance(BESKeys *keys, const string &cache_dir_key, const string &prefix_key, const string &size_key)
60 {
61  if (d_instance == 0)
62  d_instance = new BESCache3(keys, cache_dir_key, prefix_key, size_key);
63 
64  return d_instance;
65 }
66 
70 BESCache3 *
71 BESCache3::get_instance(const string &cache_dir, const string &prefix, unsigned long size)
72 {
73  if (d_instance == 0)
74  d_instance = new BESCache3(cache_dir, prefix, size);
75 
76  return d_instance;
77 }
78 
82 BESCache3 *
84 {
85  if (d_instance == 0)
86  throw BESInternalError("Tried to get the BESCache3 instance, but it hasn't been created yet", __FILE__, __LINE__);
87 
88  return d_instance;
89 }
90 
105 BESCache3::BESCache3(BESKeys *keys, const string &cache_dir_key, const string &prefix_key, const string &size_key)
106 {
107  bool found = false;
108  string cache_dir;
109  keys->get_value(cache_dir_key, cache_dir, found);
110  if (!found)
111  throw BESSyntaxUserError("The cache directory key " + cache_dir_key + " was not found in the BES configuration file", __FILE__, __LINE__);
112 
113  found = false;
114  string prefix;
115  keys->get_value(prefix_key, prefix, found);
116  if (!found)
117  throw BESSyntaxUserError("The prefix key " + prefix_key + " was not found in the BES configuration file", __FILE__, __LINE__);
118 
119  found = false;
120  string cache_size_str;
121  keys->get_value(size_key, cache_size_str, found);
122  if (!found)
123  throw BESSyntaxUserError("The size key " + size_key + " was not found in the BES configuration file", __FILE__, __LINE__);
124 
125  std::istringstream is(cache_size_str);
126  unsigned long long max_cache_size_in_bytes;
127  is >> max_cache_size_in_bytes;
128 
129  initialize(cache_dir, prefix, max_cache_size_in_bytes);
130 }
131 
139 void BESCache3::dump(ostream &strm) const
140 {
141  strm << BESIndent::LMarg << "BESCache3::dump - (" << (void *) this << ")" << endl;
142 }
143 
virtual void dump(ostream &strm) const
dumps information about this object
Definition: BESCache3.cc:139
exception thrown if inernal error encountered
BESCache3()
Definition: BESCache3.h:66
STL namespace.
error thrown if there is a user syntax error in the request or any other user error ...
mapping of key/value pairs defining different behaviors of an application.
Definition: BESKeys.h:84
Implementation of a caching mechanism for compressed data.
Definition: BESCache3.h:60
static ostream & LMarg(ostream &strm)
Definition: BESIndent.cc:73
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
Definition: BESKeys.cc:453
static BESCache3 * get_instance()
Get an instance of the BESCache3 object.
Definition: BESCache3.cc:83