Skip to content

Commit eb0b43a

Browse files
committed
add read_chunk with byte_size
1 parent f83dc5f commit eb0b43a

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

src/h5cpp/node/dataset.hpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,50 @@ class DLL_EXPORT Dataset : public Node
415415
property::DatasetTransferList::get()) const;
416416

417417

418+
//!
419+
//! \brief read dataset chunk (*since hdf5 1.10.2*)
420+
//!
421+
//! Read a chunk from a dataset to an instance of T with given byte size.
422+
//!
423+
//! \throws std::runtime_error in case of a failure
424+
//! \tparam T source type
425+
//! \param data reference to the source instance of T
426+
//! \param byte_size of data
427+
//! \param offset logical position of the first element of the chunk in the dataset's dataspace
428+
//! \param dtpl reference to a dataset transfer property list
429+
//! \return filter_mask mask of which filters are used with the chunk
430+
//!
431+
template<typename T>
432+
std::uint32_t read_chunk(T &data,
433+
size_t byte_size,
434+
std::vector<hsize_t> offset,
435+
const property::DatasetTransferList &dtpl =
436+
property::DatasetTransferList::get()) const;
437+
438+
439+
//!
440+
//! \brief read dataset chunk
441+
//!
442+
//! Read a chunk from a dataset to an instance of T.
443+
//!
444+
//! \throws std::runtime_error in case of a failure
445+
//! \tparam T source type
446+
//! \param data reference to the source instance of T
447+
//! \param byte_size of data
448+
//! \param mem_type reference to the memory data type
449+
//! \param offset logical position of the first element of the chunk in the dataset's dataspace
450+
//! \param dtpl reference to a dataset transfer property list
451+
//! \return filter_mask mask of which filters are used with the chunk
452+
//!
453+
template<typename T>
454+
std::uint32_t read_chunk(T &data,
455+
size_t byte_size,
456+
const datatype::Datatype &mem_type,
457+
std::vector<hsize_t> & offset,
458+
const property::DatasetTransferList &dtpl =
459+
property::DatasetTransferList::get()) const;
460+
461+
418462
//!
419463
//! \brief read dataset chunk
420464
//!
@@ -940,6 +984,16 @@ std::uint32_t Dataset::read_chunk(T &data,
940984
return read_chunk(data, mem_type_holder.get(data), offset, dtpl);
941985
}
942986

987+
template<typename T>
988+
std::uint32_t Dataset::read_chunk(T &data,
989+
size_t byte_size,
990+
std::vector<hsize_t> offset,
991+
const property::DatasetTransferList &dtpl) const
992+
{
993+
hdf5::datatype::DatatypeHolder mem_type_holder;
994+
return read_chunk(data, byte_size, mem_type_holder.get(data), offset, dtpl);
995+
}
996+
943997
template<typename T>
944998
std::uint32_t Dataset::read_chunk(T &data,
945999
const datatype::Datatype &mem_type,
@@ -993,6 +1047,42 @@ std::uint32_t Dataset::read_chunk(T &data,
9931047
return filter_mask;
9941048
}
9951049

1050+
1051+
template<typename T>
1052+
std::uint32_t Dataset::read_chunk(T &data,
1053+
size_t byte_size,
1054+
const datatype::Datatype &mem_type,
1055+
std::vector<hsize_t> & offset,
1056+
const property::DatasetTransferList &dtpl) const
1057+
{
1058+
std::uint32_t filter_mask;
1059+
if(mem_type.get_class() == datatype::Class::Integer)
1060+
{
1061+
#if H5_VERSION_GE(2,0,0)
1062+
if(H5Dread_chunk(static_cast<hid_t>(*this),
1063+
static_cast<hid_t>(dtpl),
1064+
offset.data(),
1065+
&filter_mask,
1066+
dataspace::ptr(data), byte_size)<0)
1067+
{
1068+
std::stringstream ss;
1069+
ss<<"Failure to read chunk data from dataset ["<<link().path()<<"]!";
1070+
error::Singleton::instance().throw_with_stack(ss.str());
1071+
}
1072+
#else
1073+
read_chunk(data, men_type, offset, dtpl);
1074+
#endif
1075+
}
1076+
else
1077+
{
1078+
std::stringstream ss;
1079+
ss<<"Failure to read non-integer chunk data from dataset ["<<link().path()<<"]!";
1080+
error::Singleton::instance().throw_with_stack(ss.str());
1081+
}
1082+
return filter_mask;
1083+
}
1084+
1085+
9961086
#endif
9971087

9981088
template<typename T>

test/node/dataset_direct_chunk_test.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ SCENARIO("testing dataset access via chunks") {
117117
dataset.read_chunk(read_chunk_value, {i, 0, 0});
118118
REQUIRE(frame == read_chunk_value);
119119
}
120+
AND_THEN("we can read chunk the data back with given buffer byte_size") {
121+
UShorts read_chunk_value(xdim * ydim);
122+
for (long long unsigned int i = 0; i != nframe; i++) {
123+
dataset.read_chunk(read_chunk_value, xdim * ydim * sizeof(UShorts), {i, 0, 0});
124+
REQUIRE(frame == read_chunk_value);
125+
}
126+
}
120127
}
121128
}
122129
}

0 commit comments

Comments
 (0)