bes  Updated for version 3.20.10
DmrppD4Opaque.cc
1 
2 // -*- mode: c++; c-basic-offset:4 -*-
3 
4 // This file is part of the BES
5 
6 // Copyright (c) 2016 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24 
25 #include "config.h"
26 
27 #include <cstring>
28 
29 #include <string>
30 #include <queue>
31 
32 #include <BESLog.h>
33 #include <BESInternalError.h>
34 #include <BESDebug.h>
35 
36 #include "CurlHandlePool.h"
37 #include "DmrppRequestHandler.h"
38 #include "DmrppD4Opaque.h"
39 #include "Chunk.h"
40 
41 using namespace libdap;
42 using namespace std;
43 
44 namespace dmrpp {
45 
46 DmrppD4Opaque &
47 DmrppD4Opaque::operator=(const DmrppD4Opaque &rhs)
48 {
49  if (this == &rhs)
50  return *this;
51 
52  dynamic_cast<D4Opaque &>(*this) = rhs; // run Constructor=
53 
54  dynamic_cast<DmrppCommon &>(*this) = rhs;
55  // FIXME Remove DmrppCommon::m_duplicate_common(rhs);
56 
57  return *this;
58 }
59 
60 void DmrppD4Opaque::insert_chunk(shared_ptr<Chunk> chunk)
61 {
62  // The size, in elements, of each of the chunk's dimensions.
63  const vector<unsigned long long> &chunk_shape = get_chunk_dimension_sizes();
64  if (chunk_shape.size() != 1) throw BESInternalError("Opaque variables' chunks can only have one dimension.", __FILE__, __LINE__);
65 
66  // The chunk's origin point a.k.a. its "position in array".
67  const vector<unsigned long long> &chunk_origin = chunk->get_position_in_array();
68 
69  char *source_buffer = chunk->get_rbuf();
70  unsigned char *target_buffer = get_buf();
71 
72  memcpy(target_buffer + chunk_origin[0], source_buffer, chunk_shape[0]);
73 }
74 
75 void DmrppD4Opaque::read_chunks()
76 {
77  for (auto chunk : get_immutable_chunks()) {
78  chunk->read_chunk();
79  if (!is_filters_empty()){
80  chunk->filter_chunk(get_filters(), get_chunk_size_in_elements(), 1 /*elem width*/);
81  }
82 
83  insert_chunk(chunk);
84  }
85 
86  set_read_p(true);
87 }
88 
98 bool
99 DmrppD4Opaque::read()
100 {
101  if (!get_chunks_loaded())
102  load_chunks(this);
103 
104  if (read_p()) return true;
105 
106  // if there are no chunks, use read a single contiguous block of data
107  // and store it in the object. Note that DmrppCommon uses a single Chunk
108  // instance to hold 'contiguous' data.
109  if (get_chunk_dimension_sizes().empty()) {
110  // read_atomic() returns a pointer to the Chunk data. When the Chunk
111  // instance is freed, this memory goes away.
112  char *data = read_atomic(name());
113  val2buf(data); // yes, it's not type-safe
114  }
115  else {
116  // Handle the more complex case where the data is chunked.
117  read_chunks();
118  }
119 
120  return true;
121 }
122 
123 void
124 DmrppD4Opaque::set_send_p(bool state)
125 {
126  if (!get_attributes_loaded())
127  load_attributes(this);
128 
129  D4Opaque::set_send_p(state);
130 }
131 
132 void DmrppD4Opaque::dump(ostream & strm) const
133 {
134  strm << BESIndent::LMarg << "DmrppD4Opaque::dump - (" << (void *) this << ")" << endl;
135  BESIndent::Indent();
136  DmrppCommon::dump(strm);
137  D4Opaque::dump(strm);
138  strm << BESIndent::LMarg << "value: " << "----" << /*d_buf <<*/ endl;
139  BESIndent::UnIndent();
140 }
141 
142 } // namespace dmrpp
143 
exception thrown if internal error encountered