Ditch StoreItem trait (#1185)
This commit is contained in:
parent
a88afb7409
commit
919c81fe7d
@ -7,8 +7,7 @@ use crate::impls::beacon_state::store_full_state;
|
||||
use crate::iter::{ParentRootBlockIterator, StateRootsIterator};
|
||||
use crate::metrics;
|
||||
use crate::{
|
||||
leveldb_store::LevelDB, DBColumn, Error, PartialBeaconState, SimpleStoreItem, Store, StoreItem,
|
||||
StoreOp,
|
||||
leveldb_store::LevelDB, DBColumn, Error, PartialBeaconState, SimpleStoreItem, Store, StoreOp,
|
||||
};
|
||||
use lru::LruCache;
|
||||
use parking_lot::{Mutex, RwLock};
|
||||
@ -460,7 +459,7 @@ impl<E: EthSpec> HotColdDB<E> {
|
||||
|
||||
// 1. Convert to PartialBeaconState and store that in the DB.
|
||||
let partial_state = PartialBeaconState::from_state_forgetful(state);
|
||||
partial_state.db_put(&self.cold_db, state_root)?;
|
||||
self.cold_db.put(state_root, &partial_state)?;
|
||||
|
||||
// 2. Store updated vector entries.
|
||||
let db = &self.cold_db;
|
||||
@ -500,7 +499,9 @@ impl<E: EthSpec> HotColdDB<E> {
|
||||
|
||||
/// Load a restore point state by its `state_root`.
|
||||
fn load_restore_point(&self, state_root: &Hash256) -> Result<BeaconState<E>, Error> {
|
||||
let mut partial_state = PartialBeaconState::db_get(&self.cold_db, state_root)?
|
||||
let mut partial_state: PartialBeaconState<E> = self
|
||||
.cold_db
|
||||
.get(state_root)?
|
||||
.ok_or_else(|| HotColdDBError::MissingRestorePoint(*state_root))?;
|
||||
|
||||
// Fill in the fields of the partial state.
|
||||
@ -680,8 +681,9 @@ impl<E: EthSpec> HotColdDB<E> {
|
||||
/// Load the state root of a restore point.
|
||||
fn load_restore_point_hash(&self, restore_point_index: u64) -> Result<Hash256, Error> {
|
||||
let key = Self::restore_point_key(restore_point_index);
|
||||
RestorePointHash::db_get(&self.cold_db, &key)?
|
||||
.map(|r| r.state_root)
|
||||
self.cold_db
|
||||
.get(&key)?
|
||||
.map(|r: RestorePointHash| r.state_root)
|
||||
.ok_or_else(|| HotColdDBError::MissingRestorePointHash(restore_point_index).into())
|
||||
}
|
||||
|
||||
@ -692,8 +694,8 @@ impl<E: EthSpec> HotColdDB<E> {
|
||||
state_root: Hash256,
|
||||
) -> Result<(), Error> {
|
||||
let key = Self::restore_point_key(restore_point_index);
|
||||
RestorePointHash { state_root }
|
||||
.db_put(&self.cold_db, &key)
|
||||
self.cold_db
|
||||
.put(&key, &RestorePointHash { state_root })
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
@ -704,13 +706,16 @@ impl<E: EthSpec> HotColdDB<E> {
|
||||
|
||||
/// Load a frozen state's slot, given its root.
|
||||
fn load_cold_state_slot(&self, state_root: &Hash256) -> Result<Option<Slot>, Error> {
|
||||
Ok(ColdStateSummary::db_get(&self.cold_db, state_root)?.map(|s| s.slot))
|
||||
Ok(self
|
||||
.cold_db
|
||||
.get(state_root)?
|
||||
.map(|s: ColdStateSummary| s.slot))
|
||||
}
|
||||
|
||||
/// Store the slot of a frozen state.
|
||||
fn store_cold_state_slot(&self, state_root: &Hash256, slot: Slot) -> Result<(), Error> {
|
||||
ColdStateSummary { slot }
|
||||
.db_put(&self.cold_db, state_root)
|
||||
self.cold_db
|
||||
.put(state_root, &ColdStateSummary { slot })
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
@ -719,7 +724,7 @@ impl<E: EthSpec> HotColdDB<E> {
|
||||
&self,
|
||||
state_root: &Hash256,
|
||||
) -> Result<Option<HotStateSummary>, Error> {
|
||||
HotStateSummary::db_get(&self.hot_db, state_root)
|
||||
self.hot_db.get(state_root)
|
||||
}
|
||||
|
||||
/// Check that the restore point frequency is valid.
|
||||
|
@ -59,23 +59,39 @@ pub trait Store<E: EthSpec>: Sync + Send + Sized + 'static {
|
||||
fn key_delete(&self, column: &str, key: &[u8]) -> Result<(), Error>;
|
||||
|
||||
/// Store an item in `Self`.
|
||||
fn put<I: StoreItem>(&self, key: &Hash256, item: &I) -> Result<(), Error> {
|
||||
item.db_put(self, key)
|
||||
fn put<I: SimpleStoreItem>(&self, key: &Hash256, item: &I) -> Result<(), Error> {
|
||||
let column = I::db_column().into();
|
||||
let key = key.as_bytes();
|
||||
|
||||
self.put_bytes(column, key, &item.as_store_bytes())
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
/// Retrieve an item from `Self`.
|
||||
fn get<I: StoreItem>(&self, key: &Hash256) -> Result<Option<I>, Error> {
|
||||
I::db_get(self, key)
|
||||
fn get<I: SimpleStoreItem>(&self, key: &Hash256) -> Result<Option<I>, Error> {
|
||||
let column = I::db_column().into();
|
||||
let key = key.as_bytes();
|
||||
|
||||
match self.get_bytes(column, key)? {
|
||||
Some(bytes) => Ok(Some(I::from_store_bytes(&bytes[..])?)),
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if the given key represents an item in `Self`.
|
||||
fn exists<I: StoreItem>(&self, key: &Hash256) -> Result<bool, Error> {
|
||||
I::db_exists(self, key)
|
||||
fn exists<I: SimpleStoreItem>(&self, key: &Hash256) -> Result<bool, Error> {
|
||||
let column = I::db_column().into();
|
||||
let key = key.as_bytes();
|
||||
|
||||
self.key_exists(column, key)
|
||||
}
|
||||
|
||||
/// Remove an item from `Self`.
|
||||
fn delete<I: StoreItem>(&self, key: &Hash256) -> Result<(), Error> {
|
||||
I::db_delete(self, key)
|
||||
fn delete<I: SimpleStoreItem>(&self, key: &Hash256) -> Result<(), Error> {
|
||||
let column = I::db_column().into();
|
||||
let key = key.as_bytes();
|
||||
|
||||
self.key_delete(column, key)
|
||||
}
|
||||
|
||||
/// Store a block in the store.
|
||||
@ -107,7 +123,7 @@ pub trait Store<E: EthSpec>: Sync + Send + Sized + 'static {
|
||||
state_root: &Hash256,
|
||||
summary: HotStateSummary,
|
||||
) -> Result<(), Error> {
|
||||
summary.db_put(self, state_root).map_err(Into::into)
|
||||
self.put(state_root, &summary).map_err(Into::into)
|
||||
}
|
||||
|
||||
/// Fetch a state from the store.
|
||||
@ -249,63 +265,6 @@ pub trait SimpleStoreItem: Sized {
|
||||
fn from_store_bytes(bytes: &[u8]) -> Result<Self, Error>;
|
||||
}
|
||||
|
||||
/// An item that may be stored in a `Store`.
|
||||
pub trait StoreItem: Sized {
|
||||
/// Store `self`.
|
||||
fn db_put<S: Store<E>, E: EthSpec>(&self, store: &S, key: &Hash256) -> Result<(), Error>;
|
||||
|
||||
/// Retrieve an instance of `Self` from `store`.
|
||||
fn db_get<S: Store<E>, E: EthSpec>(store: &S, key: &Hash256) -> Result<Option<Self>, Error>;
|
||||
|
||||
/// Return `true` if an instance of `Self` exists in `store`.
|
||||
fn db_exists<S: Store<E>, E: EthSpec>(store: &S, key: &Hash256) -> Result<bool, Error>;
|
||||
|
||||
/// Delete an instance of `Self` from `store`.
|
||||
fn db_delete<S: Store<E>, E: EthSpec>(store: &S, key: &Hash256) -> Result<(), Error>;
|
||||
}
|
||||
|
||||
impl<T> StoreItem for T
|
||||
where
|
||||
T: SimpleStoreItem,
|
||||
{
|
||||
/// Store `self`.
|
||||
fn db_put<S: Store<E>, E: EthSpec>(&self, store: &S, key: &Hash256) -> Result<(), Error> {
|
||||
let column = Self::db_column().into();
|
||||
let key = key.as_bytes();
|
||||
|
||||
store
|
||||
.put_bytes(column, key, &self.as_store_bytes())
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
/// Retrieve an instance of `Self`.
|
||||
fn db_get<S: Store<E>, E: EthSpec>(store: &S, key: &Hash256) -> Result<Option<Self>, Error> {
|
||||
let column = Self::db_column().into();
|
||||
let key = key.as_bytes();
|
||||
|
||||
match store.get_bytes(column, key)? {
|
||||
Some(bytes) => Ok(Some(Self::from_store_bytes(&bytes[..])?)),
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
/// Return `true` if an instance of `Self` exists in `Store`.
|
||||
fn db_exists<S: Store<E>, E: EthSpec>(store: &S, key: &Hash256) -> Result<bool, Error> {
|
||||
let column = Self::db_column().into();
|
||||
let key = key.as_bytes();
|
||||
|
||||
store.key_exists(column, key)
|
||||
}
|
||||
|
||||
/// Delete `self` from the `Store`.
|
||||
fn db_delete<S: Store<E>, E: EthSpec>(store: &S, key: &Hash256) -> Result<(), Error> {
|
||||
let column = Self::db_column().into();
|
||||
let key = key.as_bytes();
|
||||
|
||||
store.key_delete(column, key)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
Loading…
Reference in New Issue
Block a user