bes  Updated for version 3.20.10
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 "ArrayAggregationBase.h"
33 #include "NCMLDebug.h"
34 #include "BESDebug.h"
35 #include "BESStopWatch.h"
36 #include <libdap/Marshaller.h>
37 #include <libdap/ConstraintEvaluator.h>
38 
39 // BES debug channel we output to
40 static const string DEBUG_CHANNEL("agg_util");
41 
42 // Local flag for whether to print constraints, to help debugging
43 static const bool PRINT_CONSTRAINTS = false;
44 
45 //using libdap::Array;
46 
47 using namespace libdap;
48 
49 namespace agg_util {
50 ArrayAggregationBase::ArrayAggregationBase(const libdap::Array& proto, const AMDList& aggMembers,
51  std::auto_ptr<ArrayGetterInterface>& arrayGetter) :
52  Array(proto), _pSubArrayProto(static_cast<Array*>(const_cast<Array&>(proto).ptr_duplicate())),
53  _pArrayGetter(arrayGetter), _datasetDescs(aggMembers)
54 {
55 }
56 
58  Array(rhs), _pSubArrayProto(0) // duplicate() handles this
59  , _pArrayGetter(0) // duplicate() handles this
60  , _datasetDescs()
61 {
62  BESDEBUG(DEBUG_CHANNEL, "ArrayAggregationBase() copy ctor called!" << endl);
63  duplicate(rhs);
64 }
65 
66 /* virtual */
67 ArrayAggregationBase::~ArrayAggregationBase()
68 {
69  cleanup();
70 }
71 
72 ArrayAggregationBase&
73 ArrayAggregationBase::operator=(const ArrayAggregationBase& rhs)
74 {
75  if (this != &rhs) {
76  cleanup();
77  Array::operator=(rhs);
78  duplicate(rhs);
79  }
80  return *this;
81 }
82 
83 /* virtual */
84 ArrayAggregationBase*
86 {
87  return new ArrayAggregationBase(*this);
88 }
89 
90 /* virtual */
91 // In child classes we specialize the BaseType::serialize() method so that
92 // as data are read they are also set (using Marshaller::put_vector_part()).
93 // In those cases this method is actually not called. We keep this version
94 // so that code that depends on read() actually reading in all of the data
95 // will still work.
97 {
98  BESStopWatch sw;
99  if (BESDebug::IsSet(TIMING_LOG_KEY)) sw.start("ArrayAggregationBase::read", "");
100 
101  BESDEBUG_FUNC(DEBUG_CHANNEL, " function entered..." << endl);
102 
103  // Early exit if already done, avoid doing it twice!
104  if (read_p()) {
105  BESDEBUG_FUNC(DEBUG_CHANNEL, "read_p() set, early exit!");
106  return true;
107  }
108 
109  // Only continue if we are supposed to serialize this object at all.
110  if (!(send_p() || is_in_selection())) {
111  BESDEBUG_FUNC(DEBUG_CHANNEL, "Object not in output, skipping... name=" << name() << endl);
112  return true;
113  }
114 
115  if (PRINT_CONSTRAINTS) {
116  BESDEBUG_FUNC(DEBUG_CHANNEL, "Constraints on this Array are:" << endl);
117  printConstraints(*this);
118  }
119 
120  // call subclass impl
122 
123  if (PRINT_CONSTRAINTS) {
124  BESDEBUG_FUNC(DEBUG_CHANNEL, "After transfer, constraints on the member template Array are: " << endl);
126  }
127 
128  // Call the subclass specific algorithms to do the read
129  // and stream
131 
132  // Set the cache bit to avoid recomputing
133  set_read_p(true);
134  return true;
135 }
136 
137 const AMDList&
139 {
140  return _datasetDescs;
141 }
142 
144 
145 void ArrayAggregationBase::printConstraints(const Array& fromArray)
146 {
147  ostringstream oss;
148  AggregationUtil::printConstraints(oss, fromArray);
149  BESDEBUG(DEBUG_CHANNEL, "Constraints for Array: " << name() << ": " << oss.str() << endl);
150 }
151 
152 libdap::Array&
154 {
155  VALID_PTR(_pSubArrayProto.get());
156  return *(_pSubArrayProto.get());
157 }
158 
161 {
162  VALID_PTR(_pArrayGetter.get());
163  return *(_pArrayGetter.get());
164 }
165 
166 void ArrayAggregationBase::duplicate(const ArrayAggregationBase& rhs)
167 {
168  // Clone the template if it isn't null.
169  std::auto_ptr<Array> pTemplateClone(
170  ((rhs._pSubArrayProto.get()) ? (static_cast<Array*>(rhs._pSubArrayProto->ptr_duplicate())) : (0)));
171  _pSubArrayProto = pTemplateClone;
172 
173  // Clone the ArrayGetterInterface as well.
174  std::auto_ptr<ArrayGetterInterface> pGetterClone((rhs._pArrayGetter.get()) ? (rhs._pArrayGetter->clone()) : (0));
175  _pArrayGetter = pGetterClone;
176 
177  // full copy, will do the proper thing with refcounts.
178  _datasetDescs = rhs._datasetDescs;
179 }
180 
181 void ArrayAggregationBase::cleanup() throw ()
182 {
183  _datasetDescs.clear();
184  _datasetDescs.resize(0);
185 }
186 
187 /* virtual */
189 {
190  NCML_ASSERT_MSG(false, "** Unimplemented function: "
191  "ArrayAggregationBase::transferOutputConstraintsIntoGranuleTemplateHook(): "
192  "needs to be overridden and implemented in a base class.");
193 }
194 
195 /* virtual */
197 {
198  NCML_ASSERT_MSG(false, "** Unimplemented function: "
199  "ArrayAggregationBase::readConstrainedGranuleArraysAndAggregateData(): "
200  "needs to be overridden and implemented in a base class.");
201 }
202 
203 }
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()
ArrayAggregationBase(const libdap::Array &granuleProto, const AMDList &memberDatasets, std::auto_ptr< ArrayGetterInterface > &arrayGetter)
void printConstraints(const Array &fromArray)
const ArrayGetterInterface & getArrayGetterInterface() const
virtual ArrayAggregationBase * ptr_duplicate()
Helper class for temporarily hijacking an existing dhi to load a DDX response for one particular file...