Skip to content

Commit

Permalink
Merge pull request #35 from davidbrochart/group_v2
Browse files Browse the repository at this point in the history
Support v2's groups
  • Loading branch information
davidbrochart committed Mar 2, 2021
2 parents 4e11824 + 6850ae3 commit dd9be1b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 23 deletions.
12 changes: 8 additions & 4 deletions include/xtensor-zarr/xzarr_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
namespace xt
{
template <class store_type, class shape_type, class C>
zarray create_zarr_array(store_type store, const std::string& path, shape_type shape, shape_type chunk_shape, const std::string& dtype, char chunk_memory_layout, char chunk_separator, const C& compressor, const nlohmann::json& attrs, std::size_t chunk_pool_size, const nlohmann::json& fill_value, const std::size_t zarr_version_major = 3)
zarray create_zarr_array(store_type store, const std::string& path, shape_type shape, shape_type chunk_shape, const std::string& dtype, char chunk_memory_layout, char chunk_separator, const C& compressor, const nlohmann::json& attrs, std::size_t chunk_pool_size, const nlohmann::json& fill_value, const std::size_t zarr_version_major)
{
nlohmann::json j;
nlohmann::json compressor_config;
Expand All @@ -45,7 +45,11 @@ namespace xt
chunk_separator = '.';
j["dtype"] = dtype;
j["order"] = std::string(1, chunk_memory_layout);
if (compressor.name != "binary")
if (compressor.name == "binary")
{
j["compressor"] = nlohmann::json();
}
else
{
compressor.write_to(compressor_config);
j["compressor"] = compressor_config;
Expand Down Expand Up @@ -77,11 +81,11 @@ namespace xt
default:
break;
}
return xchunked_array_factory<store_type>::build(store, compressor.name, dtype, chunk_memory_layout, shape, chunk_shape, full_path, chunk_separator, attrs, compressor_config, chunk_pool_size, fill_value, 3);
return xchunked_array_factory<store_type>::build(store, compressor.name, dtype, chunk_memory_layout, shape, chunk_shape, full_path, chunk_separator, attrs, compressor_config, chunk_pool_size, fill_value, zarr_version_major);
}

template <class store_type>
zarray get_zarr_array(store_type store, const std::string& path, std::size_t chunk_pool_size, const std::size_t zarr_version_major = 3)
zarray get_zarr_array(store_type store, const std::string& path, std::size_t chunk_pool_size, const std::size_t zarr_version_major)
{
std::string s;
nlohmann::json attrs;
Expand Down
32 changes: 24 additions & 8 deletions include/xtensor-zarr/xzarr_group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace xt
class xzarr_group
{
public:
xzarr_group(store_type& store, const std::string& path);
xzarr_group(store_type& store, const std::string& path, const std::size_t zarr_version_major);

xzarr_group create_group(const nlohmann::json& attrs=nlohmann::json::object(), const nlohmann::json& extensions=nlohmann::json::array());

Expand All @@ -29,27 +29,43 @@ namespace xt
store_type& m_store;
nlohmann::json m_json;
std::string m_path;
std::size_t m_zarr_version_major;
};

template <class store_type>
xzarr_group<store_type>::xzarr_group(store_type& store, const std::string& path)
xzarr_group<store_type>::xzarr_group(store_type& store, const std::string& path, const std::size_t zarr_version_major)
: m_store(store)
, m_path(path)
, m_zarr_version_major(zarr_version_major)
{
auto f = m_store["meta/root" + m_path + ".group.json"];
if (f.exists())
if (zarr_version_major == 3)
{
m_json = nlohmann::json::parse(std::string(f));
auto f = m_store["meta/root" + m_path + ".group.json"];
if (f.exists())
{
m_json = nlohmann::json::parse(std::string(f));
}
}
}

template <class store_type>
xzarr_group<store_type> xzarr_group<store_type>::create_group(const nlohmann::json& attrs, const nlohmann::json& extensions)
{
m_json = nlohmann::json::object();
m_json["attributes"] = attrs;
m_json["extensions"] = extensions;
m_store["meta/root" + m_path + ".group.json"] = m_json.dump(4);
switch (m_zarr_version_major)
{
case 3:
m_json["attributes"] = attrs;
m_json["extensions"] = extensions;
m_store["meta/root" + m_path + ".group.json"] = m_json.dump(4);
break;
case 2:
m_json["zarr_format"] = 2;
m_store[m_path + ".zgroup"] = m_json.dump(4);
break;
default:
break;
}
return *this;
}

Expand Down
8 changes: 4 additions & 4 deletions include/xtensor-zarr/xzarr_hierarchy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,26 +114,26 @@ namespace xt
template <class store_type>
xzarr_group<store_type> xzarr_hierarchy<store_type>::create_group(const std::string& path, const nlohmann::json& attrs, const nlohmann::json& extensions)
{
xzarr_group<store_type> g(m_store, path);
xzarr_group<store_type> g(m_store, path, m_zarr_version_major);
return g.create_group(attrs, extensions);
}

template <class store_type>
xzarr_node<store_type> xzarr_hierarchy<store_type>::operator[](const std::string& path)
{
return xzarr_node<store_type>(m_store, path);
return xzarr_node<store_type>(m_store, path, m_zarr_version_major);
}

template <class store_type>
nlohmann::json xzarr_hierarchy<store_type>::get_children(const std::string& path)
{
return xzarr_node<store_type>(m_store, path).get_children();
return xzarr_node<store_type>(m_store, path, m_zarr_version_major).get_children();
}

template <class store_type>
nlohmann::json xzarr_hierarchy<store_type>::get_nodes(const std::string& path)
{
return xzarr_node<store_type>(m_store, path).get_nodes();
return xzarr_node<store_type>(m_store, path, m_zarr_version_major).get_nodes();
}

/************************************
Expand Down
16 changes: 9 additions & 7 deletions include/xtensor-zarr/xzarr_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace xt
class xzarr_node
{
public:
xzarr_node(store_type& store, const std::string& path);
xzarr_node(store_type& store, const std::string& path, const std::size_t zarr_version_major);

xzarr_group<store_type> create_group(const std::string& name, const nlohmann::json& attrs=nlohmann::json::object(), const nlohmann::json& extensions=nlohmann::json::array());

Expand All @@ -55,11 +55,13 @@ namespace xt
nlohmann::json m_json;
std::string m_path;
xzarr_node_type m_node_type;
std::size_t m_zarr_version_major;
};

template <class store_type>
xzarr_node<store_type>::xzarr_node(store_type& store, const std::string& path)
xzarr_node<store_type>::xzarr_node(store_type& store, const std::string& path, const std::size_t zarr_version_major)
: m_store(store)
, m_zarr_version_major(zarr_version_major)
{
m_path = path;
if (m_path.front() != '/')
Expand Down Expand Up @@ -100,7 +102,7 @@ namespace xt
xzarr_group<store_type> xzarr_node<store_type>::create_group(const std::string& name, const nlohmann::json& attrs, const nlohmann::json& extensions)
{
m_node_type = xzarr_node_type::explicit_group;
xzarr_group<store_type> g(m_store, m_path + '/' + name);
xzarr_group<store_type> g(m_store, m_path + '/' + name, m_zarr_version_major);
return g.create_group(attrs, extensions);
}

Expand All @@ -111,7 +113,7 @@ namespace xt
{
XTENSOR_THROW(std::runtime_error, "Node is not a group: " + m_path);
}
xzarr_group<store_type> g(m_store, m_path);
xzarr_group<store_type> g(m_store, m_path, m_zarr_version_major);
return g;
}

Expand All @@ -120,7 +122,7 @@ namespace xt
zarray xzarr_node<store_type>::create_array(const std::string& name, shape_type shape, shape_type chunk_shape, const std::string& dtype, O o)
{
m_node_type = xzarr_node_type::array;
return create_zarr_array(m_store, m_path + '/' + name, shape, chunk_shape, dtype, o.chunk_memory_layout, o.chunk_separator, o.compressor, o.attrs, o.chunk_pool_size, o.fill_value);
return create_zarr_array(m_store, m_path + '/' + name, shape, chunk_shape, dtype, o.chunk_memory_layout, o.chunk_separator, o.compressor, o.attrs, o.chunk_pool_size, o.fill_value, m_zarr_version_major);
}

template <class store_type>
Expand All @@ -130,7 +132,7 @@ namespace xt
{
XTENSOR_THROW(std::runtime_error, "Node is not an array: " + m_path);
}
return get_zarr_array(m_store, m_path, chunk_pool_size);
return get_zarr_array(m_store, m_path, chunk_pool_size, m_zarr_version_major);
}

template <class store_type>
Expand Down Expand Up @@ -210,7 +212,7 @@ namespace xt
template <class store_type>
xzarr_node<store_type> xzarr_node<store_type>::operator[](const std::string& name)
{
return xzarr_node<store_type>(m_store, m_path + '/' + name);
return xzarr_node<store_type>(m_store, m_path + '/' + name, m_zarr_version_major);
}

template <class store_type>
Expand Down

0 comments on commit dd9be1b

Please sign in to comment.