pub struct CongeeArc<K: From<usize> + Copy, V: Sync + Send + 'static>{ /* 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>
impl<K: From<usize> + Copy, V: Sync + Send + 'static> CongeeArc<K, V>
Sourcepub fn new() -> Self
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();
Sourcepub fn pin(&self) -> Guard
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();
Sourcepub fn is_empty(&self, guard: &Guard) -> bool
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));
Sourcepub fn remove(&self, key: K, guard: &Guard) -> Option<Arc<V>>
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));
Sourcepub fn get(&self, key: K, guard: &Guard) -> Option<Arc<V>>
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");
Sourcepub fn insert(
&self,
key: K,
val: Arc<V>,
guard: &Guard,
) -> Result<Option<Arc<V>>, OOMError>
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");
Sourcepub fn compute_if_present<F>(
&self,
key: K,
f: F,
guard: &Guard,
) -> Option<Arc<V>>
pub fn compute_if_present<F>( &self, key: K, f: F, guard: &Guard, ) -> 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());
Sourcepub fn keys(&self) -> Vec<K>
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);