use crate::{
db::{
commit::{PreparedIndexMutation, PreparedRowCommitOp},
data::DataStore,
index::IndexStore,
positioned_overlay::JournalOverlayPosition,
},
error::InternalError,
};
impl PreparedIndexMutation {
pub(crate) fn preflight_positioned(
&self,
position: JournalOverlayPosition,
) -> Result<(), InternalError> {
self.index_store
.with_borrow(|store| store.preflight_positioned_journal_entry(&self.key, position))
}
pub(crate) fn apply(self) {
self.index_store.with_borrow_mut(|store| {
if let Some(value) = self.value {
store.insert(self.key, value);
} else {
store.remove(&self.key);
}
});
}
pub(in crate::db) fn fold_recovered(self) -> Result<(), crate::error::InternalError> {
self.index_store
.with_borrow_mut(|store| store.fold_recovered_journal_entry(self.key, self.value))
}
}
impl PreparedRowCommitOp {
pub(crate) fn preflight_positioned(
&self,
position: JournalOverlayPosition,
) -> Result<(), InternalError> {
for index_op in &self.index_ops {
index_op.preflight_positioned(position)?;
}
self.data_store
.with_borrow(|store| store.preflight_positioned_journal_entry(&self.data_key, position))
}
pub(in crate::db) fn preflight_fold_recovered(&self) -> Result<(), InternalError> {
self.data_store
.with_borrow(DataStore::preflight_fold_recovered_journal)?;
self.data_index_store
.with_borrow(IndexStore::preflight_fold_recovered_journal)?;
for index_op in &self.index_ops {
index_op
.index_store
.with_borrow(IndexStore::preflight_fold_recovered_journal)?;
}
Ok(())
}
pub(crate) fn apply_positioned(
self,
position: JournalOverlayPosition,
) -> Result<(), InternalError> {
for index_op in self.index_ops {
index_op.index_store.with_borrow_mut(|store| {
store
.publish_preflighted_journal_entry(index_op.key, index_op.value, position)
.map(|_| ())
})?;
}
let data_generation = self.data_store.with_borrow_mut(|store| {
store
.publish_preflighted_journal_entry(
self.data_key,
self.data_value.map(|value| value.as_raw_row().clone()),
position,
)
.map(|_| store.generation())
})?;
self.data_index_store.with_borrow_mut(|store| {
store.mark_prefix_cardinality_data_generation(data_generation);
});
Ok(())
}
pub(in crate::db) fn fold_recovered(self) -> Result<(), InternalError> {
for index_op in self.index_ops {
index_op.fold_recovered()?;
}
let data_generation = self
.data_store
.with_borrow_mut(|store| match self.data_value {
Some(value) => store
.fold_recovered_journal_put(self.data_key, value.as_raw_row().clone())
.map(|_| store.generation()),
None => store
.fold_recovered_journal_delete(&self.data_key)
.map(|_| store.generation()),
})?;
self.data_index_store.with_borrow_mut(|store| {
store.mark_prefix_cardinality_data_generation(data_generation);
});
Ok(())
}
pub(crate) fn apply(self) {
for index_op in self.index_ops {
index_op.apply();
}
let data_generation = self.data_store.with_borrow_mut(|store| {
if let Some(value) = self.data_value {
store.insert(self.data_key, value);
} else {
store.remove(&self.data_key);
}
store.generation()
});
self.data_index_store.with_borrow_mut(|store| {
store.mark_prefix_cardinality_data_generation(data_generation);
});
}
}