Move raw buffer to I/O module

This commit is contained in:
Pekka Enberg 2023-09-30 14:08:44 +03:00
parent 1e1e096a48
commit de6f327a4a
8 changed files with 22 additions and 20 deletions

View file

@ -19,7 +19,7 @@ impl Database {
pub struct IO {}
impl lig_core::StorageIO for IO {
fn get(&self, _page_idx: usize, _buf: &mut [u8]) -> Result<()> {
fn get(&self, _page_idx: usize, _buf: &mut lig_core::Buffer) -> Result<()> {
todo!();
}
}

View file

@ -1,9 +1,8 @@
use crate::io::Buffer as IoBuffer;
use std::{mem::ManuallyDrop, sync::Mutex};
pub type RawBuffer = Vec<u8>;
pub struct BufferPool {
pub free_buffers: Mutex<Vec<RawBuffer>>,
pub free_buffers: Mutex<Vec<IoBuffer>>,
page_size: usize,
}
@ -25,7 +24,7 @@ impl BufferPool {
}
}
pub fn put(&self, buffer: RawBuffer) {
pub fn put(&self, buffer: IoBuffer) {
let mut free_buffers = self.free_buffers.lock().unwrap();
free_buffers.push(buffer);
}
@ -33,7 +32,7 @@ impl BufferPool {
pub struct Buffer<'a> {
pool: &'a BufferPool,
data: ManuallyDrop<RawBuffer>,
data: ManuallyDrop<IoBuffer>,
}
impl Drop for Buffer<'_> {
@ -44,14 +43,14 @@ impl Drop for Buffer<'_> {
}
impl<'a> Buffer<'a> {
pub fn new(pool: &'a BufferPool, data: RawBuffer) -> Self {
pub fn new(pool: &'a BufferPool, data: IoBuffer) -> Self {
Self {
pool,
data: ManuallyDrop::new(data),
}
}
pub fn data_mut(&mut self) -> &mut [u8] {
pub fn data_mut(&mut self) -> &mut IoBuffer {
&mut self.data
}
}

View file

@ -1,7 +1,7 @@
use super::Buffer;
use anyhow::{Ok, Result};
use std::cell::RefCell;
use std::io::{Read, Seek};
use std::sync::Arc;
pub struct IO {}
@ -27,7 +27,7 @@ pub struct File {
}
impl File {
pub fn pread(&self, pos: usize, buf: &mut [u8]) -> Result<()> {
pub fn pread(&self, pos: usize, buf: &mut Buffer) -> Result<()> {
let mut file = self.file.borrow_mut();
file.seek(std::io::SeekFrom::Start(pos as u64))?;
file.read_exact(buf)?;

View file

@ -1,3 +1,4 @@
use super::Buffer;
use anyhow::Result;
use std::cell::RefCell;
use std::os::unix::io::AsRawFd;
@ -37,7 +38,7 @@ pub struct File {
}
impl File {
pub fn pread(&self, pos: usize, buf: &mut [u8]) -> Result<()> {
pub fn pread(&self, pos: usize, buf: &mut Buffer) -> Result<()> {
let fd = io_uring::types::Fd(self.file.as_raw_fd());
let read_e = io_uring::opcode::Read::new(fd, buf.as_mut_ptr(), buf.len() as u32 )
.offset(pos as u64)

View file

@ -1,13 +1,15 @@
use cfg_block::cfg_block;
pub type Buffer = Vec<u8>;
cfg_block! {
#[cfg(target_os = "linux")] {
mod linux;
pub use linux::{IO, File};
pub use linux::{File, IO};
}
#[cfg(target_os = "macos")] {
mod darwin;
pub use darwin::{IO, File};
pub use darwin::{File, IO};
}
}

View file

@ -21,7 +21,7 @@ use schema::Schema;
use sqlite3_parser::{ast::Cmd, lexer::sql::Parser};
use std::sync::Arc;
pub use io::IO;
pub use io::{Buffer, IO};
pub use storage::{Storage, StorageIO};
pub use types::Value;

View file

@ -60,7 +60,7 @@ pub struct DatabaseHeader {
}
pub fn read_database_header(storage: &Storage) -> Result<DatabaseHeader> {
let mut buf = [0; 512];
let mut buf = vec![0; 512];
storage.get(1, &mut buf)?;
let mut header = DatabaseHeader::default();
header.magic.copy_from_slice(&buf[0..16]);
@ -134,7 +134,7 @@ pub fn read_btree_page(
page_idx: usize,
) -> Result<BTreePage> {
let mut buf = buffer_pool.get();
let page = &mut buf.borrow_mut().data_mut();
let page = buf.borrow_mut().data_mut();
storage.get(page_idx, page)?;
let mut pos = if page_idx == 1 {
DATABASE_HEADER_SIZE

View file

@ -1,4 +1,4 @@
use crate::io::File;
use crate::io::{Buffer, File};
use anyhow::Result;
use std::sync::Arc;
@ -17,13 +17,13 @@ impl Storage {
}
}
pub fn get(&self, page_idx: usize, buf: &mut [u8]) -> Result<()> {
pub fn get(&self, page_idx: usize, buf: &mut Buffer) -> Result<()> {
self.io.get(page_idx, buf)
}
}
pub trait StorageIO {
fn get(&self, page_idx: usize, buf: &mut [u8]) -> Result<()>;
fn get(&self, page_idx: usize, buf: &mut Buffer) -> Result<()>;
}
struct FileStorage {
@ -31,7 +31,7 @@ struct FileStorage {
}
impl StorageIO for FileStorage {
fn get(&self, page_idx: usize, buf: &mut [u8]) -> Result<()> {
fn get(&self, page_idx: usize, buf: &mut Buffer) -> Result<()> {
let page_size = buf.len();
assert!(page_idx > 0);
assert!(page_size >= 512);