mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-27 13:04:20 +01:00
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::sync::Arc;
|
|
|
|
pub struct PageSource {
|
|
io: Arc<dyn PageIO>,
|
|
}
|
|
|
|
impl PageSource {
|
|
pub fn from_io(io: Arc<dyn PageIO>) -> Self {
|
|
Self { io }
|
|
}
|
|
|
|
#[cfg(feature = "fs")]
|
|
pub fn from_file(file: Box<dyn File>) -> Self {
|
|
Self {
|
|
io: Arc::new(FileStorage::new(file)),
|
|
}
|
|
}
|
|
|
|
pub fn get(&self, page_idx: usize, c: Arc<Completion>) -> Result<()> {
|
|
self.io.get(page_idx, c)
|
|
}
|
|
}
|
|
|
|
pub trait PageIO {
|
|
fn get(&self, page_idx: usize, c: Arc<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: Arc<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 }
|
|
}
|
|
}
|