Skip to content

Commit

Permalink
disk index: move fn data_loc to MultipleSlots
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffwashington committed Mar 30, 2023
1 parent 3c2ac47 commit ef81b5a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
6 changes: 3 additions & 3 deletions bucket_map/src/bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ impl<'b, T: Clone + Copy + 'static> Bucket<T> {
if best_fit_bucket == bucket_ix && current_multiple_slots.num_slots() > 0 {
let current_bucket = &mut self.data[bucket_ix as usize];
// in place update
let elem_loc = elem.data_loc(&self.index, current_bucket);
let elem_loc = current_multiple_slots.data_loc(current_bucket);
assert!(!current_bucket.is_free(elem_loc));
let slice: &mut [T] = current_bucket.get_mut_cell_slice(elem_loc, data_len as u64);
let current_multiple_slots = elem.get_multiple_slots_mut(&mut self.index);
Expand Down Expand Up @@ -321,7 +321,7 @@ impl<'b, T: Clone + Copy + 'static> Bucket<T> {
for i in pos..pos + (max_search * 10).min(cap) {
let ix = i % cap;
if best_bucket.is_free(ix) {
let elem_loc = elem.data_loc(&self.index, current_bucket);
let elem_loc = current_multiple_slots.data_loc(current_bucket);
let old_slots = current_multiple_slots.num_slots();
let multiple_slots = elem.get_multiple_slots_mut(&mut self.index);
multiple_slots.set_storage_offset(ix);
Expand Down Expand Up @@ -354,7 +354,7 @@ impl<'b, T: Clone + Copy + 'static> Bucket<T> {
if multiple_slots.num_slots() > 0 {
let ix = multiple_slots.data_bucket_ix() as usize;
let data_bucket = &self.data[ix];
let loc = elem.data_loc(&self.index, data_bucket);
let loc = multiple_slots.data_loc(data_bucket);
let data_bucket = &mut self.data[ix];
//debug!( "DATA FREE {:?} {} {} {}", key, elem.data_location, data_bucket.capacity, elem_uid );
data_bucket.free(loc);
Expand Down
27 changes: 12 additions & 15 deletions bucket_map/src/index_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,15 @@ pub(crate) struct MultipleSlots {
}

impl MultipleSlots {
fn set_storage_capacity_when_created_pow2(&mut self, storage_capacity_when_created_pow2: u8) {
pub(crate) fn set_storage_capacity_when_created_pow2(
&mut self,
storage_capacity_when_created_pow2: u8,
) {
self.storage_cap_and_offset
.set_capacity_when_created_pow2(storage_capacity_when_created_pow2)
}

fn set_storage_offset(&mut self, storage_offset: u64) {
pub(crate) fn set_storage_offset(&mut self, storage_offset: u64) {
self.storage_cap_and_offset
.set_offset_checked(storage_offset)
.expect("New storage offset must fit into 7 bytes!")
Expand Down Expand Up @@ -130,6 +133,12 @@ impl MultipleSlots {
(Slot::BITS - (num_slots - 1).leading_zeros()) as u64
}
}

/// This function maps the original data location into an index in the current bucket storage.
/// This is coupled with how we resize bucket storages.
pub(crate) fn data_loc(&self, storage: &BucketStorage<DataBucket>) -> u64 {
self.storage_offset() << (storage.capacity_pow2 - self.storage_capacity_when_created_pow2())
}
}

/// Pack the storage offset and capacity-when-crated-pow2 fields into a single u64
Expand Down Expand Up @@ -188,18 +197,6 @@ impl<T> IndexEntryPlaceInBucket<T> {
index_entry.ref_count
}

/// This function maps the original data location into an index in the current bucket storage.
/// This is coupled with how we resize bucket storages.
pub fn data_loc(
&self,
index_bucket: &BucketStorage<IndexBucket<T>>,
storage: &BucketStorage<DataBucket>,
) -> u64 {
let multiple_slots = self.get_multiple_slots(index_bucket);
multiple_slots.storage_offset()
<< (storage.capacity_pow2 - multiple_slots.storage_capacity_when_created_pow2())
}

pub fn read_value<'a>(
&self,
index_bucket: &BucketStorage<IndexBucket<T>>,
Expand All @@ -210,7 +207,7 @@ impl<T> IndexEntryPlaceInBucket<T> {
let slice = if num_slots > 0 {
let data_bucket_ix = multiple_slots.data_bucket_ix();
let data_bucket = &data_buckets[data_bucket_ix as usize];
let loc = self.data_loc(index_bucket, data_bucket);
let loc = multiple_slots.data_loc(data_bucket);
assert!(!data_bucket.is_free(loc));
data_bucket.get_cell_slice(loc, num_slots)
} else {
Expand Down

0 comments on commit ef81b5a

Please sign in to comment.