icedb  version 0.5.1
Snow particle scattering database API
Public Member Functions | Protected Member Functions | Private Member Functions | List of all members
icedb::Tables::CanHaveTables Class Referenceabstract

The virtual base class used in all objects that can contain tables (groups and datasets). More...

#include <Table.hpp>

Inheritance diagram for icedb::Tables::CanHaveTables:
Inheritance graph
[legend]

Public Member Functions

 ~CanHaveTables ()
 
std::set< std::string > getTableNames () const
 Lists all table names that are children of this object. More...
 
bool doesTableExist (const std::string &tableName) const
 Does a table exist with the given name. More...
 
void unlinkTable (const std::string &tableName)
 Unlink a table. In HDF5, this is not the same as erasing a table, which never actually happens. More...
 
Table::Table_Type openTable (const std::string &tableName)
 
std::vector< size_t > getChunkStrategy (const std::vector< size_t > &dims)
 The default chunking strategy for this table. Used for storage i/o speed, and for compression. More...
 
template<class DataType >
Table::Table_Type createTable (const std::string &tableName, const std::vector< size_t > &dims, const std::vector< size_t > *chunks=nullptr)
 Create a table. More...
 
template<class DataType >
Table::Table_Type createTable (const std::string &tableName, std::initializer_list< size_t > dims, const std::vector< size_t > *chunks=nullptr)
 Create a table. More...
 
template<class DataType >
Table::Table_Type createTable (const std::string &tableName, std::initializer_list< size_t > dims, std::initializer_list< DataType > data, const std::vector< size_t > *chunks=nullptr)
 Create a table and writes initial data. Used with small tables. More...
 
template<class DataType >
Table::Table_Type createTable (const std::string &tableName, std::initializer_list< size_t > dims, const std::vector< DataType > &data, const std::vector< size_t > *chunks=nullptr)
 Create a table and sets the table's initial data. More...
 
template<class DataType >
Table::Table_Type createTable (const std::string &tableName, std::initializer_list< size_t > dims, const gsl::span< DataType > &data, const std::vector< size_t > *chunks=nullptr)
 Create a table and sets the table's initial data. More...
 
template<class DataType >
Table::Table_Type createTable (const std::string &tableName, std::initializer_list< size_t > dims, const gsl::span< const DataType > &data, const std::vector< size_t > *chunks=nullptr)
 Create a table and sets the table's initial data. More...
 

Protected Member Functions

 CanHaveTables ()
 Trivial constructor used when CanHaveTables is a virtual base class. More...
 
virtual void _setTablesParent (std::shared_ptr< H5::Group > obj)=0
 CanHaveTables needs post-constructor setup. This sets the base HDF5 object that gets manipulated. More...
 
virtual std::shared_ptr< H5::Group > _getTablesParent () const =0
 Gets the base HDF5 object that is manipulated. More...
 

Private Member Functions

bool valid () const
 Checks that the class is well-formed, constructed and activated. CanHaveTables needs post-constructor setup. More...
 
void _createTable (const std::string &tableName, const std::type_index &type, const std::vector< size_t > &dims, const std::vector< size_t > &chunks)
 Internal function to create a table. Does not write data. More...
 

Detailed Description

The virtual base class used in all objects that can contain tables (groups and datasets).

Definition at line 126 of file Table.hpp.

Constructor & Destructor Documentation

◆ CanHaveTables()

icedb::Tables::CanHaveTables::CanHaveTables ( )
protected

Trivial constructor used when CanHaveTables is a virtual base class.

Definition at line 245 of file Tables.cpp.

245 {}

◆ ~CanHaveTables()

icedb::Tables::CanHaveTables::~CanHaveTables ( )

Definition at line 246 of file Tables.cpp.

246 {}

Member Function Documentation

◆ _createTable()

void icedb::Tables::CanHaveTables::_createTable ( const std::string &  tableName,
const std::type_index &  type_id,
const std::vector< size_t > &  dims,
const std::vector< size_t > &  chunks 
)
private

