haematite 0.6.2

Content-addressed, branchable, actor-native storage engine
Documentation
use super::policy::BranchKind;
use super::time::Timestamp;
use crate::ids::ShardId;
use crate::tree::Hash;

/// One shard's entry in a branch ref record: where the branch diverged and
/// where its committed head currently is.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BranchShardRef {
    /// Shard this entry pins roots for.
    pub shard_id: ShardId,
    /// Divergence point used as the three-way-merge ancestor. It is immutable
    /// for the branch's durable lifetime.
    pub fork_anchor: Hash,
    /// Current committed root for this shard.
    pub head: Hash,
}

/// Durable truth for one named branch.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BranchRefRecord {
    /// Full branch name (the on-disk file name is only its hash).
    pub name: String,
    /// Creation identity, immutable for this branch generation.
    pub created: Timestamp,
    /// Immutable branch kind. Pre-marker records decode as [`BranchKind::Work`].
    pub kind: BranchKind,
    /// Namespace lineage inherited by a Work branch at fork time.
    ///
    /// Namespace records resolve to their own name and store `None` here. A
    /// directly-created or legacy Work branch has no namespace lineage.
    pub namespace_lineage: Option<String>,
    /// Durable commit sequence; `0` means created and never committed.
    pub seq: u64,
    /// When this record was installed, in caller-supplied nanoseconds.
    pub timestamp: Timestamp,
    /// Per-shard anchor/head pairs, strictly ascending by shard id.
    pub shards: Vec<BranchShardRef>,
    /// Tree roots of this head commit's parents.
    pub parents: Vec<Hash>,
}

impl BranchRefRecord {
    /// Resolves the namespace boundary from durable record state only.
    #[must_use]
    #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
    pub fn resolved_namespace_lineage(&self) -> Option<&str> {
        match self.kind {
            BranchKind::Namespace => Some(&self.name),
            BranchKind::Work => self.namespace_lineage.as_deref(),
        }
    }
}