bes  Updated for version 3.20.6
CatalogNode.cc
1 // -*- mode: c++; c-basic-offset:4 -*-
2 
3 // This file is part of the OPeNDAP Back-End Server (BES)
4 
5 // Copyright (c) 2018 OPeNDAP, Inc.
6 // Author: James Gallagher <jgallagher@opendap.org>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 //
22 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
23 
24 #include "config.h"
25 
26 #include <cassert>
27 
28 #include <string>
29 #include <ostream>
30 #include <sstream>
31 
32 #include "BESIndent.h"
33 #include "BESUtil.h"
34 
35 #include "BESCatalogList.h"
36 #include "BESInfo.h"
37 #include "CatalogNode.h"
38 #include "CatalogItem.h"
39 
40 using namespace bes;
41 using namespace std;
42 
43 CatalogNode::~CatalogNode()
44 {
45 #if ITEMS
46  for (std::vector<CatalogItem*>::iterator i = d_items.begin(), e = d_items.end(); i != e; ++i)
47  delete *i;
48  d_items.clear();
49 #endif
50 
51 #if NODES_AND_LEAVES
52  for (std::vector<CatalogItem*>::iterator i = d_nodes.begin(), e = d_nodes.end(); i != e; ++i)
53  delete *i;
54  d_nodes.clear();
55 
56  for (std::vector<CatalogItem*>::iterator i = d_leaves.begin(), e = d_leaves.end(); i != e; ++i)
57  delete *i;
58  d_leaves.clear();
59 #endif
60  delete d_no_really_im_a_leaf;
61  d_no_really_im_a_leaf = 0;;
62 }
63 
78 void
80 {
81  map<string, string> props;
82 
83  // The node may actually be a leaf. Check and act accordingly.
84  CatalogItem *im_a_leaf = get_leaf();
85  if(im_a_leaf){
86  im_a_leaf->encode_item(info);
87  }
88  else { // It's a node. Do the node dance...
89  if(get_catalog_name() != BESCatalogList::TheCatalogList()->default_catalog_name())
90  props["name"] = BESUtil::assemblePath(get_catalog_name(), get_name(), true);
91  else
92  props["name"] = get_name();
93 
94  // props["catalog"] = get_catalog_name();
95  props["lastModified"] = get_lmt();
96  ostringstream oss;
97  oss << get_item_count();
98  props["count"] = oss.str();
99 
100  info->begin_tag("node", &props);
101 
102  // Depth-first node traversal. Assume the nodes and leaves are sorted.
103  // Write the nodes first.
104  for (CatalogNode::item_citer i = nodes_begin(), e = nodes_end(); i != e; ++i) {
105  assert((*i)->get_type() == CatalogItem::node);
106  (*i)->encode_item(info);
107  }
108 
109  // then leaves
110  for (CatalogNode::item_citer i = leaves_begin(), e = leaves_end(); i != e; ++i) {
111  assert((*i)->get_type() == CatalogItem::leaf);
112  (*i)->encode_item(info);
113  }
114  info->end_tag("node");
115  }
116 }
117 
118 
123 void CatalogNode::dump(ostream &strm) const
124 {
125  strm << BESIndent::LMarg << "CatalogNode::dump - (" << (void *) this << ")" << endl;
126  BESIndent::Indent();
127 
128  strm << BESIndent::LMarg << "name: " << d_name << endl;
129  strm << BESIndent::LMarg << "catalog_name: " << d_catalog_name << endl;
130  strm << BESIndent::LMarg << "last modified time: " << d_lmt<< endl;
131 
132 
133  if(d_no_really_im_a_leaf){
134  d_no_really_im_a_leaf->dump(strm);
135  }
136  else {
137 
138  #if ITEMS
139  strm << BESIndent::LMarg << "item count: " << d_items.size() << endl;
140  #endif
141 
142 #if NODES_AND_LEAVES
143  strm << BESIndent::LMarg << "item count: " << d_nodes.size() + d_leaves.size() << endl;
144 #endif
145 
146  strm << BESIndent::LMarg << "items: ";
147  if (d_nodes.size() + d_leaves.size()) {
148  strm << endl;
149  BESIndent::Indent();
150 #if ITEMS
151  vector<CatalogItem*>::const_iterator i = d_items.begin();
152  vector<CatalogItem*>::const_iterator e = d_items.end();
153  for (; i != e; ++i) {
154  strm << BESIndent::LMarg << (*i) << endl;
155  }
156 #endif
157 #if NODES_AND_LEAVES
158  vector<CatalogItem*>::const_iterator i = d_nodes.begin();
159  vector<CatalogItem*>::const_iterator e = d_nodes.end();
160  for (; i != e; ++i) {
161  strm << BESIndent::LMarg << (*i) << endl;
162  }
163 
164  i = d_leaves.begin();
165  e = d_leaves.end();
166  for (; i != e; ++i) {
167  strm << BESIndent::LMarg << (*i) << endl;
168  }
169 #endif
170  BESIndent::UnIndent();
171  }
172  }
173  BESIndent::UnIndent();
174 }
bes::CatalogNode::encode_node
void encode_node(BESInfo *info)
Encode this CatalogNode in an info object.
Definition: CatalogNode.cc:79
bes::CatalogItem::encode_item
void encode_item(BESInfo *info)
Encode this CatalogItem in an info object.
Definition: CatalogItem.cc:53
BESInfo
informational response object
Definition: BESInfo.h:63
BESUtil::assemblePath
static std::string assemblePath(const std::string &firstPart, const std::string &secondPart, bool leadingSlash=false, bool trailingSlash=false)
Assemble path fragments making sure that they are separated by a single '/' character.
Definition: BESUtil.cc:821
BESCatalogList::TheCatalogList
static BESCatalogList * TheCatalogList()
Get the singleton BESCatalogList instance.
Definition: BESCatalogList.cc:81
bes::CatalogItem
Definition: CatalogItem.h:72
bes::CatalogNode::dump
virtual void dump(std::ostream &strm) const
Definition: CatalogNode.cc:123