Add diff chkpt modes to sqlite3 api, finish checkpoint logic and add tests

This commit is contained in:
PThorpe92
2025-07-19 19:02:06 -04:00
committed by Jussi Saurio
parent eaa6f99fa8
commit b214c3dfc8
2 changed files with 344 additions and 43 deletions

View File

@@ -1109,15 +1109,14 @@ pub unsafe extern "C" fn sqlite3_wal_checkpoint_v2(
db: *mut sqlite3,
_db_name: *const ffi::c_char,
mode: ffi::c_int,
_log_size: *mut ffi::c_int,
_checkpoint_count: *mut ffi::c_int,
log_size: *mut ffi::c_int,
checkpoint_count: *mut ffi::c_int,
) -> ffi::c_int {
if db.is_null() {
return SQLITE_MISUSE;
}
let db: &mut sqlite3 = &mut *db;
let db = db.inner.lock().unwrap();
// TODO: Checkpointing modes and reporting back log size and checkpoint count to caller.
let chkptmode = match mode {
SQLITE_CHECKPOINT_PASSIVE => CheckpointMode::Passive,
SQLITE_CHECKPOINT_RESTART => CheckpointMode::Restart,
@@ -1125,10 +1124,25 @@ pub unsafe extern "C" fn sqlite3_wal_checkpoint_v2(
SQLITE_CHECKPOINT_FULL => CheckpointMode::Full,
_ => return SQLITE_MISUSE, // Unsupported mode
};
if db.conn.checkpoint(chkptmode).is_err() {
return SQLITE_ERROR;
match db.conn.checkpoint(chkptmode) {
Ok(res) => {
if !log_size.is_null() {
(*log_size) = res.num_wal_frames as ffi::c_int;
}
if !checkpoint_count.is_null() {
(*checkpoint_count) = res.num_checkpointed_frames as ffi::c_int;
}
SQLITE_OK
}
Err(e) => {
println!("Checkpoint error: {e}");
if matches!(e, turso_core::LimboError::Busy) {
SQLITE_BUSY
} else {
SQLITE_ERROR
}
}
}
SQLITE_OK
}
/// Get the number of frames in the WAL.