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 96e77ad
Show file tree
Hide file tree
Showing 13 changed files with 21 additions and 925 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
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
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 96e77ad

Please sign in to comment.