11package dev .zarr .zarrjava .core ;
22
33import dev .zarr .zarrjava .ZarrException ;
4+ import dev .zarr .zarrjava .core .codec .CodecPipeline ;
45import dev .zarr .zarrjava .store .FilesystemStore ;
56import dev .zarr .zarrjava .store .StoreHandle ;
67import dev .zarr .zarrjava .utils .IndexingUtils ;
78import dev .zarr .zarrjava .utils .MultiArrayUtils ;
89import dev .zarr .zarrjava .utils .Utils ;
9- import dev .zarr .zarrjava .core .codec .CodecPipeline ;
1010import ucar .ma2 .InvalidRangeException ;
1111
1212import javax .annotation .Nonnull ;
2121public abstract class Array extends AbstractNode {
2222
2323 protected CodecPipeline codecPipeline ;
24- public abstract ArrayMetadata metadata ();
2524
2625 protected Array (StoreHandle storeHandle ) throws ZarrException {
2726 super (storeHandle );
@@ -47,7 +46,8 @@ public static Array open(StoreHandle storeHandle) throws IOException, ZarrExcept
4746 throw new ZarrException ("No Zarr array found at the specified location." );
4847 }
4948 }
50- /**
49+
50+ /**
5151 * Opens an existing Zarr array at a specified storage location. Automatically detects the Zarr version.
5252 *
5353 * @param path the storage location of the Zarr array
@@ -69,6 +69,8 @@ public static Array open(String path) throws IOException, ZarrException {
6969 return open (Paths .get (path ));
7070 }
7171
72+ public abstract ArrayMetadata metadata ();
73+
7274 /**
7375 * Writes a ucar.ma2.Array into the Zarr array at a specified offset. The shape of the Zarr array
7476 * needs be large enough for the write.
@@ -94,32 +96,32 @@ public void write(long[] offset, ucar.ma2.Array array, boolean parallel) {
9496 chunkStream = chunkStream .parallel ();
9597 }
9698 chunkStream .forEach (
97- chunkCoords -> {
98- try {
99- final IndexingUtils .ChunkProjection chunkProjection =
100- IndexingUtils .computeProjection (chunkCoords , metadata .shape , chunkShape , offset ,
101- shape
102- );
103-
104- ucar .ma2 .Array chunkArray ;
105- if (IndexingUtils .isFullChunk (chunkProjection .chunkOffset , chunkProjection .shape ,
106- chunkShape
107- )) {
108- chunkArray = array .sectionNoReduce (chunkProjection .outOffset ,
109- chunkProjection .shape ,
110- null
111- );
112- } else {
113- chunkArray = readChunk (chunkCoords );
114- MultiArrayUtils .copyRegion (array , chunkProjection .outOffset , chunkArray ,
115- chunkProjection .chunkOffset , chunkProjection .shape
116- );
99+ chunkCoords -> {
100+ try {
101+ final IndexingUtils .ChunkProjection chunkProjection =
102+ IndexingUtils .computeProjection (chunkCoords , metadata .shape , chunkShape , offset ,
103+ shape
104+ );
105+
106+ ucar .ma2 .Array chunkArray ;
107+ if (IndexingUtils .isFullChunk (chunkProjection .chunkOffset , chunkProjection .shape ,
108+ chunkShape
109+ )) {
110+ chunkArray = array .sectionNoReduce (chunkProjection .outOffset ,
111+ chunkProjection .shape ,
112+ null
113+ );
114+ } else {
115+ chunkArray = readChunk (chunkCoords );
116+ MultiArrayUtils .copyRegion (array , chunkProjection .outOffset , chunkArray ,
117+ chunkProjection .chunkOffset , chunkProjection .shape
118+ );
119+ }
120+ writeChunk (chunkCoords , chunkArray );
121+ } catch (ZarrException | InvalidRangeException e ) {
122+ throw new RuntimeException (e );
117123 }
118- writeChunk (chunkCoords , chunkArray );
119- } catch (ZarrException | InvalidRangeException e ) {
120- throw new RuntimeException (e );
121- }
122- });
124+ });
123125
124126 }
125127
@@ -246,7 +248,7 @@ boolean chunkIsInArray(long[] chunkCoords) {
246248 final int [] chunkShape = metadata ().chunkShape ();
247249 for (int dimIdx = 0 ; dimIdx < metadata ().ndim (); dimIdx ++) {
248250 if (chunkCoords [dimIdx ] < 0
249- || chunkCoords [dimIdx ] * chunkShape [dimIdx ] >= metadata ().shape [dimIdx ]) {
251+ || chunkCoords [dimIdx ] * chunkShape [dimIdx ] >= metadata ().shape [dimIdx ]) {
250252 return false ;
251253 }
252254 }
@@ -282,47 +284,47 @@ public ucar.ma2.Array read(final long[] offset, final int[] shape, final boolean
282284 }
283285
284286 final ucar .ma2 .Array outputArray = ucar .ma2 .Array .factory (metadata .dataType ().getMA2DataType (),
285- shape );
287+ shape );
286288 Stream <long []> chunkStream = Arrays .stream (IndexingUtils .computeChunkCoords (metadata .shape , chunkShape , offset , shape ));
287289 if (parallel ) {
288290 chunkStream = chunkStream .parallel ();
289291 }
290292 chunkStream .forEach (
291- chunkCoords -> {
292- try {
293- final IndexingUtils .ChunkProjection chunkProjection =
294- IndexingUtils .computeProjection (chunkCoords , metadata .shape , chunkShape , offset ,
295- shape
296- );
297-
298- if (chunkIsInArray (chunkCoords )) {
299- MultiArrayUtils .copyRegion (metadata .allocateFillValueChunk (),
300- chunkProjection .chunkOffset , outputArray , chunkProjection .outOffset ,
301- chunkProjection .shape
302- );
303- }
304-
305- final String [] chunkKeys = metadata .chunkKeyEncoding ().encodeChunkKey (chunkCoords );
306- final StoreHandle chunkHandle = storeHandle .resolve (chunkKeys );
307- if (!chunkHandle .exists ()) {
308- return ;
293+ chunkCoords -> {
294+ try {
295+ final IndexingUtils .ChunkProjection chunkProjection =
296+ IndexingUtils .computeProjection (chunkCoords , metadata .shape , chunkShape , offset ,
297+ shape
298+ );
299+
300+ if (chunkIsInArray (chunkCoords )) {
301+ MultiArrayUtils .copyRegion (metadata .allocateFillValueChunk (),
302+ chunkProjection .chunkOffset , outputArray , chunkProjection .outOffset ,
303+ chunkProjection .shape
304+ );
305+ }
306+
307+ final String [] chunkKeys = metadata .chunkKeyEncoding ().encodeChunkKey (chunkCoords );
308+ final StoreHandle chunkHandle = storeHandle .resolve (chunkKeys );
309+ if (!chunkHandle .exists ()) {
310+ return ;
311+ }
312+ if (codecPipeline .supportsPartialDecode ()) {
313+ final ucar .ma2 .Array chunkArray = codecPipeline .decodePartial (chunkHandle ,
314+ Utils .toLongArray (chunkProjection .chunkOffset ), chunkProjection .shape );
315+ MultiArrayUtils .copyRegion (chunkArray , new int [metadata .ndim ()], outputArray ,
316+ chunkProjection .outOffset , chunkProjection .shape
317+ );
318+ } else {
319+ MultiArrayUtils .copyRegion (readChunk (chunkCoords ), chunkProjection .chunkOffset ,
320+ outputArray , chunkProjection .outOffset , chunkProjection .shape
321+ );
322+ }
323+
324+ } catch (ZarrException e ) {
325+ throw new RuntimeException (e );
309326 }
310- if (codecPipeline .supportsPartialDecode ()) {
311- final ucar .ma2 .Array chunkArray = codecPipeline .decodePartial (chunkHandle ,
312- Utils .toLongArray (chunkProjection .chunkOffset ), chunkProjection .shape );
313- MultiArrayUtils .copyRegion (chunkArray , new int [metadata .ndim ()], outputArray ,
314- chunkProjection .outOffset , chunkProjection .shape
315- );
316- } else {
317- MultiArrayUtils .copyRegion (readChunk (chunkCoords ), chunkProjection .chunkOffset ,
318- outputArray , chunkProjection .outOffset , chunkProjection .shape
319- );
320- }
321-
322- } catch (ZarrException e ) {
323- throw new RuntimeException (e );
324- }
325- });
327+ });
326328 return outputArray ;
327329 }
328330
0 commit comments