Remove the Cursor struct (#6426)

This commit is contained in:
TristanDebrunner 2019-10-18 09:18:36 -06:00 committed by Greg Fitzgerald
parent a48dcb1421
commit 9cf9de6044
2 changed files with 29 additions and 94 deletions

View File

@ -37,17 +37,14 @@ mod meta;
mod rooted_slot_iterator;
pub use db::columns;
use db::{columns as cf, IteratorDirection, IteratorMode};
use db::{columns as cf, Column, IteratorDirection, IteratorMode};
use rocksdb::DBRawIterator;
pub type Database = db::Database;
pub type Cursor<C> = db::Cursor<C>;
pub type LedgerColumn<C> = db::LedgerColumn<C>;
pub type WriteBatch = db::WriteBatch;
type BatchProcessor = db::BatchProcessor;
pub trait Column: db::Column {}
impl<C: db::Column> Column for C {}
pub const BLOCKTREE_DIRECTORY: &str = "rocksdb";
pub const MAX_COMPLETED_SLOTS_IN_CHANNEL: usize = 100_000;
@ -903,7 +900,7 @@ impl Blocktree {
// indexes in the ledger in the range [start_index, end_index)
// for the slot with the specified slot
fn find_missing_indexes<C>(
db_iterator: &mut Cursor<C>,
db_iterator: &mut DBRawIterator,
slot: u64,
start_index: u64,
end_index: u64,
@ -919,7 +916,7 @@ impl Blocktree {
let mut missing_indexes = vec![];
// Seek to the first shred with index >= start_index
db_iterator.seek((slot, start_index));
db_iterator.seek(&C::key((slot, start_index)));
// The index of the first missing shred in the slot
let mut prev_index = start_index;
@ -933,7 +930,7 @@ impl Blocktree {
}
break;
}
let (current_slot, index) = db_iterator.key().expect("Expect a valid key");
let (current_slot, index) = C::index(&db_iterator.key().expect("Expect a valid key"));
let current_index = {
if current_slot > slot {
@ -974,8 +971,17 @@ impl Blocktree {
end_index: u64,
max_missing: usize,
) -> Vec<u64> {
if let Ok(mut db_iterator) = self.db.cursor::<cf::ShredData>() {
Self::find_missing_indexes(&mut db_iterator, slot, start_index, end_index, max_missing)
if let Ok(mut db_iterator) = self
.db
.raw_iterator_cf(self.db.cf_handle::<cf::ShredData>())
{
Self::find_missing_indexes::<cf::ShredData>(
&mut db_iterator,
slot,
start_index,
end_index,
max_missing,
)
} else {
vec![]
}
@ -1145,7 +1151,10 @@ impl Blocktree {
pub fn get_orphans(&self, max: Option<usize>) -> Vec<u64> {
let mut results = vec![];
let mut iter = self.db.cursor::<cf::Orphans>().unwrap();
let mut iter = self
.db
.raw_iterator_cf(self.db.cf_handle::<cf::Orphans>())
.unwrap();
iter.seek_to_first();
while iter.valid() {
if let Some(max) = max {
@ -1153,7 +1162,7 @@ impl Blocktree {
break;
}
}
results.push(iter.key().unwrap());
results.push(<cf::Orphans as Column>::index(&iter.key().unwrap()));
iter.next();
}
results

View File

@ -457,15 +457,6 @@ pub struct BatchProcessor {
backend: Arc<Rocks>,
}
pub struct Cursor<C>
where
C: Column,
{
db_cursor: DBRawIterator,
column: PhantomData<C>,
backend: PhantomData<Rocks>,
}
#[derive(Debug, Clone)]
pub struct LedgerColumn<C>
where
@ -547,19 +538,6 @@ impl Database {
)
}
pub fn cursor<C>(&self) -> Result<Cursor<C>>
where
C: Column,
{
let db_cursor = self.backend.raw_iterator_cf(self.cf_handle::<C>())?;
Ok(Cursor {
db_cursor,
column: PhantomData,
backend: PhantomData,
})
}
pub fn iter<C>(
&self,
iterator_mode: IteratorMode<C::Index>,
@ -606,6 +584,11 @@ impl Database {
}
}
#[inline]
pub fn raw_iterator_cf(&self, cf: ColumnFamily) -> Result<DBRawIterator> {
self.backend.raw_iterator_cf(cf)
}
// Note this returns an object that can be used to directly write to multiple column families.
// This circumvents the synchronization around APIs that in Blocktree that use
// blocktree.batch_processor, so this API should only be used if the caller is sure they
@ -640,53 +623,6 @@ impl BatchProcessor {
}
}
impl<C> Cursor<C>
where
C: Column,
{
pub fn valid(&self) -> bool {
self.db_cursor.valid()
}
pub fn seek(&mut self, key: C::Index) {
self.db_cursor.seek(C::key(key).borrow());
}
pub fn seek_to_first(&mut self) {
self.db_cursor.seek_to_first();
}
pub fn next(&mut self) {
self.db_cursor.next();
}
pub fn key(&self) -> Option<C::Index> {
if let Some(key) = self.db_cursor.key() {
Some(C::index(key.borrow()))
} else {
None
}
}
pub fn value_bytes(&self) -> Option<Vec<u8>> {
self.db_cursor.value()
}
}
impl<C> Cursor<C>
where
C: TypedColumn,
{
pub fn value(&self) -> Option<C::Type> {
if let Some(bytes) = self.db_cursor.value() {
let value = deserialize(&bytes).ok()?;
Some(value)
} else {
None
}
}
}
impl<C> LedgerColumn<C>
where
C: Column,
@ -695,16 +631,6 @@ where
self.backend.get_cf(self.handle(), C::key(key).borrow())
}
pub fn cursor(&self) -> Result<Cursor<C>> {
let db_cursor = self.backend.raw_iterator_cf(self.handle())?;
Ok(Cursor {
db_cursor,
column: PhantomData,
backend: PhantomData,
})
}
pub fn iter(
&self,
iterator_mode: IteratorMode<C::Index>,
@ -766,9 +692,9 @@ where
}
pub fn is_empty(&self) -> Result<bool> {
let mut cursor = self.cursor()?;
cursor.seek_to_first();
Ok(!cursor.valid())
let mut iter = self.backend.raw_iterator_cf(self.handle())?;
iter.seek_to_first();
Ok(!iter.valid())
}
pub fn put_bytes(&self, key: C::Index, value: &[u8]) -> Result<()> {