-
Notifications
You must be signed in to change notification settings - Fork 14
Memory store #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Memory store #36
Changes from 12 commits
2d29a3b
218f079
fa2626b
4763778
36cb46a
78fa912
bd3bd7c
e1f8e49
50b40f1
dbf7541
6094489
250daf1
a6512de
135ca6d
bb5bdf9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package dev.zarr.zarrjava.store; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| public class ConcurrentMemoryStore extends MemoryStore { | ||
| @Override | ||
| protected Map<List<String>, byte[]> map() { | ||
| return new ConcurrentHashMap<>(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return String.format("<ConcurrentMemoryStore {%s}>", hashCode()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package dev.zarr.zarrjava.store; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
| import javax.annotation.Nullable; | ||
| import java.nio.ByteBuffer; | ||
| import java.util.*; | ||
| import java.util.stream.Stream; | ||
|
|
||
| public class MemoryStore implements Store, Store.ListableStore { | ||
| private final Map<List<String>, byte[]> map = map(); | ||
|
|
||
| protected Map<List<String>, byte[]> map(){ | ||
| return new HashMap<>(); | ||
| } | ||
|
|
||
| List<String> resolveKeys(String[] keys) { | ||
| ArrayList<String> resolvedKeys = new ArrayList<>(); | ||
| for(String key:keys){ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have a code formatter set up?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, the code is not uniformly formatted. Sometimes it's two spaces and sometimes it's tabs.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could format the entire code in a separate PR, so that changes are easier to understand?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, please do. |
||
| if(key.startsWith("/")){ | ||
| key = key.substring(1); | ||
| } | ||
| resolvedKeys.addAll(Arrays.asList(key.split("/"))); | ||
| } | ||
| return resolvedKeys; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean exists(String[] keys) { | ||
| return map.containsKey(resolveKeys(keys)); | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public ByteBuffer get(String[] keys) { | ||
| return get(keys, 0); | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public ByteBuffer get(String[] keys, long start) { | ||
| return get(keys, start, -1); | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public ByteBuffer get(String[] keys, long start, long end) { | ||
| byte[] bytes = map.get(resolveKeys(keys)); | ||
| if (bytes == null) return null; | ||
| if (end < 0) end = bytes.length; | ||
| if (end > Integer.MAX_VALUE) throw new RuntimeException("TODO"); //TODO | ||
| return ByteBuffer.wrap(bytes, (int) start, (int) end); | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public void set(String[] keys, ByteBuffer bytes) { | ||
| map.put(resolveKeys(keys), bytes.array()); | ||
| } | ||
|
|
||
| @Override | ||
| public void delete(String[] keys) { | ||
| map.remove(resolveKeys(keys)); | ||
| } | ||
|
|
||
| public Stream<String> list(String[] keys) { | ||
| List<String> prefix = resolveKeys(keys); | ||
| Set<String> allKeys = new HashSet<>(); | ||
|
|
||
| for (List<String> k : map.keySet()) { | ||
| if (k.size() <= prefix.size() || ! k.subList(0, prefix.size()).equals(prefix)) | ||
| continue; | ||
| for (int i = 0; i < k.size(); i++) { | ||
| List<String> subKey = k.subList(0, i+1); | ||
| allKeys.add(String.join("/", subKey)); | ||
| } | ||
| } | ||
| return allKeys.stream(); | ||
| } | ||
|
|
||
| @Nonnull | ||
| @Override | ||
| public StoreHandle resolve(String... keys) { | ||
| return new StoreHandle(this, keys); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return String.format("<MemoryStore {%s}>", hashCode()); | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.