Repository navigation
Expand file tree
/
Copy pathReadOnlyZipStoreTest.java
More file actions
168 lines (139 loc) · 6.47 KB
/
Copy pathReadOnlyZipStoreTest.java
File metadata and controls
168 lines (139 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package dev.zarr.zarrjava.store;
import dev.zarr.zarrjava.Utils;
import dev.zarr.zarrjava.ZarrException;
import dev.zarr.zarrjava.core.Group;
import dev.zarr.zarrjava.v3.Array;
import dev.zarr.zarrjava.v3.DataType;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
public class ReadOnlyZipStoreTest extends StoreTest {
Path storePath = TESTOUTPUT.resolve("readOnlyZipStoreTest.zip");
StoreHandle storeHandleWithData;
@BeforeAll
void writeStoreHandleWithData() throws ZarrException, IOException {
Path source = TESTDATA.resolve("v2_sample").resolve("subgroup");
Utils.zipFile(source, storePath);
storeHandleWithData = new ReadOnlyZipStore(storePath).resolve("array", "0.0.0");
}
@Override
StoreHandle storeHandleWithData() {
return storeHandleWithData;
}
@Override
StoreHandle storeHandleWithoutData() {
return new ReadOnlyZipStore(storePath).resolve("nonexistent_key");
}
@Override
Store storeWithArrays() {
return new ReadOnlyZipStore(storePath);
}
@Test
public void testUnreadableArchiveThrows() {
ReadOnlyZipStore zipStore = new ReadOnlyZipStore(TESTOUTPUT.resolve("does_not_exist.zip"));
Assertions.assertThrows(StoreException.class, () -> zipStore.resolve().listChildren().count());
Assertions.assertThrows(StoreException.class, () -> zipStore.resolve("array", "0.0.0").exists());
}
@Override
@Test
public void testListChildren() {
ReadOnlyZipStore zipStore = new ReadOnlyZipStore(storePath);
BufferedZipStore bufferedZipStore = new BufferedZipStore(storePath);
Set<String> expectedKeys = bufferedZipStore.resolve().listChildren()
.map(node -> String.join("/", node))
.collect(Collectors.toSet());
Set<String> actualKeys = zipStore.resolve().listChildren()
.map(node -> String.join("/", node))
.collect(Collectors.toSet());
Assertions.assertFalse(actualKeys.isEmpty());
Assertions.assertEquals(expectedKeys, actualKeys);
}
@Override
@Test
public void testList() {
ReadOnlyZipStore zipStore = new ReadOnlyZipStore(storePath);
BufferedZipStore bufferedZipStore = new BufferedZipStore(storePath);
Set<String> expectedKeys = bufferedZipStore.resolve().list()
.map(node -> String.join("/", node))
.collect(Collectors.toSet());
Set<String> actualKeys = zipStore.resolve().list()
.map(node -> String.join("/", node))
.collect(Collectors.toSet());
Assertions.assertEquals(expectedKeys, actualKeys);
}
@Test
public void testOpen() throws ZarrException, IOException {
Path sourceDir = TESTOUTPUT.resolve("testZipStore");
Path targetDir = TESTOUTPUT.resolve("testZipStore.zip");
FilesystemStore fsStore = new FilesystemStore(sourceDir);
writeTestGroupV3(fsStore.resolve(), true);
Utils.zipFile(sourceDir, targetDir);
ReadOnlyZipStore readOnlyZipStore = new ReadOnlyZipStore(targetDir);
assertIsTestGroupV3(Group.open(readOnlyZipStore.resolve()), true);
}
@Test
public void testReadFromBufferedZipStore() throws ZarrException, IOException {
Path path = TESTOUTPUT.resolve("testReadOnlyZipStore.zip");
String archiveComment = "This is a test ZIP archive comment.";
BufferedZipStore zipStore = new BufferedZipStore(path, archiveComment);
writeTestGroupV3(zipStore.resolve(), true);
zipStore.flush();
ReadOnlyZipStore readOnlyZipStore = new ReadOnlyZipStore(path);
Assertions.assertEquals(archiveComment, readOnlyZipStore.getArchiveComment(), "ZIP archive comment from ReadOnlyZipStore does not match expected value.");
Set<String> expectedSubgroupKeys = new HashSet<>(Arrays.asList(
"array/c/1/1",
"array/c/0/0",
"array/c/0/1",
"zarr.json",
"array/c/1/0",
"array/zarr.json"
));
Set<String> actualKeys = readOnlyZipStore.resolve("subgroup").list()
.map(node -> String.join("/", node))
.collect(Collectors.toSet());
Assertions.assertEquals(expectedSubgroupKeys, actualKeys);
assertIsTestGroupV3(Group.open(readOnlyZipStore.resolve()), true);
}
@ParameterizedTest
@ValueSource(strings = {"start", "end"})
public void testPartialReadOfShardedArray(String indexLocation) throws ZarrException, IOException {
// partial shard reads fetch the shard index via a suffix read when it is located at the end
Path sourceDir = TESTOUTPUT.resolve("testShardedZipStore_" + indexLocation);
Path targetDir = TESTOUTPUT.resolve("testShardedZipStore_" + indexLocation + ".zip");
Array writeArray = Array.create(new FilesystemStore(sourceDir).resolve(), Array.metadataBuilder()
.withShape(24, 24)
.withDataType(DataType.INT32)
.withChunkShape(16, 16)
.withCodecs(c -> c.withSharding(new int[]{8, 8}, c1 -> c1.withBytes("LITTLE"), indexLocation))
.withFillValue(0)
.build());
int[] data = new int[24 * 24];
for (int i = 0; i < data.length; i++) {
data[i] = i;
}
ucar.ma2.Array expected = ucar.ma2.Array.factory(ucar.ma2.DataType.INT, new int[]{24, 24}, data);
writeArray.write(expected);
Utils.zipFile(sourceDir, targetDir);
Array[] readArrays = {
Array.open(new ReadOnlyZipStore(targetDir).resolve()),
Array.open(new BufferedZipStore(targetDir).resolve())
};
for (Array readArray : readArrays) {
Assertions.assertArrayEquals(data, (int[]) readArray.read().get1DJavaArray(ucar.ma2.DataType.INT));
ucar.ma2.Array partial = readArray.read(new long[]{3, 5}, new long[]{14, 17});
for (int i = 0; i < 14; i++) {
for (int j = 0; j < 17; j++) {
Assertions.assertEquals(data[(i + 3) * 24 + (j + 5)], partial.getInt(i * 17 + j));
}
}
}
}
}