pub struct ORSet<T: Ord + Clone> { /* private fields */ }Expand description
An observed-remove set (OR-Set), also known as an add-wins set.
Unlike the 2P-Set, elements can be freely added and removed, and re-added after removal. Each add operation generates a unique tag. Remove only removes the tags that the remover has observed, so concurrent adds are preserved.
§Example
use crdt_kit::prelude::*;
let mut s1 = ORSet::new(1);
s1.insert("apple");
s1.insert("banana");
s1.remove(&"banana");
let mut s2 = ORSet::new(2);
s2.insert("banana"); // concurrent add
s1.merge(&s2);
// banana is present because s2's add was concurrent with s1's remove
assert!(s1.contains(&"banana"));
assert!(s1.contains(&"apple"));Implementations§
Source§impl<T: Ord + Clone> ORSet<T>
impl<T: Ord + Clone> ORSet<T>
Sourcepub fn insert(&mut self, value: T)
pub fn insert(&mut self, value: T)
Insert an element into the set.
Generates a unique tag for this insertion. Even if the element was previously removed, this new tag allows it to be re-added.
Sourcepub fn remove(&mut self, value: &T) -> bool
pub fn remove(&mut self, value: &T) -> bool
Remove an element from the set.
Only removes the tags that this replica has observed. Concurrent adds on other replicas will survive the merge.
Returns true if the element was present and removed.
Sourcepub fn tombstone_count(&self) -> usize
pub fn tombstone_count(&self) -> usize
Get the number of tombstones stored.
Sourcepub fn compact_tombstones(&mut self) -> usize
pub fn compact_tombstones(&mut self) -> usize
Remove tombstones that are no longer needed.
A tombstone is only needed while it might suppress a tag that hasn’t been propagated yet. Once no live element carries the same tag, the tombstone is safe to discard.
Note: this is a local-only GC. It is safe to call at any time, but for best results call it after all peers have converged.
Returns the number of tombstones removed.
Sourcepub fn compact_tombstones_all(&mut self) -> usize
pub fn compact_tombstones_all(&mut self) -> usize
Aggressively remove all tombstones.
This is safe only when every peer has converged to the same state.
Returns the number of tombstones removed.