Skip to content

Commit

Permalink
More test cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
steviez committed Dec 10, 2024
1 parent cc1b175 commit 4b349f6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 74 deletions.
89 changes: 29 additions & 60 deletions ledger/src/blockstore/blockstore_purge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,18 +804,17 @@ pub mod tests {
}

fn get_index_bounds(blockstore: &Blockstore) -> (Box<[u8]>, Box<[u8]>) {
let first_index = {
let mut status_entry_iterator = blockstore
.transaction_status_cf
.iterator_cf_raw_key(IteratorMode::Start);
status_entry_iterator.next().unwrap().unwrap().0
};
let last_index = {
let mut status_entry_iterator = blockstore
.transaction_status_cf
.iterator_cf_raw_key(IteratorMode::End);
status_entry_iterator.next().unwrap().unwrap().0
};
let (first_index, _value) = blockstore
.transaction_status_cf
.iterator_cf_raw_key(IteratorMode::Start)
.next()
.unwrap();
let (last_index, _value) = blockstore
.transaction_status_cf
.iterator_cf_raw_key(IteratorMode::End)
.next()
.unwrap();

(first_index, last_index)
}

Expand Down Expand Up @@ -990,27 +989,13 @@ pub mod tests {
let max_slot = 19;

clear_and_repopulate_transaction_statuses_for_test(&blockstore, max_slot);
let first_index = {
let mut status_entry_iterator = blockstore
.transaction_status_cf
.iter(IteratorMode::Start)
.unwrap();
status_entry_iterator.next().unwrap().0
};
let last_index = {
let mut status_entry_iterator = blockstore
.transaction_status_cf
.iter(IteratorMode::End)
.unwrap();
status_entry_iterator.next().unwrap().0
};
let (first_index, last_index) = get_index_bounds(&blockstore);

let oldest_slot = 3;
blockstore.db.set_oldest_slot(oldest_slot);
blockstore.transaction_status_cf.compact_range_raw_key(
&cf::TransactionStatus::key(first_index),
&cf::TransactionStatus::key(last_index),
);
blockstore
.transaction_status_cf
.compact_range_raw_key(&first_index, &last_index);

let status_entry_iterator = blockstore
.transaction_status_cf
Expand All @@ -1024,27 +1009,13 @@ pub mod tests {
assert_eq!(count, max_slot - (oldest_slot - 1));

clear_and_repopulate_transaction_statuses_for_test(&blockstore, max_slot);
let first_index = {
let mut status_entry_iterator = blockstore
.transaction_status_cf
.iter(IteratorMode::Start)
.unwrap();
status_entry_iterator.next().unwrap().0
};
let last_index = {
let mut status_entry_iterator = blockstore
.transaction_status_cf
.iter(IteratorMode::End)
.unwrap();
status_entry_iterator.next().unwrap().0
};
let (first_index, last_index) = get_index_bounds(&blockstore);

let oldest_slot = 12;
blockstore.db.set_oldest_slot(oldest_slot);
blockstore.transaction_status_cf.compact_range_raw_key(
&cf::TransactionStatus::key(first_index),
&cf::TransactionStatus::key(last_index),
);
blockstore
.transaction_status_cf
.compact_range_raw_key(&first_index, &last_index);

let status_entry_iterator = blockstore
.transaction_status_cf
Expand Down Expand Up @@ -1100,18 +1071,16 @@ pub mod tests {
)
.unwrap();

let first_index = {
let mut memos_iterator = blockstore
.transaction_memos_cf
.iterator_cf_raw_key(IteratorMode::Start);
memos_iterator.next().unwrap().unwrap().0
};
let last_index = {
let mut memos_iterator = blockstore
.transaction_memos_cf
.iterator_cf_raw_key(IteratorMode::End);
memos_iterator.next().unwrap().unwrap().0
};
let (first_index, _value) = blockstore
.transaction_memos_cf
.iterator_cf_raw_key(IteratorMode::Start)
.next()
.unwrap();
let (last_index, _value) = blockstore
.transaction_memos_cf
.iterator_cf_raw_key(IteratorMode::End)
.next()
.unwrap();

// Purge at slot 0 should not affect any memos
blockstore.db.set_oldest_slot(0);
Expand Down
22 changes: 8 additions & 14 deletions ledger/src/blockstore_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2197,20 +2197,14 @@ pub mod tests {
{
pub(crate) fn iterator_cf_raw_key(
&self,
iterator_mode: IteratorMode<Vec<u8>>,
) -> DBIterator {
let cf = self.handle();
let start_key;
let iterator_mode = match iterator_mode {
IteratorMode::Start => RocksIteratorMode::Start,
IteratorMode::End => RocksIteratorMode::End,
IteratorMode::From(start_from, direction) => {
start_key = start_from.clone();
RocksIteratorMode::From(&start_key, direction)
}
};

self.backend.iterator_cf(cf, iterator_mode)
iterator_mode: IteratorMode<C::Index>,
) -> impl Iterator<Item = (Box<[u8]>, Box<[u8]>)> + '_ {
// The conversion of key back into Box<[u8]> incurs an extra
// allocation. However, this is test code and the goal is to
// maximize code reuse over efficiency
self.iter(iterator_mode)
.unwrap()
.map(|(key, value)| (Box::from(C::key(key)), value))
}
}
}

0 comments on commit 4b349f6

Please sign in to comment.