Skip to content

Commit 2611738

Browse files
committed
refactor and unify outputs of Store.list
1 parent 8e79825 commit 2611738

File tree

9 files changed

+46
-23
lines changed

9 files changed

+46
-23
lines changed

src/main/java/dev/zarr/zarrjava/core/Group.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ public static Group open(String path) throws IOException, ZarrException {
6464
}
6565

6666
@Nullable
67-
public abstract Node get(String key) throws ZarrException, IOException;
67+
public abstract Node get(String[] key) throws ZarrException, IOException;
68+
69+
@Nullable
70+
public Node get(String key) throws ZarrException, IOException {
71+
return get(new String[]{key});
72+
}
6873

6974
public Stream<Node> list() {
7075
return storeHandle.list()

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,16 @@ public void delete(String[] keys) {
120120
throw new RuntimeException(e);
121121
}
122122
}
123-
124-
public Stream<String> list(String[] keys) {
123+
public Stream<String[]> list(String[] keys) {
125124
try {
126-
return Files.list(resolveKeys(keys)).map(p -> p.toFile().getName());
125+
return Files.list(resolveKeys(keys)).map(path -> {
126+
Path relativePath = resolveKeys(keys).relativize(path);
127+
String[] parts = new String[relativePath.getNameCount()];
128+
for (int i = 0; i < relativePath.getNameCount(); i++) {
129+
parts[i] = relativePath.getName(i).toString();
130+
}
131+
return parts;
132+
});
127133
} catch (IOException e) {
128134
throw new RuntimeException(e);
129135
}

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,18 @@ public void delete(String[] keys) {
5959
map.remove(resolveKeys(keys));
6060
}
6161

62-
public Stream<String> list(String[] keys) {
63-
List<String> prefix = resolveKeys(keys);
64-
Set<String> allKeys = new HashSet<>();
62+
public Stream<String[]> list(String[] keys) {
63+
List<String> prefix = resolveKeys(keys);
64+
Set<List<String>> allKeys = new HashSet<>();
6565

66-
for (List<String> k : map.keySet()) {
67-
if (k.size() <= prefix.size() || ! k.subList(0, prefix.size()).equals(prefix))
68-
continue;
69-
for (int i = 0; i < k.size(); i++) {
70-
List<String> subKey = k.subList(0, i+1);
71-
allKeys.add(String.join("/", subKey));
66+
for (List<String> k : map.keySet()) {
67+
if (k.size() <= prefix.size() || ! k.subList(0, prefix.size()).equals(prefix))
68+
continue;
69+
for (int i = prefix.size(); i < k.size(); i++) {
70+
allKeys.add(k.subList(0, i+1));
71+
}
7272
}
73-
}
74-
return allKeys.stream();
73+
return allKeys.stream().map(k -> k.toArray(new String[0]));
7574
}
7675

7776
@Nonnull

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,15 @@ public void delete(String[] keys) {
104104
}
105105

106106
@Override
107-
public Stream<String> list(String[] keys) {
107+
public Stream<String[]> list(String[] keys) {
108108
final String fullKey = resolveKeys(keys);
109109
ListObjectsRequest req = ListObjectsRequest.builder()
110110
.bucket(bucketName).prefix(fullKey)
111111
.build();
112112
ListObjectsResponse res = s3client.listObjects(req);
113113
return res.contents()
114114
.stream()
115-
.map(p -> p.key().substring(fullKey.length() + 1));
115+
.map(p -> p.key().substring(fullKey.length() + 1).split("/"));
116116
}
117117

118118
@Nonnull

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ public interface Store {
2727

2828
interface ListableStore extends Store {
2929

30-
Stream<String> list(String[] keys);
30+
/**
31+
* Lists all keys in the store that match the given prefix keys. Keys are represented as arrays of strings,
32+
* where each string is a segment of the key path.
33+
* Keys that are exactly equal to the prefix are not included in the results.
34+
* Keys that do not contain data (i.e. "directories") are included in the results.
35+
*
36+
* @param keys The prefix keys to match.
37+
* @return A stream of key arrays that match the given prefix. Prefixed keys are not included in the results.
38+
*/
39+
Stream<String[]> list(String[] keys);
40+
41+
default Stream<String[]> list() {
42+
return list(new String[]{});
43+
}
3144
}
3245
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public boolean exists() {
5656
return store.exists(keys);
5757
}
5858

59-
public Stream<String> list() {
59+
public Stream<String[]> list() {
6060
if (!(store instanceof Store.ListableStore)) {
6161
throw new UnsupportedOperationException("The underlying store does not support listing.");
6262
}

src/main/java/dev/zarr/zarrjava/v2/Group.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public static Group create(String path, Attributes attributes) throws IOExceptio
169169
* @throws IOException if there is an error accessing the storage
170170
*/
171171
@Nullable
172-
public Node get(String key) throws ZarrException, IOException {
172+
public Node get(String[] key) throws ZarrException, IOException {
173173
StoreHandle keyHandle = storeHandle.resolve(key);
174174
try {
175175
return Node.open(keyHandle);

src/main/java/dev/zarr/zarrjava/v3/Group.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public static Group create(String path) throws IOException, ZarrException {
182182
* @throws IOException if there is an error accessing the storage
183183
*/
184184
@Nullable
185-
public Node get(String key) throws ZarrException, IOException{
185+
public Node get(String[] key) throws ZarrException, IOException{
186186
StoreHandle keyHandle = storeHandle.resolve(key);
187187
try {
188188
return Node.open(keyHandle);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public void testMemoryStoreV2(boolean useParallel) throws ZarrException, IOExcep
123123
dev.zarr.zarrjava.v2.Array array = group.createArray("array", b -> b
124124
.withShape(1024, 1024)
125125
.withDataType(dev.zarr.zarrjava.v2.DataType.UINT32)
126-
.withChunks(5, 5)
126+
.withChunks(512, 512)
127127
);
128128
array.write(ucar.ma2.Array.factory(ucar.ma2.DataType.UINT, new int[]{1024, 1024}, testData), useParallel);
129129
group.createGroup("subgroup");
@@ -195,7 +195,7 @@ Group writeTestGroupV3(Store store, boolean useParallel) throws ZarrException, I
195195
return group;
196196
}
197197

198-
void assertIsTestGroupV3(Group group, boolean useParallel) throws ZarrException {
198+
void assertIsTestGroupV3(Group group, boolean useParallel) throws ZarrException, IOException {
199199
Stream<dev.zarr.zarrjava.core.Node> nodes = group.list();
200200
Assertions.assertEquals(2, nodes.count());
201201
Array array = (Array) group.get("array");

0 commit comments

Comments
 (0)