icedb  version 0.5.1
Snow particle scattering database API
Table.hpp
Go to the documentation of this file.
1 #pragma once
2 #include "Attribute.hpp"
3 #include "compat/gsl/gsl"
4 #include "compat/gsl/gsl_assert"
5 #include "compat/gsl/span"
6 namespace icedb {
7  namespace Groups {
8  class Group;
9  }
11  namespace Tables {
12  class CanHaveTables;
13 
16  class Table : virtual public Attributes::CanHaveAttributes {
17  friend class CanHaveTables;
18  //void readAttachedDimensionScales() const;
19  protected:
21  virtual void _setTableSelf(std::shared_ptr<H5::DataSet> obj) = 0;
23  virtual std::shared_ptr<H5::DataSet> _getTableSelf() const = 0;
25  bool valid() const;
27  Table(const std::string &name = "");
28  public:
29  virtual ~Table();
31  const std::string name; // Constant, and in a base class, so no need to change.
33  typedef std::vector<size_t> Dimensions_Type;
35  typedef std::unique_ptr<Table> Table_Type;
36 
39  //bool hasAllDimensionScalesAttached() const;
40  //bool hasDimensionScaleAttached(size_t DimensionNumber) const;
41  //bool hasDimensionScaleAttached(size_t DimensionNumber, gsl::not_null<const Table *> scale) const;
42  //bool hasDimensionScalesAttached() const;
43 
45  size_t getNumDimensions() const;
47  Dimensions_Type getDimensions() const;
50  void attachDimensionScale(size_t DimensionNumber, gsl::not_null<const Table *> scale);
51  void detachDimensionScale(size_t DimensionNumber, gsl::not_null<const Table *> scale);
53  //Table_Type readDimensionScale(size_t DimensionNumber) const;
54 
56  bool isDimensionScale() const;
57 
59  void setDimensionScale(const std::string &dimensionScaleName);
61  void setDimensionScaleAxisLabel(size_t DimensionNumber, const std::string &label);
63  std::string getDimensionScaleAxisLabel(size_t DimensionNumber) const;
65  std::string getDimensionScaleName() const;
66 
68  template<class Type> bool isTableOfType() const {
69  std::type_index atype = getTableTypeId();
70  if (atype == typeid(Type)) return true;
71  return false;
72  }
73 
75  std::type_index getTableTypeId() const;
76 
83  template <class DataType>
84  void readAll(std::vector<size_t> &dims, std::vector<DataType> &data) const;
85 
86  // Fixed size of data. Cannot resize without re-creating the dataset, for now.
87  // Eventually, will support unlimited dimensions.
88  // These all require that the data being written has the correct type for the table.
89  private:
90  template <class DataType>
91  void writeAllInner(const gsl::span<const DataType> &outData) const;
92  public:
93 
99  template <class DataType>
100  void writeAll(const gsl::span<const DataType> &outData) const {
101  static_assert(icedb::Data_Types::Is_Valid_Data_Type<DataType>() == true,
102  "Data to be written must be a valid data type");
103  this->template writeAllInner<DataType>(outData);
104  }
110  template <class DataType>
111  void writeAll(const gsl::span<DataType> &outData) const {
112  this->template writeAll<DataType>(gsl::span<const DataType>(outData.cbegin(), outData.cend()));
113  }
119  template <class DataType>
120  void writeAll(const std::vector<DataType> &outData) const {
121  this->template writeAll<DataType>(gsl::span<const DataType>(outData.data(), outData.size()));
122  }
123  };
124 
128  bool valid() const;
134  void _createTable(const std::string &tableName, const std::type_index& type, const std::vector<size_t> &dims, const std::vector<size_t> &chunks);
135  protected:
137  CanHaveTables();
139  virtual void _setTablesParent(std::shared_ptr<H5::Group> obj) = 0;
141  virtual std::shared_ptr<H5::Group> _getTablesParent() const = 0;
142  public:
143  ~CanHaveTables();
145  std::set<std::string> getTableNames() const;
147  bool doesTableExist(const std::string &tableName) const;
151  void unlinkTable(const std::string &tableName);
154  Table::Table_Type openTable(const std::string &tableName);
158  inline std::vector<size_t> getChunkStrategy(const std::vector<size_t> &dims) {
159  return dims;
160  }
171  template <class DataType>
172  Table::Table_Type createTable(const std::string &tableName, const std::vector<size_t> &dims, const std::vector<size_t> *chunks = nullptr) {
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  }
192  template <class DataType>
194  const std::string &tableName,
195  std::initializer_list<size_t> dims,
196  const std::vector<size_t> *chunks = nullptr)
197  {
198  std::vector<size_t> vdims{ dims };
199  auto tbl = createTable<DataType>(tableName, vdims, chunks);
200  return tbl;
201  }
213  template <class DataType>
215  const std::string &tableName,
216  std::initializer_list<size_t> dims,
217  std::initializer_list<DataType> data,
218  const std::vector<size_t> *chunks = nullptr)
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  }
236  template <class DataType>
238  const std::string &tableName,
239  std::initializer_list<size_t> dims,
240  const std::vector<DataType> &data,
241  const std::vector<size_t> *chunks = nullptr)
242  {
243  auto tbl = createTable<DataType>(tableName, dims, chunks);
244  tbl->template writeAll<DataType>(data);
245  return tbl;
246  }
258  template <class DataType>
260  const std::string &tableName,
261  std::initializer_list<size_t> dims,
262  const gsl::span<DataType> &data,
263  const std::vector<size_t> *chunks = nullptr)
264  {
265  auto tbl = createTable<DataType>(tableName, dims, chunks);
266  tbl->template writeAll<DataType>(data);
267  return tbl;
268  }
280  template <class DataType>
282  const std::string &tableName,
283  std::initializer_list<size_t> dims,
284  const gsl::span<const DataType> &data,
285  const std::vector<size_t> *chunks = nullptr)
286  {
287  auto tbl = createTable<DataType>(tableName, dims, chunks);
288  tbl->template writeAll<DataType>(data);
289  return tbl;
290  }
291  };
292  }
293 }
bool isTableOfType() const
Check if this table has the matching Type.
Definition: Table.hpp:68
std::unique_ptr< Table > Table_Type
The preferred method of access of a Table is through std::unique_ptr.
Definition: Table.hpp:35
const std::string name
The name of the Table.
Definition: Table.hpp:31
void writeAll(const gsl::span< DataType > &outData) const
Write the passed data to the table. Writes whole table.
Definition: Table.hpp:111
Table::Table_Type createTable(const std::string &tableName, const std::vector< size_t > &dims, const std::vector< size_t > *chunks=nullptr)
Create a table.
Definition: Table.hpp:172
The virtual base class used in all objects that can contain tables (groups and datasets).
Definition: Table.hpp:126
void writeAll(const std::vector< DataType > &outData) const
Write the passed data to the table. Writes whole table. Convenience converter function.
Definition: Table.hpp:120
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&#39;s initial data.
Definition: Table.hpp:281
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&#39;s initial data.
Definition: Table.hpp:237
std::vector< size_t > Dimensions_Type
The dimensions of a Table are expressed as a vector of unsigned integers.
Definition: Table.hpp:33
This is a virtual base class for objects that can have attributes. This includes tables, groups and HDF5 files.
Definition: Attribute.hpp:61
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&#39;s initial data.
Definition: Table.hpp:259
void writeAll(const gsl::span< const DataType > &outData) const
Write the passed data to the table. Writes whole table.
Definition: Table.hpp:100
Table::Table_Type createTable(const std::string &tableName, std::initializer_list< size_t > dims, const std::vector< size_t > *chunks=nullptr)
Create a table.
Definition: Table.hpp:193
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.
Definition: Table.hpp:214
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