diff --git a/src/disk/file.rs b/src/disk/file.rs index 5288e29..a5ca991 100644 --- a/src/disk/file.rs +++ b/src/disk/file.rs @@ -1,9 +1,10 @@ use crate::api::error::{Error, Result}; use crate::api::page::Page; use crate::api::tree::Tree; +use crate::util::cache::{Cache, LruCache}; use crate::util::hex::hex; use bytes::{Buf, BufMut, BytesMut}; -use log::{debug, error, info, trace}; +use log::{debug, error, trace}; use std::cell::{Ref, RefCell, RefMut}; use std::cmp::Reverse; use std::collections::{BinaryHeap, HashSet}; @@ -12,7 +13,6 @@ use std::io::{self, Read, Seek, SeekFrom, Write}; use std::mem::size_of; use std::ops::Deref; use std::path::Path; -use crate::util::cache::{Cache, LRU}; pub(crate) struct File { /// Underlying file reference where all data is physically stored. @@ -20,7 +20,7 @@ pub(crate) struct File { head: Head, /// In-memory page cache. All page access happens only through cached page representation. - cache: RefCell>, + cache: RefCell>, dirty: RefCell>, /// Min-heap of available page identifiers (this helps avoid "gaps": empty pages inside file). @@ -76,7 +76,7 @@ impl File

{ Ok(Self { file: RefCell::new(file), head, - cache: RefCell::new(LRU::new(64)), + cache: RefCell::new(LruCache::new(64)), dirty: RefCell::new(HashSet::with_capacity(32)), empty: RefCell::new(BinaryHeap::with_capacity(32)), }) @@ -132,7 +132,7 @@ impl File

{ let this = Self { file: RefCell::new(file), head, - cache: RefCell::new(LRU::new(64)), + cache: RefCell::new(LruCache::new(64)), dirty: RefCell::new(HashSet::with_capacity(32)), empty: RefCell::new(BinaryHeap::with_capacity(16)), }; @@ -163,12 +163,12 @@ impl File

{ .borrow_mut() .seek(SeekFrom::Start(offset as u64))?; self.file.borrow_mut().read_exact(page.as_mut())?; - info!("Loading page {}", page.id()); + debug!("Loading page {}", page.id()); Ok(page) } fn save(&self, page: &P) -> io::Result<()> { - info!("Saving page {}", page.id()); + debug!("Saving page {}", page.id()); let offset = self.offset(page.id()) as u64; self.file.borrow_mut().seek(SeekFrom::Start(offset))?; self.file.borrow_mut().write_all(page.as_ref()) @@ -181,7 +181,7 @@ impl File

{ impl Tree

for File

{ fn lookup(&self, key: &[u8]) -> Result>> { - info!("lookup: {}", hex(key)); + debug!("lookup: {}", hex(key)); let mut seen = HashSet::with_capacity(8); let mut page = self.root(); loop { @@ -222,7 +222,7 @@ impl Tree

for File

{ } fn insert(&mut self, key: &[u8], val: &[u8]) -> Result<()> { - info!("insert: {} -> {}", hex(key), hex(val)); + debug!("insert: {} -> {}", hex(key), hex(val)); let mut page = self.root_mut(); let mut seen = HashSet::with_capacity(8); let mut path = Vec::with_capacity(8); @@ -311,7 +311,7 @@ impl Tree

for File

{ } fn remove(&mut self, key: &[u8]) -> Result<()> { - info!("remove: {}", hex(key)); + debug!("remove: {}", hex(key)); let mut page = self.root_mut(); let mut seen = HashSet::with_capacity(8); let mut path = Vec::with_capacity(8); @@ -492,7 +492,7 @@ impl Tree

for File

{ } fn above(&self, key: &[u8]) -> Result>> { - info!("above: {}", hex(key)); + debug!("above: {}", hex(key)); let mut path = Vec::with_capacity(8); let mut page = self.root(); @@ -544,7 +544,7 @@ impl Tree

for File

{ } fn below(&self, key: &[u8]) -> Result>> { - info!("below: {}", hex(key)); + debug!("below: {}", hex(key)); let mut path = Vec::with_capacity(8); let mut page = self.root(); diff --git a/src/util/cache.rs b/src/util/cache.rs index 1779e2c..aad97ec 100644 --- a/src/util/cache.rs +++ b/src/util/cache.rs @@ -1,8 +1,8 @@ +use log::debug; use std::cell::RefCell; use std::collections::{HashMap, HashSet}; use std::fmt::Display; use std::hash::Hash; -use log::info; pub(crate) trait Cache { fn has(&self, key: &K) -> bool; @@ -16,7 +16,7 @@ pub(crate) trait Cache { fn free(&self, key: &K); } -pub(crate) struct LRU { +pub(crate) struct LruCache { map: HashMap, lru: RefCell>, locks: RefCell>, @@ -24,7 +24,7 @@ pub(crate) struct LRU { op: RefCell, } -impl LRU { +impl LruCache { pub(crate) fn new(size: usize) -> Self { Self { map: HashMap::with_capacity(size), @@ -45,7 +45,8 @@ impl LRU { None } else { let locked = self.locks.borrow(); - self.lru.borrow() + self.lru + .borrow() .iter() .filter(|(key, _)| !locked.contains(*key)) .min_by_key(|(_, lru)| *lru) @@ -56,14 +57,14 @@ impl LRU { fn evict(&mut self) { if let Some(key) = self.lru() { - info!("Evict page {}", key); + debug!("Evict page {}", key); self.map.remove(&key); self.lru.borrow_mut().remove(&key); } } } -impl Cache for LRU { +impl Cache for LruCache { fn has(&self, key: &K) -> bool { self.map.contains_key(key) } @@ -105,10 +106,7 @@ impl Cache for LRU { } fn keys(&self) -> Vec { - self.map.keys() - .into_iter() - .cloned() - .collect() + self.map.keys().into_iter().cloned().collect() } fn lock(&self, key: &K) { @@ -126,7 +124,7 @@ mod tests { #[test] fn test_eviction() { - let mut cache = LRU::new(3); + let mut cache = LruCache::new(3); cache.put(1, 0); cache.put(2, 0); cache.put(3, 0); @@ -138,4 +136,4 @@ mod tests { keys.sort(); assert_eq!(keys, vec![2, 3, 4]); } -} \ No newline at end of file +}