From e7cc50038f527b4e78f68dc6826610838640c229 Mon Sep 17 00:00:00 2001 From: "Kim, JinSan" Date: Sat, 4 Feb 2023 05:02:33 +0900 Subject: [PATCH] test(db): cursor write operations are working properly wherever cursor is (#1161) --- .../storage/db/src/implementation/mdbx/mod.rs | 81 ++++++++++++++++++- 1 file changed, 77 insertions(+), 4 deletions(-) diff --git a/crates/storage/db/src/implementation/mdbx/mod.rs b/crates/storage/db/src/implementation/mdbx/mod.rs index 3f324f9b4c..581480dd0d 100644 --- a/crates/storage/db/src/implementation/mdbx/mod.rs +++ b/crates/storage/db/src/implementation/mdbx/mod.rs @@ -346,20 +346,86 @@ mod tests { .expect(ERROR_PUT); tx.commit().expect(ERROR_COMMIT); - let db: Arc> = test_utils::create_test_db(EnvKind::RW); - let key_to_insert = 2; let tx = db.tx_mut().expect(ERROR_INIT_TX); let mut cursor = tx.cursor_write::().unwrap(); // INSERT - cursor.seek_exact(1).unwrap(); assert_eq!(cursor.insert(key_to_insert, H256::zero()), Ok(())); assert_eq!(cursor.current(), Ok(Some((key_to_insert, H256::zero())))); // INSERT (failure) assert_eq!(cursor.insert(key_to_insert, H256::zero()), Err(Error::Write(4294936497))); assert_eq!(cursor.current(), Ok(Some((key_to_insert, H256::zero())))); + + tx.commit().expect(ERROR_COMMIT); + + // Confirm the result + let tx = db.tx().expect(ERROR_INIT_TX); + let mut cursor = tx.cursor_read::().unwrap(); + let res = cursor.walk(0).unwrap().map(|res| res.unwrap().0).collect::>(); + assert_eq!(res, vec![0, 1, 2, 3, 4, 5]); + tx.commit().expect(ERROR_COMMIT); + } + + #[test] + fn db_cursor_insert_wherever_cursor_is() { + let db: Arc> = test_utils::create_test_db(EnvKind::RW); + let tx = db.tx_mut().expect(ERROR_INIT_TX); + + // PUT + vec![0, 1, 3, 5, 7, 9] + .into_iter() + .try_for_each(|key| tx.put::(key, H256::zero())) + .expect(ERROR_PUT); + tx.commit().expect(ERROR_COMMIT); + + let tx = db.tx_mut().expect(ERROR_INIT_TX); + let mut cursor = tx.cursor_write::().unwrap(); + + // INSERT (cursor starts at last) + cursor.last().unwrap(); + assert_eq!(cursor.current(), Ok(Some((9, H256::zero())))); + + for pos in (2..=8).step_by(2) { + assert_eq!(cursor.insert(pos, H256::zero()), Ok(())); + assert_eq!(cursor.current(), Ok(Some((pos, H256::zero())))); + } + tx.commit().expect(ERROR_COMMIT); + + // Confirm the result + let tx = db.tx().expect(ERROR_INIT_TX); + let mut cursor = tx.cursor_read::().unwrap(); + let res = cursor.walk(0).unwrap().map(|res| res.unwrap().0).collect::>(); + assert_eq!(res, vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); + tx.commit().expect(ERROR_COMMIT); + } + + #[test] + fn db_cursor_append() { + let db: Arc> = test_utils::create_test_db(EnvKind::RW); + + // PUT + let tx = db.tx_mut().expect(ERROR_INIT_TX); + vec![0, 1, 2, 3, 4] + .into_iter() + .try_for_each(|key| tx.put::(key, H256::zero())) + .expect(ERROR_PUT); + tx.commit().expect(ERROR_COMMIT); + + // APPEND + let key_to_append = 5; + let tx = db.tx_mut().expect(ERROR_INIT_TX); + let mut cursor = tx.cursor_write::().unwrap(); + assert_eq!(cursor.append(key_to_append, H256::zero()), Ok(())); + tx.commit().expect(ERROR_COMMIT); + + // Confirm the result + let tx = db.tx().expect(ERROR_INIT_TX); + let mut cursor = tx.cursor_read::().unwrap(); + let res = cursor.walk(0).unwrap().map(|res| res.unwrap().0).collect::>(); + assert_eq!(res, vec![0, 1, 2, 3, 4, 5]); + tx.commit().expect(ERROR_COMMIT); } #[test] @@ -378,9 +444,16 @@ mod tests { let key_to_append = 2; let tx = db.tx_mut().expect(ERROR_INIT_TX); let mut cursor = tx.cursor_write::().unwrap(); - cursor.seek_exact(1).unwrap(); assert_eq!(cursor.append(key_to_append, H256::zero()), Err(Error::Write(4294936878))); assert_eq!(cursor.current(), Ok(Some((5, H256::zero())))); // the end of table + tx.commit().expect(ERROR_COMMIT); + + // Confirm the result + let tx = db.tx().expect(ERROR_INIT_TX); + let mut cursor = tx.cursor_read::().unwrap(); + let res = cursor.walk(0).unwrap().map(|res| res.unwrap().0).collect::>(); + assert_eq!(res, vec![0, 1, 3, 4, 5]); + tx.commit().expect(ERROR_COMMIT); } #[test]