Skip to content
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

Collection iteration #12

Merged
merged 10 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 9 additions & 18 deletions src/bin_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,9 @@ pub trait IterableStorage {
where
Self: 'a;

fn keys<'a>(&'a self, start: Option<&'a [u8]>, end: Option<&'a [u8]>)
-> Self::KeysIterator<'a>;
fn values<'a>(
&'a self,
start: Option<&'a [u8]>,
end: Option<&'a [u8]>,
) -> Self::ValuesIterator<'a>;
fn pairs<'a>(
&'a self,
start: Option<&'a [u8]>,
end: Option<&'a [u8]>,
) -> Self::PairsIterator<'a>;
fn keys<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::KeysIterator<'a>;
fn values<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::ValuesIterator<'a>;
fn pairs<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::PairsIterator<'a>;
}

pub trait RevIterableStorage {
Expand All @@ -49,17 +40,17 @@ pub trait RevIterableStorage {

fn rev_keys<'a>(
&'a self,
start: Option<&'a [u8]>,
end: Option<&'a [u8]>,
start: Option<&[u8]>,
end: Option<&[u8]>,
) -> Self::RevKeysIterator<'a>;
fn rev_values<'a>(
&'a self,
start: Option<&'a [u8]>,
end: Option<&'a [u8]>,
start: Option<&[u8]>,
end: Option<&[u8]>,
) -> Self::RevValuesIterator<'a>;
fn rev_pairs<'a>(
&'a self,
start: Option<&'a [u8]>,
end: Option<&'a [u8]>,
start: Option<&[u8]>,
end: Option<&[u8]>,
) -> Self::RevPairsIterator<'a>;
}
17 changes: 16 additions & 1 deletion src/containers/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
Storage, StorageMut,
};

use super::Storable;
use super::{KeyDecodeError, Storable};

