bes  Updated for version 3.20.13
ArrayAggregationBase.cc
1 // This file is part of the "NcML Module" project, a BES module designed
3 // to allow NcML files to be used to be used as a wrapper to add
4 // AIS to existing datasets of any format.
5 //
6 // Copyright (c) 2010 OPeNDAP, Inc.
7 // Author: Michael Johnson <m.johnson@opendap.org>
8 //
9 // For more information, please also see the main website: http://opendap.org/
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 //
25 // Please see the files COPYING and COPYRIGHT for more information on the GLPL.
26 //
27 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
29 
30 #include "config.h"
31 
32 #include <libdap/Marshaller.h>
33 #include <libdap/ConstraintEvaluator.h>
34 
35 #include "ArrayAggregationBase.h"
36 #include "NCMLDebug.h"
37 #include "BESDebug.h"
38 #include "BESStopWatch.h"
39 
40 // BES debug channel we output to
41 #define DEBUG_CHANNEL "agg_util"
42 
43 #if 0
44 // Local flag for whether to print constraints, to help debugging
45 static const bool PRINT_CONSTRAINTS = false;
46 #endif
47 
48 using namespace libdap;
49 using namespace std;
50 
51 namespace agg_util {
52 ArrayAggregationBase::ArrayAggregationBase(const libdap::Array& proto, AMDList aggMembers, unique_ptr<ArrayGetterInterface> arrayGetter) :
53  Array(proto), _pSubArrayProto(dynamic_cast<Array*>(const_cast<Array&>(proto).ptr_duplicate())),
54  _pArrayGetter(std::move(arrayGetter)), _datasetDescs(std::move(aggMembers))
55 {
56 }
57 
59  Array(rhs)// , _pSubArrayProto(0) // duplicate() handles this
60  //, _pArrayGetter(0) // duplicate() handles this
61  // , _datasetDescs()
62 {
63  BESDEBUG(DEBUG_CHANNEL, "ArrayAggregationBase() copy ctor called!" << endl);
64  duplicate(rhs);
65 }
66 
67 /* virtual */
68 ArrayAggregationBase::~ArrayAggregationBase()
69 {
70  cleanup();
71 }
72 
73 ArrayAggregationBase&
74 ArrayAggregationBase::operator=(const ArrayAggregationBase& rhs)
75 {
76  if (this != &rhs) {
77  cleanup();
78  Array::operator=(rhs);
79  duplicate(rhs);
80  }
81  return *this;
82 }
83 
84 /* virtual */
85 ArrayAggregationBase*
87 {
88  return new ArrayAggregationBase(*this);
89 }
90 
91 /* virtual */
92 // In child classes we specialize the BaseType::serialize() method so that
93 // as data are read they are also set (using Marshaller::put_vector_part()).
94 // In those cases this method is actually not called. We keep this version
95 // so that code that depends on read() actually reading in all of the data
96 // will still work.
98 {
99  BESStopWatch sw;
100  if (BESDebug::IsSet(TIMING_LOG_KEY)) sw.start("ArrayAggregationBase::read", "");
101 
102  BESDEBUG_FUNC(DEBUG_CHANNEL, " function entered..." << endl);
103 
104  // Early exit if already done, avoid doing it twice!
105  if (read_p()) {
106  BESDEBUG_FUNC(DEBUG_CHANNEL, "read_p() set, early exit!");
107  return true;
108  }
109 
110  // Only continue if we are supposed to serialize this object at all.
111  if (!(send_p() || is_in_selection())) {
112  BESDEBUG_FUNC(DEBUG_CHANNEL, "Object not in output, skipping... name=" << name() << endl);
113  return true;
114  }
115 
116 #if 0
117  if (PRINT_CONSTRAINTS) {
118  BESDEBUG_FUNC(DEBUG_CHANNEL, "Constraints on this Array are:" << endl);
119  printConstraints(*this);
120  }
121 #endif
122 
123  // call subclass impl
125 
126 #if 0
127  if (PRINT_CONSTRAINTS) {
128  BESDEBUG_FUNC(DEBUG_CHANNEL, "After transfer, constraints on the member template Array are: " << endl);
130  }
131 #endif
132 
133  // Call the subclass specific algorithms to do the read
134  // and stream
136 
137  // Set the cache bit to avoid recomputing
138  set_read_p(true);
139  return true;
140 }
141 
142 const AMDList&
144 {
145  return _datasetDescs;
146 }
147 
149 
150 void ArrayAggregationBase::printConstraints(const Array& fromArray)
151 {
152  ostringstream oss;
153  AggregationUtil::printConstraints(oss, fromArray);
154  BESDEBUG(DEBUG_CHANNEL, "Constraints for Array: " << name() << ": " << oss.str() << endl);
155 }
156 
157 libdap::Array&
159 {
160  VALID_PTR(_pSubArrayProto.get());
161  return *(_pSubArrayProto.get());
162 }
163 
166 {
167  VALID_PTR(_pArrayGetter.get());
168  return *(_pArrayGetter.get());
169 }
170 
171 void ArrayAggregationBase::duplicate(const ArrayAggregationBase& rhs)
172 {
173  // Clone the template if it isn't null.
174  _pSubArrayProto.reset(((rhs._pSubArrayProto.get()) ? (static_cast<Array*>(rhs._pSubArrayProto->ptr_duplicate())) : (nullptr)));
175 
176  // Clone the ArrayGetterInterface as well.
177  _pArrayGetter.reset(((rhs._pArrayGetter.get()) ? (rhs._pArrayGetter->clone()) : (nullptr)));
178 
179  // full copy, will do the proper thing with refcounts.
180  _datasetDescs = rhs._datasetDescs;
181 }
182 
183 void ArrayAggregationBase::cleanup() noexcept
184 {
185  _datasetDescs.clear();
186  _datasetDescs.resize(0);
187 }
188 
189 /* virtual */
191 {
192  NCML_ASSERT_MSG(false, "** Unimplemented function: "
193  "ArrayAggregationBase::transferOutputConstraintsIntoGranuleTemplateHook(): "
194  "needs to be overridden and implemented in a base class.");
195 }
196 
197 /* virtual */
199 {
200  NCML_ASSERT_MSG(false, "** Unimplemented function: "
201  "ArrayAggregationBase::readConstrainedGranuleArraysAndAggregateData(): "
202  "needs to be overridden and implemented in a base class.");
203 }
204 
205 }
static bool IsSet(const std::string &flagName)
see if the debug context flagName is set to true
Definition: BESDebug.h:168
virtual bool start(std::string name)
Definition: BESStopWatch.cc:67
static void printConstraints(std::ostream &os, const libdap::Array &fromArray)
const AMDList & getDatasetList() const
virtual void transferOutputConstraintsIntoGranuleTemplateHook()
virtual void readConstrainedGranuleArraysAndAggregateDataHook()
void printConstraints(const Array &fromArray)
ArrayAggregationBase * ptr_duplicate() override
const ArrayGetterInterface & getArrayGetterInterface() const
ArrayAggregationBase(const libdap::Array &granuleProto, AMDList memberDatasets, std::unique_ptr< ArrayGetterInterface > arrayGetter)
Helper class for temporarily hijacking an existing dhi to load a DDX response for one particular file...