Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Oct 1, 2024
1 parent 6ff1b54 commit ddb999f
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 101 deletions.
114 changes: 57 additions & 57 deletions src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ impl<K, V, C> Entry<K, V, C> {
}

/// An entry builder which can build an [`Entry`] to be inserted into the [`Wal`](crate::wal::Wal).
pub struct EntryWithKeyBuilder<KB, V, C> {
pub struct EntryWithKeyBuilder<KB, V, P> {
pub(crate) kb: KeyBuilder<KB>,
pub(crate) value: V,
pub(crate) pointer: Option<Pointer<C>>,
pub(crate) pointer: Option<P>,
pub(crate) meta: BatchEncodedEntryMeta,
}

impl<KB, V, C> EntryWithKeyBuilder<KB, V, C>
impl<KB, V, P> EntryWithKeyBuilder<KB, V, P>
where
V: Borrow<[u8]>,
{
Expand Down Expand Up @@ -154,10 +154,10 @@ impl<KB, V, C> EntryWithKeyBuilder<KB, V, C> {
}

/// An entry builder which can build an [`Entry`] to be inserted into the [`Wal`](crate::wal::Wal).
pub struct EntryWithValueBuilder<K, VB, C> {
pub struct EntryWithValueBuilder<K, VB, P> {
pub(crate) key: K,
pub(crate) vb: ValueBuilder<VB>,
pub(crate) pointer: Option<Pointer<C>>,
pub(crate) pointer: Option<P>,
pub(crate) meta: BatchEncodedEntryMeta,
}

Expand All @@ -172,7 +172,7 @@ where
}
}

impl<K, VB, C> EntryWithValueBuilder<K, VB, C> {
impl<K, VB, P> EntryWithValueBuilder<K, VB, P> {
/// Creates a new entry.
#[inline]
pub const fn new(key: K, vb: ValueBuilder<VB>) -> Self {
Expand Down Expand Up @@ -209,57 +209,6 @@ impl<K, VB, C> EntryWithValueBuilder<K, VB, C> {
}
}

/// An entry builder which can build an [`Entry`] to be inserted into the [`Wal`](crate::wal::Wal).
pub struct EntryWithBuilders<KB, VB, C> {
pub(crate) kb: KeyBuilder<KB>,
pub(crate) vb: ValueBuilder<VB>,
pub(crate) pointer: Option<Pointer<C>>,
pub(crate) meta: BatchEncodedEntryMeta,
}

impl<KB, VB, C> EntryWithBuilders<KB, VB, C> {
/// Creates a new entry.
#[inline]
pub const fn new(kb: KeyBuilder<KB>, vb: ValueBuilder<VB>) -> Self {
Self {
kb,
vb,
pointer: None,
meta: BatchEncodedEntryMeta::zero(),
}
}

/// Returns the value builder.
#[inline]
pub const fn value_builder(&self) -> &ValueBuilder<VB> {
&self.vb
}

/// Returns the key builder.
#[inline]
pub const fn key_builder(&self) -> &KeyBuilder<KB> {
&self.kb
}

/// Returns the length of the key.
#[inline]
pub const fn key_len(&self) -> usize {
self.kb.size() as usize
}

/// Returns the length of the value.
#[inline]
pub const fn value_len(&self) -> usize {
self.vb.size() as usize
}

/// Consumes the entry and returns the key and value.
#[inline]
pub fn into_components(self) -> (KeyBuilder<KB>, ValueBuilder<VB>) {
(self.kb, self.vb)
}
}

/// A wrapper around a generic type that can be used to construct a [`GenericEntry`].
#[repr(transparent)]
pub struct Generic<'a, T: ?Sized> {
Expand Down Expand Up @@ -354,6 +303,57 @@ impl<'a, K: ?Sized, V: ?Sized> GenericEntry<'a, K, V> {
}
}

/// An entry builder which can build an [`GenericEntry`] to be inserted into the [`GenericOrderWal`](crate::swmr::generic::GenericOrderWal).
pub struct EntryWithBuilders<KB, VB, P> {
pub(crate) kb: KeyBuilder<KB>,
pub(crate) vb: ValueBuilder<VB>,
pub(crate) pointer: Option<P>,
pub(crate) meta: BatchEncodedEntryMeta,
}

impl<KB, VB, P> EntryWithBuilders<KB, VB, P> {
/// Creates a new entry.
#[inline]
pub const fn new(kb: KeyBuilder<KB>, vb: ValueBuilder<VB>) -> Self {
Self {
kb,
vb,
pointer: None,
meta: BatchEncodedEntryMeta::zero(),
}
}

/// Returns the value builder.
#[inline]
pub const fn value_builder(&self) -> &ValueBuilder<VB> {
&self.vb
}

/// Returns the key builder.
#[inline]
pub const fn key_builder(&self) -> &KeyBuilder<KB> {
&self.kb
}

/// Returns the length of the key.
#[inline]
pub const fn key_len(&self) -> usize {
self.kb.size() as usize
}

/// Returns the length of the value.
#[inline]
pub const fn value_len(&self) -> usize {
self.vb.size() as usize
}

/// Consumes the entry and returns the key and value.
#[inline]
pub fn into_components(self) -> (KeyBuilder<KB>, ValueBuilder<VB>) {
(self.kb, self.vb)

Check warning on line 353 in src/entry.rs

View check run for this annotation

Codecov / codecov/patch

src/entry.rs#L352-L353

Added lines #L352 - L353 were not covered by tests
}
}

/// The reference to an entry in the [`GenericOrderWal`](crate::swmr::GenericOrderWal).
#[repr(transparent)]
pub struct GenericEntryRef<'a, K, V>
Expand Down
35 changes: 34 additions & 1 deletion src/swmr/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use crate::{

pub use crate::{
entry::{Generic, GenericEntry, GenericEntryRef},
wal::GenericBatch,
wal::{BatchWithBuilders, BatchWithKeyBuilder, BatchWithValueBuilder, GenericBatch},
};

pub use dbutils::{
Expand Down Expand Up @@ -999,6 +999,39 @@ where
self.insert_in(kb, vb)

Check warning on line 999 in src/swmr/generic.rs

View check run for this annotation

Codecov / codecov/patch

src/swmr/generic.rs#L999

Added line #L999 was not covered by tests
}

/// Inserts a batch of entries into the write-ahead log.
pub fn insert_batch_with_key_builder<'a, B>(
&'a mut self,
_batch: &'a mut B,
) -> Result<(), Among<B::Error, V::Error, Error>>
where
B: BatchWithKeyBuilder<GenericPointer<K, V>>,
{
todo!()
}

/// Inserts a batch of entries into the write-ahead log.
pub fn insert_batch_with_value_builder<'a, B>(
&'a mut self,
_batch: &'a mut B,
) -> Result<(), Among<K::Error, B::Error, Error>>
where
B: BatchWithValueBuilder<GenericPointer<K, V>>,
{
todo!()
}

/// Inserts a batch of entries into the write-ahead log.
pub fn insert_batch_with_builders<'a, B>(
&'a mut self,
_batch: &'a mut B,
) -> Result<(), Among<B::KeyError, B::ValueError, Error>>
where
B: BatchWithBuilders<GenericPointer<K, V>>,
{
todo!()
}

/// Inserts a batch of entries into the write-ahead log.
pub fn insert_batch<'a, 'b: 'a, B: GenericBatch<'b, Key = K, Value = V>>(
&'a mut self,
Expand Down
13 changes: 9 additions & 4 deletions src/wal.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use checksum::BuildChecksumer;
use core::ops::{Bound, RangeBounds};

use super::*;
use super::{pointer::Pointer, *};

pub(crate) mod sealed;

Expand Down Expand Up @@ -385,11 +385,13 @@ pub trait Wal<C, S>:
}

/// Inserts a batch of key-value pairs into the WAL.
fn insert_batch_with_key_builder<B: BatchWithKeyBuilder<Comparator = C>>(
fn insert_batch_with_key_builder<B>(
&mut self,
batch: &mut B,
) -> Result<(), Either<B::Error, Error>>
where
B: BatchWithKeyBuilder<Pointer<C>>,
B::Value: Borrow<[u8]>,
C: Comparator + CheapClone,
S: BuildChecksumer,
{
Expand All @@ -403,11 +405,13 @@ pub trait Wal<C, S>:
}

/// Inserts a batch of key-value pairs into the WAL.
fn insert_batch_with_value_builder<B: BatchWithValueBuilder<Comparator = C>>(
fn insert_batch_with_value_builder<B>(
&mut self,
batch: &mut B,
) -> Result<(), Either<B::Error, Error>>
where
B: BatchWithValueBuilder<Pointer<C>>,
B::Key: Borrow<[u8]>,
C: Comparator + CheapClone,
S: BuildChecksumer,
{
Expand All @@ -421,11 +425,12 @@ pub trait Wal<C, S>:
}

/// Inserts a batch of key-value pairs into the WAL.
fn insert_batch_with_builders<B: BatchWithBuilders<Comparator = C>>(
fn insert_batch_with_builders<B>(
&mut self,
batch: &mut B,
) -> Result<(), Among<B::KeyError, B::ValueError, Error>>
where
B: BatchWithBuilders<Pointer<C>>,
C: Comparator + CheapClone,
S: BuildChecksumer,
{
Expand Down
52 changes: 17 additions & 35 deletions src/wal/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,41 +47,34 @@ where
/// A batch of keys and values that can be inserted into the [`Wal`](super::Wal).
/// Comparing to [`Batch`], this trait is used to build
/// the key in place.
pub trait BatchWithKeyBuilder {
pub trait BatchWithKeyBuilder<P: 'static> {
/// The key builder type.
type KeyBuilder: Fn(&mut VacantBuffer<'_>) -> Result<(), Self::Error>;

/// The error for the key builder.
type Error;

/// The value type.
type Value: Borrow<[u8]>;

/// The [`Comparator`] type.
type Comparator: Comparator;
type Value;

/// The iterator type.
type IterMut<'a>: Iterator<
Item = &'a mut EntryWithKeyBuilder<Self::KeyBuilder, Self::Value, Self::Comparator>,
>
type IterMut<'a>: Iterator<Item = &'a mut EntryWithKeyBuilder<Self::KeyBuilder, Self::Value, P>>
where
Self: 'a;

/// Returns an iterator over the keys and values.
fn iter_mut(&mut self) -> Self::IterMut<'_>;
}

impl<KB, E, V, C, T> BatchWithKeyBuilder for T
impl<KB, E, V, P, T> BatchWithKeyBuilder<P> for T
where
KB: Fn(&mut VacantBuffer<'_>) -> Result<(), E>,
V: Borrow<[u8]>,
C: Comparator,
for<'a> &'a mut T: IntoIterator<Item = &'a mut EntryWithKeyBuilder<KB, V, C>>,
for<'a> &'a mut T: IntoIterator<Item = &'a mut EntryWithKeyBuilder<KB, V, P>>,
P: 'static,
{
type KeyBuilder = KB;
type Error = E;
type Value = V;
type Comparator = C;

type IterMut<'a> = <&'a mut T as IntoIterator>::IntoIter where Self: 'a;

Expand All @@ -93,41 +86,34 @@ where
/// A batch of keys and values that can be inserted into the [`Wal`](super::Wal).
/// Comparing to [`Batch`], this trait is used to build
/// the value in place.
pub trait BatchWithValueBuilder {
pub trait BatchWithValueBuilder<P: 'static> {
/// The value builder type.
type ValueBuilder: Fn(&mut VacantBuffer<'_>) -> Result<(), Self::Error>;

/// The error for the value builder.
type Error;

/// The key type.
type Key: Borrow<[u8]>;

/// The [`Comparator`] type.
type Comparator: Comparator;
type Key;

/// The iterator type.
type IterMut<'a>: Iterator<
Item = &'a mut EntryWithValueBuilder<Self::Key, Self::ValueBuilder, Self::Comparator>,
>
type IterMut<'a>: Iterator<Item = &'a mut EntryWithValueBuilder<Self::Key, Self::ValueBuilder, P>>
where
Self: 'a;

/// Returns an iterator over the keys and values.
fn iter_mut(&mut self) -> Self::IterMut<'_>;
}

impl<K, VB, E, C, T> BatchWithValueBuilder for T
impl<K, VB, E, P, T> BatchWithValueBuilder<P> for T
where
VB: Fn(&mut VacantBuffer<'_>) -> Result<(), E>,
K: Borrow<[u8]>,
C: Comparator,
for<'a> &'a mut T: IntoIterator<Item = &'a mut EntryWithValueBuilder<K, VB, C>>,
for<'a> &'a mut T: IntoIterator<Item = &'a mut EntryWithValueBuilder<K, VB, P>>,
P: 'static,
{
type Key = K;
type Error = E;
type ValueBuilder = VB;
type Comparator = C;

type IterMut<'a> = <&'a mut T as IntoIterator>::IntoIter where Self: 'a;

Expand All @@ -139,7 +125,7 @@ where
/// A batch of keys and values that can be inserted into the [`Wal`](super::Wal).
/// Comparing to [`Batch`], this trait is used to build
/// the key and value in place.
pub trait BatchWithBuilders {
pub trait BatchWithBuilders<P: 'static> {
/// The value builder type.
type ValueBuilder: Fn(&mut VacantBuffer<'_>) -> Result<(), Self::ValueError>;

Expand All @@ -152,12 +138,9 @@ pub trait BatchWithBuilders {
/// The error for the value builder.
type KeyError;

/// The [`Comparator`] type.
type Comparator: Comparator;

/// The iterator type.
type IterMut<'a>: Iterator<
Item = &'a mut EntryWithBuilders<Self::KeyBuilder, Self::ValueBuilder, Self::Comparator>,
Item = &'a mut EntryWithBuilders<Self::KeyBuilder, Self::ValueBuilder, P>,
>
where
Self: 'a;
Expand All @@ -166,18 +149,17 @@ pub trait BatchWithBuilders {
fn iter_mut(&mut self) -> Self::IterMut<'_>;
}

impl<KB, KE, VB, VE, C, T> BatchWithBuilders for T
impl<KB, KE, VB, VE, P, T> BatchWithBuilders<P> for T
where
VB: Fn(&mut VacantBuffer<'_>) -> Result<(), VE>,
KB: Fn(&mut VacantBuffer<'_>) -> Result<(), KE>,
C: Comparator,
for<'a> &'a mut T: IntoIterator<Item = &'a mut EntryWithBuilders<KB, VB, C>>,
for<'a> &'a mut T: IntoIterator<Item = &'a mut EntryWithBuilders<KB, VB, P>>,
P: 'static,
{
type KeyBuilder = KB;
type KeyError = KE;
type ValueBuilder = VB;
type ValueError = VE;
type Comparator = C;

type IterMut<'a> = <&'a mut T as IntoIterator>::IntoIter where Self: 'a;

Expand Down
Loading

0 comments on commit ddb999f

Please sign in to comment.