pub struct Item<T, E> {
prefix: &'static [u8],
Expand Down Expand Up @@ -39,13 +39,28 @@ where
T: EncodableWith<E> + DecodableWith<E>,
{
type AccessorT<S> = ItemAccess<E, T, S>;
type Key = ();
type Value = T;
type ValueDecodeError = E::DecodeError;

fn access_impl<S>(storage: S) -> ItemAccess<E, T, S> {
ItemAccess {
storage,
phantom: PhantomData,
}
}

fn decode_key(key: &[u8]) -> Result<(), KeyDecodeError> {
if key.is_empty() {
Ok(())
} else {
Err(KeyDecodeError)
}
}

fn decode_value(value: &[u8]) -> Result<Self::Value, Self::ValueDecodeError> {
T::decode(value)
}
}

pub struct ItemAccess<E, T, S> {
Expand Down
98 changes: 89 additions & 9 deletions src/containers/map.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::marker::PhantomData;
use std::{borrow::Borrow, marker::PhantomData};

use crate::{storage_branch::StorageBranch, Storage};
use crate::storage_branch::StorageBranch;
use crate::{IterableStorage, Storage};

use super::{Key, Storable};
use super::{KeyDecodeError, Storable, StorableIter};

pub struct Map<K: ?Sized, V> {
prefix: &'static [u8],
Expand All @@ -11,7 +12,7 @@ pub struct Map<K: ?Sized, V> {

impl<K, V> Map<K, V>
where
K: ?Sized,
K: OwnedKey,
V: Storable,
{
pub const fn new(prefix: &'static [u8]) -> Self {
Expand All @@ -31,17 +32,37 @@ where

impl<K, V> Storable for Map<K, V>
where
K: ?Sized,
K: OwnedKey,
V: Storable,
{
type AccessorT<S> = MapAccess<K, V, S>;
type Key = (K, V::Key);
type Value = V::Value;
type ValueDecodeError = V::ValueDecodeError;

fn access_impl<S>(storage: S) -> MapAccess<K, V, S> {
MapAccess {
storage,
phantom: PhantomData,
}
}

fn decode_key(key: &[u8]) -> Result<Self::Key, KeyDecodeError> {
let len = *key.first().ok_or(KeyDecodeError)? as usize;

if key.len() < len + 1 {
return Err(KeyDecodeError);
}

let map_key = K::from_bytes(&key[1..len + 1]).map_err(|_| KeyDecodeError)?;
let rest = V::decode_key(&key[len + 1..])?;

Ok((map_key, rest))
}

fn decode_value(value: &[u8]) -> Result<Self::Value, Self::ValueDecodeError> {
V::decode_value(value)
}
}

pub struct MapAccess<K: ?Sized, V, S> {
Expand All @@ -51,12 +72,71 @@ pub struct MapAccess<K: ?Sized, V, S> {

impl<K, V, S> MapAccess<K, V, S>
where
K: Key + ?Sized,
K: Key,
V: Storable,
S: Storage,
{
pub fn get<'s>(&'s self, key: &K) -> V::AccessorT<StorageBranch<'s, S>> {
let key = key.bytes();
V::access_impl(StorageBranch::new(&self.storage, key.to_vec()))
pub fn entry<'s, Q>(&'s self, key: &Q) -> V::AccessorT<StorageBranch<'s, S>>
where
K: Borrow<Q>,
Q: Key + ?Sized,
{
let len = key.bytes().len();
let bytes = key.bytes();
let mut key = Vec::with_capacity(len + 1);

key.push(len as u8);
key.extend_from_slice(bytes);

V::access_impl(StorageBranch::new(&self.storage, key))
}
}

impl<K, V, S> MapAccess<K, V, S>
where
K: OwnedKey,
V: Storable,
S: IterableStorage,
{
pub fn iter<'s>(
&'s self,
start: Option<&[u8]>,
end: Option<&[u8]>,
) -> StorableIter<'s, Map<K, V>, S> {
StorableIter {
inner: self.storage.pairs(start, end),
phantom: PhantomData,
}
}
}

pub trait Key {
fn bytes(&self) -> &[u8];
}

pub trait OwnedKey: Key {
fn from_bytes(bytes: &[u8]) -> Result<Self, ()>
where
Self: Sized;
}

impl Key for String {
fn bytes(&self) -> &[u8] {
self.as_bytes()
}
}

impl OwnedKey for String {
fn from_bytes(bytes: &[u8]) -> Result<Self, ()>
where
Self: Sized,
{
std::str::from_utf8(bytes).map(String::from).map_err(|_| ())
}
}

impl Key for str {
fn bytes(&self) -> &[u8] {
self.as_bytes()
}
}
46 changes: 41 additions & 5 deletions src/containers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,57 @@
mod item;
mod map;

use std::marker::PhantomData;

pub use item::{Item, ItemAccess};
pub use map::{Map, MapAccess};

use crate::IterableStorage;

pub trait Storable {
type AccessorT<S>;
type Key;
type Value;
type ValueDecodeError;

fn access_impl<S>(storage: S) -> Self::AccessorT<S>;

fn decode_key(key: &[u8]) -> Result<Self::Key, KeyDecodeError>;

fn decode_value(value: &[u8]) -> Result<Self::Value, Self::ValueDecodeError>;
}

pub trait Key {
fn bytes(&self) -> &[u8];
pub struct KeyDecodeError;

pub struct StorableIter<'i, S, B>
where
S: Storable,
B: IterableStorage + 'i,
{
inner: B::PairsIterator<'i>,
phantom: PhantomData<S>,
}

impl Key for str {
fn bytes(&self) -> &[u8] {
self.as_bytes()
impl<'i, S, B> Iterator for StorableIter<'i, S, B>
where
S: Storable,
B: IterableStorage + 'i,
{
type Item = Result<(S::Key, S::Value), KVDecodeError<S::ValueDecodeError>>;

fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|(k, v)| -> Self::Item {
match (S::decode_key(&k), S::decode_value(&v)) {
(Err(_), _) => Err(KVDecodeError::Key),
(_, Err(e)) => Err(KVDecodeError::Value(e)),
(Ok(k), Ok(v)) => Ok((k, v)),
}
})
}
}

#[derive(Debug, PartialEq)]
pub enum KVDecodeError<V> {
Key,
Value(V),
}
Loading
Loading