|
118 | 118 | ArrayV2MetadataDict, |
119 | 119 | ArrayV3Metadata, |
120 | 120 | ) |
121 | | -from zarr.core.metadata.io import save_metadata, upsert_metadata |
122 | | -from zarr.core.metadata.upgrades import upgrade_array_document |
| 121 | +from zarr.core.metadata.io import read_stored_array, save_metadata, upsert_metadata |
| 122 | +from zarr.core.metadata.upgrades import mark_upgraded, upgrade_array_document |
123 | 123 | from zarr.core.metadata.v2 import ( |
124 | 124 | CompressorLikev2, |
125 | 125 | get_object_codec_id, |
@@ -202,10 +202,30 @@ def _chunk_sizes_from_shape( |
202 | 202 | return tuple(result) |
203 | 203 |
|
204 | 204 |
|
| 205 | +def _as_json(value: Any) -> Any: |
| 206 | + """`value`, as `to_dict` returns it, with its tuples as JSON arrays.""" |
| 207 | + match value: |
| 208 | + case tuple() | list(): |
| 209 | + return [_as_json(item) for item in value] |
| 210 | + case dict(): |
| 211 | + return {key: _as_json(item) for key, item in value.items()} |
| 212 | + return value |
| 213 | + |
| 214 | + |
205 | 215 | def parse_array_metadata(data: Any, path: str | None = None) -> ArrayMetadata: |
| 216 | + """Array metadata from a metadata object or a metadata document, naming the array at |
| 217 | + `path` in warnings about how an invalid document was read. |
| 218 | +
|
| 219 | + The metadata constructors accept chunk sizes that only an invalid document holds |
| 220 | + (such as 0), as they always have; such metadata is read as its document is (see |
| 221 | + `zarr.core.metadata.upgrades`), so an array can be built from it. No data was read |
| 222 | + or written under those chunk sizes, so none of its readings is a warning.""" |
206 | 223 | if isinstance(data, ArrayMetadata): |
207 | | - return data |
208 | | - elif isinstance(data, dict): |
| 224 | + document, readings = upgrade_array_document(_as_json(data.to_dict()), data.zarr_format) |
| 225 | + if not readings: |
| 226 | + return data |
| 227 | + return mark_upgraded(parse_array_metadata(dict(document)), [None for _ in readings], path) |
| 228 | + if isinstance(data, dict): |
209 | 229 | zarr_format = data.get("zarr_format") |
210 | 230 | if zarr_format == 3: |
211 | 231 | meta_out = ArrayV3Metadata.from_dict(data, path=path) |
@@ -1617,22 +1637,31 @@ async def _save_metadata(self, metadata: ArrayMetadata, ensure_parents: bool = F |
1617 | 1637 | await save_metadata(self.store_path, metadata, ensure_parents=ensure_parents) |
1618 | 1638 |
|
1619 | 1639 | async def _store_upgraded_document(self) -> None: |
1620 | | - """Store the upgrade of this array's current stored document, if it needs one. |
| 1640 | + """Store the upgrade of this array's current stored document, if it needs one, |
| 1641 | + before chunks are written under this handle's metadata. |
1621 | 1642 |
|
1622 | 1643 | Only for metadata read from a document that had to be upgraded (see |
1623 | 1644 | `zarr.core.metadata.upgrades`). The document is read again, because the store may |
1624 | | - hold a newer one than this handle's metadata: if that one needs no upgrade (the |
1625 | | - array was re-saved or resized since, possibly by another implementation), it is |
1626 | | - left as written. Storing the same upgrade twice is harmless, so concurrent |
1627 | | - callers need no coordination. |
| 1645 | + hold a newer one than this handle's metadata. If that one lays out chunks |
| 1646 | + differently (the array was resized since by software that kept the invalid chunk |
| 1647 | + size), this handle would write chunks no reader finds, so it raises and stores |
| 1648 | + nothing. If it needs no upgrade (the array was re-saved since, possibly by another |
| 1649 | + implementation), it is left as written; if there is none, there is nothing to |
| 1650 | + upgrade. Storing the same upgrade twice is harmless, so concurrent callers need |
| 1651 | + no coordination. |
1628 | 1652 | """ |
1629 | 1653 | if not self.metadata._stored_document_upgraded: |
1630 | 1654 | return |
1631 | | - zarr_format = self.metadata.zarr_format |
1632 | | - stored = await get_array_metadata(self.store_path, zarr_format=zarr_format) |
1633 | | - upgraded, readings = upgrade_array_document(stored, zarr_format) |
1634 | | - if readings: |
1635 | | - await upsert_metadata(self.store_path, parse_array_metadata(upgraded)) |
| 1655 | + read = await read_stored_array(self.store_path, self.metadata.zarr_format) |
| 1656 | + if read is not None: |
| 1657 | + current, upgraded = read |
| 1658 | + if _chunk_layout(current) != _chunk_layout(self.metadata): |
| 1659 | + raise ValueError( |
| 1660 | + f"The metadata stored for the array at {str(self.store_path)!r} has " |
| 1661 | + "changed since this array was opened: reopen the array to write to it." |
| 1662 | + ) |
| 1663 | + if upgraded: |
| 1664 | + await upsert_metadata(self.store_path, current) |
1636 | 1665 | object.__setattr__(self.metadata, "_stored_document_upgraded", False) |
1637 | 1666 |
|
1638 | 1667 | async def _set_selection( |
@@ -4864,6 +4893,14 @@ async def create_array( |
4864 | 4893 | ) |
4865 | 4894 |
|
4866 | 4895 |
|
| 4896 | +def _chunk_layout(metadata: ArrayMetadata) -> tuple[object, tuple[int, ...] | None]: |
| 4897 | + """How an array's chunks are laid out: its chunk grid and, if it is sharded, the |
| 4898 | + inner chunk shape.""" |
| 4899 | + grid = metadata.chunks if isinstance(metadata, ArrayV2Metadata) else metadata.chunk_grid |
| 4900 | + sharding = _sharding_codec(metadata) |
| 4901 | + return grid, None if sharding is None else sharding.chunk_shape |
| 4902 | + |
| 4903 | + |
4867 | 4904 | def _sharding_codec(metadata: ArrayMetadata) -> ShardingCodec | None: |
4868 | 4905 | """The array's sharding codec, or None if the array is not sharded. |
4869 | 4906 |
|
|
0 commit comments