use super::{Arc, InvertedIndex, MemoryInvertedIndex, StorageBackendResult};
use crate::{read_control::StorageReadControl, ReadOnlySnapshot};
impl MemoryInvertedIndex {
pub(super) fn controlled_snapshot(
&self,
control: &StorageReadControl,
) -> StorageBackendResult<Arc<dyn InvertedIndex>> {
control.check()?;
let control = self.read_control.as_ref().unwrap_or(control);
control.check()?;
let memory = control.memory().reserve(size_of::<Self>())?;
let build = |state_memory| -> StorageBackendResult<Arc<dyn InvertedIndex>> {
let mut snapshot = Self {
bindings: self.bindings.retained(control)?,
state: Arc::clone(&self.state),
state_memory: None,
read_control: None,
};
snapshot.state_memory = Some(state_memory);
snapshot.read_control = Some(control.clone());
control.check()?;
let snapshot: Arc<dyn InvertedIndex> = Arc::new(snapshot);
Ok(Arc::new(
ReadOnlySnapshot::with_retention(snapshot, memory)?
.with_inverted_read_control(control)?,
))
};
match &self.state_memory {
Some(memory) => build(Arc::clone(memory)),
None => self.state.retention.with_retained(control, build),
}
}
}
#[cfg(test)]
pub(super) mod tests;