Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions firewood/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
clippy::missing_errors_doc,
reason = "Found 1 occurrences after enabling the lint."
)]
#![expect(
clippy::needless_continue,
reason = "Found 1 occurrences after enabling the lint."
)]

use firewood_storage::{
BranchNode, Children, FileIoError, HashType, Hashable, IntoHashType, NibblesIterator, Path,
Expand Down
11 changes: 5 additions & 6 deletions storage/src/checker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,11 +625,10 @@ mod test {
use super::*;
use crate::linear::memory::MemStore;
use crate::nodestore::NodeStoreHeader;
use crate::nodestore::alloc::FreeLists;
use crate::nodestore::alloc::test_utils::{
test_write_free_area, test_write_header, test_write_new_node, test_write_zeroed_area,
};
use crate::nodestore::primitives::area_size_iter;
use crate::nodestore::primitives::{FreeLists, area_size_iter};
use crate::{
BranchNode, Child, FreeListParent, LeafNode, NodeStore, Path, area_index, hash_node,
};
Expand Down Expand Up @@ -854,7 +853,7 @@ mod test {
}
high_watermark += area_size;
}
freelist[area_index.as_usize()] = next_free_block;
freelist[area_index] = next_free_block;
if num_free_areas > 0 {
free_area_counts.insert(area_size, num_free_areas);
}
Expand Down Expand Up @@ -918,7 +917,7 @@ mod test {
next_free_block1 = Some(free_list1_area1);
high_watermark += area_size1;

free_lists[AREA_INDEX1.as_usize()] = next_free_block1;
free_lists[AREA_INDEX1] = next_free_block1;

// second free list
let area_size2 = AREA_INDEX2.size();
Expand All @@ -934,12 +933,12 @@ mod test {
next_free_block2 = Some(free_list2_area1);
high_watermark += area_size2;

free_lists[AREA_INDEX2.as_usize()] = next_free_block2;
free_lists[AREA_INDEX2] = next_free_block2;

// write header
test_write_header(&mut nodestore, high_watermark, None, free_lists);

let expected_start_addr = free_lists[AREA_INDEX1.as_usize()].unwrap();
let expected_start_addr = free_lists[AREA_INDEX1].unwrap();
let expected_end_addr = LinearAddress::new(high_watermark).unwrap();
let expected_free_areas = vec![expected_start_addr..expected_end_addr];
let expected_freelist_errors = vec![CheckerError::AreaIntersects {
Expand Down
62 changes: 46 additions & 16 deletions storage/src/nodestore/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,25 @@
//! - **`NodeData`** - Serialized node content

use super::area_index_and_size;
use super::primitives::{AreaIndex, LinearAddress, index_name};
use super::primitives::{AreaIndex, FreeLists, LinearAddress, index_name};
use crate::linear::FileIoError;
use crate::logger::trace;
use crate::node::branch::{ReadSerializable, Serializable};
use crate::nodestore::NodeStoreHeader;
use integer_encoding::VarIntReader;

use std::io::{Error, ErrorKind, Read};
use std::iter::FusedIterator;

use crate::node::ExtendableBytes;
use crate::{
FreeListParent, MaybePersistedNode, ReadableStorage, WritableStorage, firewood_counter,
};
use std::io::{Error, ErrorKind, Read};
use std::iter::FusedIterator;

/// Returns the maximum size needed to encode a `VarInt`.
const fn var_int_max_size<VI>() -> usize {
const { (size_of::<VI>() * 8 + 7) / 7 }
}

/// `FreeLists` is an array of `Option<LinearAddress>` for each area size.
pub type FreeLists = [Option<LinearAddress>; AreaIndex::NUM_AREA_SIZES];

/// A [`FreeArea`] is stored at the start of the area that contained a node that
/// has been freed.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
Expand Down Expand Up @@ -327,7 +323,6 @@ impl<S: WritableStorage> NodeAllocator<'_, S> {
/// # Errors
///
/// Returns a [`FileIoError`] if the area cannot be read or written.
#[expect(clippy::indexing_slicing)]
pub fn delete_node(&mut self, node: MaybePersistedNode) -> Result<(), FileIoError> {
let Some(addr) = node.as_linear_address() else {
return Ok(());
Expand All @@ -351,16 +346,16 @@ impl<S: WritableStorage> NodeAllocator<'_, S> {

// The area that contained the node is now free.
let mut stored_area_bytes = Vec::new();
FreeArea::new(self.header.free_lists()[area_size_index.as_usize()])
FreeArea::new(self.header.free_lists()[area_size_index])
.as_bytes(area_size_index, &mut stored_area_bytes);

self.storage.write(addr.into(), &stored_area_bytes)?;

self.storage
.add_to_free_list_cache(addr, self.header.free_lists()[area_size_index.as_usize()]);
.add_to_free_list_cache(addr, self.header.free_lists()[area_size_index]);

// The newly freed block is now the head of the free list.
self.header.free_lists_mut()[area_size_index.as_usize()] = Some(addr);
self.header.free_lists_mut()[area_size_index] = Some(addr);

Ok(())
}
Expand Down Expand Up @@ -632,7 +627,7 @@ pub mod test_utils {
}

#[cfg(test)]
#[expect(clippy::unwrap_used, clippy::indexing_slicing)]
#[expect(clippy::unwrap_used)]
mod tests {
use super::*;
use crate::area_index;
Expand Down Expand Up @@ -739,7 +734,7 @@ mod tests {
next_free_block1 = Some(free_list1_area1);
offset += area_size1;

free_lists[area_index1.as_usize()] = next_free_block1;
free_lists[area_index1] = next_free_block1;

// second free list
let area_index2 = AreaIndex::new(
Expand All @@ -761,7 +756,7 @@ mod tests {
next_free_block2 = Some(free_list2_area1);
offset += area_size2;

free_lists[area_index2.as_usize()] = next_free_block2;
free_lists[area_index2] = next_free_block2;

// write header
test_write_header(&mut nodestore, offset, None, free_lists);
Expand Down Expand Up @@ -857,7 +852,7 @@ mod tests {
next_free_block1 = Some(free_list1_area1);
offset += area_size1;

free_lists[AREA_INDEX1.as_usize()] = next_free_block1;
free_lists[AREA_INDEX1] = next_free_block1;

// second free list
assert_ne!(AREA_INDEX1, AREA_INDEX2);
Expand All @@ -874,7 +869,7 @@ mod tests {
next_free_block2 = Some(free_list2_area1);
offset += area_size2;

free_lists[AREA_INDEX2.as_usize()] = next_free_block2;
free_lists[AREA_INDEX2] = next_free_block2;

// write header
test_write_header(&mut nodestore, offset, None, free_lists);
Expand Down Expand Up @@ -950,4 +945,39 @@ mod tests {
let _ = const { AreaIndex::new(1) };
let _ = const { area_index!(1) };
}

#[test]
fn test_freelists_newtype_functionality() {
// Test that FreeLists can be indexed with AreaIndex
let mut free_lists = FreeLists::default();

// Create some test addresses
let addr1 = LinearAddress::new(1000).unwrap();
let addr2 = LinearAddress::new(2000).unwrap();

// Test indexing with AreaIndex
let index1 = AreaIndex::MIN;
let index2 = AreaIndex::MAX;

// Set values using AreaIndex
free_lists[index1] = Some(addr1);
free_lists[index2] = Some(addr2);

// Get values using AreaIndex
assert_eq!(free_lists[index1], Some(addr1));
assert_eq!(free_lists[index2], Some(addr2));

// Test indexing with usize (backward compatibility)
free_lists[AreaIndex::MIN] = None;
assert_eq!(free_lists[AreaIndex::MIN], None);

assert_eq!(free_lists.iter().count(), AreaIndex::NUM_AREA_SIZES);

// Test default
let default_free_lists = FreeLists::default();
for item in &default_free_lists {
assert_eq!(item, &None);
}
assert_eq!(default_free_lists.iter().find(|item| item.is_some()), None);
}
}
8 changes: 4 additions & 4 deletions storage/src/nodestore/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
use bytemuck_derive::{Pod, Zeroable};
use std::io::{Error, ErrorKind};

use super::alloc::FreeLists;
use super::primitives::{LinearAddress, area_size_hash};
use super::primitives::{FreeLists, LinearAddress, area_size_hash};
use crate::logger::{debug, trace};

/// Can be used by filesystem tooling such as "file" to identify
Expand Down Expand Up @@ -194,7 +193,7 @@ impl NodeStoreHeader {
endian_test: 1,
root_address: None,
version: Version::new(),
free_lists: Default::default(),
free_lists: FreeLists::default(),
area_size_hash: area_size_hash()
.as_slice()
.try_into()
Expand Down Expand Up @@ -311,6 +310,7 @@ mod tests {
use crate::linear::ReadableStorage;
use crate::linear::memory::MemStore;
use crate::nodestore::NodeStore;
use crate::nodestore::primitives::FreeLists;
use std::io::Read;
use test_case::test_case;

Expand Down Expand Up @@ -339,7 +339,7 @@ mod tests {
header_stream.read_exact(&mut header_bytes).unwrap();
let header = NodeStoreHeader::from_bytes(&header_bytes);
assert_eq!(header.version, Version::new());
let empty_free_list: FreeLists = Default::default();
let empty_free_list: FreeLists = FreeLists::default();
assert_eq!(*header.free_lists(), empty_free_list);
}
}
6 changes: 3 additions & 3 deletions storage/src/nodestore/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ use std::mem::take;
use std::ops::Deref;
use std::sync::Arc;

use super::linear::WritableStorage;
use crate::hashednode::hash_node;
use crate::node::Node;
use crate::node::persist::MaybePersistedNode;
use crate::nodestore::primitives::FreeLists;
use crate::{CacheReadStrategy, FileIoError, Path, ReadableStorage, SharedNode, TrieHash};

use super::linear::WritableStorage;

impl<S: ReadableStorage> NodeStore<Committed, S> {
/// Open an existing [`NodeStore`]
/// Assumes the header is written in the [`ReadableStorage`].
Expand Down Expand Up @@ -466,7 +466,7 @@ pub struct NodeStore<T, S> {
}

impl<T, S> NodeStore<T, S> {
pub(crate) const fn freelists(&self) -> &alloc::FreeLists {
pub(crate) const fn freelists(&self) -> &FreeLists {
self.header.free_lists()
}
}
Expand Down
55 changes: 55 additions & 0 deletions storage/src/nodestore/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use crate::TrieHash;

use bytemuck_derive::{Pod, Zeroable};
use sha2::{Digest, Sha256};
use std::fmt;
use std::io::{Error, ErrorKind};
Expand Down Expand Up @@ -353,3 +354,57 @@ impl From<NonZeroU64> for LinearAddress {
LinearAddress(addr)
}
}

/// `FreeLists` is a wrapper around an array of `Option<LinearAddress>` for each area size.
/// It provides safe indexing with `AreaIndex` and similar functionality to `Children<T>`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Pod, Zeroable, Default)]
#[repr(transparent)]
pub struct FreeLists([Option<LinearAddress>; AreaIndex::NUM_AREA_SIZES]);

impl std::ops::Index<AreaIndex> for FreeLists {
type Output = Option<LinearAddress>;

fn index(&self, index: AreaIndex) -> &Self::Output {
self.0
.get(index.as_usize())
.expect("AreaIndex is guaranteed to be within bounds")
}
}

impl std::ops::IndexMut<AreaIndex> for FreeLists {
fn index_mut(&mut self, index: AreaIndex) -> &mut Self::Output {
self.0
.get_mut(index.as_usize())
.expect("AreaIndex is guaranteed to be within bounds")
}
}

impl FreeLists {
/// Get an iterator over the free lists.
pub fn iter(&self) -> std::slice::Iter<'_, Option<LinearAddress>> {
self.0.iter()
}

/// Get a mutable iterator over the free lists.
pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, Option<LinearAddress>> {
self.0.iter_mut()
}
}

impl<'a> IntoIterator for &'a FreeLists {
type Item = &'a Option<LinearAddress>;
type IntoIter = std::slice::Iter<'a, Option<LinearAddress>>;

fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}

impl<'a> IntoIterator for &'a mut FreeLists {
type Item = &'a mut Option<LinearAddress>;
type IntoIter = std::slice::IterMut<'a, Option<LinearAddress>>;

fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}
Loading