Introduce Completion type

This commit is contained in:
Pekka Enberg
2023-11-09 21:11:48 +02:00
parent 9781b2fdf2
commit 2267ee121b
7 changed files with 27 additions and 18 deletions

View File

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

View File

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

View File

@@ -1,4 +1,4 @@
use super::Buffer;
use super::Completion;
use anyhow::Result;
use std::cell::RefCell;
use std::os::unix::io::AsRawFd;
@@ -38,9 +38,9 @@ pub struct File {
}
impl File {
pub fn pread(&self, pos: usize, buf: &mut Buffer) -> Result<()> {
pub fn pread(&self, pos: usize, c: &mut Completion) -> 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 )
let read_e = io_uring::opcode::Read::new(fd, c.buf.as_mut_ptr(), buf.len() as u32 )
.offset(pos as u64)
.build();
let mut ring = self.ring.borrow_mut();

View File

@@ -1,6 +1,10 @@
use cfg_block::cfg_block;
use std::pin::Pin;
pub struct Completion<'a> {
pub buf: &'a mut Buffer,
}
pub struct Buffer {
data: Pin<Vec<u8>>,
}

View File

@@ -20,9 +20,9 @@ use schema::Schema;
use sqlite3_parser::{ast::Cmd, lexer::sql::Parser};
use std::sync::Arc;
pub use io::{Buffer};
#[cfg(feature = "fs")]
pub use io::IO;
pub use io::{Buffer, Completion};
pub use storage::{Storage, StorageIO};
pub use types::Value;

View File

@@ -24,6 +24,7 @@
///
/// For more information, see: https://www.sqlite.org/fileformat.html
use crate::buffer_pool::BufferPool;
use crate::io::{Buffer, Completion};
use crate::types::{Record, Value};
use crate::Storage;
use anyhow::{anyhow, Result};
@@ -60,8 +61,11 @@ pub struct DatabaseHeader {
}
pub fn read_database_header(storage: &Storage) -> Result<DatabaseHeader> {
let mut buf = crate::Buffer::allocate(512);
storage.get(1, &mut buf)?;
let mut buf = Buffer::allocate(512);
let mut c = Completion {
buf: buf.borrow_mut(),
};
storage.get(1, &mut c)?;
let buf = buf.as_slice();
let mut header = DatabaseHeader::default();
header.magic.copy_from_slice(&buf[0..16]);
@@ -136,7 +140,8 @@ pub fn read_btree_page(
) -> Result<BTreePage> {
let mut buf = buffer_pool.get();
let page = buf.borrow_mut().data_mut();
storage.get(page_idx, page)?;
let mut c = Completion { buf: page };
storage.get(page_idx, &mut c)?;
let mut pos = if page_idx == 1 {
DATABASE_HEADER_SIZE
} else {

View File

@@ -1,4 +1,4 @@
use crate::io::{Buffer};
use crate::io::Completion;
use anyhow::Result;
use std::sync::Arc;
@@ -18,13 +18,13 @@ impl Storage {
}
}
pub fn get(&self, page_idx: usize, buf: &mut Buffer) -> Result<()> {
self.io.get(page_idx, buf)
pub fn get(&self, page_idx: usize, c: &mut Completion) -> Result<()> {
self.io.get(page_idx, c)
}
}
pub trait StorageIO {
fn get(&self, page_idx: usize, buf: &mut Buffer) -> Result<()>;
fn get(&self, page_idx: usize, c: &mut Completion) -> Result<()>;
}
#[cfg(feature = "fs")]
@@ -34,14 +34,14 @@ struct FileStorage {
#[cfg(feature = "fs")]
impl StorageIO for FileStorage {
fn get(&self, page_idx: usize, buf: &mut Buffer) -> Result<()> {
let page_size = buf.len();
fn get(&self, page_idx: usize, c: &mut 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, buf)?;
self.file.pread(pos, c)?;
Ok(())
}
}