优化和收尾

This commit is contained in:
LIm1tZz7 committed 2026-09-25 14:21:55 +08:00
1 parent 7f5bcc289e
commit 9e8c5f1ef0
33 files changed
+3332 -598

No files matched your search

+60
View File
@@ -153,3 +153,63 @@ fn order_too_large_for_page_errors() {
let dir = tempfile::tempdir().unwrap();
assert!(DiskBTree::open(dir.path().join("t.idx"), 512, 300, DEFAULT_POOL_SIZE).is_err());
}
/// 删掉键之后查不到,别人不受影响
#[test]
fn delete_removes_only_that_key() {
let dir = tempfile::tempdir().unwrap();
let mut t = DiskBTree::open(
dir.path().join("t.idx"),
DEFAULT_PAGE_SIZE,
DEFAULT_BTREE_ORDER,
DEFAULT_POOL_SIZE,
)
.unwrap();
for i in 0..200 {
t.insert(i, i as u64 + 1).unwrap();
}
assert!(t.delete(50).unwrap(), "删一个存在的键应该返回 true");
assert_eq!(t.get(50).unwrap(), None);
assert_eq!(t.get(49).unwrap(), Some(50));
assert_eq!(t.get(51).unwrap(), Some(52));
// 再删一次:它已经不在了
assert!(!t.delete(50).unwrap());
// 遍历里也不能再有它
let all = t.iter_all().unwrap();
assert_eq!(all.len(), 199);
assert!(all.iter().all(|(k, _)| *k != 50));
}
/// 删过之后树还能继续插(惰性删除会留下稀疏节点,但不能影响正确性)
#[test]
fn insert_after_delete_still_works() {
let dir = tempfile::tempdir().unwrap();
let mut t = DiskBTree::open(
dir.path().join("t.idx"),
DEFAULT_PAGE_SIZE,
DEFAULT_BTREE_ORDER,
DEFAULT_POOL_SIZE,
)
.unwrap();
for i in 0..300 {
t.insert(i, i as u64).unwrap();
}
// 把偶数键全删掉
for i in (0..300).step_by(2) {
assert!(t.delete(i).unwrap());
}
// 再插一批新的
for i in 1000..1100 {
t.insert(i, i as u64).unwrap();
}
for i in 0..300 {
let want = if i % 2 == 0 { None } else { Some(i as u64) };
assert_eq!(t.get(i).unwrap(), want, "key {}", i);
}
for i in 1000..1100 {
assert_eq!(t.get(i).unwrap(), Some(i as u64));
}
}