LatticeMap

Struct LatticeMap 

Source
pub struct LatticeMap<K, V> { /* private fields */ }
Expand description

Pointwise map lattice over HashMap.

This is a reusable building block for CRDT states that look like “map from IDs to lattice values”.

Keys are optional; values form a join-semilattice. The induced lattice order is:

m1 ≤ m2 iff for all k, m1[k] ≤ m2[k]

Operationally, join is:

  • keys: union of the key sets
  • values: pointwise join on overlapping keys

Bottom is the empty map.

§Key growth and deletion

Under this lattice, the set of keys grows monotonically under join (it is the union of key sets). As a result, LatticeMap does not directly support deletion semantics (there is no “remove” operation that is monotone w.r.t. this order).

Different applications want different deletion semantics and may require causal context, so LatticeMap intentionally leaves deletion policy to composition.

To model deletions, encode them in the value lattice V (tombstones), or use a dedicated set/map CRDT depending on the desired semantics. For example, one simple tombstone pattern is:

use algebra::{LatticeMap, LWW};

// Treat `None` as "deleted" at the application layer.
// Conflicts resolve via LWW; a delete can be represented by
// writing `None`.
type Tombstoned<V> = LWW<Option<V>>;
type Map<K, V> = LatticeMap<K, Tombstoned<V>>;

(If this pattern becomes common, we can add a small helper wrapper type later, but the policy is intentionally left to composition.)

§Example: Watermark tracking

Track the low watermark across multiple ranks where each rank reports its progress monotonically:

use algebra::JoinSemilattice;
use algebra::LatticeMap;
use algebra::Min;

// Rank 0 reports progress: 100
let mut state1: LatticeMap<u32, Min<u64>> = LatticeMap::new();
state1.insert(0, Min(100));

// Rank 1 reports progress: 200
let mut state2: LatticeMap<u32, Min<u64>> = LatticeMap::new();
state2.insert(1, Min(200));

// Merge: union keys, pointwise min on values
let merged = state1.join(&state2);

// Low watermark is the min across all ranks
let watermark = merged.iter().map(|(_, v)| v.0).min().unwrap();
assert_eq!(watermark, 100); // min(rank0=100, rank1=200)

§Commutativity and Idempotence

Unlike a simple HashMap with last-write-wins merge, LatticeMap provides true commutativity:

use algebra::JoinSemilattice;
use algebra::LatticeMap;
use algebra::Min;

let mut a: LatticeMap<u32, Min<i64>> = LatticeMap::new();
a.insert(0, Min(10));

let mut b: LatticeMap<u32, Min<i64>> = LatticeMap::new();
b.insert(0, Min(20));

// Join is commutative: a ⊔ b = b ⊔ a
assert_eq!(a.join(&b), b.join(&a));

// Join is idempotent: a ⊔ a = a
assert_eq!(a.join(&a), a);

Implementations§

Source§

impl<K, V> LatticeMap<K, V>
where K: Eq + Hash,

Source

pub fn new() -> Self

Create an empty map lattice.

Source

pub fn insert(&mut self, k: K, v: V)

Insert or replace a value for a key.

Source

pub fn get(&self, k: &K) -> Option<&V>

Get a reference to the value for this key, if present.

Source

pub fn iter(&self) -> impl Iterator<Item = (&K, &V)>

Iterate over (key, value) pairs.

Source

pub fn as_inner(&self) -> &HashMap<K, V>

Access the underlying HashMap.

Source

pub fn into_inner(self) -> HashMap<K, V>

Consume the wrapper and return the underlying HashMap.

Source

pub fn len(&self) -> usize

Number of entries.

Source

pub fn is_empty(&self) -> bool

Is the map empty?

Trait Implementations§

Source§

impl<K, V> BoundedJoinSemilattice for LatticeMap<K, V>

Source§

fn bottom() -> Self

The bottom element of the lattice (⊥). Read more
Source§

fn join_all_from_bottom<I>(it: I) -> Self
where I: IntoIterator<Item = Self>,

Join a finite iterator of values, starting from ⊥. Read more
Source§

impl<K: Clone, V: Clone> Clone for LatticeMap<K, V>

Source§

fn clone(&self) -> LatticeMap<K, V>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Debug, V: Debug> Debug for LatticeMap<K, V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K, V> Default for LatticeMap<K, V>
where K: Eq + Hash,

Source§

fn default() -> Self

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

impl<'de, K, V> Deserialize<'de> for LatticeMap<K, V>
where K: Eq + Hash + Deserialize<'de>, V: Deserialize<'de>,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<K, V> JoinSemilattice for LatticeMap<K, V>
where K: Eq + Hash + Clone, V: JoinSemilattice + Clone,

Source§

fn join(&self, other: &Self) -> Self

The join (least upper bound).
Source§

fn join_assign(&mut self, other: &Self)

In-place variant.
Source§

fn leq(&self, other: &Self) -> bool
where Self: PartialEq,

Derived partial order: x ≤ y iff x ⊔ y = y.
Source§

fn join_all<I>(it: I) -> Option<Self>
where I: IntoIterator<Item = Self>,

Join a finite iterator of values. Returns None for empty iterators.
Source§

impl<K, V> PartialEq for LatticeMap<K, V>
where K: Eq + Hash, V: PartialEq,

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<K, V> Serialize for LatticeMap<K, V>
where K: Eq + Hash + Serialize, V: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<K, V> Eq for LatticeMap<K, V>
where K: Eq + Hash, V: Eq,

Auto Trait Implementations§

§

impl<K, V> Freeze for LatticeMap<K, V>

§

impl<K, V> RefUnwindSafe for LatticeMap<K, V>

§

impl<K, V> Send for LatticeMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for LatticeMap<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for LatticeMap<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for LatticeMap<K, V>
where K: UnwindSafe, V: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Monoid for T

Source§

fn empty() -> T

The identity element.
Source§

fn concat<I>(iter: I) -> T
where I: IntoIterator<Item = T>,

Fold an iterator using combine, starting from empty.
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

impl<T> Semigroup for T
where T: JoinSemilattice,

Source§

fn combine(&self, other: &T) -> T

Combine two elements associatively.
Source§

fn combine_assign(&mut self, other: &T)

In-place combine.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.
Source§

impl<T> CommutativeMonoid for T

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,