Skip to content

Commit aaaa6dc

Browse files
committed
core: allocate 2 registers for checkpoint opcode execution
1 parent df3850a commit aaaa6dc

File tree

5 files changed

+46
-24
lines changed

5 files changed

+46
-24
lines changed

cli/tests/test_journal.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
mod tests {
55
use assert_cmd::cargo::cargo_bin;
66
use rexpect::error::*;
7-
use rexpect::session::spawn_command;
7+
use rexpect::session::{spawn_command, PtySession};
88
use std::process;
99

1010
#[test]
@@ -14,9 +14,7 @@ mod tests {
1414
child.exp_regex(".?")?;
1515
child.send_line("pragma journal_mode;")?;
1616
child.exp_string("wal")?;
17-
child.send_line(".quit")?;
18-
child.exp_eof()?;
19-
Ok(())
17+
quit(&mut child)
2018
}
2119

2220
#[test]
@@ -26,14 +24,18 @@ mod tests {
2624
child.exp_regex(".?")?;
2725
child.send_line("pragma wal_checkpoint;")?;
2826
child.exp_string("0|0|0")?;
27+
quit(&mut child)
28+
}
29+
30+
fn quit(child: &mut PtySession) -> Result<(), Error> {
2931
child.send_line(".quit")?;
3032
child.exp_eof()?;
3133
Ok(())
3234
}
3335

3436
fn run_cli() -> process::Command {
3537
let bin_path = cargo_bin("limbo");
36-
let mut cmd = process::Command::new(bin_path);
38+
let cmd = process::Command::new(bin_path);
3739
cmd
3840
}
3941
}

core/translate/mod.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub(crate) mod subquery;
2525
use crate::schema::Schema;
2626
use crate::storage::pager::Pager;
2727
use crate::storage::sqlite3_ondisk::{DatabaseHeader, MIN_PAGE_CACHE_SIZE};
28+
use crate::storage::wal::CheckpointMode;
2829
use crate::translate::delete::translate_delete;
2930
use crate::util::PRIMARY_KEY_AUTOMATIC_INDEX_NAME_PREFIX;
3031
use crate::vdbe::builder::CursorType;
@@ -37,7 +38,6 @@ use std::cell::RefCell;
3738
use std::fmt::Display;
3839
use std::rc::{Rc, Weak};
3940
use std::str::FromStr;
40-
use crate::storage::wal::CheckpointMode;
4141

4242
/// Translate SQL statement into bytecode program.
4343
pub fn translate(
@@ -619,26 +619,38 @@ fn query_pragma(
619619
value: database_header.borrow().default_page_cache_size.into(),
620620
dest: register,
621621
});
622+
program.emit_insn(Insn::ResultRow {
623+
start_reg: register,
624+
count: 1,
625+
});
622626
}
623627
PragmaName::JournalMode => {
624628
program.emit_insn(Insn::String8 {
625629
value: "wal".into(),
626630
dest: register,
627631
});
632+
program.emit_insn(Insn::ResultRow {
633+
start_reg: register,
634+
count: 1,
635+
});
628636
}
629637
PragmaName::WalCheckpoint => {
638+
// Checkpoint uses 3 registers: P1, P2, P3. Insn::Checkpoint for more info.
639+
// Allocate two more here as one was allocated at the top.
640+
program.alloc_register();
641+
program.alloc_register();
630642
program.emit_insn(Insn::Checkpoint {
631643
database: 0,
632644
checkpoint_mode: CheckpointMode::Passive,
633645
dest: register,
634646
});
647+
program.emit_insn(Insn::ResultRow {
648+
start_reg: register,
649+
count: 3,
650+
});
635651
}
636652
}
637653

638-
program.emit_insn(Insn::ResultRow {
639-
start_reg: register,
640-
count: 1,
641-
});
642654
Ok(())
643655
}
644656

core/vdbe/explain.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::vdbe::builder::CursorType;
22

33
use super::{Insn, InsnReference, OwnedValue, Program};
44
use std::rc::Rc;
5-
use crate::storage::wal::CheckpointMode;
65

76
pub fn insn_to_str(
87
program: &Program,
@@ -85,7 +84,11 @@ pub fn insn_to_str(
8584
0,
8685
format!("r[{}]=~r[{}]", dest, reg),
8786
),
88-
Insn::Checkpoint { database, checkpoint_mode: _, dest } => (
87+
Insn::Checkpoint {
88+
database,
89+
checkpoint_mode: _,
90+
dest,
91+
} => (
8992
"Checkpoint",
9093
*database as i32,
9194
*dest as i32,

core/vdbe/insn.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::num::NonZero;
22

33
use super::{AggFunc, BranchOffset, CursorID, FuncCtx, PageIdx};
4+
use crate::storage::wal::CheckpointMode;
45
use crate::types::{OwnedRecord, OwnedValue};
56
use limbo_macros::Description;
6-
use crate::storage::wal::CheckpointMode;
77

88
#[derive(Description, Debug)]
99
pub enum Insn {
@@ -69,9 +69,9 @@ pub enum Insn {
6969
},
7070
// Checkpoint the database (applying wal file content to database file).
7171
Checkpoint {
72-
database: usize, // checkpoint database P1
72+
database: usize, // checkpoint database P1
7373
checkpoint_mode: CheckpointMode, // P2 checkpoint mode
74-
dest: usize, // P3 checkpoint result
74+
dest: usize, // P3 checkpoint result
7575
},
7676
// Divide lhs by rhs and place the remainder in dest register.
7777
Remainder {

core/vdbe/mod.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -360,16 +360,21 @@ impl Program {
360360
state.registers[*dest] = exec_bit_not(&state.registers[*reg]);
361361
state.pc += 1;
362362
}
363-
Insn::Checkpoint { database: _, checkpoint_mode: _, dest } => {
364-
// Write 1 (checkpoint SQLITE_BUSY) or 0 (not busy).
365-
// fixme currently hard coded not implemented
366-
let result = self.connection
367-
.upgrade()
368-
.unwrap()
369-
.checkpoint();
363+
Insn::Checkpoint {
364+
database: _,
365+
checkpoint_mode: _,
366+
dest,
367+
} => {
368+
let result = self.connection.upgrade().unwrap().checkpoint();
370369
match result {
371-
Ok(()) => state.registers[*dest] = OwnedValue::Integer(0),
372-
Err(err) => state.registers[*dest] = OwnedValue::Integer(1)
370+
Ok(()) => {
371+
// fixme currently checkpoint result are hard-coded
372+
// 1st register: 1 (checkpoint SQLITE_BUSY) or 0 (not busy).
373+
state.registers[*dest] = OwnedValue::Integer(0);
374+
state.registers[*dest + 1] = OwnedValue::Integer(0);
375+
state.registers[*dest + 2] = OwnedValue::Integer(0);
376+
}
377+
Err(_err) => state.registers[*dest] = OwnedValue::Integer(1),
373378
}
374379

375380
state.pc += 1;

0 commit comments

Comments
 (0)