use crate::addressing::{Hash, HASH_LEN};
use crate::error::{Result, StorageError};
use crate::store::ImmutableStore;
use fastcdc::v2020::FastCDC;
use serde::{Deserialize, Serialize};
pub const FANOUT: usize = 1024;
pub const CHILD_REF_LEN: usize = HASH_LEN + 8;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "codec", rename_all = "snake_case")]
pub enum ChunkingProfile {
Fixed { block_size: u64 },
FastCdc {
min_size: u32,
avg_size: u32,
max_size: u32,
},
}
impl Default for ChunkingProfile {
fn default() -> Self {
ChunkingProfile::FastCdc {
min_size: 4 * 1024,
avg_size: 16 * 1024,
max_size: 64 * 1024,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RootType {
Leaf,
List,
}
#[derive(Debug, Clone, Copy)]
pub struct FragmentTree {
pub root: Hash,
pub root_type: RootType,
pub depth: u32,
pub uncompressed_len: u64,
}
fn chunk_offsets(data: &[u8], profile: &ChunkingProfile) -> Vec<(usize, usize)> {
if data.is_empty() {
return Vec::new();
}
match profile {
ChunkingProfile::Fixed { block_size } => {
let block_size = (*block_size).max(1) as usize;
data.chunks(block_size)
.scan(0usize, |pos, c| {
let start = *pos;
*pos += c.len();
Some((start, c.len()))
})
.collect()
}
ChunkingProfile::FastCdc {
min_size,
avg_size,
max_size,
} => FastCDC::new(data, *min_size, *avg_size, *max_size)
.map(|c| (c.offset, c.length))
.collect(),
}
}
pub fn build_fragment_tree(
data: &[u8],
profile: &ChunkingProfile,
store: &mut dyn ImmutableStore,
) -> Result<FragmentTree> {
let offsets = chunk_offsets(data, profile);
if offsets.is_empty() {
let hash = store.put(&[])?;
return Ok(FragmentTree {
root: hash,
root_type: RootType::Leaf,
depth: 0,
uncompressed_len: 0,
});
}
let mut level: Vec<(Hash, u64)> = Vec::with_capacity(offsets.len());
for (start, len) in &offsets {
let chunk = &data[*start..*start + *len];
let hash = store.put(chunk)?;
level.push((hash, *len as u64));
}
let mut depth = 0u32;
let mut root_type = RootType::Leaf;
while level.len() > 1 {
root_type = RootType::List;
depth += 1;
let mut next = Vec::with_capacity(level.len().div_ceil(FANOUT));
for group in level.chunks(FANOUT) {
let mut body = Vec::with_capacity(group.len() * CHILD_REF_LEN);
let mut total = 0u64;
for (hash, len) in group {
body.extend_from_slice(hash.as_bytes());
body.extend_from_slice(&len.to_le_bytes());
total += len;
}
let hash = store.put(&body)?;
next.push((hash, total));
}
level = next;
}
let (root_hash, total_len) = level[0];
Ok(FragmentTree {
root: root_hash,
root_type,
depth,
uncompressed_len: total_len,
})
}
pub fn read_fragment_tree(root: Hash, depth: u32, store: &dyn ImmutableStore) -> Result<Vec<u8>> {
let mut out = Vec::new();
read_recursive(root, depth, store, &mut out)?;
Ok(out)
}
fn read_recursive(
hash: Hash,
depth: u32,
store: &dyn ImmutableStore,
out: &mut Vec<u8>,
) -> Result<()> {
let bytes = store.get(&hash)?;
if depth == 0 {
out.extend_from_slice(&bytes);
return Ok(());
}
if bytes.len() % CHILD_REF_LEN != 0 {
return Err(StorageError::CorruptManifest(format!(
"list-node {hash} body length {} is not a multiple of {CHILD_REF_LEN}",
bytes.len()
)));
}
for record in bytes.chunks(CHILD_REF_LEN) {
let child_hash = Hash::from_bytes(record[..HASH_LEN].try_into().unwrap());
read_recursive(child_hash, depth - 1, store, out)?;
}
Ok(())
}
pub fn walk_hashes(
hash: Hash,
depth: u32,
store: &dyn ImmutableStore,
live: &mut Vec<Hash>,
) -> Result<()> {
live.push(hash);
if depth == 0 {
return Ok(());
}
let bytes = store.get(&hash)?;
if bytes.len() % CHILD_REF_LEN != 0 {
return Err(StorageError::CorruptManifest(format!(
"list-node {hash} body length {} is not a multiple of {CHILD_REF_LEN}",
bytes.len()
)));
}
for record in bytes.chunks(CHILD_REF_LEN) {
let child_hash = Hash::from_bytes(record[..HASH_LEN].try_into().unwrap());
walk_hashes(child_hash, depth - 1, store, live)?;
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::addressing::PartitionId;
use crate::partition::{EncryptionMode, PartitionSpec, RetentionClass, Subject, SubjectKind};
use crate::store::PartitionStore;
use tempfile::tempdir;
fn store_for(name: &str) -> (tempfile::TempDir, PartitionStore) {
let dir = tempdir().unwrap();
let partition_id = PartitionId::for_person(name);
let spec = PartitionSpec {
subject: Subject {
kind: SubjectKind::Person,
id: name.to_string(),
},
retention_class: RetentionClass::Standard,
encryption_mode: EncryptionMode::Plaintext,
label: None,
};
let store = PartitionStore::create(dir.path(), partition_id, spec).unwrap();
(dir, store)
}
#[test]
fn empty_input_is_a_leaf() {
let (_dir, mut store) = store_for("fragment-empty");
let tree = build_fragment_tree(&[], &ChunkingProfile::default(), &mut store).unwrap();
assert_eq!(tree.root_type, RootType::Leaf);
assert_eq!(tree.depth, 0);
assert_eq!(tree.uncompressed_len, 0);
}
#[test]
fn small_input_is_a_single_leaf() {
let (_dir, mut store) = store_for("fragment-small");
let data = b"small enough to fit in one chunk";
let profile = ChunkingProfile::Fixed { block_size: 4096 };
let tree = build_fragment_tree(data, &profile, &mut store).unwrap();
assert_eq!(tree.root_type, RootType::Leaf);
assert_eq!(tree.depth, 0);
assert_eq!(tree.uncompressed_len, data.len() as u64);
let back = read_fragment_tree(tree.root, tree.depth, &store).unwrap();
assert_eq!(back, data);
}
#[test]
fn multi_chunk_input_roundtrips_through_a_list_node() {
let (_dir, mut store) = store_for("fragment-multi");
let data: Vec<u8> = (0..50_000u32).flat_map(|n| n.to_le_bytes()).collect();
let profile = ChunkingProfile::Fixed { block_size: 4096 };
let tree = build_fragment_tree(&data, &profile, &mut store).unwrap();
assert_eq!(tree.root_type, RootType::List);
assert_eq!(tree.uncompressed_len, data.len() as u64);
let back = read_fragment_tree(tree.root, tree.depth, &store).unwrap();
assert_eq!(back, data);
}
#[test]
fn deep_fanout_forces_multiple_list_levels() {
let (_dir, mut store) = store_for("fragment-deep");
let block_size = 16u64;
let data = vec![7u8; (block_size as usize) * (FANOUT + 5)];
let profile = ChunkingProfile::Fixed { block_size };
let tree = build_fragment_tree(&data, &profile, &mut store).unwrap();
assert_eq!(tree.depth, 2, "expected two fold levels above the leaves");
let back = read_fragment_tree(tree.root, tree.depth, &store).unwrap();
assert_eq!(back, data);
}
#[test]
fn identical_bytes_same_profile_yield_identical_root_hash() {
let data = b"determinism is the whole point of content addressing".repeat(200);
let profile = ChunkingProfile::default();
let (_dir_a, mut store_a) = store_for("determinism-a");
let tree_a = build_fragment_tree(&data, &profile, &mut store_a).unwrap();
let (_dir_b, mut store_b) = store_for("determinism-b");
let tree_b = build_fragment_tree(&data, &profile, &mut store_b).unwrap();
assert_eq!(tree_a.root, tree_b.root);
assert_eq!(tree_a.depth, tree_b.depth);
assert_eq!(tree_a.root_type, tree_b.root_type);
}
}