DataSpace

Type

Line Code
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
// -*- C++ -*-
//
// Package:     CondCore/CondHDF5ESSource
// Class  :     DataSet
//
// Implementation:
//     [Notes on implementation]
//
// Original Author:  Christopher Jones
//         Created:  Fri, 30 Jun 2023 15:26:30 GMT
//

// system include files
#include <cassert>

// user include files
#include "h5_DataSet.h"
#include "h5_Attribute.h"
#include "FWCore/Utilities/interface/Exception.h"

//
// constants, enums and typedefs
//

//
// static data member definitions
//
namespace {
  struct Type {
    explicit Type(hid_t iID) : id_(iID) { assert(id_ >= 0); }
    ~Type() noexcept { H5Tclose(id_); }

    void insertMember(const char* iName, size_t offset, hid_t member_id) { H5Tinsert(id_, iName, offset, member_id); }
    hid_t id_;
  };

  struct DataSpace {
    explicit DataSpace(hid_t iID) : id_(iID) { assert(id_ >= 0); }
    ~DataSpace() noexcept { H5Sclose(id_); }
    hid_t id_;
  };
}  // namespace

namespace cms::h5 {
  //
  // constructors and destructor
  //
  DataSet::DataSet(hid_t iParentID, std::string const& iName) : id_(H5Dopen2(iParentID, iName.c_str(), H5P_DEFAULT)) {
    if (id_ < 0) {
      throw cms::Exception("UnknownH5DataSet") << "unable to find dataset " << iName;
    }
  }

  DataSet::DataSet(hid_t iParentID, const void* iRef) : id_(H5Rdereference2(iParentID, H5P_DEFAULT, H5R_OBJECT, iRef)) {
    if (id_ < 0) {
      throw cms::Exception("BadH5DataSetRef") << "unable to derenfence dataset from parent " << iParentID;
    }
  }

  // DataSet::DataSet(const DataSet& rhs)
  // {
  //    // do actual copying here;
  // }

  DataSet::~DataSet() { H5Dclose(id_); }

  //
  // assignment operators
  //
  // const DataSet& DataSet::operator=(const DataSet& rhs)
  // {
  //   //An exception safe implementation is
  //   DataSet temp(rhs);
  //   swap(rhs);
  //
  //   return *this;
  // }

  //
  // member functions
  //

  //
  // const member functions
  //
  std::shared_ptr<Attribute> DataSet::findAttribute(std::string const& iName) const {
    return std::make_shared<Attribute>(id_, iName);
  }

  std::size_t DataSet::size() const {
    DataSpace space_id{H5Dget_space(id_)};

    hssize_t num_elements = H5Sget_simple_extent_npoints(space_id.id_);
    assert(num_elements >= 0);

    return num_elements;
  }

  std::size_t DataSet::storageSize() const { return H5Dget_storage_size(id_); }
  std::size_t DataSet::memorySize() const { return size(); }
  uint64_t DataSet::fileOffset() const { return H5Dget_offset(id_); }

  uint32_t DataSet::layout() const {
    auto pl = H5Dget_create_plist(id_);
    auto ret = H5Pget_layout(pl);
    H5Pclose(pl);
    return ret;
  }

  std::vector<hobj_ref_t> DataSet::readRefs() const {
    Type type_id{H5Dget_type(id_)};
    auto class_type = H5Tget_class(type_id.id_);
    if (class_type != H5T_REFERENCE) {
      throw cms::Exception("BadDataSetType") << "asked to read dataset as a ref, but it is a " << class_type;
    }

    std::vector<hobj_ref_t> refs;
    refs.resize(size());

    auto ret_value = H5Dread(id_, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, refs.data());
    if (ret_value < 0) {
      throw cms::Exception("BadH5Read") << "unable to read ref dataset " << id_;
    }
    return refs;
  }

  std::vector<char> DataSet::readBytes() const {
    Type type_id{H5Dget_type(id_)};
    auto class_type = H5Tget_class(type_id.id_);
    if (class_type != H5T_INTEGER) {
      throw cms::Exception("BadDataSetType") << "asked to read dataset as a byte, but it is a " << class_type;
    }

    std::vector<char> bytes;
    bytes.resize(size());

    auto ret_value = H5Dread(id_, H5T_STD_I8LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, bytes.data());
    if (ret_value < 0) {
      throw cms::Exception("BadH5Read") << "unable to read bytes dataset " << id_;
    }
    return bytes;
  }

  std::vector<cond::hdf5::IOVSyncValue> DataSet::readSyncValues() const {
    Type sv_type_id{H5Tcreate(H5T_COMPOUND, sizeof(cond::hdf5::IOVSyncValue))};
    sv_type_id.insertMember("high", HOFFSET(cond::hdf5::IOVSyncValue, high_), H5T_NATIVE_UINT32);
    sv_type_id.insertMember("low", HOFFSET(cond::hdf5::IOVSyncValue, low_), H5T_NATIVE_UINT32);

    {
      const Type type_id{H5Dget_type(id_)};
      if (not H5Tequal(sv_type_id.id_, type_id.id_)) {
        throw cms::Exception("BadDataSetType")
            << "asked to read dataset as a IOVSyncValue, but it is a " << type_id.id_;
      }
    }

    std::vector<cond::hdf5::IOVSyncValue> syncValues;
    syncValues.resize(size());

    auto ret_value = H5Dread(id_, sv_type_id.id_, H5S_ALL, H5S_ALL, H5P_DEFAULT, syncValues.data());
    if (ret_value < 0) {
      throw cms::Exception("BadH5Read") << "unable to read IOVSyncValue dataset " << id_;
    }
    return syncValues;
  }

  //
  // static member functions
  //
}  // namespace cms::h5