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

Generic iteration #22

Merged
merged 3 commits into from
Apr 9, 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
18 changes: 16 additions & 2 deletions crates/storey/src/containers/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use thiserror::Error;

use crate::encoding::Encoding;
use crate::encoding::{DecodableWith, EncodableWith};
use crate::storage::StorageBranch;
use crate::storage::{IterableStorage, StorageBranch};
use crate::storage::{Storage, StorageMut};

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

const META_NEXT_IX: &[u8] = &[0];
const META_LEN: &[u8] = &[1];
Expand Down Expand Up @@ -67,6 +67,20 @@ pub struct ColumnAccess<E, T, S> {
phantom: PhantomData<(E, T)>,
}

impl<E, T, S> IterableAccessor for ColumnAccess<E, T, S>
where
E: Encoding,
T: EncodableWith<E> + DecodableWith<E>,
S: IterableStorage,
{
type StorableT = Column<T, E>;
type StorageT = S;

fn storage(&self) -> &Self::StorageT {
&self.storage
}
}

impl<E, T, S> ColumnAccess<E, T, S>
where
E: Encoding,
Expand Down
19 changes: 8 additions & 11 deletions crates/storey/src/containers/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::{borrow::Borrow, marker::PhantomData};
use crate::storage::IterableStorage;
use crate::storage::StorageBranch;

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

pub struct Map<K: ?Sized, V> {
prefix: &'static [u8],
Expand Down Expand Up @@ -103,21 +104,17 @@ where
}
}

impl<K, V, S> MapAccess<K, V, S>
impl<K, V, S> IterableAccessor for 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,
}
type StorableT = Map<K, V>;
type StorageT = S;

fn storage(&self) -> &Self::StorageT {
&self.storage
}
}

Expand Down
91 changes: 87 additions & 4 deletions crates/storey/src/containers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,55 @@ pub trait Storable {
fn decode_value(value: &[u8]) -> Result<Self::Value, Self::ValueDecodeError>;
}

#[derive(Debug, PartialEq)]
pub struct KeyDecodeError;

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

pub trait IterableAccessor {
type StorableT: Storable;
type StorageT: IterableStorage;

fn storage(&self) -> &Self::StorageT;

fn pairs<'s>(
&'s self,
start: Option<&[u8]>,
end: Option<&[u8]>,
) -> StorableIter<'s, Self::StorableT, Self::StorageT> {
StorableIter {
inner: self.storage().pairs(start, end),
phantom: PhantomData,
}
}

fn keys<'s>(
&'s self,
start: Option<&[u8]>,
end: Option<&[u8]>,
) -> StorableKeys<'s, Self::StorableT, Self::StorageT> {
StorableKeys {
inner: self.storage().keys(start, end),
phantom: PhantomData,
}
}

fn values<'s>(
&'s self,
start: Option<&[u8]>,
end: Option<&[u8]>,
) -> StorableValues<'s, Self::StorableT, Self::StorageT> {
StorableValues {
inner: self.storage().values(start, end),
phantom: PhantomData,
}
}
}

pub struct StorableIter<'i, S, B>
where
S: Storable,
Expand Down Expand Up @@ -52,8 +99,44 @@ where
}
}

#[derive(Debug, PartialEq)]
pub enum KVDecodeError<V> {
Key,
Value(V),
pub struct StorableKeys<'i, S, B>
where
S: Storable,
B: IterableStorage + 'i,
{
inner: B::KeysIterator<'i>,
phantom: PhantomData<S>,
}

impl<'i, S, B> Iterator for StorableKeys<'i, S, B>
where
S: Storable,
B: IterableStorage + 'i,
{
type Item = Result<S::Key, KeyDecodeError>;

fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|k| S::decode_key(&k))
}
}

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

impl<'i, S, B> Iterator for StorableValues<'i, S, B>
where
S: Storable,
B: IterableStorage + 'i,
{
type Item = Result<S::Value, S::ValueDecodeError>;

fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|v| S::decode_value(&v))
}
}
44 changes: 39 additions & 5 deletions crates/storey/tests/containers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod common;

use storey::containers::{Column, Item, Map};
use storey::containers::{Column, Item, IterableAccessor as _, Map};
use storey::storage::Storage as _;

use common::backend::TestStorage;
Expand Down Expand Up @@ -86,7 +86,7 @@ fn simple_iteration() {
access.entry_mut("bar").set(&42).unwrap();

let items = access
.iter(None, None)
.pairs(None, None)
.collect::<Result<Vec<_>, _>>()
.unwrap();
assert_eq!(
Expand All @@ -98,6 +98,40 @@ fn simple_iteration() {
);
}

#[test]
fn keys_iteration() {
let mut storage = TestStorage::new();

let map = Map::<String, Item<u64, TestEncoding>>::new(&[0]);
let mut access = map.access(&mut storage);

access.entry_mut("foo").set(&1337).unwrap();
access.entry_mut("bar").set(&42).unwrap();

let keys = access
.keys(None, None)
.collect::<Result<Vec<_>, _>>()
.unwrap();
assert_eq!(keys, vec![("bar".to_string(), ()), ("foo".to_string(), ())])
}

#[test]
fn values_iteration() {
let mut storage = TestStorage::new();

let map = Map::<String, Item<u64, TestEncoding>>::new(&[0]);
let mut access = map.access(&mut storage);

access.entry_mut("foo").set(&1337).unwrap();
access.entry_mut("bar").set(&42).unwrap();

let values = access
.values(None, None)
.collect::<Result<Vec<_>, _>>()
.unwrap();
assert_eq!(values, vec![42, 1337])
}

#[test]
fn composable_iteration() {
let mut storage = TestStorage::new();
Expand All @@ -116,7 +150,7 @@ fn composable_iteration() {

// iterate over all items
let items = access
.iter(None, None)
.pairs(None, None)
.collect::<Result<Vec<_>, _>>()
.unwrap();
assert_eq!(
Expand All @@ -131,7 +165,7 @@ fn composable_iteration() {
// iterate over items under "foo"
let items = access
.entry("foo")
.iter(None, None)
.pairs(None, None)
.collect::<Result<Vec<_>, _>>()
.unwrap();
assert_eq!(
Expand Down Expand Up @@ -190,7 +224,7 @@ fn map_of_column() {
assert_eq!(access.entry("bar").len().unwrap(), 1);

let all = access
.iter(None, None)
.pairs(None, None)
.collect::<Result<Vec<_>, _>>()
.unwrap();
assert_eq!(
Expand Down
Loading