use std::collections::{BTreeMap, BTreeSet};
use std::sync::Arc;
use super::state::{SegmentState as FileSegmentState, State};
use super::{RetirementNotifier, SUPPORTED_BLOCK_SIZES};
use crate::{
AnonymousRoot, BlockId, BlockStoreError, NamedRoot, ReadSnapshot, RootName, RootVersion,
SegmentId, SegmentInfo, SegmentState,
};
pub struct FileSnapshot {
store_identity: u64,
identity: u64,
_state: Arc<State>,
notifier: Arc<RetirementNotifier>,
named_roots: Vec<NamedRoot>,
anonymous_roots: Vec<AnonymousRoot>,
roots: BTreeMap<u64, RootVersion>,
segment_infos: Vec<SegmentInfo>,
live_blocks: BTreeMap<u64, Vec<BlockId>>,
}
impl FileSnapshot {
pub(super) fn new(
state: Arc<State>,
store_identity: u64,
identity: u64,
notifier: Arc<RetirementNotifier>,
) -> Self {
let named_roots = state
.bindings
.iter()
.filter_map(|(name, &raw)| {
BlockId::try_from(raw)
.ok()
.map(|block| NamedRoot::new(RootName::from_validated(name.clone()), block))
})
.collect();
let named_targets: BTreeSet<u64> = state.bindings.values().copied().collect();
let anonymous_roots = state
.roots
.iter()
.filter(|raw| !named_targets.contains(raw))
.filter_map(|&raw| BlockId::try_from(raw).ok().map(AnonymousRoot::new))
.collect();
let roots = state
.roots
.iter()
.filter_map(|&raw| {
BlockId::try_from(raw)
.ok()
.map(|block| (raw, RootVersion::new(block)))
})
.collect();
let mut segment_infos = Vec::with_capacity(state.segments.len());
let mut live_blocks = BTreeMap::new();
for record in state.segments.values() {
let segment_value = (u64::from(record.version) << 4) | u64::from(record.level);
let segment = SegmentId::new(store_identity, identity, segment_value);
let blocks = (0..record.frontier)
.filter(|index| {
let word = usize::try_from(index / 64).ok();
word.and_then(|word| record.bitmap.get(word))
.is_some_and(|bits| bits & (1_u64 << (index % 64)) != 0)
})
.map(|index| {
BlockId::from_raw_unchecked(
(index << 32) | (u64::from(record.version) << 4) | u64::from(record.level),
)
})
.collect();
live_blocks.insert(segment_value, blocks);
segment_infos.push(SegmentInfo::new(
segment,
record.level,
SUPPORTED_BLOCK_SIZES[usize::from(record.level)],
record.frontier,
record.live_count,
match record.state {
FileSegmentState::Active => SegmentState::Active,
FileSegmentState::Sealed => SegmentState::Sealed,
},
));
}
Self {
store_identity,
identity,
_state: state,
notifier,
named_roots,
anonymous_roots,
roots,
segment_infos,
live_blocks,
}
}
}
impl Drop for FileSnapshot {
fn drop(&mut self) {
self.notifier.notify();
}
}
impl ReadSnapshot for FileSnapshot {
type NamedRoots<'a> = std::slice::Iter<'a, NamedRoot>;
type AnonymousRoots<'a> = std::slice::Iter<'a, AnonymousRoot>;
type SegmentInfos<'a> = std::slice::Iter<'a, SegmentInfo>;
type LiveBlocks<'a> = std::iter::Copied<std::slice::Iter<'a, BlockId>>;
fn list_named_roots(&self) -> Self::NamedRoots<'_> {
self.named_roots.iter()
}
fn list_anonymous_roots(&self) -> Self::AnonymousRoots<'_> {
self.anonymous_roots.iter()
}
fn get_named_root(&self, name: &RootName) -> Option<&NamedRoot> {
self.named_roots
.iter()
.find(|root| root.name().as_str() == name.as_str())
}
fn get_root(&self, block: BlockId) -> Option<&RootVersion> {
self.roots.get(&u64::from(block))
}
fn segment_infos(&self) -> Self::SegmentInfos<'_> {
self.segment_infos.iter()
}
fn live_blocks(&self, segment: SegmentId) -> Result<Self::LiveBlocks<'_>, BlockStoreError> {
if segment.store_identity() != self.store_identity
|| segment.snapshot_identity() != self.identity
{
return Err(BlockStoreError::SegmentNotFound);
}
self.live_blocks
.get(&segment.segment_key())
.map(|blocks| blocks.iter().copied())
.ok_or(BlockStoreError::SegmentNotFound)
}
}