Struct CongeeArc

Source
pub struct CongeeArc<K: From<usize> + Copy, V: Sync + Send + 'static>
where usize: From<K>,
{ /* private fields */ }
Expand description

A concurrent map-like data structure that uses Arc for reference counting of values.

CongeeArc provides a way to store Arc-wrapped values in a concurrent tree structure. It automatically manages reference counting when inserting, retrieving, and removing values.

Implementations§

Source§

impl<K: From<usize> + Copy, V: Sync + Send + 'static> CongeeArc<K, V>
where usize: From<K>,

Source

pub fn new() -> Self

Creates a new empty CongeeArc instance.

§Examples
use congee::CongeeArc;
use std::sync::Arc;

let tree: CongeeArc<usize, String> = CongeeArc::new();
Source

pub fn pin(&self) -> Guard

Enters an epoch.

This is necessary before performing operations on the tree. Note: this can be expensive, try to reuse it.

§Examples
use congee::CongeeArc;
use std::sync::Arc;

let tree: CongeeArc<usize, String> = CongeeArc::new();
let guard = tree.pin();
Source

pub fn is_empty(&self, guard: &Guard) -> bool

Returns true if the tree is empty.

§Examples
use congee::CongeeArc;
use std::sync::Arc;

let tree: CongeeArc<usize, String> = CongeeArc::new();
let guard = tree.pin();

assert!(tree.is_empty(&guard));

let value = Arc::new(String::from("value"));
tree.insert(1, value, &guard).unwrap();
assert!(!tree.is_empty(&guard));
Source

pub fn remove(&self, key: K, guard: &Guard) -> Option<Arc<V>>

Removes a key-value pair from the tree and returns the removed value (if present).

Note: Congee holds a reference to the removed value until the guard is flushed.

§Examples
use congee::CongeeArc;
use std::sync::Arc;

let tree: CongeeArc<usize, String> = CongeeArc::new();
let guard = tree.pin();

let value = Arc::new(String::from("hello"));
tree.insert(1, value, &guard).unwrap();

let removed = tree.remove(1, &guard).unwrap();
assert_eq!(removed.as_ref(), "hello");
assert!(tree.is_empty(&guard));
Source

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

Retrieves a value from the tree without removing it.

§Examples
use congee::CongeeArc;
use std::sync::Arc;

let tree: CongeeArc<usize, String> = CongeeArc::new();
let guard = tree.pin();

let value = Arc::new(String::from("hello"));
tree.insert(1, value.clone(), &guard).unwrap();

let retrieved = tree.get(1, &guard).unwrap();
assert_eq!(retrieved.as_ref(), "hello");
Source

pub fn insert( &self, key: K, val: Arc<V>, guard: &Guard, ) -> Result<Option<Arc<V>>, OOMError>

Inserts a key-value pair into the tree.

If the key already exists, the old value is replaced and returned.

Note: Congee holds a reference to the returned old value until the guard is flushed.

§Examples
use congee::CongeeArc;
use std::sync::Arc;

let tree: CongeeArc<usize, String> = CongeeArc::new();
let guard = tree.pin();

let value1 = Arc::new(String::from("hello"));
assert!(tree.insert(1, value1, &guard).unwrap().is_none());

let value2 = Arc::new(String::from("world"));
let old = tree.insert(1, value2, &guard).unwrap().unwrap();
assert_eq!(old.as_ref(), "hello");
Source

pub fn compute_if_present<F>( &self, key: K, f: F, guard: &Guard, ) -> Option<Arc<V>>
where F: FnMut(Arc<V>) -> Option<Arc<V>>,

Computes a new value for a key if it exists in the tree.

The function f is called with the current value and should return an optional new value. If f returns None, the key-value pair is removed from the tree. If f returns Some(new_value), the key is updated with the new value.

Note: Congee holds a reference to the returned old value until the guard is flushed.

§Examples
use congee::CongeeArc;
use std::sync::Arc;

let tree: CongeeArc<usize, String> = CongeeArc::new();
let guard = tree.pin();

let value = Arc::new(String::from("hello"));
tree.insert(1, value, &guard).unwrap();

// Update an existing value
let old = tree.compute_if_present(
    1,
    |current| Some(Arc::new(format!("{} world", current))),
    &guard
).unwrap();
assert_eq!(old.as_ref(), "hello");

let updated = tree.get(1, &guard).unwrap();
assert_eq!(updated.as_ref(), "hello world");

// Remove a value by returning None
tree.compute_if_present(1, |_| None, &guard);
assert!(tree.get(1, &guard).is_none());
Source

pub fn keys(&self) -> Vec<K>

Retrieves all keys from the tree.

Isolation level: read committed.

§Examples
use congee::CongeeArc;
use std::sync::Arc;

let tree: CongeeArc<usize, String> = CongeeArc::new();
let guard = tree.pin();

let value1 = Arc::new(String::from("value1"));
let value2 = Arc::new(String::from("value2"));
tree.insert(1, value1, &guard).unwrap();
tree.insert(2, value2, &guard).unwrap();

let keys = tree.keys();
assert!(keys.contains(&1));
assert!(keys.contains(&2));
assert_eq!(keys.len(), 2);

Trait Implementations§

Source§

impl<K: From<usize> + Copy, V: Sync + Send + 'static> Default for CongeeArc<K, V>
where usize: From<K>,

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<K, V> Freeze for CongeeArc<K, V>
where usize: Sized,

§

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

§

impl<K, V> Send for CongeeArc<K, V>
where usize: Sized, K: Send,

§

impl<K, V> Sync for CongeeArc<K, V>
where usize: Sized, K: Sync,

§

impl<K, V> Unpin for CongeeArc<K, V>
where usize: Sized, V: Unpin, K: Unpin,

§

impl<K, V> !UnwindSafe for CongeeArc<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.