ritsuko
Helper utilities for ArtifactDB C++ code
Loading...
Searching...
No Matches
validate.hpp
Go to the documentation of this file.
1#ifndef RITSUKO_HDF5_VLS_VALIDATE_HPP
2#define RITSUKO_HDF5_VLS_VALIDATE_HPP
3
4#include <string>
5#include <vector>
6#include <stdexcept>
7
8#include "H5Cpp.h"
9
10#include "../get_name.hpp"
14#include "Pointer.hpp"
15
21namespace ritsuko {
22
23namespace hdf5 {
24
25namespace vls {
26
36template<typename Offset_, typename Length_>
37inline void validate_1d_array(const H5::DataSet& handle, hsize_t full_length, hsize_t heap_length, hsize_t buffer_size) {
38 hsize_t block_size = pick_1d_block_size(handle.getCreatePlist(), full_length, buffer_size);
39 H5::DataSpace mspace(1, &block_size), dspace(1, &full_length);
40 std::vector<Pointer<Offset_, Length_> > buffer(block_size);
42
43 for (hsize_t i = 0; i < full_length; i += block_size) {
44 auto available = std::min(full_length - i, block_size);
45 constexpr hsize_t zero = 0;
46 mspace.selectHyperslab(H5S_SELECT_SET, &available, &zero);
47 dspace.selectHyperslab(H5S_SELECT_SET, &available, &i);
48
49 handle.read(buffer.data(), dtype, mspace, dspace);
50 for (hsize_t j = 0; j < available; ++j) {
51 const auto& val = buffer[j];
52 hsize_t start = val.offset;
53 hsize_t count = val.length;
54 if (start > heap_length || start + count > heap_length) {
55 throw std::runtime_error("VLS array pointers at '" + get_name(handle) + "' are out of range of the heap");
56 }
57 }
58 }
59}
60
70template<typename Offset_, typename Length_>
71void validate_nd_array(const H5::DataSet& handle, const std::vector<hsize_t>& dimensions, hsize_t heap_length, hsize_t buffer_size) {
72 std::vector<Pointer<Offset_, Length_> > buffer;
74 auto blocks = pick_nd_block_dimensions(handle.getCreatePlist(), dimensions, buffer_size);
75 IterateNdDataset iter(dimensions, blocks);
76
77 while (!iter.finished()) {
78 buffer.resize(iter.current_block_size());
79
80 // Scope this to ensure that 'mspace' doesn't get changed by
81 // 'iter.next()' before the destructor is called.
82 {
83 const auto& mspace = iter.memory_space();
84 handle.read(buffer.data(), dtype, mspace, iter.file_space());
85 for (const auto& val : buffer) {
86 hsize_t start = val.offset;
87 hsize_t count = val.length;
88 if (start > heap_length || start + count > heap_length) {
89 throw std::runtime_error("VLS array pointers at '" + get_name(handle) + "' are out of range of the heap");
90 }
91 }
92 }
93
94 iter.next();
95 }
96}
97
98}
99
100}
101
102}
103
104#endif
Iterate through an N-dimensional dataset by block.
Compound datatype for the VLS heap pointer.
Get the name of a HDF5 object.
void validate_1d_array(const H5::DataSet &handle, hsize_t full_length, hsize_t heap_length, hsize_t buffer_size)
Definition validate.hpp:37
void validate_nd_array(const H5::DataSet &handle, const std::vector< hsize_t > &dimensions, hsize_t heap_length, hsize_t buffer_size)
Definition validate.hpp:71
H5::CompType define_pointer_datatype()
Definition Pointer.hpp:60
std::string get_name(const Handle_ &handle)
Definition get_name.hpp:24
hsize_t pick_1d_block_size(const H5::DSetCreatPropList &cplist, hsize_t full_length, hsize_t buffer_size=10000)
Definition pick_1d_block_size.hpp:26
std::vector< hsize_t > pick_nd_block_dimensions(const H5::DSetCreatPropList &cplist, const std::vector< hsize_t > &dimensions, hsize_t buffer_size=10000)
Definition pick_nd_block_dimensions.hpp:26
Assorted helper functions for parsing and validation.
Definition choose_missing_placeholder.hpp:15
Pick a block size for a 1-dimensional HDF5 dataset.
Pick block dimensions for an N-dimensional HDF5 dataset.
Iterate through an N-dimensional dataset by block.
Definition IterateNdDataset.hpp:25
const H5::DataSpace & memory_space() const
Definition IterateNdDataset.hpp:132
bool finished() const
Definition IterateNdDataset.hpp:92
const H5::DataSpace & file_space() const
Definition IterateNdDataset.hpp:124
size_t current_block_size() const
Definition IterateNdDataset.hpp:101
void next()
Definition IterateNdDataset.hpp:55