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