diff --git a/src/opt/hdf5/include/h5_group.cc.h b/src/opt/hdf5/include/h5_group.cc.h new file mode 100644 index 0000000..4cb1bea --- /dev/null +++ b/src/opt/hdf5/include/h5_group.cc.h @@ -0,0 +1,29 @@ + +#ifndef __cxz_h5_group_cc_h__ +#define __cxz_h5_group_cc_h__ + +namespace CNORXZ +{ + namespace hdf5 + { + template + Group& Group::addData(const String& name, const ArrayBase& data) + { + CXZ_ASSERT(this->isOpen(), "tried to extend closed group"); + CXZ_ERROR("not implemented!!!"); + return *this; + } + + template + Group& Group::addTable(const String& name, const ArrayBase>& data, + const RangePtr& fields) + { + CXZ_ASSERT(this->isOpen(), "tried to extend closed group"); + + return *this; + } + + } +} + +#endif diff --git a/src/opt/hdf5/include/h5_group.h b/src/opt/hdf5/include/h5_group.h index 0196028..79daeff 100644 --- a/src/opt/hdf5/include/h5_group.h +++ b/src/opt/hdf5/include/h5_group.h @@ -29,6 +29,10 @@ namespace CNORXZ template Group& addData(const String& name, const ArrayBase& data); + + template + Group& addTable(const String& name, const ArrayBase>& data, + const RangePtr& fields); protected: MArray mCont; diff --git a/src/opt/hdf5/include/h5_table.cc.h b/src/opt/hdf5/include/h5_table.cc.h index 784b886..7f82327 100644 --- a/src/opt/hdf5/include/h5_table.cc.h +++ b/src/opt/hdf5/include/h5_table.cc.h @@ -16,49 +16,54 @@ namespace CNORXZ } template - STabel::STabel(const String& name, const ContentBase* _parent) : + STabel::STabel(const String& name, const ContentBase* _parent, const RangePtr& fields) : Table(name, _parent) { - // checks..!!!! + constexpr SizeT N = sizeof...(Ts); + if(mFields == nullptr){ + CXZ_ASSERT(fields != nullptr, "field names have to be initialized"); + mFields = fields; + } + CXZ_ASSERT(mFields->size() == sizeof...(Ts), "expected tuple of size = " << mFields->size() + << ", got: " << sizeof...(Ts)); + + if(mRecords == nullptr) { + auto mOffsets = MArray( mFields, iter<0,N> + ( [&](auto i) { return &std::get(t) - &t; }, + [](const auto&... e) { return Vector({e...}); }) ); + auto mSizes = MArray( mFields, iter<0,N> + ( [&](auto i) { return sizeof(std::get(t)); }, + [](const auto&... e) { return Vector({e...}); }) ); + auto mTypes = MArray( mFields, iter<0,N> + ( [&](auto i) { return getTypeId(std::get(t)); }, + [](const auto&... e) { return Vector({e...}); }) ); + } + else { + iter<0,N>( [&](auto i) { CXZ_ASSERT + ( &std::get(t) - &t == mOffsets.data()[i], + "wrong offset for field " << i << ": " << &std::get(t) - &t + << " vs " << mOffsets.data()[i] ); }, NoF{} ); + iter<0,N>( [&](auto i) { CXZ_ASSERT + ( sizeof(std::get(t)) == mSizes.data()[i], + "wrong size for field " << i << ": " << sizeof(std::get(t)) + << " vs " << mSizes.data()[i] ); }, NoF{} ); + iter<0,N>( [&](auto i) { CXZ_ASSERT + ( getTypeId(std::get(t)) == mTypes.data()[i], + "wrong type for field " << i << ": " << getTypeId(std::get(t)) + << " vs " << mTypes.data()[i] ); }, NoF{} ); + } } template STabel& STabel::appendRecord(const Tuple& t) { - if(not mCheckedFile){ - this->open(); - } - this->close(); // H5TB does not require an open dataset - CXZ_ASSERT(mFields != nullptr, - "field names have to be initialized before creating table"); - CXZ_ASSERT(mFields->size() == sizeof...(Ts), - "expected tuple of size = " << mFields->size() - << ", got: " << sizeof...(Ts)); - RangePtr appr = CRangeFactory(1).create(); - auto offsets = iter<0,sizeof...(Ts)> - ( [&](auto i) { return &std::get(t) - &t; }, - [](const auto&... e) { return Vector({e...}); }); - auto sizes = iter<0,sizeof...(Ts)> - ( [&](auto i) { return sizeof(std::get(t)); }, - [](const auto&... e) { return Vector({e...}); }); - auto types = iter<0,sizeof...(Ts)> - ( [&](auto i) { return getTypeId(std::get(t)); }, - [](const auto&... e) { return Vector({e...}); }); if(mRecords == nullptr){ mRecords = appr; - mSizes = MArray(std::move(sizes)); - mOffsets = MArray(std::move(offsets)); - mTypes = MArray(std::move(types)); - - // init } else { - // check consistency - mRecords = mRecords->extend(appr); } - // append H5TBappend_records(mParent->id(), mName, 1, sizeof(t), mOffsets.data(), mSizes.data(), &t); return *this; diff --git a/src/opt/hdf5/include/h5_table.h b/src/opt/hdf5/include/h5_table.h index 707a77c..e105974 100644 --- a/src/opt/hdf5/include/h5_table.h +++ b/src/opt/hdf5/include/h5_table.h @@ -22,12 +22,9 @@ namespace CNORXZ virtual String path() const override final; virtual String filename() const override final; - Table& openDSet(); Table& initFieldNames(const Vector& fnames); - - template - decltype(auto) iterRecords(F&& f) const; - + Table& appendRecord(SizeT n, const char* data); + Table& readRecord(SizeT pos, SizeT n, char* data); private: RangePtr mRecords; @@ -35,7 +32,7 @@ namespace CNORXZ MArray mSizes; MArray mOffsets; MArray mTypes; - hid_t mType; + hid_t mType = 0; bool mCheckedFile = false; }; @@ -44,11 +41,13 @@ namespace CNORXZ { public: DEFAULT_MEMBERS(STabel); - STabel(const String& name, const ContentBase* _parent); + STabel(const String& name, const ContentBase* _parent, const RangePtr& fields); Table& appendRecord(const Tuple& t); Table& appendRecord(const MArray>& t); + template + decltype(auto) iterRecords(F&& f) const; }; } } diff --git a/src/opt/hdf5/lib/h5_group.cc b/src/opt/hdf5/lib/h5_group.cc index c01ac58..ec130e6 100644 --- a/src/opt/hdf5/lib/h5_group.cc +++ b/src/opt/hdf5/lib/h5_group.cc @@ -90,14 +90,6 @@ namespace CNORXZ return *this; } - template - Group& Group::addData(const String& name, const ArrayBase& data) - { - CXZ_ASSERT(this->isOpen(), "tried to extend closed group"); - CXZ_ERROR(name << data.size() << " NOT IMPLEMENTED!!!"); - return *this; - } - struct InitContData { const ContentBase* parent; diff --git a/src/opt/hdf5/lib/h5_table.cc b/src/opt/hdf5/lib/h5_table.cc index 045cfa7..c60262c 100644 --- a/src/opt/hdf5/lib/h5_table.cc +++ b/src/opt/hdf5/lib/h5_table.cc @@ -8,7 +8,38 @@ namespace CNORXZ { Table::Table(const String& name, const ContentBase* _parent) : ContentBase(name, _parent) - {} + { + if(H5Lexists_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->open(); + 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)); + this->close(); + } + } Table::~Table() { @@ -27,36 +58,10 @@ namespace CNORXZ 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)); + if(mId == 0){ + mId = H5Dopen(mParent->id(), mName.c_str(), H5P_DEFAULT); + mType = H5Dget_type(mId); } - mCheckedFile = true; // now an empty mRecords etc indicates that there is currently no table at the requested location return *this; } @@ -79,15 +84,6 @@ namespace CNORXZ return mParent->filename(); } - Table& Table::openDSet() - { - if(mId == 0){ - mId = H5Dopen(mParent->id(), mName.c_str(), H5P_DEFAULT); - mType = H5Dget_type(mId); - } - return *this; - } - Table& Table::initFieldNames(const Vector& fnames) { CXZ_ASSERT(mFields == nullptr, "fields already initialized");