diff --git a/crates/katana/storage/db/src/abstraction/transaction.rs b/crates/katana/storage/db/src/abstraction/transaction.rs index 3db3e7d012..9f62a7a572 100644 --- a/crates/katana/storage/db/src/abstraction/transaction.rs +++ b/crates/katana/storage/db/src/abstraction/transaction.rs @@ -1,15 +1,24 @@ use super::cursor::{DbCursor, DbCursorMut}; +use super::{DbDupSortCursor, DbDupSortCursorMut}; use crate::error::DatabaseError; -use crate::tables::Table; +use crate::tables::{DupSort, Table}; /// Trait for read-only transaction type. pub trait DbTx { /// The cursor type. type Cursor: DbCursor; + /// The cursor type for dupsort table. + // TODO: ideally we should only define the cursor type once, + // find a way to not have to define both cursor types in both traits + type DupCursor: DbDupSortCursor; + /// Creates a cursor to iterate over a table items. fn cursor(&self) -> Result, DatabaseError>; + /// Creates a cursor to iterate over a dupsort table items. + fn cursor_dup(&self) -> Result, DatabaseError>; + /// Gets a value from a table using the given key. fn get(&self, key: T::Key) -> Result, DatabaseError>; @@ -28,9 +37,16 @@ pub trait DbTxMut: DbTx { /// The mutable cursor type. type Cursor: DbCursorMut; + /// The mutable cursor type for dupsort table. + // TODO: find a way to not have to define both cursor types in both traits + type DupCursor: DbDupSortCursorMut; + /// Creates a cursor to mutably iterate over a table items. fn cursor_mut(&self) -> Result<::Cursor, DatabaseError>; + /// Creates a cursor to iterate over a dupsort table items. + fn cursor_dup_mut(&self) -> Result<::DupCursor, DatabaseError>; + /// Inserts an item into a database. /// /// This function stores key/data pairs in the database. The default behavior is to enter the diff --git a/crates/katana/storage/db/src/mdbx/tx.rs b/crates/katana/storage/db/src/mdbx/tx.rs index bc2d0b3c35..700004422b 100644 --- a/crates/katana/storage/db/src/mdbx/tx.rs +++ b/crates/katana/storage/db/src/mdbx/tx.rs @@ -10,7 +10,7 @@ use super::cursor::Cursor; use crate::abstraction::{DbTx, DbTxMut}; use crate::codecs::{Compress, Encode}; use crate::error::DatabaseError; -use crate::tables::{Table, Tables, NUM_TABLES}; +use crate::tables::{DupSort, Table, Tables, NUM_TABLES}; use crate::utils::decode_one; /// Alias for read-only transaction. @@ -51,6 +51,7 @@ impl Tx { impl DbTx for Tx { type Cursor = Cursor; + type DupCursor = Self::Cursor; fn cursor(&self) -> Result, DatabaseError> { self.inner @@ -59,6 +60,13 @@ impl DbTx for Tx { .map_err(DatabaseError::CreateCursor) } + fn cursor_dup(&self) -> Result, DatabaseError> { + self.inner + .cursor_with_dbi(self.get_dbi::()?) + .map(Cursor::new) + .map_err(DatabaseError::CreateCursor) + } + fn get(&self, key: T::Key) -> Result::Value>, DatabaseError> { let key = Encode::encode(key); self.inner @@ -86,11 +94,19 @@ impl DbTx for Tx { impl DbTxMut for Tx { type Cursor = Cursor; + type DupCursor = ::Cursor; fn cursor_mut(&self) -> Result<::Cursor, DatabaseError> { DbTx::cursor(self) } + fn cursor_dup_mut(&self) -> Result<::DupCursor, DatabaseError> { + self.inner + .cursor_with_dbi(self.get_dbi::()?) + .map(Cursor::new) + .map_err(DatabaseError::CreateCursor) + } + fn put(&self, key: T::Key, value: T::Value) -> Result<(), DatabaseError> { let key = key.encode(); let value = value.compress();