hdf5: type declaration in separate file + some reorgs in table code

This commit is contained in:
Christian Zimmermann 2023-01-25 18:31:03 +01:00
parent 24ea3339f8
commit 2547a450d8
12 changed files with 158 additions and 52 deletions

View file

@ -1,4 +1,4 @@
#include "h5_types.cc.h" #include "h5_type_id.cc.h"
#include "h5_group.cc.h" #include "h5_group.cc.h"
#include "h5_table.cc.h" #include "h5_table.cc.h"

View file

@ -3,6 +3,6 @@
#include "h5_file.h" #include "h5_file.h"
#include "h5_group.h" #include "h5_group.h"
#include "h5_table.h" #include "h5_table.h"
#include "h5_types.h" #include "h5_type_id.h"
#include "cnorxz_hdf5.cc.h" #include "cnorxz_hdf5.cc.h"

View file

@ -2,9 +2,9 @@
#ifndef __cxz_h5_file_h__ #ifndef __cxz_h5_file_h__
#define __cxz_h5_file_h__ #define __cxz_h5_file_h__
#include "h5_types.h"
#include "h5_content_base.h" #include "h5_content_base.h"
#include "h5_group.h" #include "h5_group.h"
//#include <hdf5.h>
namespace CNORXZ namespace CNORXZ
{ {

View file

@ -2,10 +2,31 @@
#ifndef __cxz_h5_group_cc_h__ #ifndef __cxz_h5_group_cc_h__
#define __cxz_h5_group_cc_h__ #define __cxz_h5_group_cc_h__
#include "h5_group.h"
namespace CNORXZ namespace CNORXZ
{ {
namespace hdf5 namespace hdf5
{ {
template <typename... Ts>
Sptr<STable<Ts...>> Group::getTable(const String& name, Tuple<Ts...> proto)
{
auto i = this->getIndexTo(name);
auto tab = std::dynamic_pointer_cast<Table>( *i );
if(tab == nullptr){
auto stab = std::dynamic_pointer_cast<STable<Ts...>>(*i);
CXZ_ASSERT(stab != nullptr, "wrong format for table '" << name << "'");
return stab;
}
else {
const RangePtr fields = tab->fields();
(*i)->close();
*i = std::make_shared<STable<Ts...>>(name, this, fields);
return *i;
}
}
template <typename T> template <typename T>
Group& Group::addData(const String& name, const ArrayBase<T>& data) Group& Group::addData(const String& name, const ArrayBase<T>& data)
{ {

View file

@ -2,6 +2,7 @@
#ifndef __cxz_h5_group_h__ #ifndef __cxz_h5_group_h__
#define __cxz_h5_group_h__ #define __cxz_h5_group_h__
#include "h5_types.h"
#include "h5_content_base.h" #include "h5_content_base.h"
namespace CNORXZ namespace CNORXZ
@ -26,6 +27,11 @@ namespace CNORXZ
const ContentPtr& get(const String& name) const; const ContentPtr& get(const String& name) const;
Sptr<Group> getGroup(const String& name) const; Sptr<Group> getGroup(const String& name) const;
Sptr<Table> getTable(const String& name) const;
template <typename... Ts>
Sptr<STable<Ts...>> getTable(const String& name, Tuple<Ts...> proto);
const MArray<ContentPtr>& get() const; const MArray<ContentPtr>& get() const;
Group& addGroup(const String& name); Group& addGroup(const String& name);
@ -40,6 +46,8 @@ namespace CNORXZ
MArray<ContentPtr> mCont; MArray<ContentPtr> mCont;
void mkCont(); void mkCont();
AIndex<ContentPtr> getIndexTo(const String& name) const;
BIndex<ContentPtr> getIndexTo(const String& name);
}; };
} }
} }

View file

@ -3,7 +3,7 @@
#define __cxz_h5_table_cc_h__ #define __cxz_h5_table_cc_h__
#include "h5_table.h" #include "h5_table.h"
#include "h5_types.h" #include "h5_type_id.h"
namespace CNORXZ namespace CNORXZ
{ {
@ -60,24 +60,12 @@ 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; initTable(1, &t, sizeof(t), sizeof(t));
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); Table::appendRecord(1, &t, sizeof(t));
H5TBappend_records(mParent->id(), mName.c_str(), 1, sizeof(t),
mOffsets.data(), mSizes.data(), &t);
} }
return *this; return *this;
} }

View file

@ -2,6 +2,7 @@
#ifndef __cxz_h5_table_h__ #ifndef __cxz_h5_table_h__
#define __cxz_h5_table_h__ #define __cxz_h5_table_h__
#include "h5_types.h"
#include "h5_content_base.h" #include "h5_content_base.h"
namespace CNORXZ namespace CNORXZ
@ -23,8 +24,11 @@ namespace CNORXZ
virtual String filename() const override final; virtual String filename() const override final;
Table& initFieldNames(const Vector<String>& fnames); Table& initFieldNames(const Vector<String>& fnames);
Table& appendRecord(SizeT n, const char* data); Table& initTable(SizeT n, const void* data, SizeT dsize, SizeT chunk_size);
Table& appendRecord(SizeT n, const void* data, SizeT dsize);
Table& readRecord(SizeT pos, SizeT n, char* data); Table& readRecord(SizeT pos, SizeT n, char* data);
const RangePtr& fields() const;
const RangePtr& records() const;
protected: protected:
RangePtr mRecords; RangePtr mRecords;

