-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the actual implementation for the
AtomicView::latest_view
(#1994
) Closes #1581 The change finally implements an atomic view of the RocksDB database via the snapshot feature. We use a snapshot (if available) each time we read/iterate over the RocksDB. It guarantees a consistent view even if the data is removed/added/modified. `InMemory` database also implements this feature by simply cloning all `BTreeMap`s. In the future, we need to optimize this behavior and cache views - #1993. ## Checklist - [x] New behavior is reflected in tests ### Before requesting review - [x] I have reviewed the code myself - [x] I have created follow-up issues caused by this PR and linked them here
- Loading branch information
Showing
11 changed files
with
332 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
// TODO: Move the implementation from the module to here. | ||
pub mod memory_store; | ||
pub mod memory_view; |
This file contains 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 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,76 @@ | ||
use crate::database::database_description::{ | ||
on_chain::OnChain, | ||
DatabaseDescription, | ||
}; | ||
use fuel_core_storage::{ | ||
iter::{ | ||
iterator, | ||
BoxedIter, | ||
IntoBoxedIter, | ||
IterDirection, | ||
IterableStore, | ||
}, | ||
kv_store::{ | ||
KVItem, | ||
KeyValueInspect, | ||
StorageColumn, | ||
Value, | ||
}, | ||
transactional::ReferenceBytesKey, | ||
Result as StorageResult, | ||
}; | ||
use std::collections::BTreeMap; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct MemoryView<Description = OnChain> | ||
where | ||
Description: DatabaseDescription, | ||
{ | ||
pub(crate) inner: Vec<BTreeMap<ReferenceBytesKey, Value>>, | ||
pub(crate) _marker: core::marker::PhantomData<Description>, | ||
} | ||
|
||
impl<Description> MemoryView<Description> | ||
where | ||
Description: DatabaseDescription, | ||
{ | ||
pub fn iter_all<'a>( | ||
&'a self, | ||
column: Description::Column, | ||
prefix: Option<&[u8]>, | ||
start: Option<&[u8]>, | ||
direction: IterDirection, | ||
) -> impl Iterator<Item = KVItem> + 'a { | ||
let btree = &self.inner[column.as_usize()]; | ||
|
||
iterator(btree, prefix, start, direction) | ||
.map(|(key, value)| (key.clone().into(), value.clone())) | ||
.map(Ok) | ||
} | ||
} | ||
|
||
impl<Description> KeyValueInspect for MemoryView<Description> | ||
where | ||
Description: DatabaseDescription, | ||
{ | ||
type Column = Description::Column; | ||
|
||
fn get(&self, key: &[u8], column: Self::Column) -> StorageResult<Option<Value>> { | ||
Ok(self.inner[column.as_usize()].get(key).cloned()) | ||
} | ||
} | ||
|
||
impl<Description> IterableStore for MemoryView<Description> | ||
where | ||
Description: DatabaseDescription, | ||
{ | ||
fn iter_store( | ||
&self, | ||
column: Self::Column, | ||
prefix: Option<&[u8]>, | ||
start: Option<&[u8]>, | ||
direction: IterDirection, | ||
) -> BoxedIter<KVItem> { | ||
self.iter_all(column, prefix, start, direction).into_boxed() | ||
} | ||
} |
Oops, something went wrong.