Skip to content

Commit

Permalink
disk bucket: add trait fn copying_entry (solana-labs#31024)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffwashington committed Apr 3, 2023
1 parent 3c0ec53 commit 53ddb9d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions bucket_map/src/bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ impl<'b, T: Clone + Copy + 'static> Bucket<T> {
let new_ix = new_ix.unwrap();
let new_elem: &mut IndexEntry<T> = index.get_mut(new_ix);
*new_elem = *elem;
index.copying_entry(new_ix, &self.index, ix);
/*
let dbg_elem: IndexEntry = *new_elem;
assert_eq!(
Expand Down
25 changes: 25 additions & 0 deletions bucket_map/src/bucket_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ pub trait BucketOccupied {
/// initialize this struct
/// `num_elements` is the number of elements allocated in the bucket
fn new(num_elements: usize) -> Self;
/// copying entry. Any in-memory (per-bucket) data structures may need to be copied for this `ix_old`.
/// no-op by default
fn copying_entry(
&mut self,
_element_new: &mut [u8],
_ix_new: usize,
_other: &Self,
_element_old: &[u8],
_ix_old: usize,
) {
}
}

pub struct BucketStorage<O: BucketOccupied> {
Expand Down Expand Up @@ -130,6 +141,18 @@ impl<O: BucketOccupied> BucketStorage<O> {
)
}

pub(crate) fn copying_entry(&mut self, ix_new: u64, other: &Self, ix_old: u64) {
let start = self.get_start_offset_with_header(ix_new);
let start_old = other.get_start_offset_with_header(ix_old);
self.contents.copying_entry(
&mut self.mmap[start..],
ix_new as usize,
&other.contents,
&other.mmap[start_old..],
ix_old as usize,
);
}

/// true if the entry at index 'ix' is free (as opposed to being occupied)
pub fn is_free(&self, ix: u64) -> bool {
let start = self.get_start_offset_with_header(ix);
Expand Down Expand Up @@ -294,6 +317,8 @@ impl<O: BucketOccupied> BucketStorage<O> {
let index_grow = 1 << increment;
(0..old_cap as usize).for_each(|i| {
if !old_bucket.is_free(i as u64) {
self.copying_entry((i * index_grow) as u64, old_bucket, i as u64);

{
// copying from old to new. If 'occupied' bit is stored outside the data, then
// occupied has to be set on the new entry in the new bucket.
Expand Down

0 comments on commit 53ddb9d

Please sign in to comment.