use bytes::Bytes;
use super::context::ChangelogContext;
use super::store::{CHANGE_SPACE, COMMIT_SPACE, ChangelogReader, change_key, commit_key};
use super::types::{ChangeId, CommitId, CommitLoadRequest};
use crate::LixError;
use crate::storage_adapter::{
PointReadPlan, StorageAdapterRead, StorageGetOptions, StorageKey, StorageWriteSet,
};
pub(crate) async fn stage_delete_commit_projection<S>(
store: &S,
writes: &mut StorageWriteSet,
commit_id: CommitId,
) -> Result<(), LixError>
where
S: StorageAdapterRead + ?Sized,
{
let commit_ids = [commit_id];
let record = ChangelogContext::new()
.reader(store)
.load_commits(CommitLoadRequest {
commit_ids: &commit_ids,
})
.await?
.into_iter()
.next()
.and_then(|(_, record)| record);
let Some(record) = record else {
return Ok(());
};
if record.commit_id != commit_id {
return Err(LixError::new(
LixError::CODE_INTERNAL_ERROR,
format!(
"commit projection key for '{commit_id}' contains '{}'",
record.commit_id
),
));
}
writes.delete(COMMIT_SPACE, StorageKey(Bytes::from(commit_key(commit_id))));
writes.delete(
CHANGE_SPACE,
StorageKey(Bytes::from(change_key(record.change_id()))),
);
Ok(())
}
pub(crate) async fn stage_delete_standalone_change<S>(
store: &S,
writes: &mut StorageWriteSet,
change_id: ChangeId,
) -> Result<(), LixError>
where
S: StorageAdapterRead + ?Sized,
{
let key = StorageKey(Bytes::from(change_key(change_id)));
let existing = PointReadPlan::new(CHANGE_SPACE, std::slice::from_ref(&key))
.materialize(store, StorageGetOptions::default())
.await?
.value
.into_iter()
.next()
.flatten();
if existing.is_none() {
return Err(LixError::new(
LixError::CODE_INTERNAL_ERROR,
format!("retired branch control references missing standalone change '{change_id}'"),
));
}
writes.delete(CHANGE_SPACE, key);
Ok(())
}
#[cfg(test)]
pub(crate) fn stage_delete_commits(
writes: &mut StorageWriteSet,
commit_ids: impl IntoIterator<Item = CommitId>,
) {
writes.delete_batch(COMMIT_SPACE, commit_ids.into_iter().map(commit_key));
}
#[cfg(test)]
pub(crate) fn stage_delete_changes(
writes: &mut StorageWriteSet,
change_ids: impl IntoIterator<Item = ChangeId>,
) {
writes.delete_batch(CHANGE_SPACE, change_ids.into_iter().map(change_key));
}