Skip to content

Commit

Permalink
0.21.0
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Nov 27, 2024
1 parent 70b1a85 commit e12b730
Show file tree
Hide file tree
Showing 24 changed files with 1,916 additions and 2,244 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 0.21.0

- Use state pattern for `EntryRef`

## 0.20.0

- Add dynamic `SkipMap`s
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "skl"
version = "0.20.2"
version = "0.21.0"
edition = "2021"
rust-version = "1.81.0"
repository = "https://github.com/al8n/skl"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@

```toml
[dependencies]
skl = "0.20"
skl = "0.21"
```

- Enable memory map backend

```toml
[dependencies]
skl = { version = "0.20", features = ["memmap"] }
skl = { version = "0.21", features = ["memmap"] }
```

## Features
Expand Down
19 changes: 2 additions & 17 deletions src/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,10 @@ pub mod entry {

pub use dbutils::equivalentor::*;

/// The value of an entry in the [`SkipMap`].
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
pub struct Value<T: ?Sized>(T);

impl<T> core::fmt::Debug for Value<T>
where
T: core::fmt::Debug + ?Sized,
{
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.0.fmt(f)
}
}

/// Value that can be converted from a byte slice.
pub trait FromValueBytes<'a>: sealed::Sealed<'a> {}
pub trait Value<'a>: sealed::Sealed<'a> {}

impl<'a, T> FromValueBytes<'a> for T where T: sealed::Sealed<'a> {}
impl<'a, T> Value<'a> for T where T: sealed::Sealed<'a> {}

