-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'miraclx/revamp-store' into miraclx/store/bump-node
- Loading branch information
Showing
13 changed files
with
324 additions
and
34 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,16 @@ | ||
use crate::key::AsKeyParts; | ||
use crate::slice::Slice; | ||
|
||
pub trait DataType: Sized { | ||
type Error; | ||
|
||
fn from_slice(slice: Slice) -> Result<Self, Self::Error>; | ||
fn as_slice(&self) -> Result<Slice, Self::Error>; | ||
} | ||
|
||
pub trait Entry { | ||
type Key: AsKeyParts; | ||
type DataType: DataType; | ||
|
||
fn key(&self) -> &Self::Key; | ||
} |
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,85 @@ | ||
use thiserror::Error; | ||
|
||
use crate::entry::{DataType, Entry}; | ||
use crate::iter::{Iter, Structured}; | ||
use crate::key::FromKeyParts; | ||
use crate::layer::{read_only, temporal, Layer, ReadLayer, WriteLayer}; | ||
use crate::Store; | ||
|
||
pub struct StoreHandle<L = Store> { | ||
pub(crate) inner: L, | ||
} | ||
|
||
impl<L: Layer> StoreHandle<L> { | ||
pub fn new(inner: L) -> Self { | ||
Self { inner } | ||
} | ||
|
||
pub fn into_inner(self) -> L { | ||
self.inner | ||
} | ||
} | ||
|
||
impl<'k, L: ReadLayer<'k>> StoreHandle<L> { | ||
pub fn read_only(&'k self) -> read_only::ReadOnly<'k, L> { | ||
read_only::ReadOnly::new(&self.inner) | ||
} | ||
} | ||
|
||
impl<'base, 'k, 'v, L: WriteLayer<'k, 'v>> StoreHandle<L> { | ||
pub fn temporal(&'base mut self) -> temporal::Temporal<'base, 'k, 'v, L> { | ||
temporal::Temporal::new(&mut self.inner) | ||
} | ||
} | ||
|
||
#[derive(Debug, Error)] | ||
pub enum Error<E: DataType> { | ||
LayerError(eyre::Report), | ||
CodecError(E::Error), | ||
} | ||
|
||
impl<E: DataType> From<eyre::Report> for Error<E> { | ||
fn from(err: eyre::Report) -> Self { | ||
Self::LayerError(err) | ||
} | ||
} | ||
|
||
impl<'k, L: ReadLayer<'k>> StoreHandle<L> { | ||
pub fn has<E: Entry>(&self, entry: &'k E) -> Result<bool, Error<E::DataType>> { | ||
Ok(self.inner.has(entry.key())?) | ||
} | ||
|
||
pub fn get<E: Entry>(&self, entry: &'k E) -> Result<Option<E::DataType>, Error<E::DataType>> { | ||
match self.inner.get(entry.key())? { | ||
Some(value) => Ok(Some( | ||
E::DataType::from_slice(value).map_err(Error::CodecError)?, | ||
)), | ||
None => Ok(None), | ||
} | ||
} | ||
|
||
pub fn iter<E: Entry<Key: FromKeyParts>>( | ||
&'k self, | ||
start: &'k E, | ||
) -> Result<Iter<Structured<E::Key>, Structured<E::DataType>>, Error<E::DataType>> { | ||
Ok(self.inner.iter(start.key())?.structured_value()) | ||
} | ||
} | ||
|
||
impl<'k, 'v, L: WriteLayer<'k, 'v>> StoreHandle<L> { | ||
pub fn put<E: Entry>( | ||
&mut self, | ||
entry: &'k E, | ||
value: &'v E::DataType, | ||
) -> Result<(), Error<E::DataType>> { | ||
self.inner | ||
.put(entry.key(), value.as_slice().map_err(Error::CodecError)?) | ||
.map_err(Error::LayerError) | ||
} | ||
|
||
pub fn delete<E: Entry>(&mut self, entry: &'k E) -> Result<(), Error<E::DataType>> { | ||
self.inner.delete(entry.key()).map_err(Error::LayerError) | ||
} | ||
} | ||
|
||
// todo! experiment with restoring {Read,Write}Layer for StoreHandle |
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
Oops, something went wrong.