hdf5: preliminary append (init) table test

This commit is contained in:
Christian Zimmermann 2023-01-24 01:39:23 +01:00
parent 6faaf7ea15
commit 58fa19f514
9 changed files with 66 additions and 11 deletions

View file

@ -243,6 +243,12 @@ namespace CNORXZ
return mSpace[pos]; return mSpace[pos];
} }
template <typename MetaType>
const MetaType* URange<MetaType>::get() const
{
return mSpace.data();
}
template <typename MetaType> template <typename MetaType>
SizeT URange<MetaType>::getMeta(const MetaType& meta) const SizeT URange<MetaType>::getMeta(const MetaType& meta) const
{ {

View file

@ -99,6 +99,7 @@ namespace CNORXZ
virtual RangePtr extend(const RangePtr& r) const override final; virtual RangePtr extend(const RangePtr& r) const override final;
const MetaType& get(SizeT pos) const; const MetaType& get(SizeT pos) const;
const MetaType* get() const;
SizeT getMeta(const MetaType& metaPos) const; SizeT getMeta(const MetaType& metaPos) const;
private: private:

View file

@ -4,6 +4,7 @@
#include "cnorxz.h" #include "cnorxz.h"
#include <hdf5.h> #include <hdf5.h>
#include <hdf5_hl.h>
namespace CNORXZ namespace CNORXZ
{ {

View file

@ -19,7 +19,17 @@ namespace CNORXZ
const RangePtr& fields) const RangePtr& fields)
{ {
CXZ_ASSERT(this->isOpen(), "tried to extend closed group"); CXZ_ASSERT(this->isOpen(), "tried to extend closed group");
Vector<String> nvec({name});
Vector<DType> dvec({DType(name)});
auto extr = URangeFactory<String>( nvec ).create();
mCont.extend(extr);
auto ii = mCont.begin();
ii.at(dvec); // 'at' returns YIndex&, so cannot use it inline...
auto tab = std::make_shared<STable<Ts...>>(name, this, fields);
for(auto& d: data){
tab->appendRecord(d);
}
*ii = tab;
return *this; return *this;
} }

View file

@ -31,20 +31,20 @@ namespace CNORXZ
Tuple<Ts...> x; Tuple<Ts...> x;
if(mRecords == nullptr) { if(mRecords == nullptr) {
auto mOffsets = MArray<SizeT>( mFields, iter<0,N> mOffsets = MArray<SizeT>( mFields, iter<0,N>
( [&](auto i) { return &std::get<i>(x) - &x; }, ( [&](auto i) { return getTupleOffset(x, i); },
[](const auto&... e) { return Vector<SizeT>({e...}); }) ); [](const auto&... e) { return Vector<SizeT>({e...}); }) );
auto mSizes = MArray<SizeT>( mFields, iter<0,N> mSizes = MArray<SizeT>( mFields, iter<0,N>
( [&](auto i) { return sizeof(std::get<i>(x)); }, ( [&](auto i) { return sizeof(std::get<i>(x)); },
[](const auto&... e) { return Vector<SizeT>({e...}); }) ); [](const auto&... e) { return Vector<SizeT>({e...}); }) );
auto mTypes = MArray<hid_t>( mFields, iter<0,N> mTypes = MArray<hid_t>( mFields, iter<0,N>
( [&](auto i) { return getTypeId(std::get<i>(x)); }, ( [&](auto i) { return getTypeId(std::get<i>(x)); },
[](const auto&... e) { return Vector<hid_t>({e...}); }) ); [](const auto&... e) { return Vector<hid_t>({e...}); }) );
} }
else { else {
iter<0,N>( [&](auto i) { CXZ_ASSERT iter<0,N>( [&](auto i) { CXZ_ASSERT
( &std::get<i>(x) - &x == mOffsets.data()[i], ( getTupleOffset(x, i) == mOffsets.data()[i],
"wrong offset for field " << i << ": " << &std::get<i>(x) - &x "wrong offset for field " << i << ": " << getTupleOffset(x, i)
<< " vs " << mOffsets.data()[i] ); }, NoF{} ); << " vs " << mOffsets.data()[i] ); }, NoF{} );
iter<0,N>( [&](auto i) { CXZ_ASSERT iter<0,N>( [&](auto i) { CXZ_ASSERT
( sizeof(std::get<i>(x)) == mSizes.data()[i], ( sizeof(std::get<i>(x)) == mSizes.data()[i],
@ -60,15 +60,25 @@ namespace CNORXZ
template <typename... Ts> template <typename... Ts>
STable<Ts...>& STable<Ts...>::appendRecord(const Tuple<Ts...>& t) STable<Ts...>& STable<Ts...>::appendRecord(const Tuple<Ts...>& t)
{ {
constexpr hsize_t chunk_size = sizeof(t);
constexpr Int compress = 0;
RangePtr appr = CRangeFactory(1).create(); RangePtr appr = CRangeFactory(1).create();
if(mRecords == nullptr){ if(mRecords == nullptr){
mRecords = appr; mRecords = appr;
Vector<const char*> fields(mFields->size());
auto fr = std::dynamic_pointer_cast<URange<String>>(mFields);
for(auto fi = fr->begin(); fi != fr->end(); ++fi){
fields[fi.lex()] = (*fi).c_str();
}
H5TBmake_table(mName.c_str(), mParent->id(), mName.c_str(), mFields->size(),
mRecords->size(), sizeof(t), fields.data(), mOffsets.data(),
mTypes.data(), chunk_size, NULL, compress, &t);
} }
else { else {
mRecords = mRecords->extend(appr); mRecords = mRecords->extend(appr);
H5TBappend_records(mParent->id(), mName.c_str(), 1, sizeof(t),
mOffsets.data(), mSizes.data(), &t);
} }
H5TBappend_records(mParent->id(), mName, 1, sizeof(t),
mOffsets.data(), mSizes.data(), &t);
return *this; return *this;
} }
} }

View file

@ -26,7 +26,7 @@ namespace CNORXZ
Table& appendRecord(SizeT n, const char* data); Table& appendRecord(SizeT n, const char* data);
Table& readRecord(SizeT pos, SizeT n, char* data); Table& readRecord(SizeT pos, SizeT n, char* data);
private: protected:
RangePtr mRecords; RangePtr mRecords;
RangePtr mFields; // -> FIndex (position -> offset) RangePtr mFields; // -> FIndex (position -> offset)
MArray<SizeT> mSizes; MArray<SizeT> mSizes;

View file

@ -2,10 +2,24 @@
#ifndef __cxz_h5_types_cc_h__ #ifndef __cxz_h5_types_cc_h__
#define __cxz_h5_types_cc_h__ #define __cxz_h5_types_cc_h__
#include "base/types.h"
namespace CNORXZ namespace CNORXZ
{ {
namespace hdf5 namespace hdf5
{ {
template <SizeT N, typename... Ts>
SizeT getTupleOffset(const Tuple<Ts...>& t, CSizeT<N> i)
{
const PtrId beg = reinterpret_cast<PtrId>(&t);
const PtrId pos = reinterpret_cast<PtrId>(&std::get<i>(t));
return pos - beg;
}
/**************
* TypeId *
**************/
template <typename T> template <typename T>
inline hid_t TypeId<T>::get() inline hid_t TypeId<T>::get()
{ {
@ -34,6 +48,10 @@ namespace CNORXZ
return arrtype; return arrtype;
} }
/*****************
* getTypeId *
*****************/
template <typename T> template <typename T>
hid_t getTypeId(T x) hid_t getTypeId(T x)
{ {

View file

@ -6,6 +6,8 @@ namespace CNORXZ
{ {
namespace hdf5 namespace hdf5
{ {
template <SizeT N, typename... Ts>
SizeT getTupleOffset(const Tuple<Ts...>& t, CSizeT<N> i);
/************** /**************
* TypeId * * TypeId *
@ -31,7 +33,7 @@ namespace CNORXZ
*****************/ *****************/
template <typename T> template <typename T>
hid_t getTypeId(const T& x); hid_t getTypeId(T x);
} }
} }

View file

@ -67,6 +67,13 @@ namespace
h5f.addGroup("gr1"); h5f.addGroup("gr1");
h5f.addGroup("gr2"); h5f.addGroup("gr2");
EXPECT_EQ(h5f.get().size(), 2u); EXPECT_EQ(h5f.get().size(), 2u);
// move to separate test:
RangePtr rs = CRangeFactory(2).create();
RangePtr fs = URangeFactory<String>(Vector<String>({"field1","second","real"})).create();
Vector<Tuple<SizeT,Int,Double>> v({ {0, -6, 3.141}, {3, -8, 0.789} });
MArray<Tuple<SizeT,Int,Double>> a(rs, std::move(v));
std::dynamic_pointer_cast<Group>(h5f.get().data()[0])->addTable("tab1", a, fs);
} }
TEST_F(Group_Test, Read) TEST_F(Group_Test, Read)