-
Notifications
You must be signed in to change notification settings - Fork 68
starknet_patricia_storage: two layer storage #13990
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
Open
ArielElp
wants to merge
1
commit into
ariel/move_tests_module
Choose a base branch
from
ariel/add_two_layer_storage
base: ariel/move_tests_module
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+127
−0
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,3 +15,4 @@ pub mod short_key_storage; | |
| #[cfg(test)] | ||
| pub mod storage_test; | ||
| pub mod storage_trait; | ||
| pub mod two_layer_storage; | ||
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,70 @@ | ||
| use crate::storage_trait::{ | ||
| DbKey, | ||
| DbValue, | ||
| ImmutableReadOnlyStorage, | ||
| PatriciaStorageResult, | ||
| ReadOnlyStorage, | ||
| }; | ||
|
|
||
| #[cfg(test)] | ||
| #[path = "two_layer_storage_test.rs"] | ||
| mod two_layer_storage_test; | ||
| /// Overlay reads on top of a borrowed base [`ImmutableReadOnlyStorage`]: `overlay` is consulted | ||
| /// first via [`ImmutableReadOnlyStorage::get`] / [`ImmutableReadOnlyStorage::mget`]; on miss, | ||
| /// reads relay to `base`. | ||
| /// | ||
| /// [`ReadOnlyStorage::get_mut`] / [`ReadOnlyStorage::mget_mut`] use the same immutable overlay and | ||
| /// base paths on overlay misses so the composite implements [`ReadOnlyStorage`] while holding | ||
| /// `&'a Base`. Patricia paths reads do not mutate the underlying storage. | ||
| /// This allows passing `TwoLayerStorage` to `fetch_all_patricia_paths`, which requires `&mut` | ||
| /// [`ReadOnlyStorage`]. | ||
| pub struct TwoLayerStorage<'a, Overlay, Base> | ||
| where | ||
| Overlay: ImmutableReadOnlyStorage + Sync, | ||
| Base: ImmutableReadOnlyStorage + Sync + ?Sized, | ||
| { | ||
| overlay: Overlay, | ||
| base: &'a Base, | ||
| } | ||
|
|
||
| impl<'a, Overlay, Base> TwoLayerStorage<'a, Overlay, Base> | ||
| where | ||
| Overlay: ImmutableReadOnlyStorage + Sync, | ||
| Base: ImmutableReadOnlyStorage + Sync + ?Sized, | ||
| { | ||
| pub fn new(overlay: Overlay, base: &'a Base) -> Self { | ||
| Self { overlay, base } | ||
| } | ||
| } | ||
|
|
||
| impl<'a, Overlay, Base> ReadOnlyStorage for TwoLayerStorage<'a, Overlay, Base> | ||
| where | ||
| Overlay: ImmutableReadOnlyStorage + Sync, | ||
| Base: ImmutableReadOnlyStorage + Sync + ?Sized, | ||
| { | ||
| async fn get_mut(&mut self, key: &DbKey) -> PatriciaStorageResult<Option<DbValue>> { | ||
| Ok(match self.overlay.get(key).await? { | ||
| Some(v) => Some(v), | ||
| None => self.base.get(key).await?, | ||
| }) | ||
| } | ||
|
|
||
| async fn mget_mut(&mut self, keys: &[&DbKey]) -> PatriciaStorageResult<Vec<Option<DbValue>>> { | ||
| let mut out = self.overlay.mget(keys).await?; | ||
| let mut miss_indices = Vec::new(); | ||
| let mut miss_keys = Vec::new(); | ||
| for (i, v) in out.iter().enumerate() { | ||
| if v.is_none() { | ||
| miss_indices.push(i); | ||
| miss_keys.push(keys[i]); | ||
| } | ||
| } | ||
| if !miss_keys.is_empty() { | ||
| let fetched = self.base.mget(&miss_keys).await?; | ||
| for (idx, val) in miss_indices.into_iter().zip(fetched) { | ||
| out[idx] = val; | ||
| } | ||
| } | ||
| Ok(out) | ||
| } | ||
| } |
56 changes: 56 additions & 0 deletions
56
crates/starknet_patricia_storage/src/two_layer_storage_test.rs
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,56 @@ | ||
| use super::TwoLayerStorage; | ||
| use crate::map_storage::MapStorage; | ||
| use crate::storage_trait::{DbKey, DbValue, ReadOnlyStorage, Storage}; | ||
|
|
||
| #[tokio::test] | ||
| async fn read_falls_through_to_base() { | ||
| let key = DbKey(vec![1, 2, 3]); | ||
| let val = DbValue(vec![9]); | ||
| let mut base = MapStorage::default(); | ||
| base.0.insert(key.clone(), val.clone()); | ||
|
|
||
| let mut two = TwoLayerStorage::new(MapStorage::default(), &base); | ||
| assert_eq!(two.get_mut(&key).await.unwrap(), Some(val)); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn overlay_shadows_base() { | ||
| let key = DbKey(vec![1]); | ||
| let base_val = DbValue(vec![1]); | ||
| let over_val = DbValue(vec![2]); | ||
| let mut base = MapStorage::default(); | ||
| base.0.insert(key.clone(), base_val); | ||
|
|
||
| let mut two = TwoLayerStorage::new(MapStorage::default(), &base); | ||
| two.overlay.set(key.clone(), over_val.clone()).await.unwrap(); | ||
| assert_eq!(two.get_mut(&key).await.unwrap(), Some(over_val)); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn delete_drops_overlay_entry_and_sees_base() { | ||
| let key = DbKey(vec![7]); | ||
| let base_val = DbValue(vec![42]); | ||
| let mut base = MapStorage::default(); | ||
| base.0.insert(key.clone(), base_val.clone()); | ||
|
|
||
| let mut two = TwoLayerStorage::new(MapStorage::default(), &base); | ||
| two.overlay.set(key.clone(), DbValue(vec![99])).await.unwrap(); | ||
| two.overlay.delete(&key).await.unwrap(); | ||
| assert_eq!(two.get_mut(&key).await.unwrap(), Some(base_val)); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn mget_mut_uses_immutable_base_mget_on_miss() { | ||
| let key_base_only = DbKey(vec![3]); | ||
| let key_overlay = DbKey(vec![4]); | ||
| let base_val = DbValue(vec![11]); | ||
| let over_val = DbValue(vec![22]); | ||
| let mut base = MapStorage::default(); | ||
| base.0.insert(key_base_only.clone(), base_val.clone()); | ||
|
|
||
| let mut layered = TwoLayerStorage::new(MapStorage::default(), &base); | ||
| layered.overlay.set(key_overlay.clone(), over_val.clone()).await.unwrap(); | ||
|
|
||
| let keys = [&key_base_only, &key_overlay]; | ||
| assert_eq!(layered.mget_mut(&keys).await.unwrap(), vec![Some(base_val), Some(over_val)]); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.