diff --git a/src/allocator.rs b/src/allocator.rs index 9afe9a5..2447f2a 100644 --- a/src/allocator.rs +++ b/src/allocator.rs @@ -569,7 +569,7 @@ impl SlabPointer { pub fn allocate_page(&self, db: &mut Db) -> FilePointer { let Slab { head, size } = self.read(db); - println!("allocate_page({size})"); + println!("allocate_slab_page({size})"); let size = size.get(); diff --git a/src/lib.rs b/src/lib.rs index 8e3aa13..18529cf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +#![allow(unused)] use std::{ collections::{BTreeMap, VecDeque}, fmt::Debug, @@ -14,7 +15,6 @@ mod transaction; use allocator::{AllocatorState, GeneralPurposeAllocator, SlabListPointer, SlabPointer}; use atomic_arc::AtomicArc; -use mapped::ReaderTrait; use memmap::{Mmap, MmapMut}; use transaction::TransactionHandle; use zerocopy::{AsBytes, FromBytes, LayoutVerified, Unaligned, LE}; @@ -236,7 +236,7 @@ impl Db { { // if the snapshot is uniqe we are the only owner and can free the epoch. - println!("freeing epoch"); + // println!("freeing epoch"); let snapshot = snapshots.pop_front().unwrap(); @@ -487,10 +487,11 @@ impl Db { #[cfg(test)] mod tests { + use super::*; + use mapped::ReaderTrait; use std::io::Write; use std::process::Stdio; - use super::*; #[derive(Debug, Clone, Copy)] enum Operation { Allocate { size: u64 }, @@ -719,14 +720,31 @@ mod tests { assert_eq!(data.generation.get(), i); + let n = { + let mut next = data.list; + let mut n = 0; + while !next.is_null() { + next = transaction.read::(next).next; + n += 1; + } + n + }; + + let next = if n >= 5 { + transaction.read::(data.list).next + } else { + data.list + }; + let (elem_ptr, element) = transaction.allocate::(); - element.next = data.list; + element.next = next; element.data = i.into(); let (root, data) = transaction.modify::(root); data.list = elem_ptr.start; data.generation = (i + 1).into(); + root.start }); @@ -749,8 +767,10 @@ mod tests { ptr = element.next; } - assert_eq!(items.len(), i + 1); - for (expected, &is) in items.iter().rev().enumerate() { + assert_eq!(items.len(), (i + 1).min(5)); + assert_eq!(items[0], i as u64); + + for (expected, &is) in items.iter().skip(1).rev().enumerate() { assert_eq!(expected as u64, is); } } diff --git a/src/transaction.rs b/src/transaction.rs index 7a93b00..66995db 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -132,7 +132,11 @@ impl<'t> TransactionHandle<'t> { } } - pub fn free(&mut self, range: FileRange) { + pub fn free(&mut self, at: FilePointer) { + self.free_range(at.range(size_of::() as u64)) + } + + pub fn free_range(&mut self, range: FileRange) { let mut freed = false; if let Some(allocation) = self.new.remove(&range.start) { @@ -141,7 +145,7 @@ impl<'t> TransactionHandle<'t> { freed = true; } - for (_, replaced) in &mut self.replaced { + for replaced in self.replaced.values_mut() { if replaced.from == range || replaced.to == Some(range) { replaced.to = None; freed = true;