diff --git a/crates/storage/provider/src/providers/rocksdb/provider.rs b/crates/storage/provider/src/providers/rocksdb/provider.rs index db5f4c8e88..32576e6cf5 100644 --- a/crates/storage/provider/src/providers/rocksdb/provider.rs +++ b/crates/storage/provider/src/providers/rocksdb/provider.rs @@ -821,11 +821,14 @@ impl RocksDBProvider { Ok(()) } - /// Gets the first (smallest key) entry from the specified table. - pub fn first(&self) -> ProviderResult> { + /// Retrieves the first or last entry from a table based on the iterator mode. + fn get_boundary( + &self, + mode: IteratorMode<'_>, + ) -> ProviderResult> { self.execute_with_operation_metric(RocksDBOperation::Get, T::NAME, |this| { let cf = this.get_cf_handle::()?; - let mut iter = this.0.iterator_cf(cf, IteratorMode::Start); + let mut iter = this.0.iterator_cf(cf, mode); match iter.next() { Some(Ok((key_bytes, value_bytes))) => { @@ -846,29 +849,16 @@ impl RocksDBProvider { }) } - /// Gets the last (largest key) entry from the specified table. - pub fn last(&self) -> ProviderResult> { - self.execute_with_operation_metric(RocksDBOperation::Get, T::NAME, |this| { - let cf = this.get_cf_handle::()?; - let mut iter = this.0.iterator_cf(cf, IteratorMode::End); + /// Gets the first (smallest key) entry from the specified table. + #[inline] + pub fn first(&self) -> ProviderResult> { + self.get_boundary::(IteratorMode::Start) + } - match iter.next() { - Some(Ok((key_bytes, value_bytes))) => { - let key = ::decode(&key_bytes) - .map_err(|_| ProviderError::Database(DatabaseError::Decode))?; - let value = T::Value::decompress(&value_bytes) - .map_err(|_| ProviderError::Database(DatabaseError::Decode))?; - Ok(Some((key, value))) - } - Some(Err(e)) => { - Err(ProviderError::Database(DatabaseError::Read(DatabaseErrorInfo { - message: e.to_string().into(), - code: -1, - }))) - } - None => Ok(None), - } - }) + /// Gets the last (largest key) entry from the specified table. + #[inline] + pub fn last(&self) -> ProviderResult> { + self.get_boundary::(IteratorMode::End) } /// Creates an iterator over all entries in the specified table.