bes  Updated for version 3.20.5
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 
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 BESContainerStorageList *BESContainerStorageList::_instance = 0;
48 
49 BESContainerStorageList::BESContainerStorageList() :
50  _first(0)
51 {
52 }
53 
54 BESContainerStorageList::~BESContainerStorageList()
55 {
56  BESContainerStorageList::persistence_list *pl = _first;
57  while (pl) {
58  if (pl->_persistence_obj) {
59  delete pl->_persistence_obj;
60  }
61  BESContainerStorageList::persistence_list *next = pl->_next;
62  delete pl;
63  pl = next;
64  }
65 }
66 
80 {
81  bool ret = false;
82  if (!_first) {
83  _first = new BESContainerStorageList::persistence_list;
84  _first->_persistence_obj = cp;
85  _first->_reference = 1;
86  _first->_next = 0;
87  ret = true;
88  }
89  else {
90  BESContainerStorageList::persistence_list *pl = _first;
91  bool done = false;
92  while (done == false) {
93  if (pl->_persistence_obj->get_name() != cp->get_name()) {
94  if (pl->_next) {
95  pl = pl->_next;
96  }
97  else {
98  pl->_next = new BESContainerStorageList::persistence_list;
99  pl->_next->_reference = 1;
100  pl->_next->_persistence_obj = cp;
101  pl->_next->_next = 0;
102  done = true;
103  ret = true;
104  }
105  }
106  else {
107  done = true;
108  ret = false;
109  }
110  }
111  }
112  return ret;
113 }
114 
125 bool BESContainerStorageList::ref_persistence(const string &persist_name)
126 {
127  bool ret = false;
128  BESContainerStorageList::persistence_list *pl = _first;
129 
130  bool done = false;
131  while (done == false) {
132  if (pl) {
133  if (pl->_persistence_obj && pl->_persistence_obj->get_name() == persist_name) {
134  done = true;
135  ret = true;
136  pl->_reference++;
137  }
138  else {
139  pl = pl->_next;
140  }
141  }
142  else {
143  done = true;
144  }
145  }
146  return ret;
147 }
148 
161 bool BESContainerStorageList::deref_persistence(const string &persist_name)
162 {
163  bool ret = false;
164  BESContainerStorageList::persistence_list *pl = _first;
165  BESContainerStorageList::persistence_list *last = 0;
166 
167  bool done = false;
168  while (done == false) {
169  if (pl) {
170  if (pl->_persistence_obj && pl->_persistence_obj->get_name() == persist_name) {
171  ret = true;
172  done = true;
173  pl->_reference--;
174  if (!pl->_reference) {
175  if (pl == _first) {
176  _first = _first->_next;
177  }
178  else {
179  if (!last)
180  throw BESInternalError("ContainerStorageList last is null", __FILE__, __LINE__);
181  last->_next = pl->_next;
182  }
183  delete pl->_persistence_obj;
184  delete pl;
185  pl = 0;
186  }
187  }
188  else {
189  last = pl;
190  pl = pl->_next;
191  }
192  }
193  else {
194  done = true;
195  }
196  }
197 
198  return ret;
199 }
200 
210 BESContainerStorageList::find_persistence(const string &persist_name)
211 {
212  BESContainerStorage *ret = NULL;
213  BESContainerStorageList::persistence_list *pl = _first;
214  bool done = false;
215  while (done == false) {
216  if (pl) {
217  if (persist_name == pl->_persistence_obj->get_name()) {
218  ret = pl->_persistence_obj;
219  done = true;
220  }
221  else {
222  pl = pl->_next;
223  }
224  }
225  else {
226  done = true;
227  }
228  }
229  return ret;
230 }
231 
232 bool BESContainerStorageList::isnice()
233 {
234  bool ret = false;
235  string key = "BES.Container.Persistence";
236  bool found = false;
237  string isnice;
238  TheBESKeys::TheKeys()->get_value(key, isnice, found);
239  if (isnice == "Nice" || isnice == "nice" || isnice == "NICE")
240  ret = true;
241  else
242  ret = false;
243  return ret;
244 }
245 
269 BESContainer *
270 BESContainerStorageList::look_for(const string &sym_name)
271 {
272  BESContainer *ret_container = 0;
273  BESContainerStorageList::persistence_list *pl = _first;
274  bool done = false;
275  while (done == false) {
276  if (pl) {
277  ret_container = pl->_persistence_obj->look_for(sym_name);
278  if (ret_container) {
279  done = true;
280  }
281  else {
282  pl = pl->_next;
283  }
284  }
285  else {
286  done = true;
287  }
288  }
289  if (!ret_container) {
290  if (isnice()) {
291  LOG("Could not find the symbolic name " << sym_name << endl);
292  }
293  else {
294  string s = (string) "Could not find the symbolic name " + sym_name;
295  throw BESSyntaxUserError(s, __FILE__, __LINE__);
296  }
297  }
298 
299  return ret_container;
300 }
301 
316 void
317 BESContainerStorageList::delete_container(const std::string &sym_name)
318 {
319  BESContainerStorageList::persistence_list *pl = _first;
320  while (pl) {
321  (void) pl->_persistence_obj->del_container(sym_name);
322 
323  pl = pl->_next;
324  }
325 }
326 
340 {
341  BESContainerStorageList::persistence_list *pl = _first;
342  while (pl) {
343  map<string, string> props;
344  props["name"] = pl->_persistence_obj->get_name();
345  info.begin_tag("store", &props);
346  pl->_persistence_obj->show_containers(info);
347  info.end_tag("store");
348  pl = pl->_next;
349  }
350 }
351 
359 void BESContainerStorageList::dump(ostream &strm) const
360 {
361  strm << BESIndent::LMarg << "BESContainerStorageList::dump - (" << (void *) this << ")" << endl;
362  BESIndent::Indent();
363  BESContainerStorageList::persistence_list *pl = _first;
364  if (pl) {
365  strm << BESIndent::LMarg << "container storage:" << endl;
366  BESIndent::Indent();
367  while (pl) {
368  pl->_persistence_obj->dump(strm);
369  pl = pl->_next;
370  }
371  BESIndent::UnIndent();
372  }
373  else {
374  strm << BESIndent::LMarg << "container storage: empty" << endl;
375  }
376  BESIndent::UnIndent();
377 }
378 
380 BESContainerStorageList::TheList()
381 {
382  if (_instance == 0) {
383  _instance = new BESContainerStorageList;
384  }
385  return _instance;
386 }
387 
BESContainerStorageList::show_containers
virtual void show_containers(BESInfo &info)
show information for each container in each persistence store
Definition: BESContainerStorageList.cc:339
BESContainerStorageList::deref_persistence
virtual bool deref_persistence(const std::string &persist_name)
dereference a persistent store in the list.
Definition: BESContainerStorageList.cc:161
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:270
BESContainerStorage
provides persistent storage for data storage information represented by a container.
Definition: BESContainerStorage.h:72
BESContainerStorageList::dump
virtual void dump(std::ostream &strm) const
dumps information about this object
Definition: BESContainerStorageList.cc:359
BESInfo
informational response object
Definition: BESInfo.h:68
BESContainerStorageList::add_persistence
virtual bool add_persistence(BESContainerStorage *p)
Add a persistent store to the list.
Definition: BESContainerStorageList.cc:79
BESContainerStorage::get_name
virtual const std::string & get_name() const
retrieve the name of this persistent store
Definition: BESContainerStorage.h:96
TheBESKeys::TheKeys
static TheBESKeys * TheKeys()
Definition: TheBESKeys.cc:61
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:210
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:317
BESInternalError
exception thrown if inernal 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:420
BESContainer
A container is something that holds data. E.G., a netcdf file or a database entry.
Definition: BESContainer.h:68
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:125