Skip to content

Commit

Permalink
add scaffolding for supporting wal checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
sonhmai authored and sonhmai committed Jan 15, 2025
1 parent 256c0d4 commit 807e0f4
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 0 deletions.
100 changes: 100 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ miette = { version = "7.4.0", features = ["fancy"] }

[features]
io_uring = ["limbo_core/io_uring"]

[dev-dependencies]
assert_cmd = "^2"
predicates = "^3"

# rexpect does not support windows https://github.com/rust-cli/rexpect/issues/11
#[target.'cfg(not(windows))'.dev-dependencies]
rexpect = "0.6.0"
35 changes: 35 additions & 0 deletions cli/tests/test_journal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use assert_cmd::cargo::cargo_bin;
use rexpect::error::*;
use rexpect::session::spawn_command;
use std::process;

#[test]
fn test_pragma_journal_mode_wal() -> Result<(), Error> {
let mut child = spawn_command(run_cli(), Some(1000))?;
child.exp_regex("limbo>")?; // skip everything until limbo cursor appear
child.exp_regex(".?")?;
child.send_line("pragma journal_mode;")?;
child.exp_string("wal")?;
child.send_line(".quit")?;
child.exp_eof()?;
Ok(())
}

#[ignore = "wal checkpoint not yet implemented"]
#[test]
fn test_pragma_wal_checkpoint() -> Result<(), Error> {
let mut child = spawn_command(run_cli(), Some(1000))?;
child.exp_regex("limbo>")?; // skip everything until limbo cursor appear
child.exp_regex(".?")?;
child.send_line("pragma wal_checkpoint;")?;
child.exp_string("0|0|0")?;
child.send_line(".quit")?;
child.exp_eof()?;
Ok(())
}

fn run_cli() -> process::Command {
let bin_path = cargo_bin("limbo");
let mut cmd = process::Command::new(bin_path);
cmd
}
10 changes: 10 additions & 0 deletions core/translate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,10 @@ fn update_pragma(
query_pragma("journal_mode", header, program)?;
Ok(())
}
PragmaName::WalCheckpoint => {
// TODO
Ok(())
}
}
}

Expand All @@ -620,6 +624,12 @@ fn query_pragma(
dest: register,
});
}
PragmaName::WalCheckpoint => {
program.emit_insn(Insn::Checkpoint {
reg: 12, // TODO fix hard-coded
dest: register,
});
}
}

program.emit_insn(Insn::ResultRow {
Expand Down
9 changes: 9 additions & 0 deletions core/vdbe/explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ pub fn insn_to_str(
0,
format!("r[{}]=~r[{}]", dest, reg),
),
Insn::Checkpoint { reg, dest } => (
"Checkpoint",
*reg as i32,
*dest as i32,
0,
OwnedValue::build_text(Rc::new("".to_string())),
0,
format!("r[{}]=~r[{}]", dest, reg),
),
Insn::Remainder { lhs, rhs, dest } => (
"Remainder",
*lhs as i32,
Expand Down
6 changes: 6 additions & 0 deletions core/vdbe/insn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ pub enum Insn {
reg: usize,
dest: usize,
},
// Checkpoint the database (applying wal file content to database file).
Checkpoint {
// TODO support registers as in sqlite
reg: usize,
dest: usize,
},
// Divide lhs by rhs and place the remainder in dest register.
Remainder {
lhs: usize,
Expand Down
6 changes: 6 additions & 0 deletions core/vdbe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,12 @@ impl Program {
state.registers[*dest] = exec_bit_not(&state.registers[*reg]);
state.pc += 1;
}
Insn::Checkpoint { reg: _, dest } => {
// Write 1 (checkpoint SQLITE_BUSY) or 0 (not busy).
// fixme currently hard coded not implemented
state.registers[*dest] = OwnedValue::Integer(0);
state.pc += 1;
}
Insn::Null { dest, dest_end } => {
if let Some(dest_end) = dest_end {
for i in *dest..=*dest_end {
Expand Down
3 changes: 3 additions & 0 deletions vendored/sqlite3-parser/src/parser/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,8 @@ pub enum PragmaName {
CacheSize,
/// `journal_mode` pragma
JournalMode,
/// trigger a checkpoint to run on database(s) if WAL is enabled
WalCheckpoint,
}

impl FromStr for PragmaName {
Expand All @@ -1599,6 +1601,7 @@ impl FromStr for PragmaName {
fn from_str(input: &str) -> Result<Self, Self::Err> {
match input {
"cache_size" => Ok(PragmaName::CacheSize),
"wal_checkpoint" => Ok(PragmaName::WalCheckpoint),
"journal_mode" => Ok(PragmaName::JournalMode),
_ => Err(()),
}
Expand Down

0 comments on commit 807e0f4

Please sign in to comment.