View file

@ -1,6 +1,6 @@
#ifndef __cxz_h5_types_cc_h__ #ifndef __cxz_h5_type_id_cc_h__
#define __cxz_h5_types_cc_h__ #define __cxz_h5_type_id_cc_h__
#include "base/types.h" #include "base/types.h"

View file

@ -0,0 +1,40 @@
#ifndef __cxz_h5_type_id_h__
#define __cxz_h5_type_id_h__
namespace CNORXZ
{
namespace hdf5
{
template <SizeT N, typename... Ts>
SizeT getTupleOffset(const Tuple<Ts...>& t, CSizeT<N> i);
/**************
* TypeId *
**************/
template <typename T>
struct TypeId { static inline hid_t get(); };
template <>
struct TypeId<SizeT> { static inline hid_t get(); };
template <>
struct TypeId<Int> { static inline hid_t get(); };
template <>
struct TypeId<Double> { static inline hid_t get(); };
template <typename T, SizeT N>
struct TypeId<Arr<T,N>> { static inline hid_t get(); };
/*****************
* getTypeId *
*****************/
template <typename T>
hid_t getTypeId(T x);
}
}
#endif

View file

@ -1,39 +1,27 @@
#ifndef __cxz_h5_types_h__ #ifndef __h5_types_h__
#define __cxz_h5_types_h__ #define __h5_types_h__
namespace CNORXZ namespace CNORXZ
{ {
namespace hdf5 namespace hdf5
{ {
template <SizeT N, typename... Ts> // definition -> h5_content_base.h
SizeT getTupleOffset(const Tuple<Ts...>& t, CSizeT<N> i); class ContentBase;
/************** // definition -> h5_group.h
* TypeId * class Group;
**************/
template <typename T> // definition -> h5_file.h
struct TypeId { static inline hid_t get(); }; class File;
template <> // definition -> h5_table.h
struct TypeId<SizeT> { static inline hid_t get(); }; class Table;
template <> // definition -> h5_table.h
struct TypeId<Int> { static inline hid_t get(); }; template <typename... Ts>
class STable;
template <>
struct TypeId<Double> { static inline hid_t get(); };
template <typename T, SizeT N>
struct TypeId<Arr<T,N>> { static inline hid_t get(); };
/*****************
* getTypeId *
*****************/
template <typename T>
hid_t getTypeId(T x);
} }
} }

View file

@ -73,10 +73,7 @@ namespace CNORXZ
const ContentPtr& Group::get(const String& name) const const ContentPtr& Group::get(const String& name) const
{ {
CXZ_ASSERT(this->isOpen(), "tried to get content of closed group"); auto i = this->getIndexTo(name);
auto dvec = [](const String& n) { return Vector<DType>({DType(n)}); };
auto i = mCont.begin();
i.at(dvec(name));
return *i; return *i;
} }
@ -191,5 +188,23 @@ namespace CNORXZ
initCont, reinterpret_cast<void*>(&icd) ); initCont, reinterpret_cast<void*>(&icd) );
} }
AIndex<ContentPtr> Group::getIndexTo(const String& name) const
{
CXZ_ASSERT(this->isOpen(), "tried to get content of closed group");
auto dvec = [](const String& n) { return Vector<DType>({DType(n)}); };
auto i = mCont.begin();
i.at(dvec(name));
return i;
}
BIndex<ContentPtr> Group::getIndexTo(const String& name)
{
CXZ_ASSERT(this->isOpen(), "tried to get content of closed group");
auto dvec = [](const String& n) { return Vector<DType>({DType(n)}); };
auto i = mCont.begin();
i.at(dvec(name));
return i;
}
} }
} }

View file

@ -90,5 +90,47 @@ namespace CNORXZ
mFields = URangeFactory<String>(fnames).create(); mFields = URangeFactory<String>(fnames).create();
return *this; return *this;
} }
Table& Table::initTable(SizeT n, const void* data, SizeT dsize, SizeT chunk_size)
{
const Int compress = 0;
mRecords = CRangeFactory(n).create();
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();
}
const herr_t err = H5TBmake_table
(mName.c_str(), mParent->id(), mName.c_str(), mFields->size(), mRecords->size(), dsize,
fields.data(), mOffsets.data(), mTypes.data(), chunk_size, NULL, compress, data);
CXZ_ASSERT(err >= 0, "error while initialzing table: error code = " << err);
return *this;
}
Table& Table::appendRecord(SizeT n, const void* data, SizeT dsize)
{
mRecords = mRecords->extend( CRangeFactory(1).create() );
const herr_t err = H5TBappend_records(mParent->id(), mName.c_str(), n, dsize,
mOffsets.data(), mSizes.data(), data);
CXZ_ASSERT(err >= 0, "error while appending record to table: error code = " << err);
return *this;
}
Table& Table::readRecord(SizeT pos, SizeT n, char* data)
{
CXZ_ERROR("not implemented!!!");
return *this;
}
const RangePtr& Table::fields() const
{
return mFields;
}
const RangePtr& Table::records() const
{
return mRecords;
}
} }
} }