use bincode::{Decode, Encode};
use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use super::address::{Address, RevisionId, SpaceId};
use super::block::BlockId;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Encode, Decode)]
pub struct SnapshotId(pub u64);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Encode, Decode)]
pub struct BlockIndexEntry {
pub block_id: BlockId,
pub max_key: u128,
}
#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)]
pub struct Snapshot {
pub id: SnapshotId,
pub space: SpaceId,
pub revision: RevisionId,
pub parent: Option<SnapshotId>,
pub blocks: BTreeMap<u128, BlockIndexEntry>,
}
impl Snapshot {
pub fn root(id: SnapshotId, space: SpaceId) -> Self {
Self {
id,
space,
revision: RevisionId::ZERO,
parent: None,
blocks: BTreeMap::new(),
}
}
pub fn diff_blocks(&self, other: &Snapshot) -> Vec<BlockId> {
self.blocks
.values()
.filter(|e| !other.blocks.values().any(|o| o.block_id == e.block_id))
.map(|e| e.block_id)
.collect()
}
pub fn may_contain(&self, _address: &Address) -> bool {
!self.blocks.is_empty()
}
}