Internal function to create a table. Does not write data.

Parameters
tableNameis the name of the table.
typeis the type of the table.
dimsare the dimensions of the table.
chunksare the HDF5 chunking parameters, used when writing a table.
Note
Must ensure NetCDF-4 compatability.

Definition at line 250 of file Tables.cpp.

References icedb::fs::hdf5::useZLIB(), and icedb::Tables::Table::valid().

255  {
256  Expects(valid());
257  std::vector<hsize_t> hdata;
258  for (const auto &d : dims) hdata.push_back(static_cast<hsize_t>(d));
259  std::vector<hsize_t> cdata;
260  for (const auto &c : chunks) cdata.push_back(static_cast<hsize_t>(c));
261 
262  //fs::hdf5::addDatasetArray
263  /* Turn on creation order tracking. */
264  //assert(H5Pset_attr_creation_order(plistid, H5P_CRT_ORDER_TRACKED | H5P_CRT_ORDER_INDEXED) >= 0);
265  std::shared_ptr<H5::DSetCreatPropList> plist = std::make_shared<H5::DSetCreatPropList>();
266  int fillvalue = -1;
267  plist->setFillValue(H5::PredType::NATIVE_INT, &fillvalue);
268  plist->setChunk(static_cast<int>(cdata.size()), cdata.data());
269  if (fs::hdf5::useZLIB()) {
270  plist->setDeflate(fs::hdf5::useZLIB());
271  }
272 #if ICEDB_H5_CREATEPROPLIST_SETATTRCRTORDER == 1
273  plist->setAttrCrtOrder(H5P_CRT_ORDER_TRACKED | H5P_CRT_ORDER_INDEXED);
274 #else
275  {
276  hid_t id = plist->getId();
277  herr_t e = H5Pset_attr_creation_order(id, H5P_CRT_ORDER_TRACKED | H5P_CRT_ORDER_INDEXED);
278  if (e < 0) throw;
279  }
280 #endif
281 
282  if (type_id == typeid(uint64_t))fs::hdf5::createDatasetRaw<uint64_t, H5::Group>(_getTablesParent().get(), tableName.c_str(), dims, plist);
283  else if (type_id == typeid(int64_t))fs::hdf5::createDatasetRaw<int64_t, H5::Group>(_getTablesParent().get(), tableName.c_str(), dims, plist);
284  else if (type_id == typeid(uint32_t))fs::hdf5::createDatasetRaw<uint32_t, H5::Group>(_getTablesParent().get(), tableName.c_str(), dims, plist);
285  else if (type_id == typeid(int32_t))fs::hdf5::createDatasetRaw<int32_t, H5::Group>(_getTablesParent().get(), tableName.c_str(), dims, plist);
286  else if (type_id == typeid(uint16_t))fs::hdf5::createDatasetRaw<uint16_t, H5::Group>(_getTablesParent().get(), tableName.c_str(), dims, plist);
287  else if (type_id == typeid(int16_t))fs::hdf5::createDatasetRaw<int16_t, H5::Group>(_getTablesParent().get(), tableName.c_str(), dims, plist);
288  else if (type_id == typeid(uint8_t))fs::hdf5::createDatasetRaw<uint8_t, H5::Group>(_getTablesParent().get(), tableName.c_str(), dims, plist);
289  else if (type_id == typeid(int8_t))fs::hdf5::createDatasetRaw<int8_t, H5::Group>(_getTablesParent().get(), tableName.c_str(), dims, plist);
290  else if (type_id == typeid(float))fs::hdf5::createDatasetRaw<float, H5::Group>(_getTablesParent().get(), tableName.c_str(), dims, plist);
291  else if (type_id == typeid(double))fs::hdf5::createDatasetRaw<double, H5::Group>(_getTablesParent().get(), tableName.c_str(), dims, plist);
292  else if (type_id == typeid(char))fs::hdf5::createDatasetRaw<char, H5::Group>(_getTablesParent().get(), tableName.c_str(), dims, plist);
293  else if (type_id == typeid(std::string))fs::hdf5::createDatasetRaw<std::string, H5::Group>(_getTablesParent().get(), tableName.c_str(), dims, plist);
294  else throw(std::invalid_argument("Unhandled data type"));
295  }
virtual std::shared_ptr< H5::Group > _getTablesParent() const =0
Gets the base HDF5 object that is manipulated.
bool valid() const
Checks that the class is well-formed, constructed and activated. CanHaveTables needs post-constructor...
Definition: Tables.cpp:247
Here is the call graph for this function:

