|
| 1 | +use std::ops::Bound; |
| 2 | + |
| 3 | +use bytes::Bytes; |
| 4 | +use tempfile::tempdir; |
| 5 | + |
| 6 | +use crate::{ |
| 7 | + compact::CompactionOptions, |
| 8 | + lsm_storage::{LsmStorageOptions, MiniLsm}, |
| 9 | + tests::harness::check_lsm_iter_result_by_key, |
| 10 | +}; |
| 11 | + |
| 12 | +#[test] |
| 13 | +fn test_txn_integration() { |
| 14 | + let dir = tempdir().unwrap(); |
| 15 | + let options = LsmStorageOptions::default_for_week2_test(CompactionOptions::NoCompaction); |
| 16 | + let storage = MiniLsm::open(&dir, options.clone()).unwrap(); |
| 17 | + let txn1 = storage.new_txn().unwrap(); |
| 18 | + let txn2 = storage.new_txn().unwrap(); |
| 19 | + txn1.put(b"test1", b"233"); |
| 20 | + txn2.put(b"test2", b"233"); |
| 21 | + check_lsm_iter_result_by_key( |
| 22 | + &mut txn1.scan(Bound::Unbounded, Bound::Unbounded).unwrap(), |
| 23 | + vec![(Bytes::from("test1"), Bytes::from("233"))], |
| 24 | + ); |
| 25 | + check_lsm_iter_result_by_key( |
| 26 | + &mut txn2.scan(Bound::Unbounded, Bound::Unbounded).unwrap(), |
| 27 | + vec![(Bytes::from("test2"), Bytes::from("233"))], |
| 28 | + ); |
| 29 | + let txn3 = storage.new_txn().unwrap(); |
| 30 | + check_lsm_iter_result_by_key( |
| 31 | + &mut txn3.scan(Bound::Unbounded, Bound::Unbounded).unwrap(), |
| 32 | + vec![], |
| 33 | + ); |
| 34 | + txn1.commit().unwrap(); |
| 35 | + txn2.commit().unwrap(); |
| 36 | + check_lsm_iter_result_by_key( |
| 37 | + &mut txn3.scan(Bound::Unbounded, Bound::Unbounded).unwrap(), |
| 38 | + vec![], |
| 39 | + ); |
| 40 | + drop(txn3); |
| 41 | + check_lsm_iter_result_by_key( |
| 42 | + &mut storage.scan(Bound::Unbounded, Bound::Unbounded).unwrap(), |
| 43 | + vec![ |
| 44 | + (Bytes::from("test1"), Bytes::from("233")), |
| 45 | + (Bytes::from("test2"), Bytes::from("233")), |
| 46 | + ], |
| 47 | + ); |
| 48 | + let txn4 = storage.new_txn().unwrap(); |
| 49 | + assert_eq!(txn4.get(b"test1").unwrap(), Some(Bytes::from("233"))); |
| 50 | + assert_eq!(txn4.get(b"test2").unwrap(), Some(Bytes::from("233"))); |
| 51 | + check_lsm_iter_result_by_key( |
| 52 | + &mut txn4.scan(Bound::Unbounded, Bound::Unbounded).unwrap(), |
| 53 | + vec![ |
| 54 | + (Bytes::from("test1"), Bytes::from("233")), |
| 55 | + (Bytes::from("test2"), Bytes::from("233")), |
| 56 | + ], |
| 57 | + ); |
| 58 | + txn4.put(b"test2", b"2333"); |
| 59 | + assert_eq!(txn4.get(b"test1").unwrap(), Some(Bytes::from("233"))); |
| 60 | + assert_eq!(txn4.get(b"test2").unwrap(), Some(Bytes::from("2333"))); |
| 61 | + check_lsm_iter_result_by_key( |
| 62 | + &mut txn4.scan(Bound::Unbounded, Bound::Unbounded).unwrap(), |
| 63 | + vec![ |
| 64 | + (Bytes::from("test1"), Bytes::from("233")), |
| 65 | + (Bytes::from("test2"), Bytes::from("2333")), |
| 66 | + ], |
| 67 | + ); |
| 68 | + txn4.delete(b"test2"); |
| 69 | + assert_eq!(txn4.get(b"test1").unwrap(), Some(Bytes::from("233"))); |
| 70 | + assert_eq!(txn4.get(b"test2").unwrap(), None); |
| 71 | + check_lsm_iter_result_by_key( |
| 72 | + &mut txn4.scan(Bound::Unbounded, Bound::Unbounded).unwrap(), |
| 73 | + vec![(Bytes::from("test1"), Bytes::from("233"))], |
| 74 | + ); |
| 75 | +} |
0 commit comments