Skip to content

Commit 8ad3738

Browse files
committed
v2 writer
1 parent e6142d8 commit 8ad3738

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

src/zarr/core/group.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,14 +331,44 @@ def to_buffer_dict(self, prototype: BufferPrototype) -> dict[str, Buffer]:
331331
)
332332
}
333333
else:
334-
return {
334+
items = {
335335
ZGROUP_JSON: prototype.buffer.from_bytes(
336336
json.dumps({"zarr_format": self.zarr_format}, indent=json_indent).encode()
337337
),
338338
ZATTRS_JSON: prototype.buffer.from_bytes(
339339
json.dumps(self.attributes, indent=json_indent).encode()
340340
),
341341
}
342+
if self.consolidated_metadata:
343+
d = {
344+
ZGROUP_JSON: {"zarr_format": self.zarr_format},
345+
ZATTRS_JSON: self.attributes,
346+
}
347+
consolidated_metadata = self.consolidated_metadata.to_dict()["metadata"]
348+
assert isinstance(consolidated_metadata, dict)
349+
for k, v in consolidated_metadata.items():
350+
attrs = v.pop("attributes", None)
351+
d[f"{k}/{ZATTRS_JSON}"] = attrs
352+
if "shape" in v:
353+
# it's an array
354+
d[f"{k}/{ZARRAY_JSON}"] = v
355+
else:
356+
d[f"{k}/{ZGROUP_JSON}"] = {
357+
"zarr_format": self.zarr_format,
358+
"consolidated_metadata": {
359+
"metadata": {},
360+
"must_understand": False,
361+
"kind": "inline",
362+
},
363+
}
364+
365+
items[ZMETADATA_V2_JSON] = prototype.buffer.from_bytes(
366+
json.dumps(
367+
{"metadata": d, "zarr_consolidated_format": 1}, indent=json_indent
368+
).encode()
369+
)
370+
371+
return items
342372

343373
def __init__(
344374
self,

tests/v3/test_metadata/test_consolidated.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from zarr.core.buffer.core import default_buffer_prototype
2020
from zarr.core.group import ConsolidatedMetadata, GroupMetadata
2121
from zarr.core.metadata import ArrayV3Metadata
22+
from zarr.core.metadata.v2 import ArrayV2Metadata
2223
from zarr.store.common import StorePath
2324

2425
if TYPE_CHECKING:
@@ -479,3 +480,44 @@ async def test_open_consolidated_raises_async(self, zarr_format: ZarrFormat):
479480

480481
with pytest.raises(ValueError):
481482
await zarr.api.asynchronous.open_consolidated(store, zarr_format=None)
483+
484+
async def test_consolidated_metadata_v2(self):
485+
store = zarr.store.MemoryStore(mode="w")
486+
g = await AsyncGroup.from_store(store, attributes={"key": "root"}, zarr_format=2)
487+
await g.create_array(name="a", shape=(1,), attributes={"key": "a"})
488+
g1 = await g.create_group(name="g1", attributes={"key": "g1"})
489+
await g1.create_group(name="g2", attributes={"key": "g2"})
490+
491+
await zarr.api.asynchronous.consolidate_metadata(store)
492+
result = await zarr.api.asynchronous.open_consolidated(store, zarr_format=2)
493+
494+
expected = GroupMetadata(
495+
attributes={"key": "root"},
496+
zarr_format=2,
497+
consolidated_metadata=ConsolidatedMetadata(
498+
metadata={
499+
"a": ArrayV2Metadata(
500+
shape=(1,),
501+
dtype="float64",
502+
attributes={"key": "a"},
503+
chunks=(1,),
504+
fill_value=0.0,
505+
order="C",
506+
),
507+
"g1": GroupMetadata(
508+
attributes={"key": "g1"},
509+
zarr_format=2,
510+
consolidated_metadata=ConsolidatedMetadata(
511+
metadata={
512+
"g2": GroupMetadata(
513+
attributes={"key": "g2"},
514+
zarr_format=2,
515+
consolidated_metadata=ConsolidatedMetadata(metadata={}),
516+
)
517+
}
518+
),
519+
),
520+
}
521+
),
522+
)
523+
assert result.metadata == expected

0 commit comments

Comments
 (0)