use std::sync::Arc;
use crate::mvcc::commit::RecordWriteKind;
use crate::mvcc::{
CommitSequence, PreparedRecordWrite, PrivateRecordRevision, RecordMetadata, VersionResult,
};
use crate::read_control::StorageReadControl;
use super::super::run::{RunEntry, SpilledRun};
use super::super::Change;
use crate::mvcc::key::RecordKey;
pub(in crate::mvcc::overlay) enum TieredChange<'a> {
Resident(&'a Change),
Spilled {
entry: RunEntry,
run: Arc<SpilledRun>,
},
}
impl TieredChange<'_> {
pub(in crate::mvcc::overlay) fn key(&self) -> &[u8] {
match self {
Self::Resident(change) => change.write.key(),
Self::Spilled { entry, .. } => entry.key.bytes(),
}
}
pub(in crate::mvcc::overlay) fn shared_key(&self) -> RecordKey {
match self {
Self::Resident(change) => change.write.shared_key(),
Self::Spilled { entry, .. } => entry.key.clone(),
}
}
pub(in crate::mvcc::overlay) fn expected(&self) -> Option<CommitSequence> {
match self {
Self::Resident(change) => change.write.expected(),
Self::Spilled { entry, .. } => entry.expected,
}
}
pub(in crate::mvcc::overlay) fn metadata(&self) -> RecordMetadata {
RecordMetadata {
revision: self.expected(),
live: match self {
Self::Resident(change) => change.write.value().is_some(),
Self::Spilled { entry, .. } => entry.value.is_some(),
},
}
}
pub(in crate::mvcc::overlay) fn kind(&self) -> RecordWriteKind {
match self {
Self::Resident(change) => change.write.kind(),
Self::Spilled { entry, .. } => entry.kind,
}
}
pub(in crate::mvcc::overlay) fn identity(&self) -> PrivateRecordRevision {
match self {
Self::Resident(change) => change.identity,
Self::Spilled { entry, .. } => entry.identity,
}
}
pub(in crate::mvcc::overlay) fn write(
&self,
control: &StorageReadControl,
) -> VersionResult<PreparedRecordWrite> {
match self {
Self::Resident(change) => Ok(change.write.clone()),
Self::Spilled { entry, run } => {
let value = entry
.value
.map(|location| run.load_value(location, control))
.transpose()?;
Ok(
PreparedRecordWrite::from_shared(entry.key.clone(), entry.expected, value)
.with_kind(entry.kind),
)
}
}
}
}