SkipList

Struct SkipList 

Source
pub struct SkipList<K: Eq + Hash, V> { /* private fields */ }
Expand description

A concurrent, multi-version, transactional skiplist.

SkipList is the core data structure that stores key-value pairs. It supports highly concurrent reads and writes using Multi-Version Concurrency Control (MVCC) and Serializable Snapshot Isolation (SSI).

Implementations§

Source§

impl<K, V> SkipList<K, V>
where K: Ord + Clone + Send + Sync + 'static + Hash + Eq + Borrow<str> + Serialize + DeserializeOwned, V: Clone + Send + Sync + 'static + Serialize + DeserializeOwned,

Source

pub async fn vacuum(&self) -> Result<(usize, usize), ()>

Scans the database and removes dead data versions to reclaim space.

This function performs two main tasks:

  1. Version Pruning: It iterates through every key’s version chain and removes any version that is considered “dead”. A version is dead if it was expired by a transaction that has since committed, and that transaction is older than any currently active transaction (i.e., it’s before the “vacuum horizon”).

  2. Node Pruning: If, after pruning versions, a key’s version chain becomes empty, the node itself is logically marked as deleted. A subsequent traversal of the skiplist will then physically unlink and reclaim the memory for this node.

Finally, it updates the TransactionManager to allow it to prune its own historical transaction status records, further saving memory.

§Returns

A Result containing a tuple of (versions_removed, keys_removed).

Source§

impl<K, V> SkipList<K, V>
where K: Ord + Clone + Send + Sync + 'static + Hash + Eq + Serialize + DeserializeOwned, V: Clone + Send + Sync + 'static + Serialize + DeserializeOwned,

Source

pub fn new() -> Self

Creates a new, empty SkipList with the default max level.

Source

pub fn with_max_level(max_level: usize) -> Self

Creates a new, empty SkipList with a specified max level.

Source

pub fn with_max_level_and_p(max_level: usize, p: f64) -> Self

Creates a new, empty SkipList with a specified max level and probability factor.

Source

pub fn transaction_manager(&self) -> &Arc<TransactionManager<K, V>>

Returns a reference to the associated TransactionManager.

Source

pub fn len(&self) -> usize

Returns the approximate number of keys in the skiplist.

This is an approximation because it may not reflect in-flight additions or removals.

Source

pub fn is_empty(&self) -> bool

Returns true if the skiplist contains no keys.

Source

pub fn get(&self, key: &K, transaction: &Transaction<K, V>) -> Option<Arc<V>>

Retrieves the value associated with key that is visible to the given transaction.

This method traverses the skiplist to find the node for the given key. It then walks the version chain for that node to find the most recent version that is visible according to the transaction’s Snapshot.

As part of the SSI protocol, this operation adds the key to the transaction’s read set.

Source

pub fn contains_key(&self, key: &K, transaction: &Transaction<K, V>) -> bool

Checks if a key exists and is visible to the given transaction.

Source

pub async fn insert( &self, key: K, value: Arc<V>, transaction: &Transaction<K, V>, )

Inserts a key-value pair as part of a transaction.

If the key already exists, this prepends a new version to its version chain. If the key does not exist, this creates a new Node and links it into the skiplist.

This operation adds the key to the transaction’s write set for SSI conflict detection.

Source

pub async fn remove( &self, key: &K, transaction: &Transaction<K, V>, ) -> Option<Arc<V>>

Logically removes a key as part of a transaction.

This finds the latest visible version of the key and atomically sets its expirer_txid to the current transaction’s ID. The actual data is not removed until the vacuum process runs.

This operation adds the key to the transaction’s write set.

§Returns

Returns the value that was removed if a visible version was found, otherwise None.

Source

pub fn range(&self, start: &K, end: &K, snapshot: &Snapshot) -> Vec<(K, Arc<V>)>

Scans a range of keys and returns the visible versions as a Vec.

Source

pub fn range_stream<'a>( &'a self, start: &'a K, end: &'a K, snapshot: &'a Snapshot, ) -> impl Stream<Item = (K, Arc<V>)> + 'a

Returns a stream that yields visible key-value pairs within a given range.

Source§

impl<K, V> SkipList<K, V>
where K: Ord + Clone + Send + Sync + 'static + Borrow<str> + Hash + Eq + Serialize + DeserializeOwned, V: Clone + Send + Sync + 'static + Serialize + DeserializeOwned,

Source

pub fn prefix_scan(&self, prefix: &str, snapshot: &Snapshot) -> Vec<(K, Arc<V>)>

Scans for keys starting with a given prefix and returns the visible versions as a Vec.

Source

pub fn prefix_scan_stream<'a>( &'a self, prefix: &'a str, snapshot: &'a Snapshot, ) -> impl Stream<Item = (K, Arc<V>)> + 'a

Returns a stream that yields visible key-value pairs for keys starting with a given prefix.

Trait Implementations§

Source§

impl<K, V> Default for SkipList<K, V>
where K: Ord + Clone + Send + Sync + 'static + Hash + Eq + Serialize + DeserializeOwned, V: Clone + Send + Sync + 'static + Serialize + DeserializeOwned,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<K, V> !Freeze for SkipList<K, V>

§

impl<K, V> !RefUnwindSafe for SkipList<K, V>

§

impl<K, V> Send for SkipList<K, V>
where K: Send + Sync, V: Sync + Send,

§

impl<K, V> Sync for SkipList<K, V>
where K: Send + Sync, V: Sync + Send,

§

impl<K, V> Unpin for SkipList<K, V>

§

impl<K, V> !UnwindSafe for SkipList<K, V>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.