Skip to content

Commit

Permalink
coalesce data and coding index
Browse files Browse the repository at this point in the history
  • Loading branch information
carllin committed Jan 12, 2020
1 parent ad4d41e commit fb86f1b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 52 deletions.
10 changes: 5 additions & 5 deletions ledger/src/blocktree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ impl Blocktree {

fn should_insert_coding_shred(
shred: &Shred,
coding_index: &CodingIndex,
coding_index: &ShredIndex,
last_root: &RwLock<u64>,
) -> bool {
let slot = shred.slot();
Expand Down Expand Up @@ -907,7 +907,7 @@ impl Blocktree {
fn should_insert_data_shred(
shred: &Shred,
slot_meta: &SlotMeta,
data_index: &DataIndex,
data_index: &ShredIndex,
last_root: &RwLock<u64>,
) -> bool {
let shred_index = u64::from(shred.index());
Expand Down Expand Up @@ -965,7 +965,7 @@ impl Blocktree {
fn insert_data_shred(
&self,
slot_meta: &mut SlotMeta,
data_index: &mut DataIndex,
data_index: &mut ShredIndex,
shred: &Shred,
write_batch: &mut WriteBatch,
) -> Result<()> {
Expand Down Expand Up @@ -5117,7 +5117,7 @@ pub mod tests {
}

// Test the data index doesn't have anything extra
let num_data_in_index = index.data().num_data();
let num_data_in_index = index.data().num_shreds();
assert_eq!(num_data_in_index, num_data);

// Test the set of coding shreds in the index and in the coding column
Expand All @@ -5130,7 +5130,7 @@ pub mod tests {
}

// Test the data index doesn't have anything extra
let num_coding_in_index = index.coding().num_coding();
let num_coding_in_index = index.coding().num_shreds();
assert_eq!(num_coding_in_index, num_coding);
}
}
59 changes: 12 additions & 47 deletions ledger/src/blocktree_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,13 @@ pub struct SlotMeta {
/// Index recording presence/absence of shreds
pub struct Index {
pub slot: Slot,
data: DataIndex,
coding: CodingIndex,
data: ShredIndex,
coding: ShredIndex,
}

#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
pub struct DataIndex {
/// Map representing presence/absence of data shreds
index: BTreeSet<u64>,
}

#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
/// Erasure coding information
pub struct CodingIndex {
/// Map from set index, to hashmap from shred index to presence bool
pub struct ShredIndex {
/// Map representing presence/absence of shreds
index: BTreeSet<u64>,
}

Expand Down Expand Up @@ -78,56 +71,28 @@ impl Index {
pub(crate) fn new(slot: Slot) -> Self {
Index {
slot,
data: DataIndex::default(),
coding: CodingIndex::default(),
data: ShredIndex::default(),
coding: ShredIndex::default(),
}
}

pub fn data(&self) -> &DataIndex {
pub fn data(&self) -> &ShredIndex {
&self.data
}
pub fn coding(&self) -> &CodingIndex {
pub fn coding(&self) -> &ShredIndex {
&self.coding
}

pub fn data_mut(&mut self) -> &mut DataIndex {
pub fn data_mut(&mut self) -> &mut ShredIndex {
&mut self.data
}
pub fn coding_mut(&mut self) -> &mut CodingIndex {
pub fn coding_mut(&mut self) -> &mut ShredIndex {
&mut self.coding
}
}

impl CodingIndex {
pub fn num_coding(&self) -> usize {
self.index.len()
}

pub fn present_in_bounds(&self, bounds: impl RangeBounds<u64>) -> usize {
self.index.range(bounds).count()
}

pub fn is_present(&self, index: u64) -> bool {
self.index.contains(&index)
}

pub fn set_present(&mut self, index: u64, presence: bool) {
if presence {
self.index.insert(index);
} else {
self.index.remove(&index);
}
}

pub fn set_many_present(&mut self, presence: impl IntoIterator<Item = (u64, bool)>) {
for (idx, present) in presence.into_iter() {
self.set_present(idx, present);
}
}
}

impl DataIndex {
pub fn num_data(&self) -> usize {
impl ShredIndex {
pub fn num_shreds(&self) -> usize {
self.index.len()
}

Expand Down

0 comments on commit fb86f1b

Please sign in to comment.