Skip to content

Commit

Permalink
use core wal file for wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
pereman2 committed Nov 14, 2024
1 parent 4a55e0b commit 87c80b2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 71 deletions.
80 changes: 15 additions & 65 deletions bindings/wasm/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use limbo_core::{OpenFlags, Page, Result, IO};
use limbo_core::{OpenFlags, Page, Pager, Result, WalFile, IO};
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;
Expand All @@ -20,7 +20,13 @@ impl Database {
.open_file(path, limbo_core::OpenFlags::None, false)
.unwrap();
let page_io = Rc::new(DatabaseStorage::new(file));
let wal = Rc::new(RefCell::new(Wal {}));
let db_header = Pager::begin_open(page_io.clone()).unwrap();
let wal_path = format!("{}-wal", path);
let wal = Rc::new(RefCell::new(WalFile::new(
io.clone(),
wal_path,
db_header.borrow().page_size as usize,
)));
let db = limbo_core::Database::open(io, page_io, wal).unwrap();
let conn = db.connect();
Database { db, conn }
Expand Down Expand Up @@ -56,7 +62,7 @@ impl Statement {
}
array.push(&row_array);
}
Ok(limbo_core::RowResult::IO) => todo!(),
Ok(limbo_core::RowResult::IO) => {}
Ok(limbo_core::RowResult::Done) => break,
Err(e) => panic!("Error: {:?}", e),
}
Expand Down Expand Up @@ -109,15 +115,15 @@ impl limbo_core::File for File {
_buffer: Rc<std::cell::RefCell<limbo_core::Buffer>>,
_c: Rc<limbo_core::Completion>,
) -> Result<()> {
todo!()
Ok(())
}

fn sync(&self, _c: Rc<limbo_core::Completion>) -> Result<()> {
todo!()
Ok(())
}

fn size(&self) -> Result<u64> {
todo!()
Ok(self.vfs.size(self.fd))
}
}

Expand Down Expand Up @@ -213,65 +219,6 @@ impl limbo_core::DatabaseStorage for DatabaseStorage {
}
}

pub struct Wal {}

impl limbo_core::Wal for Wal {
fn begin_read_tx(&self) -> Result<()> {
Ok(())
}

fn end_read_tx(&self) -> Result<()> {
Ok(())
}

fn find_frame(&self, _page_id: u64) -> Result<Option<u64>> {
Ok(None)
}

fn begin_write_tx(&self) -> Result<()> {
todo!()
}

fn end_write_tx(&self) -> Result<()> {
todo!()
}

fn read_frame(
&self,
_frame_id: u64,
_page: Rc<std::cell::RefCell<limbo_core::Page>>,
_buffer_pool: Rc<limbo_core::BufferPool>,
) -> Result<()> {
todo!()
}

fn should_checkpoint(&self) -> bool {
false
}

fn append_frame(
&mut self,
_page: Rc<RefCell<Page>>,
_db_size: u32,
_pager: &limbo_core::Pager,
_write_counter: Rc<RefCell<usize>>,
) -> Result<()> {
todo!()
}

fn checkpoint(
&mut self,
_pager: &limbo_core::Pager,
_write_counter: Rc<RefCell<usize>>,
) -> Result<limbo_core::CheckpointStatus> {
todo!()
}

fn sync(&mut self) -> Result<limbo_core::CheckpointStatus> {
Ok(limbo_core::CheckpointStatus::Done)
}
}

#[wasm_bindgen(module = "/vfs.js")]
extern "C" {
type VFS;
Expand All @@ -290,6 +237,9 @@ extern "C" {

#[wasm_bindgen(method)]
fn pread(this: &VFS, fd: i32, buffer: &mut [u8], offset: usize) -> i32;

#[wasm_bindgen(method)]
fn size(this: &VFS, fd: i32) -> u64;
}

#[wasm_bindgen(start)]
Expand Down
7 changes: 6 additions & 1 deletion bindings/wasm/vfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class VFS {
pwrite(fd, buffer, offset) {
return fs.writeSync(fd, buffer, 0, buffer.length, offset);
}

size(fd) {
let stats = fs.fstatSync(fd);
return BigInt(stats.size);
}
}

module.exports = { VFS };
module.exports = { VFS };
3 changes: 1 addition & 2 deletions core/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ use std::{cell::RefCell, rc::Rc};
#[cfg(feature = "fs")]
use storage::database::FileStorage;
use storage::sqlite3_ondisk::DatabaseHeader;
#[cfg(feature = "fs")]
use storage::wal::WalFile;
pub use storage::wal::WalFile;

use translate::optimizer::optimize_plan;
use translate::planner::prepare_select_plan;
Expand Down
3 changes: 0 additions & 3 deletions core/storage/wal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ pub trait Wal {
fn sync(&mut self) -> Result<CheckpointStatus>;
}

#[cfg(feature = "fs")]
pub struct WalFile {
io: Arc<dyn crate::io::IO>,
wal_path: String,
Expand All @@ -80,7 +79,6 @@ pub enum CheckpointStatus {
IO,
}

#[cfg(feature = "fs")]
impl Wal for WalFile {
/// Begin a read transaction.
fn begin_read_tx(&self) -> Result<()> {
Expand Down Expand Up @@ -232,7 +230,6 @@ impl Wal for WalFile {
}
}

#[cfg(feature = "fs")]
impl WalFile {
pub fn new(io: Arc<dyn IO>, wal_path: String, page_size: usize) -> Self {
Self {
Expand Down

0 comments on commit 87c80b2

Please sign in to comment.