-
Notifications
You must be signed in to change notification settings - Fork 17
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
Merged
Merged
Memory store #36
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2d29a3b
add MemoryStore
brokkoli71 218f079
memorystore map string keys
brokkoli71 fa2626b
fix memorystore with List<String> keys
brokkoli71 4763778
Merge branch 'main' into memory-store
brokkoli71 36cb46a
remove code duplication and add v2.Group.setAttributes
brokkoli71 78fa912
add ConcurrentMemoryStore
brokkoli71 bd3bd7c
add tests
brokkoli71 e1f8e49
fix Group.writeMetadata
brokkoli71 50b40f1
add attributes to tests
brokkoli71 dbf7541
Merge branch 'main' into memory-store
brokkoli71 6094489
Array.create and Group.create with no store/path arguments should def…
brokkoli71 250daf1
Add Javadoc comments for Group methods
brokkoli71 a6512de
make MemoryStore allways concurrent
brokkoli71 135ca6d
end index validation in MemoryStore
brokkoli71 bb5bdf9
Merge branch 'main' into memory-store
brokkoli71 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package dev.zarr.zarrjava.store; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
| import javax.annotation.Nullable; | ||
| import java.nio.ByteBuffer; | ||
| import java.util.*; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.stream.Stream; | ||
|
|
||
| public class MemoryStore implements Store, Store.ListableStore { | ||
| private final Map<List<String>, byte[]> map = new ConcurrentHashMap<>(); | ||
|
|
||
| List<String> resolveKeys(String[] keys) { | ||
| ArrayList<String> resolvedKeys = new ArrayList<>(); | ||
| for(String key:keys){ | ||
| 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 IllegalArgumentException("End index too large"); | ||
| 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()); | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have a code formatter set up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, please do.