|
1 | 1 | #pragma once |
2 | 2 |
|
3 | | -#include <hdf5.h> |
4 | 3 | #include <h5cpp/all> |
5 | | - |
6 | 4 | namespace h5::generated::sn__sensor__chunked_compressed_t_ { |
7 | | - |
8 | | -struct row_t { |
9 | | - unsigned long long timestamp_ns; |
10 | | - hvl_t samples; |
11 | | -}; |
12 | | - |
13 | | -inline hid_t compound_type() { |
14 | | - static const hid_t ct = []{ |
15 | | - hid_t v_samples = H5Tvlen_create(H5T_NATIVE_DOUBLE); |
16 | | - hid_t ct = H5Tcreate(H5T_COMPOUND, sizeof(row_t)); |
17 | | - H5Tinsert(ct, "timestamp_ns", HOFFSET(row_t, timestamp_ns), H5T_NATIVE_ULLONG); |
18 | | - H5Tinsert(ct, "samples", HOFFSET(row_t, samples), v_samples); |
| 5 | + struct row_t { |
| 6 | + unsigned long long timestamp_ns; |
| 7 | + hvl_t samples; |
| 8 | + }; |
| 9 | + inline hid_t compound_type() { |
| 10 | + static const hid_t ct = []{ |
| 11 | + hid_t v_samples = H5Tvlen_create(H5T_NATIVE_DOUBLE); |
| 12 | + hid_t ct = H5Tcreate(H5T_COMPOUND, sizeof(row_t)); |
| 13 | + H5Tinsert(ct, "timestamp_ns", HOFFSET(row_t, timestamp_ns), H5T_NATIVE_ULLONG); |
| 14 | + H5Tinsert(ct, "samples", HOFFSET(row_t, samples), v_samples); |
| 15 | + return ct; |
| 16 | + }(); |
19 | 17 | return ct; |
20 | | - }(); |
21 | | - return ct; |
22 | | -} |
23 | | - |
| 18 | + } |
24 | 19 | } // namespace h5::generated::sn__sensor__chunked_compressed_t_ |
25 | 20 |
|
26 | 21 | namespace h5 { |
27 | | -template<> inline h5::ds_t scatter<sn::sensor::chunked_compressed_t>( |
28 | | - hid_t fd, const std::string& path, const sn::sensor::chunked_compressed_t& obj) { |
29 | | - using namespace ::h5::generated::sn__sensor__chunked_compressed_t_; |
30 | | - h5::ds_t ds; |
31 | | - h5::mute(); |
32 | | - bool exists = H5Lexists(fd, path.c_str(), H5P_DEFAULT) > 0; |
33 | | - h5::unmute(); |
34 | | - if (exists) { |
35 | | - ds = h5::open(fd, path, h5::default_dapl); |
36 | | - } else { |
37 | | - h5::dcpl_t dcpl{H5Pcreate(H5P_DATASET_CREATE)}; |
38 | | - hsize_t chunk = 128; |
39 | | - H5Pset_chunk(dcpl, 1, &chunk); |
40 | | - H5Pset_deflate(dcpl, 9); |
41 | | - hsize_t cur = 0; |
42 | | - hsize_t max = H5S_UNLIMITED; |
43 | | - hid_t space = H5Screate_simple(1, &cur, &max); |
44 | | - ds = h5::createds(fd, path, compound_type(), h5::sp_t{space}, |
45 | | - h5::default_lcpl, dcpl, h5::default_dapl); |
| 22 | + template<> inline h5::ds_t scatter<sn::sensor::chunked_compressed_t>( |
| 23 | + hid_t fd, const std::string& path, const sn::sensor::chunked_compressed_t& obj) { |
| 24 | + using namespace ::h5::generated::sn__sensor__chunked_compressed_t_; |
| 25 | + h5::ds_t ds; |
| 26 | + h5::mute(); |
| 27 | + bool exists = H5Lexists(fd, path.c_str(), H5P_DEFAULT) > 0; |
| 28 | + h5::unmute(); |
| 29 | + if (!exists) { |
| 30 | + h5::dcpl_t dcpl{H5Pcreate(H5P_DATASET_CREATE)}; |
| 31 | + hsize_t chunk = 128; |
| 32 | + H5Pset_chunk(dcpl, 1, &chunk); |
| 33 | + H5Pset_deflate(dcpl, 9); |
| 34 | + hsize_t cur = 0; |
| 35 | + hsize_t max = H5S_UNLIMITED; |
| 36 | + hid_t space = H5Screate_simple(1, &cur, &max); |
| 37 | + ds = h5::createds(fd, path, compound_type(), h5::sp_t{space}, |
| 38 | + h5::default_lcpl, dcpl, h5::default_dapl); |
| 39 | + } else ds = h5::open(fd, path, h5::default_dapl); |
| 40 | + hsize_t row = h5::detail::next_row(ds); |
| 41 | + hsize_t samples_len = obj.samples.size(); |
| 42 | + auto* samples_ptr = obj.samples.data(); |
| 43 | + row_t r{ |
| 44 | + obj.timestamp_ns, |
| 45 | + hvl_t{samples_len, (void*)samples_ptr} |
| 46 | + }; |
| 47 | + herr_t err = h5::detail::write_one_row(ds, compound_type(), row, &r); |
| 48 | + (void)err; |
| 49 | + return ds; |
46 | 50 | } |
47 | | - hsize_t row = h5::detail::next_row(ds); |
48 | | - hsize_t samples_len = obj.samples.size(); |
49 | | - auto* samples_ptr = obj.samples.data(); |
50 | | - row_t r{ |
51 | | - obj.timestamp_ns, |
52 | | - hvl_t{samples_len, (void*)samples_ptr} |
53 | | - }; |
54 | | - herr_t err = h5::detail::write_one_row(ds, compound_type(), row, &r); |
55 | | - (void)err; |
56 | | - return ds; |
57 | | -} |
58 | 51 | } // namespace h5 |
59 | 52 |
|
60 | 53 | namespace h5 { |
61 | | -template<> inline void gather<sn::sensor::chunked_compressed_t>( |
62 | | - hid_t fd, const std::string& path, sn::sensor::chunked_compressed_t& obj) { |
63 | | - using namespace ::h5::generated::sn__sensor__chunked_compressed_t_; |
64 | | - h5::ds_t ds = h5::open(fd, path, h5::default_dapl); |
65 | | - hsize_t nrows = h5::detail::next_row(ds); |
66 | | - if (nrows == 0) return; |
67 | | - row_t r{}; |
68 | | - herr_t err = h5::detail::read_one_row(ds, compound_type(), nrows - 1, &r); |
69 | | - (void)err; |
70 | | - obj.timestamp_ns = r.timestamp_ns; |
71 | | - obj.samples.assign(static_cast<double*>(r.samples.p), static_cast<double*>(r.samples.p) + r.samples.len); |
72 | | - H5Treclaim(compound_type(), H5S_ALL, H5P_DEFAULT, &r); |
73 | | -} |
| 54 | + template<> inline void gather<sn::sensor::chunked_compressed_t>( |
| 55 | + hid_t fd, const std::string& path, sn::sensor::chunked_compressed_t& obj) { |
| 56 | + using namespace ::h5::generated::sn__sensor__chunked_compressed_t_; |
| 57 | + h5::ds_t ds = h5::open(fd, path, h5::default_dapl); |
| 58 | + hsize_t nrows = h5::detail::next_row(ds); |
| 59 | + if (nrows == 0) return; |
| 60 | + row_t r{}; |
| 61 | + herr_t err = h5::detail::read_one_row(ds, compound_type(), nrows - 1, &r); |
| 62 | + (void)err; |
| 63 | + obj.timestamp_ns = r.timestamp_ns; |
| 64 | + obj.samples.assign(static_cast<double*>(r.samples.p), static_cast<double*>(r.samples.p) + r.samples.len); |
| 65 | + hid_t reclaim_space = H5Screate(H5S_SCALAR); |
| 66 | + #if H5_VERSION_GE(1,12,0) |
| 67 | + H5Treclaim(compound_type(), reclaim_space, H5P_DEFAULT, &r); |
| 68 | + #else |
| 69 | + H5Dvlen_reclaim(compound_type(), reclaim_space, H5P_DEFAULT, &r); |
| 70 | + #endif |
| 71 | + H5Sclose(reclaim_space); |
| 72 | + } |
74 | 73 | } // namespace h5 |
75 | 74 |
|
76 | 75 | H5CPP_REGISTER_SCATTER(sn::sensor::chunked_compressed_t); |
|
0 commit comments