mod sealed {
pub trait Sealed<'a> {
Expand Down
27 changes: 13 additions & 14 deletions src/dynamic/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use rarena_allocator::Allocator as _;

use crate::{
allocator::{Allocator, Deallocator, Meta, Node, NodePointer, Pointer, ValuePointer},
dynamic::Value,
encode_key_size_and_height,
error::Error,
internal::RefMeta,
Expand All @@ -24,14 +25,12 @@ use crate::{
mod entry;
pub use entry::EntryRef;

use super::FromValueBytes;

mod api;
pub(super) mod iterator;

type UpdateOk<'a, 'b, A, RC, C> = Either<
Option<EntryRef<'a, A, RC, C, Option<&'a [u8]>>>,
Result<EntryRef<'a, A, RC, C, Option<&'a [u8]>>, EntryRef<'a, A, RC, C, Option<&'a [u8]>>>,
Option<EntryRef<'a, Option<&'a [u8]>, C, A, RC>>,
Result<EntryRef<'a, Option<&'a [u8]>, C, A, RC>, EntryRef<'a, Option<&'a [u8]>, C, A, RC>>,
>;

/// A fast, cocnurrent map implementation based on skiplist that supports forward
Expand Down Expand Up @@ -321,9 +320,9 @@ where
nd: &mut <A::Node as Node>::Pointer,
version: Version,
contains_key: impl Fn(&[u8]) -> bool,
) -> Option<EntryRef<'a, A, R, C, V>>
) -> Option<EntryRef<'a, V, C, A, R>>
where
V: FromValueBytes<'a> + 'a,
V: Value<'a> + 'a,
{
loop {
unsafe {
Expand Down Expand Up @@ -355,9 +354,9 @@ where
nd: &mut <A::Node as Node>::Pointer,
version: Version,
contains_key: impl Fn(&[u8]) -> bool,
) -> Option<EntryRef<'a, A, R, C, V>>
) -> Option<EntryRef<'a, V, C, A, R>>
where
V: FromValueBytes<'a> + 'a,
V: Value<'a> + 'a,
{
loop {
unsafe {
Expand Down Expand Up @@ -415,9 +414,9 @@ where
nd: &mut <A::Node as Node>::Pointer,
version: Version,
contains_key: impl Fn(&[u8]) -> bool,
) -> Option<EntryRef<'a, A, R, C, V>>
) -> Option<EntryRef<'a, V, C, A, R>>
where
V: FromValueBytes<'a> + 'a,
V: Value<'a> + 'a,
{
loop {
unsafe {
Expand Down Expand Up @@ -449,9 +448,9 @@ where
nd: &mut <A::Node as Node>::Pointer,
version: Version,
contains_key: impl Fn(&[u8]) -> bool,
) -> Option<EntryRef<'a, A, R, C, V>>
) -> Option<EntryRef<'a, V, C, A, R>>
where
V: FromValueBytes<'a> + 'a,
V: Value<'a> + 'a,
{
loop {
unsafe {
Expand Down Expand Up @@ -1113,7 +1112,7 @@ where
unsafe fn upsert_value<'a, 'b: 'a>(
&'a self,
version: Version,
old: EntryRef<'a, A, R, C, Option<&'a [u8]>>,
old: EntryRef<'a, Option<&'a [u8]>, C, A, R>,
old_node: <A::Node as Node>::Pointer,
key: &Key<'a, 'b, A>,
value_offset: u32,
Expand Down Expand Up @@ -1154,7 +1153,7 @@ where
unsafe fn upsert<'a, 'b: 'a, E>(
&'a self,
version: Version,
old: EntryRef<'a, A, R, C, Option<&'a [u8]>>,
old: EntryRef<'a, Option<&'a [u8]>, C, A, R>,
old_node: <A::Node as Node>::Pointer,
key: &Key<'a, 'b, A>,
value_builder: Option<ValueBuilder<impl FnOnce(&mut VacantBuffer<'a>) -> Result<usize, E>>>,
Expand Down
16 changes: 8 additions & 8 deletions src/dynamic/list/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,30 +174,30 @@ where
}

/// Returns the first entry in the map.
pub fn first(&self, version: Version) -> Option<EntryRef<'_, A, RC, C, &[u8]>> {
pub fn first(&self, version: Version) -> Option<EntryRef<'_, &[u8], C, A, RC>> {
self.iter(version).next()
}

/// Returns the last entry in the map.
pub fn last(&self, version: Version) -> Option<EntryRef<'_, A, RC, C, &[u8]>> {
pub fn last(&self, version: Version) -> Option<EntryRef<'_, &[u8], C, A, RC>> {
self.iter(version).last()
}

/// Returns the first entry in the map.
pub fn first_versioned(&self, version: Version) -> Option<EntryRef<'_, A, RC, C, Option<&[u8]>>> {
pub fn first_versioned(&self, version: Version) -> Option<EntryRef<'_, Option<&[u8]>, C, A, RC>> {
self.iter_all_versions(version).next()
}

/// Returns the last entry in the map.
pub fn last_versioned(&self, version: Version) -> Option<EntryRef<'_, A, RC, C, Option<&[u8]>>> {
pub fn last_versioned(&self, version: Version) -> Option<EntryRef<'_, Option<&[u8]>, C, A, RC>> {
self.iter_all_versions(version).last()
}

/// Returns the value associated with the given key, if it exists.
///
/// This method will return `None` if the entry is marked as removed. If you want to get the entry even if it is marked as removed,
/// you can use [`get_versioned`](SkipList::get_versioned).
pub fn get(&self, version: Version, key: &[u8]) -> Option<EntryRef<'_, A, RC, C, &[u8]>> {
pub fn get(&self, version: Version, key: &[u8]) -> Option<EntryRef<'_, &[u8], C, A, RC>> {
unsafe {
let (n, eq) = self.find_near(version, key, false, true); // findLessOrEqual.

Expand Down Expand Up @@ -231,7 +231,7 @@ where
&self,
version: Version,
key: &[u8],
) -> Option<EntryRef<'_, A, RC, C, Option<&[u8]>>> {
) -> Option<EntryRef<'_, Option<&[u8]>, C, A, RC>> {
unsafe {
let (n, eq) = self.find_near(version, key, false, true); // findLessOrEqual.

Expand Down Expand Up @@ -272,7 +272,7 @@ where
&self,
version: Version,
upper: Bound<&[u8]>,
) -> Option<EntryRef<'_, A, RC, C, &[u8]>> {
) -> Option<EntryRef<'_, &[u8], C, A, RC>> {
self.iter(version).seek_upper_bound(upper)
}

Expand All @@ -282,7 +282,7 @@ where
&self,
version: Version,
lower: Bound<&[u8]>,
) -> Option<EntryRef<'_, A, RC, C, &[u8]>> {
) -> Option<EntryRef<'_, &[u8], C, A, RC>> {
self.iter(version).seek_lower_bound(lower)
}

Expand Down
20 changes: 10 additions & 10 deletions src/dynamic/list/api/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ where
version: Version,
key: &'b [u8],
value: &'b [u8],
) -> Result<Option<EntryRef<'a, A, R, C, &'a [u8]>>, Error> {
) -> Result<Option<EntryRef<'a, &'a [u8], C, A, R>>, Error> {
self.insert_at_height(version, self.random_height(), key, value)
}

