Skip to content

Commit

Permalink
Rename LRU to LruCache, keep internal logging at debug level
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-melnychuk committed Nov 21, 2021
1 parent c27ea8a commit 1a40ad6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
24 changes: 12 additions & 12 deletions src/disk/file.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -12,15 +13,14 @@ 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<P: Page> {
/// Underlying file reference where all data is physically stored.
file: RefCell<fs::File>,
head: Head,

/// In-memory page cache. All page access happens only through cached page representation.
cache: RefCell<LRU<u32, P>>,
cache: RefCell<LruCache<u32, P>>,
dirty: RefCell<HashSet<u32>>,

/// Min-heap of available page identifiers (this helps avoid "gaps": empty pages inside file).
Expand Down Expand Up @@ -76,7 +76,7 @@ impl<P: Page> File<P> {
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)),
})
Expand Down Expand Up @@ -132,7 +132,7 @@ impl<P: Page> File<P> {
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)),
};
Expand Down Expand Up @@ -163,12 +163,12 @@ impl<P: Page> File<P> {
.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())
Expand All @@ -181,7 +181,7 @@ impl<P: Page> File<P> {

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

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);
Expand Down Expand Up @@ -311,7 +311,7 @@ impl<P: Page> Tree<P> for File<P> {
}

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);
Expand Down Expand Up @@ -492,7 +492,7 @@ impl<P: Page> Tree<P> for File<P> {
}

fn above(&self, key: &[u8]) -> Result<Option<Ref<[u8]>>> {
info!("above: {}", hex(key));
debug!("above: {}", hex(key));

let mut path = Vec::with_capacity(8);
let mut page = self.root();
Expand Down Expand Up @@ -544,7 +544,7 @@ impl<P: Page> Tree<P> for File<P> {
}

fn below(&self, key: &[u8]) -> Result<Option<Ref<[u8]>>> {
info!("below: {}", hex(key));
debug!("below: {}", hex(key));

let mut path = Vec::with_capacity(8);
let mut page = self.root();
Expand Down
22 changes: 10 additions & 12 deletions src/util/cache.rs
Original file line number Diff line number Diff line change
@@ -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<K: Clone + Eq + PartialEq + Hash, V> {
fn has(&self, key: &K) -> bool;
Expand All @@ -16,15 +16,15 @@ pub(crate) trait Cache<K: Clone + Eq + PartialEq + Hash, V> {
fn free(&self, key: &K);
}

pub(crate) struct LRU<K, V> {
pub(crate) struct LruCache<K, V> {
map: HashMap<K, V>,
lru: RefCell<HashMap<K, u64>>,
locks: RefCell<HashSet<K>>,
cap: usize,
op: RefCell<u64>,
}

impl<K: Clone + Eq + Hash + Display, V> LRU<K, V> {
impl<K: Clone + Eq + Hash + Display, V> LruCache<K, V> {
pub(crate) fn new(size: usize) -> Self {
Self {
map: HashMap::with_capacity(size),
Expand All @@ -45,7 +45,8 @@ impl<K: Clone + Eq + Hash + Display, V> LRU<K, V> {
None
} else {
let locked = self.locks.borrow();
self.lru.borrow()
self.lru
.borrow()
.iter()
.filter(|(key, _)| !locked.contains(*key))
.min_by_key(|(_, lru)| *lru)
Expand All @@ -56,14 +57,14 @@ impl<K: Clone + Eq + Hash + Display, V> LRU<K, V> {

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<K: Clone + Hash + Eq + Display, V> Cache<K, V> for LRU<K, V> {
impl<K: Clone + Hash + Eq + Display, V> Cache<K, V> for LruCache<K, V> {
fn has(&self, key: &K) -> bool {
self.map.contains_key(key)
}
Expand Down Expand Up @@ -105,10 +106,7 @@ impl<K: Clone + Hash + Eq + Display, V> Cache<K, V> for LRU<K, V> {
}

fn keys(&self) -> Vec<K> {
self.map.keys()
.into_iter()
.cloned()
.collect()
self.map.keys().into_iter().cloned().collect()
}

fn lock(&self, key: &K) {
Expand All @@ -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);
Expand All @@ -138,4 +136,4 @@ mod tests {
keys.sort();
assert_eq!(keys, vec![2, 3, 4]);
}
}
}

0 comments on commit 1a40ad6

Please sign in to comment.