diff --git a/bindings/wasm/lib.rs b/bindings/wasm/lib.rs index 28970b057..c7007a9a2 100644 --- a/bindings/wasm/lib.rs +++ b/bindings/wasm/lib.rs @@ -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!(); } } diff --git a/core/io/darwin.rs b/core/io/darwin.rs index 64ecc1369..eac13e84d 100644 --- a/core/io/darwin.rs +++ b/core/io/darwin.rs @@ -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(()) } } \ No newline at end of file diff --git a/core/io/linux.rs b/core/io/linux.rs index 91677be8c..55e1c8af0 100644 --- a/core/io/linux.rs +++ b/core/io/linux.rs @@ -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(); diff --git a/core/io/mod.rs b/core/io/mod.rs index aaa0ea65b..78f988049 100644 --- a/core/io/mod.rs +++ b/core/io/mod.rs @@ -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>, } diff --git a/core/lib.rs b/core/lib.rs index cbd8ea37c..0e9b8f6e0 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -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; diff --git a/core/sqlite3_ondisk.rs b/core/sqlite3_ondisk.rs index 0e933aa76..e62b1a501 100644 --- a/core/sqlite3_ondisk.rs +++ b/core/sqlite3_ondisk.rs @@ -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 { - 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 { 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 { diff --git a/core/storage.rs b/core/storage.rs index 635391f7b..7f49e2201 100644 --- a/core/storage.rs +++ b/core/storage.rs @@ -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(()) } }