Expand All @@ -40,7 +40,7 @@ where
height: Height,
key: &'b [u8],
value: &'b [u8],
) -> Result<Option<EntryRef<'a, A, R, C, &'a [u8]>>, Error> {
) -> Result<Option<EntryRef<'a, &'a [u8], C, A, R>>, Error> {
self.validate(height, key.len(), value.len())?;

let val_len = value.len();
Expand Down Expand Up @@ -90,7 +90,7 @@ where
height: Height,
key: &'b [u8],
value_builder: ValueBuilder<impl FnOnce(&mut VacantBuffer<'a>) -> Result<usize, E>>,
) -> Result<Option<EntryRef<'a, A, R, C, &'a [u8]>>, Either<E, Error>> {
) -> Result<Option<EntryRef<'a, &'a [u8], C, A, R>>, Either<E, Error>> {
self
.validate(height, key.len(), value_builder.size())
.map_err(Either::Right)?;
Expand Down Expand Up @@ -129,7 +129,7 @@ where
height: Height,
key: &'b [u8],
value: &'b [u8],
) -> Result<Option<EntryRef<'a, A, R, C, &'a [u8]>>, Error> {
) -> Result<Option<EntryRef<'a, &'a [u8], C, A, R>>, Error> {
self.validate(height, key.len(), value.len())?;

let val_len = value.len();
Expand Down Expand Up @@ -180,7 +180,7 @@ where
height: Height,
key: &'b [u8],
value_builder: ValueBuilder<impl FnOnce(&mut VacantBuffer<'a>) -> Result<usize, E>>,
) -> Result<Option<EntryRef<'a, A, R, C, &'a [u8]>>, Either<E, Error>> {
) -> Result<Option<EntryRef<'a, &'a [u8], C, A, R>>, Either<E, Error>> {
self
.validate(height, key.len(), value_builder.size())
.map_err(Either::Right)?;
Expand Down Expand Up @@ -224,7 +224,7 @@ where
height: Height,
key_builder: KeyBuilder<impl FnOnce(&mut VacantBuffer<'a>) -> Result<usize, KE>>,
value_builder: ValueBuilder<impl FnOnce(&mut VacantBuffer<'a>) -> Result<usize, VE>>,
) -> Result<Option<EntryRef<'a, A, R, C, &'a [u8]>>, Among<KE, VE, Error>> {
) -> Result<Option<EntryRef<'a, &'a [u8], C, A, R>>, Among<KE, VE, Error>> {
self
.validate(height, key_builder.size(), value_builder.size())
.map_err(Among::Right)?;
Expand Down Expand Up @@ -273,7 +273,7 @@ where
height: Height,
key_builder: KeyBuilder<impl FnOnce(&mut VacantBuffer<'a>) -> Result<usize, KE>>,
value_builder: ValueBuilder<impl FnOnce(&mut VacantBuffer<'a>) -> Result<usize, VE>>,
) -> Result<Option<EntryRef<'a, A, R, C, &'a [u8]>>, Among<KE, VE, Error>> {
) -> Result<Option<EntryRef<'a, &'a [u8], C, A, R>>, Among<KE, VE, Error>> {
self
.validate(height, key_builder.size(), value_builder.size())
.map_err(Among::Right)?;
Expand Down Expand Up @@ -323,7 +323,7 @@ where
key: &'b [u8],
success: Ordering,
failure: Ordering,
) -> Result<Option<EntryRef<'a, A, R, C, &'a [u8]>>, Error> {
) -> Result<Option<EntryRef<'a, &'a [u8], C, A, R>>, Error> {
self.validate(height, key.len(), 0)?;

self
Expand Down Expand Up @@ -370,7 +370,7 @@ where
version: Version,
height: Height,
key: &'b [u8],
) -> Result<Option<EntryRef<'a, A, R, C, &'a [u8]>>, Error> {
) -> Result<Option<EntryRef<'a, &'a [u8], C, A, R>>, Error> {
self.validate(height, key.len(), 0)?;

self
Expand Down Expand Up @@ -416,7 +416,7 @@ where
version: Version,
height: Height,
key_builder: KeyBuilder<impl FnOnce(&mut VacantBuffer<'a>) -> Result<usize, E>>,
) -> Result<Option<EntryRef<'a, A, R, C, &'a [u8]>>, Either<E, Error>> {
) -> Result<Option<EntryRef<'a, &'a [u8], C, A, R>>, Either<E, Error>> {
self
.validate(height, key_builder.size(), 0)
.map_err(Either::Right)?;
Expand Down
Loading

0 comments on commit e12b730

Please sign in to comment.