◆ _getTablesParent()

virtual std::shared_ptr<H5::Group> icedb::Tables::CanHaveTables::_getTablesParent ( ) const
protectedpure virtual

Gets the base HDF5 object that is manipulated.

Implemented in icedb::Tables::CanHaveTables_impl.

◆ _setTablesParent()

virtual void icedb::Tables::CanHaveTables::_setTablesParent ( std::shared_ptr< H5::Group >  obj)
protectedpure virtual

CanHaveTables needs post-constructor setup. This sets the base HDF5 object that gets manipulated.

Implemented in icedb::Tables::CanHaveTables_impl.

◆ createTable() [1/6]

template<class DataType >
Table::Table_Type icedb::Tables::CanHaveTables::createTable ( const std::string &  tableName,
const std::vector< size_t > &  dims,
const std::vector< size_t > *  chunks = nullptr 
)
inline

Create a table.

Parameters
DataTypeis the type of the data
tableNameis the name of the table
Exceptions
ifa table with this name already exists
ifthe base object is read only
Parameters
dimsare the dimensions of the table
chunksis the size of each N-dimensional chunk, used for storage and compression. HDF5 writes each table as a group of chunked objects. When reading or writing, entire chunks are always read or written.
Returns
The new table.

Definition at line 172 of file Table.hpp.

172  {
173  Expects(!doesTableExist(tableName));
174  std::vector<size_t> chunksGuess;
175  if (!chunks) {
176  chunksGuess = getChunkStrategy(dims);
177  chunks = &chunksGuess;
178  }
179  _createTable(tableName, Data_Types::getType<DataType>(), dims, *chunks);
180  return openTable(tableName);
181  }
bool doesTableExist(const std::string &tableName) const
Does a table exist with the given name.
Definition: Tables.cpp:309
Table::Table_Type openTable(const std::string &tableName)
Definition: Tables.cpp:321
std::vector< size_t > getChunkStrategy(const std::vector< size_t > &dims)
The default chunking strategy for this table. Used for storage i/o speed, and for compression...
Definition: Table.hpp:158
void _createTable(const std::string &tableName, const std::type_index &type, const std::vector< size_t > &dims, const std::vector< size_t > &chunks)
Internal function to create a table. Does not write data.
Definition: Tables.cpp:250

◆ createTable() [2/6]

template<class DataType >
Table::Table_Type icedb::Tables::CanHaveTables::createTable ( const std::string &  tableName,
std::initializer_list< size_t >  dims,
const std::vector< size_t > *  chunks = nullptr 
)
inline

Create a table.

Parameters
DataTypeis the type of the data
tableNameis the name of the table
Exceptions
ifa table with this name already exists
ifthe base object is read only
Parameters
dimsare the dimensions of the table
chunksis the size of each N-dimensional chunk, used for storage and compression. HDF5 writes each table as a group of chunked objects. When reading or writing, entire chunks are always read or written.
Returns
The new table.

Definition at line 193 of file Table.hpp.

197  {
198  std::vector<size_t> vdims{ dims };
199  auto tbl = createTable<DataType>(tableName, vdims, chunks);
200  return tbl;
201  }

◆ createTable() [3/6]

template<class DataType >
Table::Table_Type icedb::Tables::CanHaveTables::createTable ( const std::string &  tableName,
std::initializer_list< size_t >  dims,
std::initializer_list< DataType >  data,
const std::vector< size_t > *  chunks = nullptr 
)
inline

