CMS 3D CMS Logo

h5_DataSet.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: CondCore/CondHDF5ESSource
4 // Class : DataSet
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author: Christopher Jones
10 // Created: Fri, 30 Jun 2023 15:26:30 GMT
11 //
12 
13 // system include files
14 #include <cassert>
15 
16 // user include files
17 #include "h5_DataSet.h"
18 #include "h5_Attribute.h"
20 
21 //
22 // constants, enums and typedefs
23 //
24 
25 //
26 // static data member definitions
27 //
28 namespace {
29  struct Type {
30  explicit Type(hid_t iID) : id_(iID) { assert(id_ >= 0); }
31  ~Type() noexcept { H5Tclose(id_); }
32 
33  void insertMember(const char* iName, size_t offset, hid_t member_id) { H5Tinsert(id_, iName, offset, member_id); }
34  hid_t id_;
35  };
36 
37  struct DataSpace {
38  explicit DataSpace(hid_t iID) : id_(iID) { assert(id_ >= 0); }
39  ~DataSpace() noexcept { H5Sclose(id_); }
40  hid_t id_;
41  };
42 } // namespace
43 
44 namespace cms::h5 {
45  //
46  // constructors and destructor
47  //
48  DataSet::DataSet(hid_t iParentID, std::string const& iName) : id_(H5Dopen2(iParentID, iName.c_str(), H5P_DEFAULT)) {
49  if (id_ < 0) {
50  throw cms::Exception("UnknownH5DataSet") << "unable to find dataset " << iName;
51  }
52  }
53 
54  DataSet::DataSet(hid_t iParentID, const void* iRef) : id_(H5Rdereference2(iParentID, H5P_DEFAULT, H5R_OBJECT, iRef)) {
55  if (id_ < 0) {
56  throw cms::Exception("BadH5DataSetRef") << "unable to derenfence dataset from parent " << iParentID;
57  }
58  }
59 
60  // DataSet::DataSet(const DataSet& rhs)
61  // {
62  // // do actual copying here;
63  // }
64 
65  DataSet::~DataSet() { H5Dclose(id_); }
66 
67  //
68  // assignment operators
69  //
70  // const DataSet& DataSet::operator=(const DataSet& rhs)
71  // {
72  // //An exception safe implementation is
73  // DataSet temp(rhs);
74  // swap(rhs);
75  //
76  // return *this;
77  // }
78 
79  //
80  // member functions
81  //
82 
83  //
84  // const member functions
85  //
86  std::shared_ptr<Attribute> DataSet::findAttribute(std::string const& iName) const {
87  return std::make_shared<Attribute>(id_, iName);
88  }
89 
90  std::size_t DataSet::size() const {
91  DataSpace space_id{H5Dget_space(id_)};
92 
93  hssize_t num_elements = H5Sget_simple_extent_npoints(space_id.id_);
94  assert(num_elements >= 0);
95 
96  return num_elements;
97  }
98 
99  std::size_t DataSet::storageSize() const { return H5Dget_storage_size(id_); }
100  std::size_t DataSet::memorySize() const { return size(); }
101  uint64_t DataSet::fileOffset() const { return H5Dget_offset(id_); }
102 
103  uint32_t DataSet::layout() const {
104  auto pl = H5Dget_create_plist(id_);
105  auto ret = H5Pget_layout(pl);
106  H5Pclose(pl);
107  return ret;
108  }
109 
110  std::vector<hobj_ref_t> DataSet::readRefs() const {
111  Type type_id{H5Dget_type(id_)};
112  auto class_type = H5Tget_class(type_id.id_);
113  if (class_type != H5T_REFERENCE) {
114  throw cms::Exception("BadDataSetType") << "asked to read dataset as a ref, but it is a " << class_type;
115  }
116 
117  std::vector<hobj_ref_t> refs;
118  refs.resize(size());
119 
120  auto ret_value = H5Dread(id_, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, refs.data());
121  if (ret_value < 0) {
122  throw cms::Exception("BadH5Read") << "unable to read ref dataset " << id_;
123  }
124  return refs;
125  }
126 
127  std::vector<char> DataSet::readBytes() const {
128  Type type_id{H5Dget_type(id_)};
129  auto class_type = H5Tget_class(type_id.id_);
130  if (class_type != H5T_INTEGER) {
131  throw cms::Exception("BadDataSetType") << "asked to read dataset as a byte, but it is a " << class_type;
132  }
133 
134  std::vector<char> bytes;
135  bytes.resize(size());
136 
137  auto ret_value = H5Dread(id_, H5T_STD_I8LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, bytes.data());
138  if (ret_value < 0) {
139  throw cms::Exception("BadH5Read") << "unable to read bytes dataset " << id_;
140  }
141  return bytes;
142  }
143 
144  std::vector<cond::hdf5::IOVSyncValue> DataSet::readSyncValues() const {
145  Type sv_type_id{H5Tcreate(H5T_COMPOUND, sizeof(cond::hdf5::IOVSyncValue))};
146  sv_type_id.insertMember("high", HOFFSET(cond::hdf5::IOVSyncValue, high_), H5T_NATIVE_UINT32);
147  sv_type_id.insertMember("low", HOFFSET(cond::hdf5::IOVSyncValue, low_), H5T_NATIVE_UINT32);
148 
149  {
150  const Type type_id{H5Dget_type(id_)};
151  if (not H5Tequal(sv_type_id.id_, type_id.id_)) {
152  throw cms::Exception("BadDataSetType")
153  << "asked to read dataset as a IOVSyncValue, but it is a " << type_id.id_;
154  }
155  }
156 
157  std::vector<cond::hdf5::IOVSyncValue> syncValues;
158  syncValues.resize(size());
159 
160  auto ret_value = H5Dread(id_, sv_type_id.id_, H5S_ALL, H5S_ALL, H5P_DEFAULT, syncValues.data());
161  if (ret_value < 0) {
162  throw cms::Exception("BadH5Read") << "unable to read IOVSyncValue dataset " << id_;
163  }
164  return syncValues;
165  }
166 
167  //
168  // static member functions
169  //
170 } // namespace cms::h5
std::vector< cond::hdf5::IOVSyncValue > readSyncValues() const
Definition: h5_DataSet.cc:144
uint32_t layout() const
Definition: h5_DataSet.cc:103
ret
prodAgent to be discontinued
assert(be >=bs)
std::size_t memorySize() const
Definition: h5_DataSet.cc:100
uint64_t fileOffset() const
Definition: h5_DataSet.cc:101
std::shared_ptr< Attribute > findAttribute(std::string const &iName) const
Definition: h5_DataSet.cc:86
DataSet(hid_t iParentID, std::string const &iName)
Definition: h5_DataSet.cc:48
unsigned long long uint64_t
Definition: Time.h:13
std::vector< hobj_ref_t > readRefs() const
Definition: h5_DataSet.cc:110
std::vector< char > readBytes() const
Definition: h5_DataSet.cc:127
std::size_t size() const
Definition: h5_DataSet.cc:90
std::size_t storageSize() const
Definition: h5_DataSet.cc:99