libdap Updated for version 3.21.0
libdap4 is an implementation of OPeNDAP's DAP protocol.
D4Sequence.cc
1// -*- mode: c++; c-basic-offset:4 -*-
2
3// This file is part of libdap, A C++ implementation of the OPeNDAP Data
4// Access Protocol.
5
6// Copyright (c) 2013 OPeNDAP, Inc.
7// Author: James Gallagher <jgallagher@opendap.org>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22//
23// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24
25#include "config.h"
26
27// #define DODS_DEBUG
28
29#include <algorithm>
30#include <sstream>
31#include <string>
32
33#include "D4Sequence.h"
34
35#include "D4StreamMarshaller.h"
36#include "D4StreamUnMarshaller.h"
37
38#include "D4FilterClause.h" // also contains D4FilterClauseList
39#include "D4RValue.h"
40
41#include "DapIndent.h"
42#include "Error.h"
43#include "InternalErr.h"
44#include "debug.h"
45#include "escaping.h"
46#include "util.h"
47
48#undef CLEAR_LOCAL_DATA
49
50using namespace std;
51
52namespace libdap {
53
54// Private member functions
55
56// A reminder of these type defs
57//
58// typedef vector<BaseType *> D4SeqRow;
59// typedef vector<D4SeqRow *> D4SeqValues;
60// D4SeqValues d_values;
61
62void D4Sequence::m_duplicate(const D4Sequence &s) {
63 d_length = s.d_length;
64#if INDEX_SUBSETTING
65 d_starting_row_number = s.d_starting_row_number;
66 d_ending_row_number = s.d_ending_row_number;
67 d_row_stride = s.d_row_stride;
68#endif
69 // Deep copy for the values
70 for (D4SeqValues::const_iterator i = s.d_values.begin(), e = s.d_values.end(); i != e; ++i) {
71 D4SeqRow &row = **i;
72 D4SeqRow *dest = new D4SeqRow;
73 for (D4SeqRow::const_iterator j = row.begin(), e = row.end(); j != e; ++j) {
74 // *j is a BaseType*
75 dest->push_back((*j)->ptr_duplicate());
76 }
77
78 d_values.push_back(dest);
79 }
80
81 d_copy_clauses = s.d_copy_clauses;
82 d_clauses = (s.d_clauses != 0) ? new D4FilterClauseList(*s.d_clauses) : 0; // deep copy if != 0
83}
84
85// Public member functions
86
95D4Sequence::D4Sequence(const string &n)
96 : Constructor(n, dods_sequence_c, true /* is dap4 */), d_clauses(0), d_copy_clauses(true), d_length(0) {}
97
108D4Sequence::D4Sequence(const string &n, const string &d)
109 : Constructor(n, d, dods_sequence_c, true /* is dap4 */), d_clauses(0), d_copy_clauses(true), d_length(0) {}
110
112D4Sequence::D4Sequence(const D4Sequence &rhs) : Constructor(rhs) { m_duplicate(rhs); }
113
115
116static inline void delete_bt(BaseType *bt_ptr) { delete bt_ptr; }
117
118static inline void delete_rows(D4SeqRow *bt_row_ptr) {
119 for_each(bt_row_ptr->begin(), bt_row_ptr->end(), delete_bt);
120
121 delete bt_row_ptr;
122}
123
124D4Sequence::~D4Sequence() {
126 delete d_clauses;
127}
128
130 if (!d_values.empty()) {
131 for_each(d_values.begin(), d_values.end(), delete_rows);
132 d_values.resize(0);
133 }
134
135 set_read_p(false);
136}
137
138D4Sequence &D4Sequence::operator=(const D4Sequence &rhs) {
139 if (this == &rhs)
140 return *this;
141 Constructor::operator=(rhs);
142 m_duplicate(rhs);
143 return *this;
144}
145
170 bool eof = false;
171 bool done = false;
172
173 do {
174 eof = read();
175 if (eof) { // bail if EOF
176 continue;
177 }
178 // if we are supposed to filter and the clauses eval to true, we're done
179 else if (filter && d_clauses && d_clauses->value()) {
180 d_length++;
181 done = true;
182 }
183 // else if we're not supposed to filter or there are no clauses, we're done
184 else if (!filter || !d_clauses) {
185 d_length++;
186 done = true;
187 }
188
189 // Set up the next call to get another row's worth of data
190 set_read_p(false);
191
192 } while (!eof && !done);
193
194 return !eof;
195}
196
198
221 DBG(cerr << __PRETTY_FUNCTION__ << " BEGIN" << endl);
222
223 if (read_p())
224 return;
225
226 // Read the data values, then serialize. NB: read_next_instance sets d_length
227 // evaluates the filter expression
228 while (read_next_instance(filter)) {
229 DBG(cerr << "read_sequence_values() - Adding row" << endl);
230 D4SeqRow *row = new D4SeqRow;
231 for (Vars_iter i = d_vars.begin(), e = d_vars.end(); i != e; i++) {
232 if ((*i)->send_p()) {
233 DBG(cerr << ":serialize() - reading data for " << (*i)->type_name() << " " << (*i)->name() << endl);
234 if ((*i)->type() == dods_sequence_c) {
235 DBG(cerr << "Reading child sequence values for " << (*i)->name() << endl);
236 D4Sequence *d4s = static_cast<D4Sequence *>(*i);
237 d4s->read_sequence_values(filter);
238 d4s->d_copy_clauses = false;
239 row->push_back(d4s->ptr_duplicate());
240 d4s->d_copy_clauses = true; // Must be sure to not break the object in general
241 row->back()->set_read_p(true);
242 } else {
243 // store the variable's value.
244 row->push_back((*i)->ptr_duplicate());
245 // the copy should have read_p true to prevent the serialize() call
246 // below in the nested for loops from triggering a second call to
247 // read().
248 row->back()->set_read_p(true);
249 }
250 }
251 }
252
253 // When specializing this, use set_value()
254 d_values.push_back(row);
255 DBG(cerr << " read_sequence_values() - Row completed" << endl);
256 }
257
258 set_length(d_values.size());
259
260 DBGN(cerr << __PRETTY_FUNCTION__ << " END added " << d_values.size() << endl);
261}
262
282void D4Sequence::serialize(D4StreamMarshaller &m, DMR &dmr, bool filter) {
283 DBGN(cerr << __PRETTY_FUNCTION__ << " BEGIN" << endl);
284
285 // Read the data values, then serialize. NB: read_next_instance sets d_length
286 // evaluates the filter expression
287 read_sequence_values(filter);
288
289 // write D4Sequecne::length(); don't include the length in the checksum
290 m.put_count(d_length);
291
292 // By this point the d_values object holds all and only the values to be sent;
293 // use the serialize methods to send them (but no need to test send_p).
294 for (D4SeqValues::iterator i = d_values.begin(), e = d_values.end(); i != e; ++i) {
295 for (D4SeqRow::iterator j = (*i)->begin(), f = (*i)->end(); j != f; ++j) {
296 (*j)->serialize(m, dmr, /*eval,*/ false);
297 }
298 }
299
300 DBGN(cerr << __PRETTY_FUNCTION__ << " END" << endl);
301}
302
304 int64_t um_count = um.get_count();
305
306 set_length(um_count);
307
308 for (int64_t i = 0; i < d_length; ++i) {
309 D4SeqRow *row = new D4SeqRow;
310 for (Vars_iter i = d_vars.begin(), e = d_vars.end(); i != e; ++i) {
311 (*i)->deserialize(um, dmr);
312 row->push_back((*i)->ptr_duplicate());
313 }
314 d_values.push_back(row);
315 }
316}
317
329 if (!d_clauses)
330 d_clauses = new D4FilterClauseList();
331 return *d_clauses;
332}
333
334#if INDEX_SUBSETTING
343virtual void set_row_number_constraint(int start, int stop, int stride) {
344 if (stop < start)
345 throw Error(malformed_expr, "Starting row number must precede the ending row number.");
346
347 d_starting_row_number = start;
348 d_row_stride = stride;
349 d_ending_row_number = stop;
350}
351#endif
352
358 if (row >= d_values.size())
359 return 0;
360 return d_values[row];
361}
362
368BaseType *D4Sequence::var_value(size_t row_num, const string &name) {
369 D4SeqRow *row = row_value(row_num);
370 if (!row)
371 return nullptr;
372
373 auto elem = find_if(row->begin(), row->end(), [name](const BaseType *btp) { return btp->name() == name; });
374
375 return (elem != row->end()) ? *elem : nullptr;
376}
377
383BaseType *D4Sequence::var_value(size_t row_num, size_t i) {
384 D4SeqRow *row = row_value(row_num);
385 if (!row)
386 return 0;
387
388 if (i >= row->size())
389 return 0;
390
391 return (*row)[i];
392}
393
394void D4Sequence::print_one_row(ostream &out, int row, string space, bool print_row_num) {
395 if (print_row_num)
396 out << "\n" << space << row << ": ";
397
398 out << "{ ";
399
400 int elements = element_count();
401 int j = 0;
402 BaseType *bt_ptr = 0;
403
404 // This version of print_one_row() works for both data read with
405 // deserialize(), where each variable is assumed to have valid data, and
406 // intern_data(), where some/many variables do not. Because of that, it's
407 // not correct to assume that all of the elements will be printed, which
408 // is what the old code did.
409
410 // Print the first value
411 while (j < elements && !bt_ptr) {
412 bt_ptr = var_value(row, j++);
413 if (bt_ptr) { // data
414 if (bt_ptr->type() == dods_sequence_c)
415 static_cast<D4Sequence *>(bt_ptr)->print_val_by_rows(out, space + " ", false, print_row_num);
416 else
417 bt_ptr->print_val(out, space, false);
418 }
419 }
420
421 // Print the remaining values
422 while (j < elements) {
423 bt_ptr = var_value(row, j++);
424 if (bt_ptr) { // data
425 out << ", ";
426 if (bt_ptr->type() == dods_sequence_c)
427 static_cast<D4Sequence *>(bt_ptr)->print_val_by_rows(out, space + " ", false, print_row_num);
428 else
429 bt_ptr->print_val(out, space, false);
430 }
431 }
432
433 out << " }";
434}
435
436void D4Sequence::print_val_by_rows(ostream &out, string space, bool print_decl_p, bool print_row_numbers) {
437 if (print_decl_p) {
438 print_decl(out, space, false);
439 out << " = ";
440 }
441
442 out << "{ ";
443
444 if (length() != 0) {
445 int rows = length() - 1; // -1 because the last row is treated specially
446 for (int i = 0; i < rows; ++i) {
447 print_one_row(out, i, space, print_row_numbers);
448 out << ", ";
449 }
450 print_one_row(out, rows, space, print_row_numbers);
451 }
452
453 out << " }";
454
455 if (print_decl_p)
456 out << ";\n";
457}
458
459void D4Sequence::print_val(ostream &out, string space, bool print_decl_p) {
460 DBG(cerr << name() << " isa " << type_name() << endl);
461
462 print_val_by_rows(out, space, print_decl_p, false);
463}
464
473void D4Sequence::dump(ostream &strm) const {
474 strm << DapIndent::LMarg << "Sequence::dump - (" << (void *)this << ")" << endl;
475 DapIndent::Indent();
476 Constructor::dump(strm);
477 strm << DapIndent::LMarg << "# rows deserialized: " << d_length << endl;
478 strm << DapIndent::LMarg << "bracket notation information:" << endl;
479
480 DapIndent::Indent();
481#if INDEX_SUBSETTING
482 strm << DapIndent::LMarg << "starting row #: " << d_starting_row_number << endl;
483 strm << DapIndent::LMarg << "row stride: " << d_row_stride << endl;
484 strm << DapIndent::LMarg << "ending row #: " << d_ending_row_number << endl;
485#endif
486 DapIndent::UnIndent();
487
488 DapIndent::UnIndent();
489}
490
491} // namespace libdap
The basic data type for the DODS DAP types.
Definition BaseType.h:118
virtual string type_name() const
Returns the type of the class instance as a string.
Definition BaseType.cc:335
virtual string name() const
Returns the name of the class instance.
Definition BaseType.cc:296
virtual bool read_p()
Has this variable been read?
Definition BaseType.cc:410
virtual Type type() const
Returns the type of the class instance.
Definition BaseType.cc:329
virtual void print_val(FILE *out, string space="", bool print_decl_p=true)
Prints the value of the variable.
Definition BaseType.cc:949
int element_count(bool leaves=false) override
Count the members of constructor types.
void print_decl(ostream &out, string space=" ", bool print_semi=true, bool constraint_info=false, bool constrained=false) override
Print an ASCII representation of the variable structure.
void set_read_p(bool state) override
Set the 'read_p' property for the Constructor and its members.
bool read() override
Read the elements of Constructor marked for transmission.
void dump(ostream &strm) const override
dumps information about this object
List of DAP4 Filter Clauses.
Holds a sequence.
Definition D4Sequence.h:131
virtual D4SeqRow * row_value(size_t row)
Get a whole row from the sequence.
virtual bool read_next_instance(bool filter)
Read the next instance of the sequence While the rest of the variables' read() methods are assumed to...
BaseType * ptr_duplicate() override
D4Sequence(const string &n)
The Sequence constructor.
Definition D4Sequence.cc:95
int length() const override
The number of elements in a Sequence object.
Definition D4Sequence.h:189
D4FilterClauseList & clauses()
Access the filter clauses for this D4Sequence.
void set_length(int64_t count) override
Definition D4Sequence.h:195
bool serialize(ConstraintEvaluator &, DDS &, Marshaller &, bool) override
Move data to the net, then remove them from the object.
Definition D4Sequence.h:203
bool deserialize(UnMarshaller &, DDS *, bool) override
Receive data from the net.
Definition D4Sequence.h:206
void dump(ostream &strm) const override
dumps information about this object
void print_val(ostream &out, string space="", bool print_decl_p=true) override
Prints the value of the variable.
void intern_data() override
Read data into this variable.
void read_sequence_values(bool filter)
Read a Sequence's value into memory.
virtual BaseType * var_value(size_t row, const string &name)
Get the BaseType pointer to the named variable of a given row.
void clear_local_data() override
Marshaller that knows how to marshal/serialize dap data objects to a C++ iostream using DAP4's receiv...
virtual void put_count(int64_t count)
Read data from the stream made by D4StreamMarshaller.
A class for error processing.
Definition Error.h:92
top level DAP object to house generic methods
Definition AISConnect.cc:30
vector< BaseType * > D4SeqRow
Definition D4Sequence.h:42