Skip to content

Commit fa2626b

Browse files
committed
fix memorystore with List<String> keys
1 parent 218f079 commit fa2626b

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,11 @@
55
import javax.annotation.Nonnull;
66
import javax.annotation.Nullable;
77
import java.nio.ByteBuffer;
8-
import java.nio.file.Path;
9-
import java.util.HashMap;
10-
import java.util.HashSet;
11-
import java.util.Map;
12-
import java.util.Set;
8+
import java.util.*;
139
import java.util.stream.Stream;
1410

1511
public class MemoryStore implements Store, Store.ListableStore {
16-
private final Map<String, byte[]> map = new HashMap<>();
12+
private final Map<List<String>, byte[]> map = new HashMap<>();
1713
Separator separator;
1814

1915
public MemoryStore(Separator separator){
@@ -24,8 +20,15 @@ public MemoryStore(){
2420
this(Separator.SLASH);
2521
}
2622

27-
String resolveKeys(String[] keys) {
28-
return String.join(separator.getValue(), keys);
23+
List<String> resolveKeys(String[] keys) {
24+
ArrayList<String> resolvedKeys = new ArrayList<>();
25+
for(String key:keys){
26+
if(key.startsWith("/")){
27+
key = key.substring(1);
28+
}
29+
resolvedKeys.addAll(Arrays.asList(key.split("/")));
30+
}
31+
return resolvedKeys;
2932
}
3033

3134
@Override
@@ -67,16 +70,15 @@ public void delete(String[] keys) {
6770
}
6871

6972
public Stream<String> list(String[] keys) {
70-
String prefix = resolveKeys(keys);
73+
List<String> prefix = resolveKeys(keys);
7174
Set<String> allKeys = new HashSet<>();
7275

73-
for (String k : map.keySet()) {
74-
if (!k.startsWith(prefix)) continue;
75-
String current = "";
76-
for (String s : k.split(separator.getValue())) {
77-
current += s;
78-
allKeys.add(current);
79-
current += separator.getValue();
76+
for (List<String> k : map.keySet()) {
77+
if (k.size() <= prefix.size() || ! k.subList(0, prefix.size()).equals(prefix))
78+
continue;
79+
for (int i = 0; i < k.size(); i++) {
80+
List<String> subKey = k.subList(0, i+1);
81+
allKeys.add(String.join("/", subKey));
8082
}
8183
}
8284
return allKeys.stream();

0 commit comments

Comments
 (0)