bes  Updated for version 3.20.6
BESContainerStorageList.cc
1 // BESContainerStorageList.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 <iostream>
34 
35 using std::endl;
36 using std::string;
37 using std::ostream;
38 
39 #include "BESContainerStorageList.h"
40 #include "BESContainerStorage.h"
41 #include "BESSyntaxUserError.h"
42 #include "BESContainer.h"
43 #include "TheBESKeys.h"
44 #include "BESLog.h"
45 #include "BESInfo.h"
46 
47 #include "BESDebug.h"
48 
49 BESContainerStorageList *BESContainerStorageList::_instance = 0;
50 
51 BESContainerStorageList::BESContainerStorageList() :
52  _first(0)
53 {
54 }
55 
56 BESContainerStorageList::~BESContainerStorageList()
57 {
58  BESContainerStorageList::persistence_list *pl = _first;
59  while (pl) {
60  if (pl->_persistence_obj) {
61  delete pl->_persistence_obj;
62  }
63  BESContainerStorageList::persistence_list *next = pl->_next;
64  delete pl;
65  pl = next;
66  }
67 }
68 
82 {
83  bool ret = false;
84  if (!_first) {
85  _first = new BESContainerStorageList::persistence_list;
86  _first->_persistence_obj = cp;
87  _first->_reference = 1;
88  _first->_next = 0;
89  ret = true;
90  }
91  else {
92  BESContainerStorageList::persistence_list *pl = _first;
93  bool done = false;
94  while (done == false) {
95  if (pl->_persistence_obj->get_name() != cp->get_name()) {
96  if (pl->_next) {
97  pl = pl->_next;
98  }
99  else {
100  pl->_next = new BESContainerStorageList::persistence_list;
101  pl->_next->_reference = 1;
102  pl->_next->_persistence_obj = cp;
103  pl->_next->_next = 0;
104  done = true;
105  ret = true;
106  }
107  }
108  else {
109  done = true;
110  ret = false;
111  }
112  }
113  }
114  return ret;
115 }
116 
127 bool BESContainerStorageList::ref_persistence(const string &persist_name)
128 {
129  bool ret = false;
130  BESContainerStorageList::persistence_list *pl = _first;
131 
132  bool done = false;
133  while (done == false) {
134  if (pl) {
135  if (pl->_persistence_obj && pl->_persistence_obj->get_name() == persist_name) {
136  done = true;
137  ret = true;
138  pl->_reference++;
139  }
140  else {
141  pl = pl->_next;
142  }
143  }
144  else {
145  done = true;
146  }
147  }
148  return ret;
149 }
150 
163 bool BESContainerStorageList::deref_persistence(const string &persist_name)
164 {
165  bool ret = false;
166  BESContainerStorageList::persistence_list *pl = _first;
167  BESContainerStorageList::persistence_list *last = 0;
168 
169  bool done = false;
170  while (done == false) {
171  if (pl) {
172  if (pl->_persistence_obj && pl->_persistence_obj->get_name() == persist_name) {
173  ret = true;
174  done = true;
175  pl->_reference--;
176  if (!pl->_reference) {
177  if (pl == _first) {
178  _first = _first->_next;
179  }
180  else {
181  if (!last)
182  throw BESInternalError("ContainerStorageList last is null", __FILE__, __LINE__);
183  last->_next = pl->_next;
184  }
185  delete pl->_persistence_obj;
186  delete pl;
187  pl = 0;
188  }
189  }
190  else {
191  last = pl;
192  pl = pl->_next;
193  }
194  }
195  else {
196  done = true;
197  }
198  }
199 
200  return ret;
201 }
202 
212 BESContainerStorageList::find_persistence(const string &persist_name)
213 {
214  BESContainerStorage *ret = NULL;
215  BESContainerStorageList::persistence_list *pl = _first;
216  bool done = false;
217  while (done == false) {
218  if (pl) {
219  if (persist_name == pl->_persistence_obj->get_name()) {
220  ret = pl->_persistence_obj;
221  done = true;
222  }
223  else {
224  pl = pl->_next;
225  }
226  }
227  else {
228  done = true;
229  }
230  }
231  return ret;
232 }
233 
234 bool BESContainerStorageList::isnice()
235 {
236  bool ret = false;
237  string key = "BES.Container.Persistence";
238  bool found = false;
239  string isnice;
240  TheBESKeys::TheKeys()->get_value(key, isnice, found);
241  if (isnice == "Nice" || isnice == "nice" || isnice == "NICE")
242  ret = true;
243  else
244  ret = false;
245  return ret;
246 }
247 
271 BESContainer *
272 BESContainerStorageList::look_for(const string &sym_name)
273 {
274  BESContainer *ret_container = 0;
275  BESContainerStorageList::persistence_list *pl = _first;
276  bool done = false;
277  while (done == false) {
278  if (pl) {
279  ret_container = pl->_persistence_obj->look_for(sym_name);
280  if (ret_container) {
281  done = true;
282  }
283  else {
284  pl = pl->_next;
285  }
286  }
287  else {
288  done = true;
289  }
290  }
291  if (!ret_container) {
292  if (isnice()) {
293  LOG("Could not find the symbolic name " << sym_name << endl);
294  }
295  else {
296  string s = (string) "Could not find the symbolic name " + sym_name;
297  throw BESSyntaxUserError(s, __FILE__, __LINE__);
298  }
299  }
300 
301  return ret_container;
302 }
303 
318 void
319 BESContainerStorageList::delete_container(const std::string &sym_name)
320 {
321  BESContainerStorageList::persistence_list *pl = _first;
322  while (pl) {
323  (void) pl->_persistence_obj->del_container(sym_name);
324 
325  pl = pl->_next;
326  }
327 }
328 
342 {
343  BESContainerStorageList::persistence_list *pl = _first;
344  while (pl) {
345  std::map<string, string> props;
346  props["name"] = pl->_persistence_obj->get_name();
347  info.begin_tag("store", &props);
348  pl->_persistence_obj->show_containers(info);
349  info.end_tag("store");
350  pl = pl->_next;
351  }
352 }
353 
361 void BESContainerStorageList::dump(ostream &strm) const
362 {
363  strm << BESIndent::LMarg << "BESContainerStorageList::dump - (" << (void *) this << ")" << endl;
364  BESIndent::Indent();
365  BESContainerStorageList::persistence_list *pl = _first;
366  if (pl) {
367  strm << BESIndent::LMarg << "container storage:" << endl;
368  BESIndent::Indent();
369  while (pl) {
370  pl->_persistence_obj->dump(strm);
371  pl = pl->_next;
372  }
373  BESIndent::UnIndent();
374  }
375  else {
376  strm << BESIndent::LMarg << "container storage: empty" << endl;
377  }
378  BESIndent::UnIndent();
379 }
380 
382 BESContainerStorageList::TheList()
383 {
384  if (_instance == 0) {
385  _instance = new BESContainerStorageList;
386  }
387  return _instance;
388 }
389 
BESContainerStorageList::show_containers
virtual void show_containers(BESInfo &info)
show information for each container in each persistence store
Definition: BESContainerStorageList.cc:341
BESContainerStorageList::deref_persistence
virtual bool deref_persistence(const std::string &persist_name)
dereference a persistent store in the list.
Definition: BESContainerStorageList.cc:163
BESContainerStorageList::look_for
virtual BESContainer * look_for(const std::string &sym_name)
look for the specified container information in the list of persistent stores.
Definition: BESContainerStorageList.cc:272
BESContainerStorage
provides persistent storage for data storage information represented by a container.
Definition: BESContainerStorage.h:67
BESContainerStorageList::dump
virtual void dump(std::ostream &strm) const
dumps information about this object
Definition: BESContainerStorageList.cc:361
BESInfo
informational response object
Definition: BESInfo.h:63
BESContainerStorageList::add_persistence
virtual bool add_persistence(BESContainerStorage *p)
Add a persistent store to the list.
Definition: BESContainerStorageList.cc:81
BESContainerStorage::get_name
virtual const std::string & get_name() const
retrieve the name of this persistent store
Definition: BESContainerStorage.h:91
TheBESKeys::TheKeys
static TheBESKeys * TheKeys()
Definition: TheBESKeys.cc:62
BESSyntaxUserError
error thrown if there is a user syntax error in the request or any other user error
Definition: BESSyntaxUserError.h:41
BESContainerStorageList::find_persistence
virtual BESContainerStorage * find_persistence(const std::string &persist_name)
find the persistence store with the given name
Definition: BESContainerStorageList.cc:212
BESContainerStorageList::delete_container
virtual void delete_container(const std::string &sym_name)
scan all of the container stores and remove any containers called
Definition: BESContainerStorageList.cc:319
BESInternalError
exception thrown if internal error encountered
Definition: BESInternalError.h:43
TheBESKeys::get_value
void get_value(const std::string &s, std::string &val, bool &found)
Retrieve the value of a given key, if set.
Definition: TheBESKeys.cc:272
BESContainer
A container is something that holds data. E.G., a netcdf file or a database entry.
Definition: BESContainer.h:65
BESContainerStorageList
Provides a mechanism for accessing container information from different container stores registered w...
Definition: BESContainerStorageList.h:71
BESContainerStorageList::ref_persistence
virtual bool ref_persistence(const std::string &persist_name)
refence the specified persistent store if in the list
Definition: BESContainerStorageList.cc:127