Create a table and writes initial data. Used with small tables.

Parameters
DataTypeis the type of the data
tableNameis the name of the table
Exceptions
ifa table with this name already exists
ifthe base object is read only
Parameters
dimsare the dimensions of the table
dataare the initial data of the table
chunksis the size of each N-dimensional chunk, used for storage and compression. HDF5 writes each table as a group of chunked objects. When reading or writing, entire chunks are always read or written.
Returns
The new table.

Definition at line 214 of file Table.hpp.

219  {
220  auto tbl = createTable<DataType>(tableName, dims, chunks);
221  //std::vector<DataType> vdata{ data };
222  tbl->template writeAll<DataType>(gsl::span<const DataType>(data.begin(), data.end()));
223  return tbl;
224  }

◆ createTable() [4/6]

template<class DataType >
Table::Table_Type icedb::Tables::CanHaveTables::createTable ( const std::string &  tableName,
std::initializer_list< size_t >  dims,
const std::vector< DataType > &  data,
const std::vector< size_t > *  chunks = nullptr 
)
inline

Create a table and sets the table's initial data.

Parameters
DataTypeis the type of the data
tableNameis the name of the table
Exceptions
ifa table with this name already exists
ifthe base object is read only
Parameters
dimsare the dimensions of the table
dataare the data of the table
chunksis the size of each N-dimensional chunk, used for storage and compression. HDF5 writes each table as a group of chunked objects. When reading or writing, entire chunks are always read or written.
Returns
The new table.

Definition at line 237 of file Table.hpp.

242  {
243  auto tbl = createTable<DataType>(tableName, dims, chunks);
244  tbl->template writeAll<DataType>(data);
245  return tbl;
246  }

◆ createTable() [5/6]

template<class DataType >
Table::Table_Type icedb::Tables::CanHaveTables::createTable ( const std::string &  tableName,
std::initializer_list< size_t >  dims,
const gsl::span< DataType > &  data,
const std::vector< size_t > *  chunks = nullptr 
)
inline

Create a table and sets the table's initial data.

Parameters
DataTypeis the type of the data
tableNameis the name of the table
Exceptions
ifa table with this name already exists
ifthe base object is read only
Parameters
dimsare the dimensions of the table
dataare the data of the table
chunksis the size of each N-dimensional chunk, used for storage and compression. HDF5 writes each table as a group of chunked objects. When reading or writing, entire chunks are always read or written.
Returns
The new table.

Definition at line 259 of file Table.hpp.

264  {
265  auto tbl = createTable<DataType>(tableName, dims, chunks);
266  tbl->template writeAll<DataType>(data);
267  return tbl;
268  }

◆ createTable() [6/6]

template<class DataType >
Table::Table_Type icedb::Tables::CanHaveTables::createTable ( const std::string &  tableName,
std::initializer_list< size_t >  dims,
const gsl::span< const DataType > &  data,
const std::vector< size_t > *  chunks = nullptr 
)
inline

Create a table and sets the table's initial data.

Parameters
DataTypeis the type of the data
tableNameis the name of the table
Exceptions
ifa table with this name already exists
ifthe base object is read only
Parameters
dimsare the dimensions of the table
dataare the data of the table
chunksis the size of each N-dimensional chunk, used for storage and compression. HDF5 writes each table as a group of chunked objects. When reading or writing, entire chunks are always read or written.
Returns
The new table.

Definition at line 281 of file Table.hpp.

286  {
287  auto tbl = createTable<DataType>(tableName, dims, chunks);
288  tbl->template writeAll<DataType>(data);
289  return tbl;
290  }

◆ doesTableExist()

bool icedb::Tables::CanHaveTables::doesTableExist ( const std::string &  tableName) const

Does a table exist with the given name.

Definition at line 309 of file Tables.cpp.

