Skip to content

Commit

Permalink
chore: remove redundant utility methods (#144)
Browse files Browse the repository at this point in the history
#142 introduced some utility methods in the `Bound` struct. We had
copies of these utility methods elsewhere, which are no longer needed
and are removed in this commit.
  • Loading branch information
ielashi authored Sep 25, 2023
1 parent 77d2a17 commit af272e4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 32 deletions.
10 changes: 5 additions & 5 deletions src/btreemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mod allocator;
mod iter;
mod node;
use crate::{
storable::{max_size, Bound as StorableBound},
storable::Bound as StorableBound,
types::{Address, NULL},
Memory, Storable,
};
Expand Down Expand Up @@ -197,8 +197,8 @@ where
/// This is only exposed for testing and benchmarking.
#[cfg(any(feature = "profiler", test))]
pub fn new_v1(memory: M) -> Self {
let max_key_size = max_size::<K>();
let max_value_size = max_size::<V>();
let max_key_size = K::BOUND.max_size();
let max_value_size = V::BOUND.max_size();

let btree = Self {
root_addr: NULL,
Expand Down Expand Up @@ -235,12 +235,12 @@ where
max_value_size: expected_value_size,
}) => {
assert!(
max_size::<K>() <= expected_key_size,
K::BOUND.max_size() <= expected_key_size,
"max_key_size must be <= {expected_key_size}"
);

assert!(
max_size::<V>() <= expected_value_size,
V::BOUND.max_size() <= expected_value_size,
"max_value_size must be <= {expected_value_size}"
);
}
Expand Down
12 changes: 4 additions & 8 deletions src/btreemap/node/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,7 @@
use super::*;
use crate::btreemap::Allocator;
use crate::{
btreemap::node::io::NodeWriter,
storable::{is_fixed_size, max_size},
types::NULL,
};
use crate::{btreemap::node::io::NodeWriter, types::NULL};

// Initial page
pub(super) const OVERFLOW_ADDRESS_OFFSET: Bytes = Bytes::new(7);
Expand Down Expand Up @@ -159,9 +155,9 @@ impl<K: Storable + Ord + Clone> Node<K> {
let mut buf = vec![];
for _ in 0..num_entries {
// Load the key's size.
let key_size = if is_fixed_size::<K>() {
let key_size = if K::BOUND.is_fixed_size() {
// Key is fixed in size. The size of the key is always its max size.
max_size::<K>()
K::BOUND.max_size()
} else {
// Key is not fixed in size. Read the size from memory.
let value = read_u32(&reader, offset);
Expand Down Expand Up @@ -251,7 +247,7 @@ impl<K: Storable + Ord + Clone> Node<K> {
let key_bytes = key.to_bytes_checked();

// Write the size of the key if it isn't fixed in size.
if !is_fixed_size::<K>() {
if !K::BOUND.is_fixed_size() {
writer.write_u32(offset, key_bytes.len() as u32);
offset += U32_SIZE;
}
Expand Down
19 changes: 0 additions & 19 deletions src/storable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,25 +531,6 @@ pub(crate) const fn bounds<A: Storable>() -> Bounds {
}
}

/// Returns the max size of the given type if bounded, panics if unbounded.
pub const fn max_size<A: Storable>() -> u32 {
if let Bound::Bounded { max_size, .. } = A::BOUND {
max_size
} else {
panic!("Cannot get max size of unbounded type.");
}
}

/// Returns true if the type is fixed in size, false otherwise.
pub const fn is_fixed_size<A: Storable>() -> bool {
if let Bound::Bounded { is_fixed_size, .. } = A::BOUND {
is_fixed_size
} else {
// Unbounded types do not have a fixed size.
false
}
}

fn decode_size(src: &[u8], bounds: &Bounds) -> usize {
if bounds.is_fixed_size {
bounds.max_size as usize
Expand Down

0 comments on commit af272e4

Please # to comment.