32 #include <Marshaller.h>
35 #include "BESStopWatch.h"
37 #include "ArrayJoinExistingAggregation.h"
39 #include "AggregationException.h"
40 #include "AggregationUtil.h"
41 #include "NCMLDebug.h"
43 static const string DEBUG_CHANNEL(NCML_MODULE_DBG_CHANNEL_2);
44 static const bool PRINT_CONSTRAINTS =
false;
48 #undef USE_LOCAL_TIMEOUT_SCHEME
53 const AMDList& memberDatasets, std::auto_ptr<ArrayGetterInterface>& arrayGetter,
const Dimension& joinDim) :
56 BESDEBUG_FUNC(DEBUG_CHANNEL,
"Making the aggregated outer dimension be: " + joinDim.
toString() +
"\n");
61 libdap::Array::dimension& rOuterDim = *(dim_begin());
62 NCML_ASSERT_MSG(rOuterDim.name == joinDim.name,
"The outer dimension name of this is not the expected "
63 "outer dimension name! Broken precondition: This ctor cannot be called "
64 "without this being true!");
65 rOuterDim.size = joinDim.size;
71 if (PRINT_CONSTRAINTS) {
75 BESDEBUG_FUNC(DEBUG_CHANNEL,
"Constrained Dims after set are: " + oss.str());
85 ArrayJoinExistingAggregation::~ArrayJoinExistingAggregation()
90 ArrayJoinExistingAggregation&
91 ArrayJoinExistingAggregation::operator=(
const ArrayJoinExistingAggregation& rhs)
95 ArrayAggregationBase::operator=(rhs);
102 ArrayJoinExistingAggregation*
118 bool ArrayJoinExistingAggregation::serialize(libdap::ConstraintEvaluator &eval, libdap::DDS &dds, libdap::Marshaller &m,
122 if (BESISDEBUG(TIMING_LOG)) sw.
start(
"ArrayJoinExistingAggregation::serialize",
"");
129 if (!(send_p() || is_in_selection())) {
130 BESDEBUG_FUNC(DEBUG_CHANNEL,
"Object not in output, skipping... name=" << name() << endl);
142 if (PRINT_CONSTRAINTS) {
143 BESDEBUG_FUNC(DEBUG_CHANNEL,
"Constraints on this Array are:" << endl);
150 if (PRINT_CONSTRAINTS) {
151 BESDEBUG_FUNC(DEBUG_CHANNEL,
"After transfer, constraints on the member template Array are: " << endl);
158 const Array::dimension& outerDim = *(dim_begin());
160 "Aggregating datasets array with outer dimension constraints: " <<
" start=" << outerDim.start <<
" stride=" << outerDim.stride <<
" stop=" << outerDim.stop << endl);
165 m.put_vector_start(length());
167 reserve_value_capacity();
172 NCML_ASSERT(!datasets.empty());
173 int currDatasetIndex = 0;
174 const AggMemberDataset* pCurrDataset = (datasets[currDatasetIndex]).get();
176 int outerDimIndexOfCurrDatasetHead = 0;
177 int currDatasetSize = int(pCurrDataset->getCachedDimensionSize(_joinDim.name));
178 bool currDatasetWasRead =
false;
181 unsigned int nextOutputBufferElementIndex = 0;
186 for (
int outerDimIndex = outerDim.start; outerDimIndex <= outerDim.stop && outerDimIndex < outerDim.size;
187 outerDimIndex += outerDim.stride) {
189 int localGranuleIndex = outerDimIndex - outerDimIndexOfCurrDatasetHead;
193 while (localGranuleIndex >= currDatasetSize) {
194 localGranuleIndex -= currDatasetSize;
195 outerDimIndexOfCurrDatasetHead += currDatasetSize;
197 NCML_ASSERT(currDatasetIndex <
int(datasets.size()));
198 pCurrDataset = datasets[currDatasetIndex].get();
199 currDatasetSize = pCurrDataset->getCachedDimensionSize(_joinDim.name);
200 currDatasetWasRead =
false;
202 BESDEBUG_FUNC(DEBUG_CHANNEL,
203 "The constraint traversal passed a granule boundary " <<
"on the outer dimension and is stepping forward into " <<
"granule index=" << currDatasetIndex << endl);
208 if (!currDatasetWasRead) {
209 BESDEBUG_FUNC(DEBUG_CHANNEL,
210 " Current granule dataset was traversed but not yet " "read and copied into output. Mapping constraints " "and calling read()..." << endl);
219 Array::Dim_iter outerDimIt = granuleConstraintTemplate.dim_begin();
224 outerDimIt->size = currDatasetSize;
225 outerDimIt->c_size = currDatasetSize;
230 int granuleStopIndex = std::min(outerDim.stop - outerDimIndexOfCurrDatasetHead,
231 currDatasetSize - 1);
236 int clampedStride = std::min(outerDim.stride, currDatasetSize);
238 granuleConstraintTemplate.add_constraint(outerDimIt, localGranuleIndex, clampedStride,
240 #if USE_LOCAL_TIMEOUT_SCHEME
246 nextOutputBufferElementIndex,
249 const_cast<AggMemberDataset&
>(*pCurrDataset),
256 #if USE_LOCAL_TIMEOUT_SCHEME
264 this->set_value_slice_from_row_major_vector(*pDatasetArray, nextOutputBufferElementIndex);
267 pDatasetArray->clear_local_data();
271 currDatasetWasRead =
true;
273 BESDEBUG_FUNC(DEBUG_CHANNEL,
274 " The granule index " << currDatasetIndex <<
" was read with constraints and copied into the aggregation output." << endl);
278 catch (AggregationException& ex) {
279 THROW_NCML_PARSE_ERROR(-1, ex.what());
289 status = libdap::Array::serialize(eval, dds, m, ce_eval);
293 status = libdap::Array::serialize(eval, dds, m, ce_eval);
302 void ArrayJoinExistingAggregation::duplicate(
const ArrayJoinExistingAggregation& rhs)
304 _joinDim = rhs._joinDim;
307 void ArrayJoinExistingAggregation::cleanup() throw ()
328 if (BESISDEBUG(TIMING_LOG))
329 sw.
start(
"ArrayJoinExistingAggregation::readConstrainedGranuleArraysAndAggregateDataHook",
"");
332 const Array::dimension& outerDim = *(dim_begin());
334 "Aggregating datasets array with outer dimension constraints: " <<
" start=" << outerDim.start <<
" stride=" << outerDim.stride <<
" stop=" << outerDim.stop << endl);
338 reserve_value_capacity();
342 NCML_ASSERT(!datasets.empty());
343 int currDatasetIndex = 0;
346 int outerDimIndexOfCurrDatasetHead = 0;
348 bool currDatasetWasRead =
false;
351 unsigned int nextOutputBufferElementIndex = 0;
356 for (
int outerDimIndex = outerDim.start; outerDimIndex <= outerDim.stop && outerDimIndex < outerDim.size;
357 outerDimIndex += outerDim.stride) {
359 int localGranuleIndex = outerDimIndex - outerDimIndexOfCurrDatasetHead;
363 while (localGranuleIndex >= currDatasetSize) {
364 localGranuleIndex -= currDatasetSize;
365 outerDimIndexOfCurrDatasetHead += currDatasetSize;
367 NCML_ASSERT(currDatasetIndex <
int(datasets.size()));
368 pCurrDataset = datasets[currDatasetIndex].get();
370 currDatasetWasRead =
false;
372 BESDEBUG_FUNC(DEBUG_CHANNEL,
373 "The constraint traversal passed a granule boundary " <<
"on the outer dimension and is stepping forward into " <<
"granule index=" << currDatasetIndex << endl);
378 if (!currDatasetWasRead) {
379 BESDEBUG_FUNC(DEBUG_CHANNEL,
380 " Current granule dataset was traversed but not yet " "read and copied into output. Mapping constraints " "and calling read()..." << endl);
389 Array::Dim_iter outerDimIt = granuleConstraintTemplate.dim_begin();
394 outerDimIt->size = currDatasetSize;
395 outerDimIt->c_size = currDatasetSize;
400 int granuleStopIndex = std::min(outerDim.stop - outerDimIndexOfCurrDatasetHead, currDatasetSize - 1);
405 int clampedStride = std::min(outerDim.stride, currDatasetSize);
407 granuleConstraintTemplate.add_constraint(outerDimIt, localGranuleIndex, clampedStride, granuleStopIndex);
411 nextOutputBufferElementIndex,
419 currDatasetWasRead =
true;
421 BESDEBUG_FUNC(DEBUG_CHANNEL,
422 " The granule index " << currDatasetIndex <<
" was read with constraints and copied into the aggregation output." << endl);
428 THROW_NCML_PARSE_ERROR(-1, ex.what());