diff --git a/crates/db/src/kv/mod.rs b/crates/db/src/kv/mod.rs index d67f21209..25f2e98be 100644 --- a/crates/db/src/kv/mod.rs +++ b/crates/db/src/kv/mod.rs @@ -175,12 +175,12 @@ mod tests { // PUT let tx = env.begin_mut_tx().expect(ERROR_INIT_TX); - tx.put(PlainState, key, value.clone()).expect(ERROR_PUT); + tx.put::(key, value.clone()).expect(ERROR_PUT); tx.commit().expect(ERROR_COMMIT); // GET let tx = env.begin_tx().expect(ERROR_INIT_TX); - let result = tx.get(PlainState, key).expect(ERROR_GET); + let result = tx.get::(key).expect(ERROR_GET); assert!(result.expect(ERROR_RETURN_VALUE) == value); tx.commit().expect(ERROR_COMMIT); } @@ -199,7 +199,7 @@ mod tests { // PUT let result = env.update(|tx| { - tx.put(PlainState, key, value.clone()).expect(ERROR_PUT); + tx.put::(key, value.clone()).expect(ERROR_PUT); 200 }); assert!(result.expect(ERROR_RETURN_VALUE) == 200); @@ -208,7 +208,7 @@ mod tests { let env = Env::::open(&path, EnvKind::RO).expect(ERROR_DB_CREATION); // GET - let result = env.view(|tx| tx.get(PlainState, key).expect(ERROR_GET)).expect(ERROR_GET); + let result = env.view(|tx| tx.get::(key).expect(ERROR_GET)).expect(ERROR_GET); assert!(result == Some(value)) } diff --git a/crates/db/src/kv/table.rs b/crates/db/src/kv/table.rs index f34c78c9a..5f760c135 100644 --- a/crates/db/src/kv/table.rs +++ b/crates/db/src/kv/table.rs @@ -27,15 +27,14 @@ impl Object for T where T: Encode + Decode {} /// Generic trait that a database table should follow. pub trait Table: Send + Sync + Debug + 'static { + /// Return table name as it is present inside the MDBX. + const NAME: &'static str; /// Key element of `Table`. type Key: Encode; /// Value element of `Table`. type Value: Object; /// Seek Key element of `Table`. type SeekKey: Encode; - - /// Return name as it is present inside the MDBX. - fn name(&self) -> &'static str; } /// DupSort allows for keys not to be repeated in the database, diff --git a/crates/db/src/kv/tables.rs b/crates/db/src/kv/tables.rs index 6b92ab0d0..00fe64774 100644 --- a/crates/db/src/kv/tables.rs +++ b/crates/db/src/kv/tables.rs @@ -33,14 +33,11 @@ macro_rules! table { pub struct $name; impl $crate::kv::table::Table for $name { + const NAME: &'static str = $name::const_name(); type Key = $key; type Value = $value; type SeekKey = $seek; - /// Return $name as it is present inside the database. - fn name(&self) -> &'static str { - $name::const_name() - } } impl $name { diff --git a/crates/db/src/kv/tx.rs b/crates/db/src/kv/tx.rs index fdd46529d..3737c5cad 100644 --- a/crates/db/src/kv/tx.rs +++ b/crates/db/src/kv/tx.rs @@ -33,25 +33,23 @@ impl<'env, K: TransactionKind, E: EnvironmentKind> Tx<'env, K, E> { } /// Open cursor on `table`. - pub fn cursor<'a, T: Table>(&'a self, table: T) -> Result, KVError> + pub fn cursor<'a, T: Table>(&'a self) -> Result, KVError> where 'env: 'a, T: Table, { - let table_name = table.name(); - Ok(Cursor { - inner: self.inner.cursor(&self.inner.open_db(Some(table_name))?)?, - table: table_name, + inner: self.inner.cursor(&self.inner.open_db(Some(T::NAME))?)?, + table: T::NAME, _dbi: PhantomData, }) } /// Gets value associated with `key` on `table`. If it's a DUPSORT table, then returns the first /// entry. - pub fn get(&self, table: T, key: T::Key) -> ValueOnlyResult { + pub fn get(&self, key: T::Key) -> ValueOnlyResult { self.inner - .get(&self.inner.open_db(Some(table.name()))?, key.encode().as_ref())? + .get(&self.inner.open_db(Some(T::NAME))?, key.encode().as_ref())? .map(decode_one::) .transpose() } @@ -65,23 +63,18 @@ impl<'env, K: TransactionKind, E: EnvironmentKind> Tx<'env, K, E> { impl<'a, E: EnvironmentKind> Tx<'a, RW, E> { /// Opens `table` and inserts `(key, value)` pair. If the `key` already exists, it replaces the /// value it if the table doesn't support DUPSORT. - pub fn put(&self, table: T, k: T::Key, v: T::Value) -> Result<(), KVError> + pub fn put(&self, k: T::Key, v: T::Value) -> Result<(), KVError> where T: Table, { self.inner - .put( - &self.inner.open_db(Some(table.name()))?, - &k.encode(), - &v.encode(), - WriteFlags::UPSERT, - ) + .put(&self.inner.open_db(Some(T::NAME))?, &k.encode(), &v.encode(), WriteFlags::UPSERT) .map_err(KVError::Put) } /// Deletes the `(key, value)` entry on `table`. When `value` is `None`, all entries with `key` /// are to be deleted. Otherwise, only the item matching that data shall be. - pub fn delete(&self, table: T, key: T::Key, value: Option) -> Result + pub fn delete(&self, key: T::Key, value: Option) -> Result where T: Table, { @@ -93,16 +86,16 @@ impl<'a, E: EnvironmentKind> Tx<'a, RW, E> { }; self.inner - .del(&self.inner.open_db(Some(table.name()))?, key.encode(), data) + .del(&self.inner.open_db(Some(T::NAME))?, key.encode(), data) .map_err(KVError::Delete) } /// Empties `table`. - pub fn clear(&self, table: T) -> Result<(), KVError> + pub fn clear(&self) -> Result<(), KVError> where T: Table, { - self.inner.clear_db(&self.inner.open_db(Some(table.name()))?)?; + self.inner.clear_db(&self.inner.open_db(Some(T::NAME))?)?; Ok(()) }