FETPAPI 0.3.0.0
This project provides C++ classes which facilitate the developement of ETP1.2 clients and servers.
All Classes Namespaces Functions Variables Pages
GetFullDataArrayHandlers.h
1/*-----------------------------------------------------------------------
2Licensed to the Apache Software Foundation (ASF) under one
3or more contributor license agreements. See the NOTICE file
4distributed with this work for additional information
5regarding copyright ownership. The ASF licenses this file
6to you under the Apache License, Version 2.0 (the
7"License"; you may not use this file except in compliance
8with the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing,
13software distributed under the License is distributed on an
14"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15KIND, either express or implied. See the License for the
16specific language governing permissions and limitations
17under the License.
18-----------------------------------------------------------------------*/
19#pragma once
20
21#include "DataArrayHandlers.h"
22
23namespace ETP_NS
24{
28 template<class T>
30 {
31 public:
32 GetFullDataArrayHandlers(AbstractSession* mySession, T* values): DataArrayHandlers(mySession), values(values) {}
33 virtual ~GetFullDataArrayHandlers() = default;
34
40
46 {
47 if (msg.arrayMetadata.empty()) {
48 return;
49 }
50
51 if (msg.arrayMetadata.size() == 1) {
52 dataArrayMetadata = msg.arrayMetadata.begin()->second;
53 return;
54 }
55
56 throw std::range_error("These handlers cannot work with more than one DataArray in GetDataArrayMetadataResponse");
57 }
58
63 void on_GetDataSubarraysResponse(const Energistics::Etp::v12::Protocol::DataArray::GetDataSubarraysResponse& msg, int64_t) final;
64
69
70 void setDataSubarrays(const std::string & key, const Energistics::Etp::v12::Datatypes::DataArrayTypes::GetDataSubarraysType& dataSubArray) {
71 dataSubarrays[key] = dataSubArray;
72 }
73
74 private:
78 T* const values;
80 std::map<std::string, Energistics::Etp::v12::Datatypes::DataArrayTypes::GetDataSubarraysType> dataSubarrays;
81 };
82
84 if (msg.dataArrays.size() == 1) {
85 auto dataArray = msg.dataArrays.begin()->second;
86 if (dataArray.data.item.idx() == 0) {
87 Energistics::Etp::v12::Datatypes::ArrayOfBoolean& avroArray = dataArray.data.item.get_ArrayOfBoolean();
88 for (auto i = 0; i < avroArray.values.size(); ++i) {
89 values[i] = avroArray.values[i];
90 }
91 }
92 else if (dataArray.data.item.idx() == 1) {
93 Energistics::Etp::v12::Datatypes::ArrayOfInt& avroArray = dataArray.data.item.get_ArrayOfInt();
94 for (auto i = 0; i < avroArray.values.size(); ++i) {
95 values[i] = avroArray.values[i];
96 }
97 }
98 else if (dataArray.data.item.idx() == 2) {
99 Energistics::Etp::v12::Datatypes::ArrayOfLong& avroArray = dataArray.data.item.get_ArrayOfLong();
100 for (auto i = 0; i < avroArray.values.size(); ++i) {
101 values[i] = avroArray.values[i];
102 }
103 }
104 else if (dataArray.data.item.idx() == 3) {
105 Energistics::Etp::v12::Datatypes::ArrayOfFloat& avroArray = dataArray.data.item.get_ArrayOfFloat();
106 for (auto i = 0; i < avroArray.values.size(); ++i) {
107 values[i] = avroArray.values[i];
108 }
109 }
110 else if (dataArray.data.item.idx() == 4) {
111 Energistics::Etp::v12::Datatypes::ArrayOfDouble& avroArray = dataArray.data.item.get_ArrayOfDouble();
112 for (auto i = 0; i < avroArray.values.size(); ++i) {
113 values[i] = avroArray.values[i];
114 }
115 }
116 /*
117 else if (dataArray.data.item.idx() == 5) {
118 Energistics::Etp::v12::Datatypes::ArrayOfString& avroArray = dataArray.data.item.get_ArrayOfString();
119 for (auto i = 0; i < avroArray.values.size(); ++i) {
120 values[i] = avroArray.values[i];
121 }
122 }
123 */
124 else if (dataArray.data.item.idx() == 6) {
125 std::string& avroValues = dataArray.data.item.get_bytes();
126 for (auto i = 0; i < avroValues.size(); ++i) {
127 values[i] = avroValues[i];
128 }
129 }
130 }
131 else {
132 throw std::range_error("These handlers can only work with a single DataArray in GetDataArraysResponse");
133 }
134 }
135
137 for (const auto& receivedKeyValue : msg.dataSubarrays) {
138 std::string receivedKey = receivedKeyValue.first;
139 auto iterator = dataSubarrays.find(receivedKey);
140 if (iterator == dataSubarrays.end()) {
141 throw std::invalid_argument("The data sub array has not been registered.");
142 }
143
144 auto dataArray = receivedKeyValue.second;
145 size_t dataArrayValueCount = iterator->second.counts[0];
146 for (int dimIndex = 1; dimIndex < iterator->second.counts.size(); ++dimIndex) {
147 dataArrayValueCount *= iterator->second.counts[dimIndex];
148 }
149 auto currentStarts = iterator->second.starts;
150
151 size_t subarrayOffset = 0;
152 while (subarrayOffset < dataArrayValueCount) {
153 // Compute the offset in the receiving array
154 size_t arrayOffset = currentStarts.back();
155 for (int64_t dimIndex = iterator->second.counts.size() - 2; dimIndex >= 0; --dimIndex) {
156 size_t multiplier = iterator->second.counts[dimIndex + 1];
157 for (size_t dimIndex2 = dimIndex + 2; dimIndex2 < iterator->second.counts.size(); ++dimIndex2) {
158 multiplier *= iterator->second.counts[dimIndex2];
159 }
160 arrayOffset += currentStarts[dimIndex] * multiplier;
161 }
162
163 // Copy from the ETP subarray to the receiving array
164 if (dataArray.data.item.idx() == 0) {
165 Energistics::Etp::v12::Datatypes::ArrayOfBoolean& avroArray = dataArray.data.item.get_ArrayOfBoolean();
166 for (auto i = 0; i < iterator->second.counts.back(); ++i) {
167 values[i + arrayOffset] = avroArray.values[i + subarrayOffset];
168 }
169 }
170 else if (dataArray.data.item.idx() == 1) {
171 Energistics::Etp::v12::Datatypes::ArrayOfInt& avroArray = dataArray.data.item.get_ArrayOfInt();
172 for (auto i = 0; i < iterator->second.counts.back(); ++i) {
173 values[i + arrayOffset] = avroArray.values[i + subarrayOffset];
174 }
175 }
176 else if (dataArray.data.item.idx() == 2) {
177 Energistics::Etp::v12::Datatypes::ArrayOfLong& avroArray = dataArray.data.item.get_ArrayOfLong();
178 for (auto i = 0; i < iterator->second.counts.back(); ++i) {
179 values[i + arrayOffset] = avroArray.values[i + subarrayOffset];
180 }
181 }
182 else if (dataArray.data.item.idx() == 3) {
183 Energistics::Etp::v12::Datatypes::ArrayOfFloat& avroArray = dataArray.data.item.get_ArrayOfFloat();
184 for (auto i = 0; i < iterator->second.counts.back(); ++i) {
185 values[i + arrayOffset] = avroArray.values[i + subarrayOffset];
186 }
187 }
188 else if (dataArray.data.item.idx() == 4) {
189 Energistics::Etp::v12::Datatypes::ArrayOfDouble& avroArray = dataArray.data.item.get_ArrayOfDouble();
190 for (auto i = 0; i < iterator->second.counts.back(); ++i) {
191 values[i + arrayOffset] = avroArray.values[i + subarrayOffset];
192 }
193 }
194 /*
195 else if (dataArray.data.item.idx() == 5) {
196 Energistics::Etp::v12::Datatypes::ArrayOfString& avroArray = dataArray.data.item.get_ArrayOfString();
197 for (auto i = 0; i < avroArray.values.size(); ++i) {
198 values[i] = avroArray.values[i];
199 }
200 }
201 */
202 else if (dataArray.data.item.idx() == 6) {
203 std::string& avroValues = dataArray.data.item.get_bytes();
204 for (auto i = 0; i < iterator->second.counts.back(); ++i) {
205 values[i + arrayOffset] = avroValues[i + subarrayOffset];
206 }
207 }
208
209 // Compute the new starts in the ETP subarray
210 for (int64_t dimIndex = iterator->second.counts.size() - 2; dimIndex >= 0; --dimIndex) {
211 if (currentStarts[dimIndex] + 1 < iterator->second.starts[dimIndex] + iterator->second.counts[dimIndex]) {
212 ++currentStarts[dimIndex];
213 break;
214 }
215 else {
216 currentStarts[dimIndex] = iterator->second.starts[dimIndex];
217 }
218 }
219
220 subarrayOffset += iterator->second.counts.back();
221 }
222 }
223 }
224}
Definition AbstractSession.h:51
Definition DataArrayHandlers.h:26
Definition GetFullDataArrayHandlers.h:30
const Energistics::Etp::v12::Datatypes::DataArrayTypes::DataArrayMetadata & getDataArrayMetadata() const
Definition GetFullDataArrayHandlers.h:68
void on_GetDataArraysResponse(const Energistics::Etp::v12::Protocol::DataArray::GetDataArraysResponse &msg, int64_t correlationId) final
Definition GetFullDataArrayHandlers.h:83
void on_GetDataArrayMetadataResponse(const Energistics::Etp::v12::Protocol::DataArray::GetDataArrayMetadataResponse &msg, int64_t) final
Definition GetFullDataArrayHandlers.h:45