Skip to content

Commit 7bc8b4a

Browse files
committed
Sort zarr.json files in breadth-first order within BufferedZipStore
1 parent 025265c commit 7bc8b4a

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/main/java/dev/zarr/zarrjava/store/BufferedZipStore.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,30 @@ private void writeBuffer() throws IOException{
3333
if (archiveComment != null) {
3434
zos.setComment(archiveComment);
3535
}
36+
Stream<String[]> entries = bufferStore.list().sorted(
37+
(a, b) -> {
38+
boolean aIsZarr = a.length > 0 && a[a.length - 1].equals("zarr.json");
39+
boolean bIsZarr = b.length > 0 && b[b.length - 1].equals("zarr.json");
40+
// first all zarr.json files
41+
if (aIsZarr && !bIsZarr) {
42+
return -1;
43+
} else if (!aIsZarr && bIsZarr) {
44+
return 1;
45+
} else if (aIsZarr && bIsZarr) {
46+
// sort zarr.json in BFS order within same depth by lexicographical order
47+
if (a.length != b.length) {
48+
return Integer.compare(a.length, b.length);
49+
} else {
50+
return String.join("/", a).compareTo(String.join("/", b));
51+
}
52+
} else {
53+
// then all other files in lexicographical order
54+
return String.join("/", a).compareTo(String.join("/", b));
55+
}
56+
}
57+
);
3658

37-
bufferStore.list().forEach(keys -> {
59+
entries.forEach(keys -> {
3860
try {
3961
if (keys == null || keys.length == 0) {
4062
// skip root entry

src/test/java/dev/zarr/zarrjava/ZarrStoreTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public void testZipStoreRequirements() throws ZarrException, IOException {
225225
}
226226

227227
// correct order of zarr.json files
228-
String[] expectedZarrJsonOrder = new String[]{
228+
String[] expectedFirstEntries = new String[]{
229229
"zarr.json",
230230
"a1/zarr.json",
231231
"g1/zarr.json",
@@ -236,11 +236,12 @@ public void testZipStoreRequirements() throws ZarrException, IOException {
236236
"g2/g2_1/zarr.json",
237237
"g1/g1_1/g1_1_1/zarr.json"
238238
};
239-
String[] actualZarrJsonOrder = entries.stream()
239+
String[] actualFirstEntries = entries.stream()
240240
.map(ZipArchiveEntry::getName)
241-
.limit(expectedZarrJsonOrder.length)
241+
.limit(expectedFirstEntries.length)
242242
.toArray(String[]::new);
243-
Assertions.assertArrayEquals(expectedZarrJsonOrder, actualZarrJsonOrder, "zarr.json files are not in the expected breadth-first order");
243+
244+
Assertions.assertArrayEquals(expectedFirstEntries, actualFirstEntries, "zarr.json files are not in the expected breadth-first order");
244245
}
245246
}
246247

0 commit comments

Comments
 (0)