small cleanup

This commit is contained in:
soruh 2023-08-02 23:26:59 +02:00
parent 40c690e2d0
commit 05f61e177e
3 changed files with 33 additions and 9 deletions

View File

@ -569,7 +569,7 @@ impl SlabPointer {
pub fn allocate_page(&self, db: &mut Db) -> FilePointer { pub fn allocate_page(&self, db: &mut Db) -> FilePointer {
let Slab { head, size } = self.read(db); let Slab { head, size } = self.read(db);
println!("allocate_page({size})"); println!("allocate_slab_page({size})");
let size = size.get(); let size = size.get();

View File

@ -1,3 +1,4 @@
#![allow(unused)]
use std::{ use std::{
collections::{BTreeMap, VecDeque}, collections::{BTreeMap, VecDeque},
fmt::Debug, fmt::Debug,
@ -14,7 +15,6 @@ mod transaction;
use allocator::{AllocatorState, GeneralPurposeAllocator, SlabListPointer, SlabPointer}; use allocator::{AllocatorState, GeneralPurposeAllocator, SlabListPointer, SlabPointer};
use atomic_arc::AtomicArc; use atomic_arc::AtomicArc;
use mapped::ReaderTrait;
use memmap::{Mmap, MmapMut}; use memmap::{Mmap, MmapMut};
use transaction::TransactionHandle; use transaction::TransactionHandle;
use zerocopy::{AsBytes, FromBytes, LayoutVerified, Unaligned, LE}; 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. // 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(); let snapshot = snapshots.pop_front().unwrap();
@ -487,10 +487,11 @@ impl Db {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*;
use mapped::ReaderTrait;
use std::io::Write; use std::io::Write;
use std::process::Stdio; use std::process::Stdio;
use super::*;
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
enum Operation { enum Operation {
Allocate { size: u64 }, Allocate { size: u64 },
@ -719,14 +720,31 @@ mod tests {
assert_eq!(data.generation.get(), i); assert_eq!(data.generation.get(), i);
let n = {
let mut next = data.list;
let mut n = 0;
while !next.is_null() {
next = transaction.read::<DataList>(next).next;
n += 1;
}
n
};
let next = if n >= 5 {
transaction.read::<DataList>(data.list).next
} else {
data.list
};
let (elem_ptr, element) = transaction.allocate::<DataList>(); let (elem_ptr, element) = transaction.allocate::<DataList>();
element.next = data.list; element.next = next;
element.data = i.into(); element.data = i.into();
let (root, data) = transaction.modify::<DataHeader>(root); let (root, data) = transaction.modify::<DataHeader>(root);
data.list = elem_ptr.start; data.list = elem_ptr.start;
data.generation = (i + 1).into(); data.generation = (i + 1).into();
root.start root.start
}); });
@ -749,8 +767,10 @@ mod tests {
ptr = element.next; ptr = element.next;
} }
assert_eq!(items.len(), i + 1); assert_eq!(items.len(), (i + 1).min(5));
for (expected, &is) in items.iter().rev().enumerate() { assert_eq!(items[0], i as u64);
for (expected, &is) in items.iter().skip(1).rev().enumerate() {
assert_eq!(expected as u64, is); assert_eq!(expected as u64, is);
} }
} }

View File

@ -132,7 +132,11 @@ impl<'t> TransactionHandle<'t> {
} }
} }
pub fn free(&mut self, range: FileRange) { pub fn free<T>(&mut self, at: FilePointer) {
self.free_range(at.range(size_of::<T>() as u64))
}
pub fn free_range(&mut self, range: FileRange) {
let mut freed = false; let mut freed = false;
if let Some(allocation) = self.new.remove(&range.start) { if let Some(allocation) = self.new.remove(&range.start) {
@ -141,7 +145,7 @@ impl<'t> TransactionHandle<'t> {
freed = true; freed = true;
} }
for (_, replaced) in &mut self.replaced { for replaced in self.replaced.values_mut() {
if replaced.from == range || replaced.to == Some(range) { if replaced.from == range || replaced.to == Some(range) {
replaced.to = None; replaced.to = None;
freed = true; freed = true;