use alloc::vec::Vec;
use crate::{
blob::BlobMeta,
collections::Set,
depth::{Depth, DepthMetric},
digest::Digest,
id::SedimentreeId,
loose_commit::LooseCommit,
};
#[derive(
Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, minicbor::Encode, minicbor::Decode,
)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Fragment {
#[n(0)]
summary: FragmentSummary,
#[n(1)]
checkpoints: Vec<Digest<LooseCommit>>,
#[n(2)]
digest: Digest<Fragment>,
}
impl Fragment {
#[must_use]
pub fn new(
head: Digest<LooseCommit>,
boundary: Vec<Digest<LooseCommit>>,
checkpoints: Vec<Digest<LooseCommit>>,
blob_meta: BlobMeta,
) -> Self {
let digest = {
let mut hasher = blake3::Hasher::new();
hasher.update(head.as_bytes());
for end in &boundary {
hasher.update(end.as_bytes());
}
hasher.update(blob_meta.digest().as_bytes());
for checkpoint in &checkpoints {
hasher.update(checkpoint.as_bytes());
}
Digest::from_bytes(*hasher.finalize().as_bytes())
};
Self {
summary: FragmentSummary {
head,
boundary,
blob_meta,
},
checkpoints,
digest,
}
}
#[must_use]
pub fn supports<M: DepthMetric>(&self, other: &FragmentSummary, hash_metric: &M) -> bool {
if &self.summary == other {
return true;
}
if self.depth(hash_metric) < other.depth(hash_metric) {
return false;
}
if self.summary.head == other.head
&& self
.checkpoints
.iter()
.collect::<Set<_>>()
.is_superset(&other.boundary.iter().collect::<Set<_>>())
{
return true;
}
if self.checkpoints.contains(&other.head)
&& other
.boundary
.iter()
.all(|end| self.checkpoints.contains(end))
{
return true;
}
if self.checkpoints.contains(&other.head)
&& self
.summary
.boundary
.iter()
.collect::<Set<_>>()
.is_superset(&other.boundary.iter().collect::<Set<_>>())
{
return true;
}
false
}
#[must_use]
pub fn supports_block(&self, fragment_end: Digest<LooseCommit>) -> bool {
self.checkpoints.contains(&fragment_end) || self.summary.boundary.contains(&fragment_end)
}
#[must_use]
pub const fn summary(&self) -> &FragmentSummary {
&self.summary
}
#[must_use]
pub fn depth<M: DepthMetric>(&self, hash_metric: &M) -> Depth {
self.summary.depth(hash_metric)
}
#[must_use]
pub const fn head(&self) -> Digest<LooseCommit> {
self.summary.head
}
#[must_use]
pub const fn boundary(&self) -> &[Digest<LooseCommit>] {
self.summary.boundary.as_slice()
}
#[must_use]
pub const fn checkpoints(&self) -> &Vec<Digest<LooseCommit>> {
&self.checkpoints
}
#[must_use]
pub const fn digest(&self) -> Digest<Fragment> {
self.digest
}
}
#[derive(
Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, minicbor::Encode, minicbor::Decode,
)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FragmentSummary {
#[n(0)]
head: Digest<LooseCommit>,
#[n(1)]
boundary: Vec<Digest<LooseCommit>>,
#[n(2)]
blob_meta: BlobMeta,
}
impl FragmentSummary {
#[must_use]
pub const fn new(
head: Digest<LooseCommit>,
boundary: Vec<Digest<LooseCommit>>,
blob_meta: BlobMeta,
) -> Self {
Self {
head,
boundary,
blob_meta,
}
}
#[must_use]
pub const fn head(&self) -> Digest<LooseCommit> {
self.head
}
#[must_use]
pub const fn boundary(&self) -> &[Digest<LooseCommit>] {
self.boundary.as_slice()
}
#[must_use]
pub const fn blob_meta(&self) -> BlobMeta {
self.blob_meta
}
#[must_use]
pub fn depth<M: DepthMetric>(&self, hash_metric: &M) -> Depth {
hash_metric.to_depth(self.head)
}
}
#[derive(Debug, Clone)]
pub struct FragmentSpec {
id: SedimentreeId,
head: Digest<LooseCommit>,
checkpoints: Vec<Digest<LooseCommit>>,
boundary: Vec<Digest<LooseCommit>>,
}
impl FragmentSpec {
#[must_use]
pub const fn new(
id: SedimentreeId,
head: Digest<LooseCommit>,
checkpoints: Vec<Digest<LooseCommit>>,
boundary: Vec<Digest<LooseCommit>>,
) -> Self {
Self {
id,
head,
checkpoints,
boundary,
}
}
#[must_use]
pub const fn id(&self) -> SedimentreeId {
self.id
}
#[must_use]
pub const fn head(&self) -> Digest<LooseCommit> {
self.head
}
#[must_use]
pub const fn boundary(&self) -> &[Digest<LooseCommit>] {
self.boundary.as_slice()
}
#[must_use]
pub const fn checkpoints(&self) -> &Vec<Digest<LooseCommit>> {
&self.checkpoints
}
}