icedb  version 0.5.1
Snow particle scattering database API
Groups.cpp
Go to the documentation of this file.
1 #include "../icedb/Group.hpp"
2 #include "../private/hdf5_load.h"
3 #include "../private/hdf5_supplemental.hpp"
4 #include "../private/Group_impl.hpp"
5 #include "../icedb/compat/gsl/gsl_assert"
6 
7 namespace icedb {
8  namespace Groups {
9  Group::Group() : name{} {}
10  Group::Group(const std::string &name) : name{ name } {}
12 
13  std::string const Group::_icedb_obj_type_identifier = { "_icedb_obj_type" };
14 
16 
18 
19  Group_impl::Group_impl(const std::string &name, gsl::not_null<H5::Group*> parent)
20  : Group{ name }
21  {
22  auto ugrp = fs::hdf5::openGroup(parent.get(), name.c_str());
23  //grp = std::shared_ptr<H5::Group>(ugrp.release());
24  grp = std::shared_ptr<H5::Group>(ugrp.release()); // , mem::icedb_delete<H5::Group>());
25  this->_setAttributeParent(grp);
26  this->_setTablesParent(grp);
27  }
28  Group_impl::Group_impl(const std::string &name, gsl::not_null<H5::H5File*> parent)
29  : Group{ name }
30  {
31  auto ugrp = fs::hdf5::openGroup(parent.get(), name.c_str());
32  //grp = std::shared_ptr<H5::Group>(ugrp.release());
33  grp = std::shared_ptr<H5::Group>(ugrp.release()); // , mem::icedb_delete<H5::Group>());
34  this->_setAttributeParent(grp);
35  this->_setTablesParent(grp);
36  }
37 
38 
39  Group_impl::Group_impl(const std::string &name, gsl::not_null<const Group*> parent)
40  : Group{ name }
41  {
42  auto ugrp = fs::hdf5::openGroup(parent->getHDF5Group().get(), name.c_str());
43  //grp = std::shared_ptr<H5::Group>(ugrp.release());
44  grp = std::shared_ptr<H5::Group>(ugrp.release()); // , mem::icedb_delete<H5::Group>());
45  this->_setAttributeParent(grp);
46  this->_setTablesParent(grp);
47  }
48 
50  : Group( "UNKNOWN" )
51  {
52  this->grp = grp;
53  this->_setAttributeParent(grp);
54  this->_setTablesParent(grp);
55  }
56 
57  std::shared_ptr<H5::Group> Group_impl::getHDF5Group() const {
58  return grp;
59  }
60 
61  Group::Group_ptr Group_impl::createGroup(const std::string &groupName) {
62  return std::move(Group::createGroup(groupName, this->grp.get()));
63  }
64 
66  template<class HPointerType>
67  Group::Group_ptr _Impl_createGroup(const std::string &groupName, gsl::not_null<HPointerType*> parent) {
68  //_impl->grp->createGroup(groupName); // Bad for NetCDF. See http://www.unidata.ucar.edu/software/netcdf/docs/file_format_specifications.html#creation_order
69  hid_t baseGrpID = parent->getId();
70  /* Create group, with link_creation_order set in the group
71  * creation property list. */
72  // No suitable C++ methods found.
73 
74  hid_t gcpl_id = H5Pcreate(H5P_GROUP_CREATE);
75  assert(gcpl_id >= 0);
76  assert(H5Pset_link_creation_order(gcpl_id, H5P_CRT_ORDER_TRACKED | H5P_CRT_ORDER_INDEXED) >= 0);
77  assert(H5Pset_attr_creation_order(gcpl_id, H5P_CRT_ORDER_TRACKED | H5P_CRT_ORDER_INDEXED) >= 0);
78  hid_t newGrp_id = H5Gcreate2(baseGrpID, groupName.c_str(), H5P_DEFAULT, gcpl_id, H5P_DEFAULT);
79  assert(newGrp_id >= 0);
80  assert(H5Gclose(newGrp_id) >= 0);
81  assert(H5Pclose(gcpl_id) >= 0);
82 
83  return std::make_unique<Group_impl>(groupName, parent);
84  //return Group::Group_ptr(new Group_impl(groupName, parent));
85  }
86 #if ICEDB_H5_UNIFIED_GROUP_FILE == 1
87  Group::Group_ptr Group::createGroup(const std::string &groupName, gsl::not_null<ICEDB_H5_GROUP_OWNER_PTR> parent) {
88  return _Impl_createGroup<ICEDB_H5_GROUP_OWNER>(groupName, parent);
89  }
90 #else
91  Group::Group_ptr Group::createGroup(const std::string &groupName, gsl::not_null<H5::Group*> parent) {
92  return _Impl_createGroup<H5::Group>(groupName, parent);
93  }
94 
95  Group::Group_ptr Group::createGroup(const std::string &groupName, gsl::not_null<H5::H5File*> parent) {
96  return _Impl_createGroup<H5::H5File>(groupName, parent);
97  }
98 #endif
99 
100 
101  Group::Group_ptr Group::createGroup(const std::string &name, gsl::not_null<const Group*> parent) {
102  return std::move(Group::createGroup(name, parent->getHDF5Group().get()));
103  }
104 
105 
106 
107  Group::Group_ptr Group_impl::openGroup(const std::string &groupName) const {
108  return std::make_unique<Group_impl>(groupName, grp.get());
109  //return std::move(Group::Group_ptr(new Group_impl(groupName, grp.get())));
110  }
111 
112 
113 #if ICEDB_H5_UNIFIED_GROUP_FILE == 1
114  Group::Group_ptr Group::openGroup(const std::string &name, gsl::not_null<ICEDB_H5_GROUP_OWNER_PTR> parent) {
115  return std::make_unique<Group_impl>(name, parent);
116  //return std::move(Group::Group_ptr( new Group_impl(name, parent)));
117  }
118 #else
119  Group::Group_ptr Group::openGroup(const std::string &name, gsl::not_null<H5::Group*> parent) {
120  return std::make_unique<Group_impl>(name, parent);
121  //return std::move(Group::Group_ptr( new Group_impl(name, parent)));
122  }
123  Group::Group_ptr Group::openGroup(const std::string &name, gsl::not_null<H5::H5File*> parent) {
124  return std::make_unique<Group_impl>(name, parent);
125  //return std::move(Group::Group_ptr( new Group_impl(name, parent)));
126  }
127 
128 #endif
129 
131  return std::make_unique<Group_impl>(parent);
132  }
133 
134  Group::Group_ptr Group::openGroup(const std::string &name, gsl::not_null<const Group*> parent) {
135  return std::move(Group::openGroup(name, parent->getHDF5Group().get()));
136  }
137 
138  void Group_impl::deleteGroup(const std::string &groupName) {
139  grp->unlink(groupName);
140  }
141 
142  bool Group_impl::doesGroupExist(const std::string &groupName) const {
143  auto gnames = getGroupNames();
144  if (gnames.count(groupName)) return true;
145  return false;
146  }
147 
148  std::set<std::string> Group_impl::getGroupNames() const {
149  auto objs = fs::hdf5::getGroupMembersTypes(*(grp.get()));
150  std::set<std::string> res;
151  for (const auto &o : objs)
152  {
153  if (o.second == H5G_obj_t::H5G_GROUP) res.insert(o.first);
154  }
155  return res;
156  }
157 
158  Group::Group_ptr Group::mount(const std::string &subdirname,
159  gsl::not_null<const Group*> containingParent,
160  gsl::not_null<H5::H5File*> pointsTo)
161  {
164  H5::PropList pl(H5P_DEFAULT);
165  containingParent->getHDF5Group()->mount(subdirname, *(pointsTo.get()), pl);
166  return openGroup(subdirname, containingParent);
167  }
168  }
169 }
Group::Group_ptr _Impl_createGroup(const std::string &groupName, gsl::not_null< HPointerType *> parent)
This exists because HDF5&#39;s API changed.
Definition: Groups.cpp:67
std::set< std::string > getGroupNames() const override
Definition: Groups.cpp:148
std::shared_ptr< H5::Group > Group_HDF_shared_ptr
Definition: Group.hpp:32
Group_HDF_shared_ptr getHDF5Group() const override
Get the fundamental HDF5 object that the group is built on.
Definition: Groups.cpp:57
virtual void _setAttributeParent(std::shared_ptr< H5::H5Object > obj) override
Definition: Attributes.cpp:24
A group is similar to a folder / directory. It can have Attributes and Tables.
Definition: Group.hpp:22
virtual Group_ptr createGroup(const std::string &groupName)=0
Create a group.
void _setTablesParent(std::shared_ptr< H5::Group > obj) override
CanHaveTables needs post-constructor setup. This sets the base HDF5 object that gets manipulated...
Definition: Tables.cpp:343
Group_HDF_shared_ptr grp
Definition: Group_impl.hpp:17
virtual Group_ptr openGroup(const std::string &groupName) const =0
Opens a group.
bool doesGroupExist(const std::string &groupName) const override
Does a group with this name exist?
Definition: Groups.cpp:142
HDFgroup_t openGroup(gsl::not_null< ICEDB_H5_GROUP_OWNER_PTR > base, gsl::not_null< const char *> name)
Definition: export-hdf5.cpp:75
std::unique_ptr< Groups::Group > Group_ptr
Definition: Group.hpp:30
Group_ptr openGroup(const std::string &groupName) const override
Opens a group.
Definition: Groups.cpp:107
Group_ptr createGroup(const std::string &groupName) override
Create a group.
Definition: Groups.cpp:61
std::shared_ptr< H5::H5Object > parent
static Group_ptr mount(const std::string &subdirname, gsl::not_null< const Group *> containingParent, gsl::not_null< H5::H5File *> pointsTo)
Mount an HDF5 file as a child &#39;group&#39;.
Definition: Groups.cpp:158
void deleteGroup(const std::string &groupName) override
Unlink the specified child group.
Definition: Groups.cpp:138
std::map< std::string, H5G_obj_t > getGroupMembersTypes(const ICEDB_H5_GETNUMOBJS_OWNER &base)
static const std::string _icedb_obj_type_identifier
The tag used in icedb to identify a group.
Definition: Group.hpp:29
const std::string name
The name of the group.
Definition: Group.hpp:34
virtual ~Group()
Definition: Groups.cpp:15