-
Couldn't load subscription status.
- Fork 18
feat: Added in-memory storage for testing purposes #59
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?
Conversation
|
👋 Thanks for assigning @tankyleo as a reviewer! |
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.
Thanks for looking into this!
Generally goes into the right direction, but we def. need to avoid re-allocating everything on every operation.
4980a75 to
25d57e3
Compare
|
@tnull Have done the required changes |
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.
Looks much better, but I think we still need to handle global_version properly, even if we're currently not using it client-side.
| } | ||
| } | ||
|
|
||
| async fn put( |
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.
Seems we'll also need to track a global_version and set it if it's set in the request?
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.
@tnull I've added handling for global_version in put and delete:
- In
put:- If
request.global_versionis set, check it against the current global version - After successful transaction, increment the global version and store it.
- If
- In
delete:- Increment the global version after a successful delete
|
🔔 1st Reminder Hey @tankyleo! This PR has been waiting for your review. |
25d57e3 to
9012e95
Compare
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.
One comment, will take another look once @tankyleo also had a chance to do a review round here.
rust/impls/src/in_memory_store.rs
Outdated
| created_at: now, | ||
| last_updated_at: now, | ||
| }; | ||
| guard.insert(global_key, record); |
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.
Shouldn't we only update the entry if it's already existing, so that we don't always override created_at? Maybe rather use guard.entry().or_insert_with(..) (here and elsewhere)?
9012e95 to
3b434d0
Compare
|
@tankyleo Can you please review it! |
|
🔔 3rd Reminder Hey @tankyleo! This PR has been waiting for your review. |
|
🔔 4th Reminder Hey @tankyleo! This PR has been waiting for your review. |
|
🔔 5th Reminder Hey @tankyleo! This PR has been waiting for your review. |
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.
Sorry for the delay !
rust/impls/src/in_memory_store.rs
Outdated
| Self { store: Arc::new(Mutex::new(HashMap::new())) } | ||
| } | ||
|
|
||
| fn build_vss_record(&self, user_token: String, store_id: String, kv: KeyValue) -> VssDbRecord { |
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.
Let's make this into a helper function, as it does not need &self and does not return Self
rust/impls/src/in_memory_store.rs
Outdated
| } | ||
| } | ||
|
|
||
| fn build_key(user_token: &str, store_id: &str, key: &str) -> String { |
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.
Same here pull this out into a helper function in this file
rust/impls/src/in_memory_store.rs
Outdated
| match entry { | ||
| std::collections::hash_map::Entry::Occupied(mut occ) => { | ||
| let existing = occ.get_mut(); | ||
| if existing.version >= record.version { |
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.
For already existing records, a conflict error should be returned anytime the versions don't match, unless record.version has been set to -1 (meaning non-conditional update).
rust/impls/src/in_memory_store.rs
Outdated
| if existing.version != record.version { | ||
| return Ok(()); | ||
| } |
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.
If record.version is -1, this should be skipped and we should do a non-conditional delete.
| version: record.version, | ||
| }), | ||
| }) | ||
| } else if request.key == GLOBAL_VERSION_KEY { |
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.
Looks like by the time we are here, we know the GLOBAL_VERSION_KEY does not have a value, otherwise guard.get would have returned Some previously. We can just return the GetObjectResponse below directly with version: 0.
rust/impls/src/in_memory_store.rs
Outdated
| for kv in &request.transaction_items { | ||
| let key = Self::build_key(&user_token, &store_id, &kv.key); | ||
| if let Some(existing) = guard.get(&key) { | ||
| if existing.version >= kv.version { |
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.
Similar to the above comment, shouldn't this just make sure that the existing.version == kv.version, otherwise throw a conflict ? Unless the kv.version is -1.
| let key = Self::build_key(&user_token, &store_id, &kv.key); | ||
| if let Some(existing) = guard.get(&key) { | ||
| if existing.version >= kv.version { | ||
| return Err(VssError::ConflictError(format!( |
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.
Let's pick a single spot to return ConflictError here similar to how things are done in postgres_store. As much as possible we should be checking for conflicts in a single spot.
| use api::kv_store::INITIAL_RECORD_VERSION; | ||
| use bytes::Bytes; | ||
| use tokio::test; | ||
|
|
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.
Like we have in postgres_store, let's add a line similar to below here and make sure the tests pass
define_kv_store_tests!(InMemoryKvStoreTest, InMemoryBackendImpl, InMemoryBackendImpl::new());
| pub(crate) const INIT_DB_CMD: &str = "CREATE DATABASE"; | ||
| #[cfg(test)] | ||
| const DROP_DB_CMD: &str = "DROP DATABASE"; | ||
| pub(crate) const DROP_DB_CMD: &str = "DROP DATABASE"; |
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.
Thanks ! let's pull the formatting changes in postgres_store + pub(crate) changes into a separate commit, and keep the in_memory_store changes to a single commit
3b434d0 to
e0c31bb
Compare
|
@tankyleo Have done with the required changes! Can you please review it |
8003119 to
5898609
Compare
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.
When testing integration with LDK Node locally I found that the tests are currently failing. I now opened #62 to add LDK Node integration tests to our CI here. It would be great if that could land first, and we could also add a CI job for the in-memory store as part of this PR then, ensuring the implementation actually works as expected.
|
🔔 1st Reminder Hey @tankyleo! This PR has been waiting for your review. |
Have added in_memory store for testing purpose.
We can edit config file to use specific store either postgresql or memory