From 9cf9de6044e12247c9d1d9fa43830fad9265109f Mon Sep 17 00:00:00 2001 From: TristanDebrunner Date: Fri, 18 Oct 2019 09:18:36 -0600 Subject: [PATCH] Remove the Cursor struct (#6426) --- core/src/blocktree.rs | 33 +++++++++------ core/src/blocktree/db.rs | 90 ++++------------------------------------ 2 files changed, 29 insertions(+), 94 deletions(-) diff --git a/core/src/blocktree.rs b/core/src/blocktree.rs index 34dde3bed4..0fa555d8c0 100644 --- a/core/src/blocktree.rs +++ b/core/src/blocktree.rs @@ -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 = db::Cursor; pub type LedgerColumn = db::LedgerColumn; pub type WriteBatch = db::WriteBatch; type BatchProcessor = db::BatchProcessor; -pub trait Column: db::Column {} -impl 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( - db_iterator: &mut Cursor, + 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 { - if let Ok(mut db_iterator) = self.db.cursor::() { - 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::()) + { + Self::find_missing_indexes::( + &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) -> Vec { let mut results = vec![]; - let mut iter = self.db.cursor::().unwrap(); + let mut iter = self + .db + .raw_iterator_cf(self.db.cf_handle::()) + .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(::index(&iter.key().unwrap())); iter.next(); } results diff --git a/core/src/blocktree/db.rs b/core/src/blocktree/db.rs index 7e2b06a922..6166e8dd8e 100644 --- a/core/src/blocktree/db.rs +++ b/core/src/blocktree/db.rs @@ -457,15 +457,6 @@ pub struct BatchProcessor { backend: Arc, } -pub struct Cursor -where - C: Column, -{ - db_cursor: DBRawIterator, - column: PhantomData, - backend: PhantomData, -} - #[derive(Debug, Clone)] pub struct LedgerColumn where @@ -547,19 +538,6 @@ impl Database { ) } - pub fn cursor(&self) -> Result> - where - C: Column, - { - let db_cursor = self.backend.raw_iterator_cf(self.cf_handle::())?; - - Ok(Cursor { - db_cursor, - column: PhantomData, - backend: PhantomData, - }) - } - pub fn iter( &self, iterator_mode: IteratorMode, @@ -606,6 +584,11 @@ impl Database { } } + #[inline] + pub fn raw_iterator_cf(&self, cf: ColumnFamily) -> Result { + 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 Cursor -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 { - if let Some(key) = self.db_cursor.key() { - Some(C::index(key.borrow())) - } else { - None - } - } - - pub fn value_bytes(&self) -> Option> { - self.db_cursor.value() - } -} - -impl Cursor -where - C: TypedColumn, -{ - pub fn value(&self) -> Option { - if let Some(bytes) = self.db_cursor.value() { - let value = deserialize(&bytes).ok()?; - Some(value) - } else { - None - } - } -} - impl LedgerColumn where C: Column, @@ -695,16 +631,6 @@ where self.backend.get_cf(self.handle(), C::key(key).borrow()) } - pub fn cursor(&self) -> Result> { - 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, @@ -766,9 +692,9 @@ where } pub fn is_empty(&self) -> Result { - 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<()> {