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,
{
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)
}
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}")))
}
}