From 1f5843895ebfef95088824ed2ae501adda0b6e79 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sun, 22 Jan 2023 15:41:42 +0100 Subject: [PATCH] h5 table, untested --- src/opt/hdf5/include/h5_table.cc.h | 25 ++++++++ src/opt/hdf5/include/h5_table.h | 19 +++++-- src/opt/hdf5/lib/CMakeLists.txt | 1 + src/opt/hdf5/lib/h5_group.cc | 33 ++++++++++- src/opt/hdf5/lib/h5_table.cc | 91 ++++++++++++++++++++++++++++++ 5 files changed, 161 insertions(+), 8 deletions(-) create mode 100644 src/opt/hdf5/include/h5_table.cc.h create mode 100644 src/opt/hdf5/lib/h5_table.cc diff --git a/src/opt/hdf5/include/h5_table.cc.h b/src/opt/hdf5/include/h5_table.cc.h new file mode 100644 index 0000000..bba1265 --- /dev/null +++ b/src/opt/hdf5/include/h5_table.cc.h @@ -0,0 +1,25 @@ + +#ifndef __cxz_h5_table_cc_h__ +#define __cxz_h5_table_cc_h__ + +#include "h5_table.h" + +namespace CNORXZ +{ + namespace hdf5 + { + template + decltype(auto) iterRecords(F&& f) const + { + + } + + template + Table& appendRecord(const Tuple& t) const + { + + } + } +} + +#endif diff --git a/src/opt/hdf5/include/h5_table.h b/src/opt/hdf5/include/h5_table.h index c1feb64..8ed657a 100644 --- a/src/opt/hdf5/include/h5_table.h +++ b/src/opt/hdf5/include/h5_table.h @@ -21,16 +21,25 @@ namespace CNORXZ virtual Table& close() override final; virtual String path() const override final; virtual String filename() const override final; + + Table& openDSet(); template - decltype(auto) readRecords(F&& f) const; - + decltype(auto) iterRecords(F&& f) const; + + template + Table& appendRecord(const Tuple& t) const; + + template + Table& appendRecord(const MArray>& t) const; + private: RangePtr mRecords; RangePtr mFields; // -> FIndex (position -> offset) - MArray mSizes; - MArray mOffsets; - MArray mTypes; + MArray mSizes; + MArray mOffsets; + MArray mTypes; + hid_t mType; }; } } diff --git a/src/opt/hdf5/lib/CMakeLists.txt b/src/opt/hdf5/lib/CMakeLists.txt index 651cd17..0029314 100644 --- a/src/opt/hdf5/lib/CMakeLists.txt +++ b/src/opt/hdf5/lib/CMakeLists.txt @@ -3,6 +3,7 @@ set(libcnorxzhdf5_a_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/h5_content_base.cc ${CMAKE_CURRENT_SOURCE_DIR}/h5_file.cc ${CMAKE_CURRENT_SOURCE_DIR}/h5_group.cc + ${CMAKE_CURRENT_SOURCE_DIR}/h5_table.cc ) add_library(cnorxzhdf5_obj OBJECT diff --git a/src/opt/hdf5/lib/h5_group.cc b/src/opt/hdf5/lib/h5_group.cc index c909808..c01ac58 100644 --- a/src/opt/hdf5/lib/h5_group.cc +++ b/src/opt/hdf5/lib/h5_group.cc @@ -1,5 +1,6 @@ #include "h5_group.h" +#include "h5_table.h" namespace CNORXZ { @@ -109,6 +110,26 @@ namespace CNORXZ names->push_back(String(name)); return 0; } + + static bool isTable(hid_t id) + { + if(not H5Aexists(id, "CLASS")){ + return false; + } + hid_t attrid = H5Aopen(id, "CLASS", H5P_DEFAULT); + const hid_t atype = H5Aget_type(attrid); + const SizeT asize = H5Tget_size(atype); + Vector buff(asize); + const herr_t ret = H5Aread(attrid, atype, buff.data()); + H5Tclose(atype); + H5Aclose(attrid); + if(ret != 0){ + return false; + } + else { + return String(buff.data()) == "TABLE"; + } + } static herr_t initCont(hid_t id, const char* name, const H5L_info_t* info, void* x) { @@ -129,11 +150,17 @@ namespace CNORXZ case H5O_TYPE_GROUP: { *index = std::make_shared(sname, icd->parent); break; - }/* + } case H5O_TYPE_DATASET: { - *index = std::make_shared(sname, ); + if(isTable(id)){ + *index = std::make_shared(sname, icd->parent); + } + else { + CXZ_ERROR("IMPLEMENT!!!"); + //*index = std::make_shared(sname, icd->parent); + } break; - }*/ + } default: return 1; } diff --git a/src/opt/hdf5/lib/h5_table.cc b/src/opt/hdf5/lib/h5_table.cc new file mode 100644 index 0000000..27e91c8 --- /dev/null +++ b/src/opt/hdf5/lib/h5_table.cc @@ -0,0 +1,91 @@ + +#include +#include "h5_table.h" + +namespace CNORXZ +{ + namespace hdf5 + { + Table::Table(const String& name, const ContentBase* _parent) : + ContentBase(name, _parent) + {} + + Table::~Table() + { + this->close(); + } + + ContentType Table::type() const + { + return ContentType::TABLE; + } + + bool Table::ro() const + { + return mParent->ro(); + } + + Table& Table::open() + { + if(H5Oexists_by_name(mParent->id(), mName.c_str(), H5P_DEFAULT)){ + hsize_t nfields = 0; + hsize_t nrecords = 0; + H5TBget_table_info(mParent->id(), mName.c_str(), &nfields, &nrecords); + mRecords = CRangeFactory( nrecords ).create(); + Vector fieldsptr(nfields); + Vector offsets(nfields); + Vector sizes(nfields); + SizeT typesize = 0; + H5TBget_field_info(mParent->id(), mName.c_str(), fieldsptr.data(), sizes.data(), + offsets.data(), &typesize); + Vector fields(nfields); + for(SizeT i = 0; i != nfields; ++i){ + fields[i] = fieldsptr[i]; + } + mFields = URangeFactory( std::move(fields) ).create(); + mSizes = MArray(mFields, std::move(sizes)); + mOffsets = MArray(mFields, std::move(offsets)); + this->openDSet(); + const SizeT n = H5Tget_nmembers(mType); + CXZ_ASSERT(n == mFields->size(), + "number of dataset members does not match number of fields: " + << n << " vs " << mFields->size()); + Vector types(n); + for(SizeT i = 0; i != n; ++i){ + types[i] = H5Tget_member_class( mType, i ); + } + mTypes = MArray(mFields, std::move(types)); + } + return *this; + } + + Table& Table::close() + { + if(mId != 0){ + H5Tclose(mType); + H5Dclose(mId); + } + return *this; + } + + String Table::path() const + { + return mParent->path() + "/" + mName; + } + + String Table::filename() const + { + return mParent->filename(); + } + + Table& Table::openDSet() + { + if(mId == 0){ + mId = H5Dopen(mParent->id(), mName.c_str(), H5P_DEFAULT); + mType = H5Dget_type(mId); + } + return *this; + } + + } +}