bes  Updated for version 3.20.10
BESDefinitionStorageList.cc
1 // BESDefinitionStorageList.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 #include <mutex>
35 #include "config.h"
36 
37 #ifdef HAVE_STDLIB_H
38 #include <stdlib.h>
39 #endif
40 
41 using std::endl;
42 using std::string;
43 using std::ostream;
44 
45 #include "BESDefinitionStorageList.h"
46 #include "BESDefinitionStorage.h"
47 #include "BESDefine.h"
48 #include "BESInfo.h"
49 
50 BESDefinitionStorageList *BESDefinitionStorageList::d_instance = nullptr;
51 static std::once_flag d_euc_init_once;
52 
53 BESDefinitionStorageList::BESDefinitionStorageList() :
54  _first(0)
55 {
56 }
57 
58 BESDefinitionStorageList::~BESDefinitionStorageList()
59 {
60  BESDefinitionStorageList::persistence_list *pl = _first;
61  while (pl) {
62  if (pl->_persistence_obj) {
63  delete pl->_persistence_obj;
64  }
65  BESDefinitionStorageList::persistence_list *next = pl->_next;
66  delete pl;
67  pl = next;
68  }
69 }
70 
84 {
85  std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
86 
87  bool ret = false;
88  if (!_first) {
89  _first = new BESDefinitionStorageList::persistence_list;
90  _first->_persistence_obj = cp;
91  _first->_reference = 1;
92  _first->_next = 0;
93  ret = true;
94  }
95  else {
96  BESDefinitionStorageList::persistence_list *pl = _first;
97  bool done = false;
98  while (done == false) {
99  if (pl->_persistence_obj->get_name() != cp->get_name()) {
100  if (pl->_next) {
101  pl = pl->_next;
102  }
103  else {
104  pl->_next = new BESDefinitionStorageList::persistence_list;
105  pl->_next->_persistence_obj = cp;
106  pl->_next->_reference = 1;
107  pl->_next->_next = 0;
108  done = true;
109  ret = true;
110  }
111  }
112  else {
113  done = true;
114  ret = false;
115  }
116  }
117  }
118  return ret;
119 }
120 
129 bool BESDefinitionStorageList::ref_persistence(const string &persist_name)
130 {
131  std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
132 
133  bool ret = false;
134  BESDefinitionStorageList::persistence_list *pl = _first;
135 
136  bool done = false;
137  while (done == false) {
138  if (pl) {
139  if (pl->_persistence_obj && pl->_persistence_obj->get_name() == persist_name) {
140  ret = true;
141  done = true;
142  pl->_reference++;
143  }
144  else {
145  pl = pl->_next;
146  }
147  }
148  else {
149  done = true;
150  }
151  }
152 
153  return ret;
154 }
155 
166 bool BESDefinitionStorageList::deref_persistence(const string &persist_name)
167 {
168  std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
169 
170  bool ret = false;
171  BESDefinitionStorageList::persistence_list *pl = _first;
172  BESDefinitionStorageList::persistence_list *last = 0;
173 
174  bool done = false;
175  while (done == false) {
176  if (pl) {
177  if (pl->_persistence_obj && pl->_persistence_obj->get_name() == persist_name) {
178  ret = true;
179  done = true;
180  pl->_reference--;
181  if (!pl->_reference) {
182  if (pl == _first) {
183  _first = _first->_next;
184  }
185  else {
186  if (!last) throw BESInternalError("ContainerStorageList last is null", __FILE__, __LINE__);
187  last->_next = pl->_next;
188  }
189  delete pl->_persistence_obj;
190  delete pl;
191  pl = 0;
192  }
193  }
194  else {
195  last = pl;
196  pl = pl->_next;
197  }
198  }
199  else {
200  done = true;
201  }
202  }
203 
204  return ret;
205 }
206 
217 {
218  std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
219 
220  BESDefinitionStorage *ret = NULL;
221  BESDefinitionStorageList::persistence_list *pl = _first;
222  bool done = false;
223  while (done == false) {
224  if (pl) {
225  if (persist_name == pl->_persistence_obj->get_name()) {
226  ret = pl->_persistence_obj;
227  done = true;
228  }
229  else {
230  pl = pl->_next;
231  }
232  }
233  else {
234  done = true;
235  }
236  }
237  return ret;
238 }
239 
250 BESDefine *
251 BESDefinitionStorageList::look_for(const string &def_name)
252 {
253  std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
254 
255  BESDefine *ret_def = NULL;
256  BESDefinitionStorageList::persistence_list *pl = _first;
257  bool done = false;
258  while (done == false) {
259  if (pl) {
260  ret_def = pl->_persistence_obj->look_for(def_name);
261  if (ret_def) {
262  done = true;
263  }
264  else {
265  pl = pl->_next;
266  }
267  }
268  else {
269  done = true;
270  }
271  }
272  return ret_def;
273 }
274 
290 {
291  std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
292 
293  BESDefinitionStorageList::persistence_list *pl = _first;
294  bool first = true;
295  while (pl) {
296  if (!first) {
297  // separate each store with a blank line
298  info.add_break(1);
299  }
300  first = false;
301  std::map<string, string> props;
302  props["name"] = pl->_persistence_obj->get_name();
303  info.begin_tag("store", &props);
304  pl->_persistence_obj->show_definitions(info);
305  info.end_tag("store");
306  pl = pl->_next;
307  }
308 }
309 
317 void BESDefinitionStorageList::dump(ostream &strm) const
318 {
319  std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
320 
321  strm << BESIndent::LMarg << "BESDefinitionStorageList::dump - (" << (void *) this << ")" << endl;
322  BESIndent::Indent();
323  if (_first) {
324  strm << BESIndent::LMarg << "registered definition storage:" << endl;
325  BESIndent::Indent();
326  BESDefinitionStorageList::persistence_list *pl = _first;
327  while (pl) {
328  pl->_persistence_obj->dump(strm);
329  pl = pl->_next;
330  }
331  BESIndent::UnIndent();
332  }
333  else {
334  strm << BESIndent::LMarg << "registered definition storage: none" << endl;
335  }
336  BESIndent::UnIndent();
337 }
338 
340 BESDefinitionStorageList::TheList()
341 {
342  std::call_once(d_euc_init_once,BESDefinitionStorageList::initialize_instance);
343  return d_instance;
344 }
345 
346 void BESDefinitionStorageList::initialize_instance() {
347  d_instance = new BESDefinitionStorageList;
348 #ifdef HAVE_ATEXIT
349  atexit(delete_instance);
350 #endif
351 }
352 
353 void BESDefinitionStorageList::delete_instance() {
354  delete d_instance;
355  d_instance = 0;
356 }
357 
Provides a mechanism for accessing definitions from different definition stores registered with this ...
virtual BESDefinitionStorage * find_persistence(const std::string &persist_name)
find the persistence store with the given name
virtual bool add_persistence(BESDefinitionStorage *p)
Add a persistent store to the list.
virtual BESDefine * look_for(const std::string &def_name)
look for the specified definition in the list of defintion stores.
virtual bool deref_persistence(const std::string &persist_name)
de-reference a persistent store in the list
virtual bool ref_persistence(const std::string &persist_name)
reference a persistent store in the list
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual void show_definitions(BESInfo &info)
show information for each definition in each persistence store
provides persistent storage for a specific view of different containers including contraints and aggr...
virtual const std::string & get_name() const
retrieve the name of this persistent store
informational response object
Definition: BESInfo.h:63
exception thrown if internal error encountered