mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-24 03:34:18 +01:00
Use Rc instead of Arc and replace the concurrent LRU with single-threaded SIEVE. Fixes #23 Fixes #29
57 lines
1.2 KiB
Rust
57 lines
1.2 KiB
Rust
use crate::io::Completion;
|
|
#[cfg(feature = "fs")]
|
|
use crate::io::File;
|
|
use anyhow::Result;
|
|
use std::rc::Rc;
|
|
|
|
pub struct PageSource {
|
|
io: Rc<dyn PageIO>,
|
|
}
|
|
|
|
impl PageSource {
|
|
pub fn from_io(io: Rc<dyn PageIO>) -> Self {
|
|
Self { io }
|
|
}
|
|
|
|
#[cfg(feature = "fs")]
|
|
pub fn from_file(file: Box<dyn File>) -> Self {
|
|
Self {
|
|
io: Rc::new(FileStorage::new(file)),
|
|
}
|
|
}
|
|
|
|
pub fn get(&self, page_idx: usize, c: Rc<Completion>) -> Result<()> {
|
|
self.io.get(page_idx, c)
|
|
}
|
|
}
|
|
|
|
pub trait PageIO {
|
|
fn get(&self, page_idx: usize, c: Rc<Completion>) -> Result<()>;
|
|
}
|
|
|
|
#[cfg(feature = "fs")]
|
|
struct FileStorage {
|
|
file: Box<dyn crate::io::File>,
|
|
}
|
|
|
|
#[cfg(feature = "fs")]
|
|
impl PageIO for FileStorage {
|
|
fn get(&self, page_idx: usize, c: Rc<Completion>) -> Result<()> {
|
|
let page_size = c.buf().len();
|
|
assert!(page_idx > 0);
|
|
assert!(page_size >= 512);
|
|
assert!(page_size <= 65536);
|
|
assert!((page_size & (page_size - 1)) == 0);
|
|
let pos = (page_idx - 1) * page_size;
|
|
self.file.pread(pos, c)?;
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "fs")]
|
|
impl FileStorage {
|
|
pub fn new(file: Box<dyn crate::io::File>) -> Self {
|
|
Self { file }
|
|
}
|
|
}
|