309  {
310  const auto tnames = getTableNames();
311  if (tnames.count(tableName)) return true;
312  return false;
313  }
std::set< std::string > getTableNames() const
Lists all table names that are children of this object.
Definition: Tables.cpp:297

◆ getChunkStrategy()

std::vector<size_t> icedb::Tables::CanHaveTables::getChunkStrategy ( const std::vector< size_t > &  dims)
inline

The default chunking strategy for this table. Used for storage i/o speed, and for compression.

Todo:
Use a more intelligent strategy
Returns
the ideal size of each chunk of the dataset.

Definition at line 158 of file Table.hpp.

158  {
159  return dims;
160  }

◆ getTableNames()

std::set< std::string > icedb::Tables::CanHaveTables::getTableNames ( ) const

Lists all table names that are children of this object.

Definition at line 297 of file Tables.cpp.

References icedb::fs::hdf5::getGroupMembersTypes(), and icedb::Tables::Table::valid().

297  {
298  Expects(valid());
299  auto base = _getTablesParent();
300  auto objs = fs::hdf5::getGroupMembersTypes(*(base.get()));
301  std::set<std::string> res;
302  for (const auto &o : objs)
303  {
304  if (o.second == H5G_obj_t::H5G_DATASET) res.insert(o.first);
305  }
306  return res;
307  }
virtual std::shared_ptr< H5::Group > _getTablesParent() const =0
Gets the base HDF5 object that is manipulated.
bool valid() const
Checks that the class is well-formed, constructed and activated. CanHaveTables needs post-constructor...
Definition: Tables.cpp:247
std::map< std::string, H5G_obj_t > getGroupMembersTypes(const ICEDB_H5_GETNUMOBJS_OWNER &base)
Here is the call graph for this function:

◆ openTable()

Table::Table_Type icedb::Tables::CanHaveTables::openTable ( const std::string &  tableName)

Open the table with the matching name.

Exceptions
ifa table with this name does not exist.

Definition at line 321 of file Tables.cpp.

References icedb::Tables::Table::valid().

321  {
322  Expects(valid());
323  const auto base = _getTablesParent();
324  const auto tbl = std::make_shared<H5::DataSet>(base->openDataSet(tableName));
325  Table::Table_Type res = std::make_unique<Table_impl>(tbl,tableName);
326  return res;
327  }
std::unique_ptr< Table > Table_Type
The preferred method of access of a Table is through std::unique_ptr.
Definition: Table.hpp:35
virtual std::shared_ptr< H5::Group > _getTablesParent() const =0
Gets the base HDF5 object that is manipulated.
bool valid() const
Checks that the class is well-formed, constructed and activated. CanHaveTables needs post-constructor...
Definition: Tables.cpp:247
Here is the call graph for this function:

◆ unlinkTable()

void icedb::Tables::CanHaveTables::unlinkTable ( const std::string &  tableName)

Unlink a table. In HDF5, this is not the same as erasing a table, which never actually happens.

Exceptions
ifa table with name tableName does not exist.
ifthe open file is read only

Definition at line 315 of file Tables.cpp.

References icedb::Tables::Table::valid().

315  {
316  Expects(valid());
317  auto base = _getTablesParent();
318  base->unlink(tableName.c_str());
319  }
virtual std::shared_ptr< H5::Group > _getTablesParent() const =0
Gets the base HDF5 object that is manipulated.
bool valid() const
Checks that the class is well-formed, constructed and activated. CanHaveTables needs post-constructor...
Definition: Tables.cpp:247
Here is the call graph for this function:

◆ valid()

bool icedb::Tables::CanHaveTables::valid ( ) const
private

Checks that the class is well-formed, constructed and activated. CanHaveTables needs post-constructor setup.

Definition at line 247 of file Tables.cpp.

247 { if (_getTablesParent()) return true; return false; }
virtual std::shared_ptr< H5::Group > _getTablesParent() const =0
Gets the base HDF5 object that is manipulated.

The documentation for this class was generated from the following files: