Skip to content

Commit

Permalink
bumpup dbutils version
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Sep 25, 2024
1 parent 9d6a145 commit 5190b18
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 970 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "orderwal"
version = "0.3.0"
version = "0.3.1"
edition = "2021"
repository = "https://github.com/al8n/orderwal"
homepage = "https://github.com/al8n/orderwal"
Expand All @@ -26,7 +26,7 @@ xxhash64 = ["dbutils/xxhash64", "std"]
[dependencies]
among = { version = "0.1", default-features = false, features = ["either"] }
bitflags = { version = "1", default-features = false }
dbutils = { version = "0.3", default-features = false, features = ["crc32fast"] }
dbutils = { version = "0.4", default-features = false, features = ["crc32fast"] }
rarena-allocator = { version = "0.3", default-features = false, features = ["memmap"] }
crossbeam-skiplist = { version = "0.1", default-features = false, package = "crossbeam-skiplist-pr1132" }
paste = "1"
Expand Down
6 changes: 3 additions & 3 deletions examples/zero_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl<'a> KeyRef<'a, Person> for PersonRef<'a> {
Comparable::compare(a, self).reverse()
}

fn compare_binary(this: &[u8], other: &[u8]) -> cmp::Ordering {
unsafe fn compare_binary(this: &[u8], other: &[u8]) -> cmp::Ordering {
let (this_id_size, this_id) = decode_u64_varint(this).unwrap();
let (other_id_size, other_id) = decode_u64_varint(other).unwrap();

Expand All @@ -107,10 +107,10 @@ impl Type for Person {
encoded_u64_varint_len(self.id) + self.name.len()
}

fn encode(&self, buf: &mut [u8]) -> Result<(), Self::Error> {
fn encode(&self, buf: &mut [u8]) -> Result<usize, Self::Error> {
let id_size = encode_u64_varint(self.id, buf)?;
buf[id_size..].copy_from_slice(self.name.as_bytes());
Ok(())
Ok(id_size + self.name.len())
}
}

Expand Down
55 changes: 14 additions & 41 deletions src/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,56 +1,29 @@
pub use dbutils::buffer::VacantBuffer;
pub use dbutils::{buffer::VacantBuffer, builder};

macro_rules! builder {
($($name:ident($size:ident)),+ $(,)?) => {
macro_rules! builder_ext {
($($name:ident),+ $(,)?) => {
$(
paste::paste! {
#[doc = "A " [< $name: snake>] " builder for the wal, which requires the " [< $name: snake>] " size for accurate allocation and a closure to build the " [< $name: snake>]]
#[derive(Copy, Clone, Debug)]
pub struct [< $name Builder >] <F> {
size: $size,
f: F,
}

impl<F> [< $name Builder >]<F> {
#[doc = "Creates a new `" [<$name Builder>] "` with the given size and builder closure."]
impl<F> $name<F> {
#[doc = "Creates a new `" $name "` with the given size and builder closure which requires `FnOnce`."]
#[inline]
pub const fn once<E>(size: $size, f: F) -> Self
pub const fn once<E>(size: u32, f: F) -> Self
where
F: for<'a> FnOnce(&mut VacantBuffer<'a>) -> Result<(), E>,
{
Self { size, f }
}

#[doc = "Creates a new `" [<$name Builder>] "` with the given size and builder closure."]
#[inline]
pub const fn new<E>(size: $size, f: F) -> Self
where
F: for<'a> Fn(&mut VacantBuffer<'a>) -> Result<(), E>,
{
Self { size, f }
}

#[doc = "Returns the required" [< $name: snake>] "size."]
#[inline]
pub const fn size(&self) -> $size {
self.size
}

#[doc = "Returns the " [< $name: snake>] "builder closure."]
#[inline]
pub const fn builder(&self) -> &F {
&self.f
}

/// Deconstructs the value builder into the size and the builder closure.
#[inline]
pub fn into_components(self) -> ($size, F) {
(self.size, self.f)
}
}
}
)*
};
}

builder!(Value(u32), Key(u32));
builder!(
/// A value builder for the wal, which requires the value size for accurate allocation and a closure to build the value.
pub ValueBuilder(u32);
/// A key builder for the wal, which requires the key size for accurate allocation and a closure to build the key.
pub KeyBuilder(u32);
);

builder_ext!(ValueBuilder, KeyBuilder,);
4 changes: 2 additions & 2 deletions src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,13 @@ impl<T: Type> Generic<'_, T> {
}

#[inline]
pub(crate) fn encode(&self, buf: &mut [u8]) -> Result<(), T::Error> {
pub(crate) fn encode(&self, buf: &mut [u8]) -> Result<usize, T::Error> {
match &self.data {
Among::Left(val) => val.encode(buf),
Among::Middle(val) => val.encode(buf),
Among::Right(val) => {
buf.copy_from_slice(val);
Ok(())
Ok(buf.len())
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ extern crate std;

pub use dbutils::{
checksum::{self, Crc32},
Ascend, CheapClone, Comparator, Descend,
traits::{Ascend, Descend},
CheapClone, Comparator,
};

#[cfg(feature = "xxhash3")]
Expand Down
3 changes: 2 additions & 1 deletion src/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ where
for<'a> K::Ref<'a>: KeyRef<'a, K>,
{
fn cmp(&self, other: &Self) -> cmp::Ordering {
<K::Ref<'_> as KeyRef<K>>::compare_binary(self.as_key_slice(), other.as_key_slice())
// SAFETY: WALs guarantee that the self and other must be the same as the result returned by `<K as Type>::encode`.
unsafe { <K::Ref<'_> as KeyRef<K>>::compare_binary(self.as_key_slice(), other.as_key_slice()) }
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/swmr/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ where
for<'a> K::Ref<'a>: KeyRef<'a, K>,
{
fn cmp(&self, other: &Self) -> cmp::Ordering {
<K::Ref<'_> as KeyRef<K>>::compare_binary(self.as_key_slice(), other.as_key_slice())
unsafe { <K::Ref<'_> as KeyRef<K>>::compare_binary(self.as_key_slice(), other.as_key_slice()) }
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/swmr/generic/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl<'a> KeyRef<'a, Person> for PersonRef<'a> {
Comparable::compare(a, self).reverse()
}

fn compare_binary(this: &[u8], other: &[u8]) -> cmp::Ordering {
unsafe fn compare_binary(this: &[u8], other: &[u8]) -> cmp::Ordering {
let (this_id_size, this_id) = decode_u64_varint(this).unwrap();
let (other_id_size, other_id) = decode_u64_varint(other).unwrap();
PersonRef {
Expand All @@ -143,10 +143,10 @@ impl Type for Person {
encoded_u64_varint_len(self.id) + self.name.len()
}

fn encode(&self, buf: &mut [u8]) -> Result<(), Self::Error> {
fn encode(&self, buf: &mut [u8]) -> Result<usize, Self::Error> {
let id_size = encode_u64_varint(self.id, buf)?;
buf[id_size..].copy_from_slice(self.name.as_bytes());
Ok(())
Ok(id_size + self.name.len())
}
}

Expand Down
7 changes: 2 additions & 5 deletions src/swmr/wal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ use crate::{
error::Error,
pointer::Pointer,
wal::sealed::{Constructor, Sealed, WalCore},
Options,
};
use dbutils::{
checksum::{BuildChecksumer, Crc32},
Ascend,
Ascend, Options,
};
use dbutils::checksum::{BuildChecksumer, Crc32};
use rarena_allocator::{either::Either, Allocator};

pub use crate::{
Expand Down
8 changes: 4 additions & 4 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,7 @@ pub(crate) fn insert_batch_with_key_builder<W: Wal<Ascend, Crc32>>(wal: &mut W)

for i in 0..N {
batch.push(EntryWithKeyBuilder::new(
KeyBuilder::new(4, move |buf| buf.put_u32_be(i)),
KeyBuilder::new(4, move |buf: &mut VacantBuffer<'_>| buf.put_u32_be(i)),
i.to_be_bytes(),
));
}
Expand All @@ -1203,7 +1203,7 @@ pub(crate) fn insert_batch_with_value_builder<W: Wal<Ascend, Crc32>>(wal: &mut W
for i in 0..N {
batch.push(EntryWithValueBuilder::new(
i.to_be_bytes(),
ValueBuilder::new(4, move |buf| buf.put_u32_be(i)),
ValueBuilder::new(4, move |buf: &mut VacantBuffer<'_>| buf.put_u32_be(i)),
));
}

Expand All @@ -1226,8 +1226,8 @@ pub(crate) fn insert_batch_with_builders<W: Wal<Ascend, Crc32>>(wal: &mut W) {

for i in 0..N {
batch.push(EntryWithBuilders::new(
KeyBuilder::new(4, move |buf| buf.put_u32_be(i)),
ValueBuilder::new(4, move |buf| buf.put_u32_be(i)),
KeyBuilder::new(4, move |buf: &mut VacantBuffer<'_>| buf.put_u32_be(i)),
ValueBuilder::new(4, move |buf: &mut VacantBuffer<'_>| buf.put_u32_be(i)),
));
}

Expand Down
75 changes: 4 additions & 71 deletions src/wal/type.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,9 @@
use core::cmp;

use among::Among;
use dbutils::equivalent::Comparable;

mod impls;
pub use impls::*;

/// The type trait for limiting the types that can be used as keys and values in the [`GenericOrderWal`](crate::swmr::GenericOrderWal).
///
/// This trait and its implementors can only be used with the [`GenericOrderWal`](crate::swmr::GenericOrderWal) type, otherwise
/// the correctness of the implementations is not guaranteed.
pub trait Type: core::fmt::Debug {
/// The reference type for the type.
type Ref<'a>: TypeRef<'a>;

/// The error type for encoding the type into a binary format.
type Error;

/// Returns the length of the encoded type size.
fn encoded_len(&self) -> usize;

/// Encodes the type into a bytes slice, you can assume that the buf length is equal to the value returned by [`encoded_len`](Type::encoded_len).
fn encode(&self, buf: &mut [u8]) -> Result<(), Self::Error>;

/// Encodes the type into a [`Vec<u8>`].
#[inline]
fn encode_into_vec(&self) -> Result<Vec<u8>, Self::Error> {
let mut buf = vec![0; self.encoded_len()];
self.encode(&mut buf)?;
Ok(buf)
}
}

impl<T: Type> Type for &T {
type Ref<'a> = T::Ref<'a>;
type Error = T::Error;

#[inline]
fn encoded_len(&self) -> usize {
T::encoded_len(*self)
}

#[inline]
fn encode(&self, buf: &mut [u8]) -> Result<(), Self::Error> {
T::encode(*self, buf)
}
}
pub use dbutils::traits::{KeyRef, Type, TypeRef};

pub(crate) trait InsertAmongExt<T: Type> {
fn encoded_len(&self) -> usize;
fn encode(&self, buf: &mut [u8]) -> Result<(), T::Error>;
fn encode(&self, buf: &mut [u8]) -> Result<usize, T::Error>;
}

impl<T: Type> InsertAmongExt<T> for Among<T, &T, &[u8]> {
Expand All @@ -63,35 +17,14 @@ impl<T: Type> InsertAmongExt<T> for Among<T, &T, &[u8]> {
}

#[inline]
fn encode(&self, buf: &mut [u8]) -> Result<(), T::Error> {
fn encode(&self, buf: &mut [u8]) -> Result<usize, T::Error> {
match self {
Among::Left(t) => t.encode(buf),
Among::Middle(t) => t.encode(buf),
Among::Right(t) => {
buf.copy_from_slice(t);
Ok(())
Ok(buf.len())
}
}
}
}

/// The reference type trait for the [`Type`] trait.
pub trait TypeRef<'a>: core::fmt::Debug {
/// Creates a reference type from a binary slice, when using it with [`GenericOrderWal`](crate::swmr::GenericOrderWal),
/// you can assume that the slice is the same as the one returned by [`encode`](Type::encode).
///
/// ## Safety
/// - the `src` must the same as the one returned by [`encode`](Type::encode).
unsafe fn from_slice(src: &'a [u8]) -> Self;
}

/// The key reference trait for comparing `K` in the [`GenericOrderWal`](crate::swmr::GenericOrderWal).
pub trait KeyRef<'a, K>: Ord + Comparable<K> {
/// Compares with a type `Q` which can be borrowed from [`T::Ref`](Type::Ref).
fn compare<Q>(&self, a: &Q) -> cmp::Ordering
where
Q: ?Sized + Ord + Comparable<Self>;

/// Compares two binary formats of the `K` directly.
fn compare_binary(a: &[u8], b: &[u8]) -> cmp::Ordering;
}
Loading

0 comments on commit 5190b18

Please sign in to comment.