Discussion draft: change DB Writer to take value references (#13672)

This commit is contained in:
James Prestwich
2025-01-07 12:38:09 -05:00
committed by GitHub
parent 2b301aa102
commit 3e980e61d8
41 changed files with 149 additions and 137 deletions

View File

@@ -241,7 +241,7 @@ impl<T: Table> DbCursorRW<T> for Cursor<RW, T> {
/// it will append the value to the subkey, even if the subkeys are the same. So if you want
/// to properly upsert, you'll need to `seek_exact` & `delete_current` if the key+subkey was
/// found, before calling `upsert`.
fn upsert(&mut self, key: T::Key, value: T::Value) -> Result<(), DatabaseError> {
fn upsert(&mut self, key: T::Key, value: &T::Value) -> Result<(), DatabaseError> {
let key = key.encode();
let value = compress_to_buf_or_ref!(self, value);
self.execute_with_operation_metric(
@@ -263,7 +263,7 @@ impl<T: Table> DbCursorRW<T> for Cursor<RW, T> {
)
}
fn insert(&mut self, key: T::Key, value: T::Value) -> Result<(), DatabaseError> {
fn insert(&mut self, key: T::Key, value: &T::Value) -> Result<(), DatabaseError> {
let key = key.encode();
let value = compress_to_buf_or_ref!(self, value);
self.execute_with_operation_metric(
@@ -287,7 +287,7 @@ impl<T: Table> DbCursorRW<T> for Cursor<RW, T> {
/// Appends the data to the end of the table. Consequently, the append operation
/// will fail if the inserted key is less than the last table key
fn append(&mut self, key: T::Key, value: T::Value) -> Result<(), DatabaseError> {
fn append(&mut self, key: T::Key, value: &T::Value) -> Result<(), DatabaseError> {
let key = key.encode();
let value = compress_to_buf_or_ref!(self, value);
self.execute_with_operation_metric(

View File

@@ -479,7 +479,7 @@ impl DatabaseEnv {
if Some(&version) != last_version.as_ref() {
version_cursor.upsert(
SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs(),
version,
&version,
)?;
tx.commit()?;
}
@@ -580,8 +580,8 @@ mod tests {
let entry_0 = StorageEntry { key: B256::with_last_byte(1), value: U256::from(0) };
let entry_1 = StorageEntry { key: B256::with_last_byte(1), value: U256::from(1) };
dup_cursor.upsert(Address::with_last_byte(1), entry_0).expect(ERROR_UPSERT);
dup_cursor.upsert(Address::with_last_byte(1), entry_1).expect(ERROR_UPSERT);
dup_cursor.upsert(Address::with_last_byte(1), &entry_0).expect(ERROR_UPSERT);
dup_cursor.upsert(Address::with_last_byte(1), &entry_1).expect(ERROR_UPSERT);
assert_eq!(
dup_cursor.walk(None).unwrap().collect::<Result<Vec<_>, _>>(),
@@ -910,12 +910,12 @@ mod tests {
let mut cursor = tx.cursor_write::<CanonicalHeaders>().unwrap();
// INSERT
assert_eq!(cursor.insert(key_to_insert, B256::ZERO), Ok(()));
assert_eq!(cursor.insert(key_to_insert, &B256::ZERO), Ok(()));
assert_eq!(cursor.current(), Ok(Some((key_to_insert, B256::ZERO))));
// INSERT (failure)
assert_eq!(
cursor.insert(key_to_insert, B256::ZERO),
cursor.insert(key_to_insert, &B256::ZERO),
Err(DatabaseWriteError {
info: Error::KeyExist.into(),
operation: DatabaseWriteOperation::CursorInsert,
@@ -947,11 +947,11 @@ mod tests {
let subkey2 = B256::random();
let entry1 = StorageEntry { key: subkey1, value: U256::ZERO };
assert!(dup_cursor.insert(key, entry1).is_ok());
assert!(dup_cursor.insert(key, &entry1).is_ok());
// Can't insert
let entry2 = StorageEntry { key: subkey2, value: U256::ZERO };
assert!(dup_cursor.insert(key, entry2).is_err());
assert!(dup_cursor.insert(key, &entry2).is_err());
}
#[test]
@@ -964,9 +964,9 @@ mod tests {
let key3 = Address::with_last_byte(3);
let mut cursor = tx.cursor_write::<PlainAccountState>().unwrap();
assert!(cursor.insert(key1, Account::default()).is_ok());
assert!(cursor.insert(key2, Account::default()).is_ok());
assert!(cursor.insert(key3, Account::default()).is_ok());
assert!(cursor.insert(key1, &Account::default()).is_ok());
assert!(cursor.insert(key2, &Account::default()).is_ok());
assert!(cursor.insert(key3, &Account::default()).is_ok());
// Seek & delete key2
cursor.seek_exact(key2).unwrap();
@@ -1002,7 +1002,7 @@ mod tests {
assert_eq!(cursor.current(), Ok(Some((9, B256::ZERO))));
for pos in (2..=8).step_by(2) {
assert_eq!(cursor.insert(pos, B256::ZERO), Ok(()));
assert_eq!(cursor.insert(pos, &B256::ZERO), Ok(()));
assert_eq!(cursor.current(), Ok(Some((pos, B256::ZERO))));
}
tx.commit().expect(ERROR_COMMIT);
@@ -1031,7 +1031,7 @@ mod tests {
let key_to_append = 5;
let tx = db.tx_mut().expect(ERROR_INIT_TX);
let mut cursor = tx.cursor_write::<CanonicalHeaders>().unwrap();
assert_eq!(cursor.append(key_to_append, B256::ZERO), Ok(()));
assert_eq!(cursor.append(key_to_append, &B256::ZERO), Ok(()));
tx.commit().expect(ERROR_COMMIT);
// Confirm the result
@@ -1059,7 +1059,7 @@ mod tests {
let tx = db.tx_mut().expect(ERROR_INIT_TX);
let mut cursor = tx.cursor_write::<CanonicalHeaders>().unwrap();
assert_eq!(
cursor.append(key_to_append, B256::ZERO),
cursor.append(key_to_append, &B256::ZERO),
Err(DatabaseWriteError {
info: Error::KeyMismatch.into(),
operation: DatabaseWriteOperation::CursorAppend,
@@ -1088,15 +1088,15 @@ mod tests {
let key = Address::random();
let account = Account::default();
cursor.upsert(key, account).expect(ERROR_UPSERT);
cursor.upsert(key, &account).expect(ERROR_UPSERT);
assert_eq!(cursor.seek_exact(key), Ok(Some((key, account))));
let account = Account { nonce: 1, ..Default::default() };
cursor.upsert(key, account).expect(ERROR_UPSERT);
cursor.upsert(key, &account).expect(ERROR_UPSERT);
assert_eq!(cursor.seek_exact(key), Ok(Some((key, account))));
let account = Account { nonce: 2, ..Default::default() };
cursor.upsert(key, account).expect(ERROR_UPSERT);
cursor.upsert(key, &account).expect(ERROR_UPSERT);
assert_eq!(cursor.seek_exact(key), Ok(Some((key, account))));
let mut dup_cursor = tx.cursor_dup_write::<PlainStorageState>().unwrap();
@@ -1104,12 +1104,12 @@ mod tests {
let value = U256::from(1);
let entry1 = StorageEntry { key: subkey, value };
dup_cursor.upsert(key, entry1).expect(ERROR_UPSERT);
dup_cursor.upsert(key, &entry1).expect(ERROR_UPSERT);
assert_eq!(dup_cursor.seek_by_key_subkey(key, subkey), Ok(Some(entry1)));
let value = U256::from(2);
let entry2 = StorageEntry { key: subkey, value };
dup_cursor.upsert(key, entry2).expect(ERROR_UPSERT);
dup_cursor.upsert(key, &entry2).expect(ERROR_UPSERT);
assert_eq!(dup_cursor.seek_by_key_subkey(key, subkey), Ok(Some(entry1)));
assert_eq!(dup_cursor.next_dup_val(), Ok(Some(entry2)));
}
@@ -1127,7 +1127,7 @@ mod tests {
.try_for_each(|val| {
cursor.append(
transition_id,
AccountBeforeTx { address: Address::with_last_byte(val), info: None },
&AccountBeforeTx { address: Address::with_last_byte(val), info: None },
)
})
.expect(ERROR_APPEND);
@@ -1153,7 +1153,7 @@ mod tests {
assert_eq!(
cursor.append(
transition_id - 1,
AccountBeforeTx { address: Address::with_last_byte(subkey_to_append), info: None }
&AccountBeforeTx { address: Address::with_last_byte(subkey_to_append), info: None }
),
Err(DatabaseWriteError {
info: Error::KeyMismatch.into(),
@@ -1166,7 +1166,7 @@ mod tests {
assert_eq!(
cursor.append(
transition_id,
AccountBeforeTx { address: Address::with_last_byte(subkey_to_append), info: None }
&AccountBeforeTx { address: Address::with_last_byte(subkey_to_append), info: None }
),
Ok(())
);