minerva 0.2.0

Causal ordering for distributed systems
extern crate alloc;

use alloc::vec::Vec;

use crate::metis::{DotFun, DotMap, DotSet, Dotted};

use super::{CursorStore, Replica};

impl Replica {
    /// Moves this replica's own cursor to `position` under a device key.
    pub fn move_cursor(&mut self, device: u32, position: u64) {
        let _ = self.cursors.compose_super(
            |assigned| {
                let leaf = DotFun::singleton(assigned, position);
                DotMap::singleton(device, leaf)
            },
            |held| held.get(&device).map(DotFun::observed).unwrap_or_default(),
        );
    }

    /// The surviving cursor sibling positions under `device`.
    #[must_use]
    pub fn cursor_positions(&self, device: u32) -> Vec<u64> {
        self.cursors
            .state()
            .store()
            .get(&device)
            .map(|leaf| leaf.values().copied().collect())
            .unwrap_or_default()
    }

    /// The cursor delta owed to an explicit peer context.
    #[must_use]
    pub fn cursor_owed_to(&self, peer_context: &DotSet) -> Dotted<CursorStore> {
        self.cursors.owed_to(peer_context)
    }

    /// Everything this cursor layer has ever seen.
    #[must_use]
    pub fn cursor_context(&self) -> &DotSet {
        self.cursors.state().context()
    }

    /// Absorbs a cursor delta from a peer.
    pub fn absorb_cursor(&mut self, from: u32, delta: &Dotted<CursorStore>) {
        let _ = self.cursors.absorb(from, delta);
    }
}