Add enc/decode traits to store
This commit is contained in:
parent
157d4900aa
commit
cf8a24c2bd
16
beacon_node/db2/src/impls.rs
Normal file
16
beacon_node/db2/src/impls.rs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
/*
|
||||||
|
use types::*;
|
||||||
|
|
||||||
|
impl StoreEncode for Hash256 {
|
||||||
|
fn as_store_bytes(&self) -> Vec<u8> {
|
||||||
|
self.as_bytes().to_vec()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StoreDecode for Hash256 {
|
||||||
|
fn from_store_bytes(bytes: &mut [u8]) -> Vec<u8> {
|
||||||
|
Hash256::from_slice()
|
||||||
|
self.as_bytes().to_vec()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
@ -1,9 +1,8 @@
|
|||||||
// mod disk_db;
|
// mod disk_db;
|
||||||
mod errors;
|
mod errors;
|
||||||
|
mod impls;
|
||||||
mod memory_db;
|
mod memory_db;
|
||||||
|
|
||||||
use db_encode::{db_encode, DBDecode, DBEncode};
|
|
||||||
|
|
||||||
pub use self::memory_db::MemoryDB;
|
pub use self::memory_db::MemoryDB;
|
||||||
pub use errors::Error;
|
pub use errors::Error;
|
||||||
pub use types::*;
|
pub use types::*;
|
||||||
@ -35,6 +34,14 @@ pub trait Store: Sync + Send + Sized {
|
|||||||
fn key_delete(&self, col: &str, key: &[u8]) -> Result<(), Error>;
|
fn key_delete(&self, col: &str, key: &[u8]) -> Result<(), Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait StoreEncode {
|
||||||
|
fn as_store_bytes(&self) -> Vec<u8>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait StoreDecode: Sized {
|
||||||
|
fn from_store_bytes(bytes: &mut [u8]) -> Result<Self, Error>;
|
||||||
|
}
|
||||||
|
|
||||||
pub enum DBColumn {
|
pub enum DBColumn {
|
||||||
Block,
|
Block,
|
||||||
State,
|
State,
|
||||||
@ -52,7 +59,7 @@ impl<'a> Into<&'a str> for DBColumn {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait StorableItem: DBEncode + DBDecode {
|
pub trait StorableItem: StoreEncode + StoreDecode + Sized {
|
||||||
fn db_column() -> DBColumn;
|
fn db_column() -> DBColumn;
|
||||||
|
|
||||||
fn db_put(&self, store: &impl Store, key: &Hash256) -> Result<(), Error> {
|
fn db_put(&self, store: &impl Store, key: &Hash256) -> Result<(), Error> {
|
||||||
@ -60,7 +67,7 @@ pub trait StorableItem: DBEncode + DBDecode {
|
|||||||
let key = key.as_bytes();
|
let key = key.as_bytes();
|
||||||
|
|
||||||
store
|
store
|
||||||
.put_bytes(column, key, &db_encode(self))
|
.put_bytes(column, key, &self.as_store_bytes())
|
||||||
.map_err(|e| e.into())
|
.map_err(|e| e.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,10 +76,7 @@ pub trait StorableItem: DBEncode + DBDecode {
|
|||||||
let key = key.as_bytes();
|
let key = key.as_bytes();
|
||||||
|
|
||||||
match store.get_bytes(column, key)? {
|
match store.get_bytes(column, key)? {
|
||||||
Some(bytes) => {
|
Some(mut bytes) => Ok(Some(Self::from_store_bytes(&mut bytes[..])?)),
|
||||||
let (item, _index) = Self::db_decode(&bytes, 0)?;
|
|
||||||
Ok(Some(item))
|
|
||||||
}
|
|
||||||
None => Ok(None),
|
None => Ok(None),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -95,16 +99,28 @@ pub trait StorableItem: DBEncode + DBDecode {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use db_encode_derive::{DBDecode, DBEncode};
|
use ssz::{ssz_encode, Decodable};
|
||||||
use ssz::Decodable;
|
|
||||||
use ssz_derive::{Decode, Encode};
|
use ssz_derive::{Decode, Encode};
|
||||||
|
|
||||||
#[derive(PartialEq, Debug, Encode, Decode, DBEncode, DBDecode)]
|
#[derive(PartialEq, Debug, Encode, Decode)]
|
||||||
struct StorableThing {
|
struct StorableThing {
|
||||||
a: u64,
|
a: u64,
|
||||||
b: u64,
|
b: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl StoreEncode for StorableThing {
|
||||||
|
fn as_store_bytes(&self) -> Vec<u8> {
|
||||||
|
ssz_encode(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StoreDecode for StorableThing {
|
||||||
|
fn from_store_bytes(bytes: &mut [u8]) -> Result<Self, Error> {
|
||||||
|
let (item, _) = Self::ssz_decode(bytes, 0)?;
|
||||||
|
Ok(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl StorableItem for StorableThing {
|
impl StorableItem for StorableThing {
|
||||||
fn db_column() -> DBColumn {
|
fn db_column() -> DBColumn {
|
||||||
DBColumn::Block
|
DBColumn::Block
|
||||||
|
Loading…
Reference in New Issue
Block a user