link-cli 3.5.0

A CLI tool and reusable library for links manipulation backed by a LiNo-notation doublet storage engine.
Documentation
//! Migration state shares the row transaction's durable commit decision.

use super::*;

const PREFIX: &str = "__transactions:migrations:";
const CHECKPOINT: &str = "checkpoint";

impl<T, S, L> GenericTransactionsDecorator<T, S, L>
where
    T: LinkReference,
    S: LinksStorage<T>,
    L: TransitionLogStore,
{
    /// The latest committed migration state, independent of row retention.
    pub fn migration_state(&mut self) -> Result<Option<String>, LinkError> {
        let mut state = None;
        for entry in self.log_store.read_log_entries()? {
            let Some((owner, payload)) = entry
                .strip_prefix(PREFIX)
                .and_then(|entry| entry.split_once(':'))
            else {
                continue;
            };
            if owner == CHECKPOINT
                || u128::from_str_radix(owner, 16).is_ok_and(|id| self.committed.contains(&id))
            {
                state = Some(payload.to_owned());
            }
        }
        Ok(state)
    }

    /// Attaches a serialized state to the currently open transaction.
    pub fn stage_migration_state(&mut self, state: &str) -> Result<(), LinkError> {
        self.ensure_write_allowed()?;
        let owner = self.current.as_ref().ok_or_else(|| {
            LinkError::Transaction("Migration state requires an open transaction.".into())
        })?;
        if state.contains(['\n', '\r', '\0']) {
            return Err(LinkError::InvalidFormat(
                "Invalid migration state line.".into(),
            ));
        }
        self.append_journal_entry(&format!("{PREFIX}{:032x}:{state}", owner.id))
    }

    pub(super) fn compact_migration_state(&mut self) -> Result<Option<String>, LinkError> {
        Ok(self
            .migration_state()?
            .map(|state| format!("{PREFIX}{CHECKPOINT}:{state}")))
    }
}