@@ -3085,6 +3085,7 @@ async def create_hierarchy(
30853085 # ensure that all nodes have the same zarr_format, and add implicit groups as needed
30863086 nodes_parsed = _parse_hierarchy_dict (data = nodes_normed_keys )
30873087 redundant_implicit_groups = []
3088+ to_delete_keys : list [str ] = []
30883089
30893090 # empty hierarchies should be a no-op
30903091 if len (nodes_parsed ) > 0 :
@@ -3117,13 +3118,7 @@ async def create_hierarchy(
31173118 if overwrite :
31183119 # we will remove any nodes that collide with arrays and non-implicit groups defined in
31193120 # nodes
3120-
3121- # track the keys of nodes we need to delete
3122- to_delete_keys = []
3123- to_delete_keys .extend (
3124- [k for k , v in nodes_parsed .items () if k not in implicit_group_keys ]
3125- )
3126- await asyncio .gather (* (store .delete_dir (key ) for key in to_delete_keys ))
3121+ to_delete_keys = [k for k in nodes_parsed if k not in implicit_group_keys ]
31273122 else :
31283123 # This type is long.
31293124 coros : (
@@ -3183,7 +3178,11 @@ async def create_hierarchy(
31833178 else :
31843179 nodes_explicit [k ] = v
31853180
3186- async for key , node in create_nodes (store = store , nodes = nodes_explicit ):
3181+ # Build every node before deleting or storing anything: metadata that no array or
3182+ # group can be built from then fails with the store untouched.
3183+ built = _build_nodes (store , nodes_explicit )
3184+ await asyncio .gather (* (store .delete_dir (key ) for key in to_delete_keys ))
3185+ async for key , node in _store_nodes (store , nodes_explicit , built ):
31873186 yield key , node
31883187
31893188
@@ -3213,7 +3212,26 @@ async def create_nodes(
32133212 AsyncGroup | AsyncArray
32143213 The created nodes in the order they are created.
32153214 """
3215+ async for key , node in _store_nodes (store , nodes , _build_nodes (store , nodes )):
3216+ yield key , node
3217+
32163218
3219+ def _build_nodes (
3220+ store : Store , nodes : Mapping [str , GroupMetadata | ArrayV2Metadata | ArrayV3Metadata ]
3221+ ) -> dict [str , AsyncGroup | AnyAsyncArray ]:
3222+ """The array or group each of `nodes` describes, at its path in `store`."""
3223+ return {
3224+ path : _build_node (store = store , path = path , metadata = meta ) for path , meta in nodes .items ()
3225+ }
3226+
3227+
3228+ async def _store_nodes (
3229+ store : Store ,
3230+ nodes : Mapping [str , GroupMetadata | ArrayV2Metadata | ArrayV3Metadata ],
3231+ built : Mapping [str , AsyncGroup | AnyAsyncArray ],
3232+ ) -> AsyncIterator [tuple [str , AsyncGroup | AnyAsyncArray ]]:
3233+ """Store the metadata of `nodes` and yield the nodes `_build_nodes` built from them
3234+ (see `create_nodes`)."""
32173235 # Note: the only way to alter this value is via the config. If that's undesirable for some reason,
32183236 # then we should consider adding a keyword argument to this function
32193237 semaphore = asyncio .Semaphore (config .get ("async.concurrency" ))
@@ -3241,7 +3259,7 @@ async def create_nodes(
32413259 node_name = created_key [: created_key .rfind ("/" )]
32423260 meta_out = nodes [node_name ]
32433261 if meta_out .zarr_format == 3 :
3244- yield node_name , _build_node ( store = store , path = node_name , metadata = meta_out )
3262+ yield node_name , built [ node_name ]
32453263 else :
32463264 # For zarr v2
32473265 # we only want to yield when both the metadata and attributes are created
@@ -3256,7 +3274,7 @@ async def create_nodes(
32563274 meta_done = _join_paths ([node_name , ZARRAY_JSON ]) in created_object_keys
32573275
32583276 if meta_done and attrs_done :
3259- yield node_name , _build_node ( store = store , path = node_name , metadata = meta_out )
3277+ yield node_name , built [ node_name ]
32603278
32613279 continue
32623280
0 commit comments