1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
/*
* (C) Copyright 2025- ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation nor
* does it submit to any jurisdiction.
*/
#include <chunked_data_view/ChunkedDataView.h>
#include <chunked_data_view/ChunkedDataViewBuilder.h>
#include <chunked_data_view/DataLayout.h>
#include <chunked_data_view/Extractor.h>
#include <chunked_data_view/Fdb.h>
#include <eckit/io/MemoryHandle.h>
#include <eckit/testing/Test.h>
#include <fdb5/api/helpers/FDBToolRequest.h>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
#include "chunked_data_view/Axis.h"
#include "chunked_data_view/AxisDefinition.h"
#include "chunked_data_view/ListIterator.h"
#include "eckit/io/DataHandle.h"
#include "fdb5/database/FieldLocation.h"
#include "fdb5/database/Inspector.h"
#include "metkit/mars/MarsRequest.h"
using fdb5::FDBToolRequest;
//==============================================================================
namespace cdv = chunked_data_view;
std::unique_ptr<eckit::DataHandle> makeHandle(const std::vector<double>& values) {
const size_t size = values.size() * sizeof(std::decay_t<decltype(values)>::value_type) + 2 * sizeof(size_t);
const size_t bytesPerValue = 8;
auto handle = std::make_unique<eckit::MemoryHandle>(size);
size_t _{};
handle->openForWrite(_);
size_t countValues = values.size();
handle->write(&countValues, sizeof(size_t));
handle->write(&bytesPerValue, sizeof(size_t));
handle->write(values.data(), values.size() * sizeof(std::decay_t<decltype(values)>::value_type));
handle->close();
return handle;
};
struct MockListIterator final : public chunked_data_view::ListIteratorInterface {
using vec2 = std::vector<std::tuple<fdb5::Key, std::vector<double>>>;
vec2 data_;
vec2::const_iterator iter_;
MockListIterator(vec2 data) : data_(std::move(data)), iter_(std::begin(data_)) {};
std::optional<std::tuple<fdb5::Key, std::unique_ptr<eckit::DataHandle>>> next() {
iter_++;
if (std::end(data_) == iter_) {
return std::nullopt;
}
return std::make_tuple(std::get<0>(*iter_), makeHandle(std::get<1>(*iter_)));
};
};
struct MockFdb final : public cdv::FdbInterface {
using RetFunc = std::function<std::unique_ptr<eckit::DataHandle>(const metkit::mars::MarsRequest&)>;
using InsFunc =
std::function<std::unique_ptr<chunked_data_view::ListIteratorInterface>(const metkit::mars::MarsRequest&)>;
explicit MockFdb(RetFunc retFunc, InsFunc insFunc) : retFn(std::move(retFunc)), insFn(std::move(insFunc)) {}
std::unique_ptr<eckit::DataHandle> retrieve(const metkit::mars::MarsRequest& request) override {
return retFn(request);
};
std::unique_ptr<chunked_data_view::ListIteratorInterface> inspect(
const metkit::mars::MarsRequest& request) override {
return insFn(request);
}
RetFunc retFn{};
InsFunc insFn{};
};
struct FakeExtractor : public cdv::Extractor {
cdv::DataLayout layout(eckit::DataHandle& handle) const override {
cdv::DataLayout layout{};
handle.openForRead();
EXPECT_EQUAL(handle.read(&layout.countValues, sizeof(layout.countValues)), sizeof(layout.countValues));
EXPECT_EQUAL(handle.read(&layout.bytesPerValue, sizeof(layout.bytesPerValue)), sizeof(layout.bytesPerValue));
handle.close();
return layout;
}
void writeInto(const metkit::mars::MarsRequest& request,
std::unique_ptr<chunked_data_view::ListIteratorInterface> list_iterator,
const std::vector<chunked_data_view::Axis>& axes, const chunked_data_view::DataLayout& layout,
float* ptr, size_t len, size_t expected_msg_count) const override {
cdv::DataLayout readLayout{};
while (auto res = list_iterator->next()) {
if (!res) {
break;
}
const auto& key = std::get<0>(*res);
auto& data_handle = std::get<1>(*res);
data_handle->openForRead();
EXPECT_EQUAL(data_handle->read(&readLayout.countValues, sizeof(layout.countValues)), 8l);
EXPECT_EQUAL(data_handle->read(&readLayout.bytesPerValue, sizeof(layout.bytesPerValue)), 4l);
const size_t totalBytes = layout.countValues * layout.bytesPerValue;
EXPECT_EQUAL(data_handle->read(ptr, totalBytes), totalBytes);
}
};
};
CASE("ChunkedDataView | View from 1 request | Can compute shape") {
const std::string keys{
"type=an,"
"domain=g,"
"expver=0001,"
"stream=oper,"
"date=2020-01-01/to/2020-01-04,"
"levtype=sfc,"
"param=v/u,"
"time=0/6/12/18"};
const auto request = FDBToolRequest::requestsFromString(keys).at(0).request();
const auto view =
cdv::ChunkedDataViewBuilder(
std::make_unique<MockFdb>([](auto& _) { return makeHandle({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); },
[](auto& _) -> std::unique_ptr<chunked_data_view::ListIteratorInterface> {
return std::make_unique<MockListIterator>(
std::vector<std::tuple<fdb5::Key, std::vector<double>>>{std::make_tuple(
fdb5::Key(), std::vector<double>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})});
}))
.addPart(keys,
{cdv::AxisDefinition{{"date"}, true}, cdv::AxisDefinition{{"time"}, true},
cdv::AxisDefinition{{"param"}, false}},
std::make_unique<FakeExtractor>())
.build();
//
// Expect to get: 4 dates, 4 times, 2 fields, 10 values per field (implicit axis)
EXPECT_EQUAL(view->shape(), (std::vector<size_t>{4, 4, 2, 10}));
EXPECT_EQUAL(view->chunks(), (std::vector<size_t>{4, 4, 1, 1}));
EXPECT_EQUAL(view->chunkShape(), (std::vector<size_t>{1, 1, 2, 10}));
}
CASE("ChunkedDataView | View from 2 requests | Can compute shape") {
const std::string keys{
"type=an,"
"domain=g,"
"expver=0001,"
"stream=oper,"
"date=2020-01-01/to/2020-01-04,"
"levtype=sfc,"
"param=v/u,"
"time=0/6/12/18"};
const auto request = FDBToolRequest::requestsFromString(keys).at(0).request();
const auto view =
cdv::ChunkedDataViewBuilder(
std::make_unique<MockFdb>(
[](auto& _) { return makeHandle({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); },
[](auto& _) -> std::unique_ptr<chunked_data_view::ListIteratorInterface> {
return std::make_unique<MockListIterator>(std::vector<std::tuple<fdb5::Key, std::vector<double>>>{
std::make_tuple(fdb5::Key(), std::vector<double>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
});
}))
.addPart(keys,
{cdv::AxisDefinition{{"date"}, true}, cdv::AxisDefinition{{"time"}, true},
cdv::AxisDefinition{{"param"}, true}},
std::make_unique<FakeExtractor>())
.addPart(keys,
{cdv::AxisDefinition{{"date"}, true}, cdv::AxisDefinition{{"time"}, true},
cdv::AxisDefinition{{"param"}, true}},
std::make_unique<FakeExtractor>())
.extendOnAxis(2)
.build();
// Expect to get: 4 dates, 4 times, 2*2 fields (2 per request), 10 values per field (implicit axis)
EXPECT_EQUAL(view->shape(), (std::vector<size_t>{4, 4, 4, 10}));
}
CASE("ChunkedDataView | View from 2 requests | Can compute shape, combined axis") {
const std::string keys{
"type=an,"
"domain=g,"
"expver=0001,"
"stream=oper,"
"date=2020-01-01/to/2020-01-04,"
"levtype=sfc,"
"param=v/u,"
"time=0/6/12/18"};
const auto request = FDBToolRequest::requestsFromString(keys).at(0).request();
const auto view =
cdv::ChunkedDataViewBuilder(
std::make_unique<MockFdb>(
[](auto& _) { return makeHandle({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); },
[](auto& _) -> std::unique_ptr<chunked_data_view::ListIteratorInterface> {
return std::make_unique<MockListIterator>(std::vector<std::tuple<fdb5::Key, std::vector<double>>>{
std::make_tuple(fdb5::Key(), std::vector<double>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
});
}))
.addPart(keys, {cdv::AxisDefinition{{"date", "time"}, true}, cdv::AxisDefinition{{"param"}, true}},
std::make_unique<FakeExtractor>())
.addPart(keys, {cdv::AxisDefinition{{"date", "time"}, true}, cdv::AxisDefinition{{"param"}, true}},
std::make_unique<FakeExtractor>())
.extendOnAxis(1)
.build();
// Expect to get: 16 date/times (4 dates * 4 times), 2*2 fields (2 per request), 10 values per field (implicit axis)
EXPECT_EQUAL(view->shape(), (std::vector<size_t>{16, 4, 10}));
}
CASE("ChunkedDataView - Can build") {
const std::string keys{
"type=an,"
"domain=g,"
"expver=0001,"
"stream=oper,"
"date=2020-01-01/to/2020-01-04,"
"levtype=sfc,"
"param=v/u,"
"time=0/6/12/18"};
const auto request = FDBToolRequest::requestsFromString(keys).at(0).request();
EXPECT_NO_THROW(
cdv::ChunkedDataViewBuilder(
std::make_unique<MockFdb>(
[](auto& _) { return makeHandle({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); },
[](auto& _) -> std::unique_ptr<chunked_data_view::ListIteratorInterface> {
return std::make_unique<MockListIterator>(std::vector<std::tuple<fdb5::Key, std::vector<double>>>{
std::make_tuple(fdb5::Key(), std::vector<double>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
});
}))
.addPart(keys,
{cdv::AxisDefinition{{"date"}, true}, cdv::AxisDefinition{{"time"}, true},
cdv::AxisDefinition{{"param"}, true}},
std::make_unique<FakeExtractor>())
.build());
}
int main(int argc, char** argv) {
return ::eckit::testing::run_tests(argc, argv);
}
|