Skip to content

Commit

Permalink
libsql: Wire up xCheckpointSeqCount
Browse files Browse the repository at this point in the history
  • Loading branch information
penberg committed Dec 11, 2024
1 parent e9e0405 commit bb7f011
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ impl Wal for InjectorWal {
self.inner.savepoint_undo(rollback_data)
}

fn checkpoint_seq_count(&self) -> Result<u32> {
self.inner.checkpoint_seq_count()
}

fn frame_count(&self, locked: i32) -> Result<u32> {
self.inner.frame_count(locked)
}
Expand Down
4 changes: 4 additions & 0 deletions libsql-sqlite3/test/rust_suite/src/virtual_wal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ mod tests {
self.0.read_frame_raw(page_no, buffer)
}

fn checkpoint_seq_count(&self) -> libsql_sys::wal::Result<u32> {
self.0.checkpoint_seq_count()
}

fn frame_count(&self, locked: i32) -> libsql_sys::wal::Result<u32> {
self.0.frame_count(locked)
}
Expand Down
4 changes: 4 additions & 0 deletions libsql-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ impl Wal for DurableWal {
todo!()
}

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

fn frame_count(&self, _locked: i32) -> Result<u32> {
todo!()
}
Expand Down
6 changes: 6 additions & 0 deletions libsql-sys/src/wal/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ macro_rules! create_either {
}
}

fn checkpoint_seq_count(&self) -> super::Result<u32> {
match self {
$( $name::$t(inner) => inner.checkpoint_seq_count() ),*
}
}

fn frame_count(&self, locked: i32) -> super::Result<u32> {
match self {
$( $name::$t(inner) => inner.frame_count(locked) ),*
Expand Down
19 changes: 19 additions & 0 deletions libsql-sys/src/wal/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub(crate) fn construct_libsql_wal<W: Wal>(wal: *mut W) -> libsql_wal {
xUndo: Some(undo::<W>),
xSavepoint: Some(savepoint::<W>),
xSavepointUndo: Some(savepoint_undo::<W>),
xCheckpointSeqCount: Some(checkpoint_seq_count::<W>),
xFrameCount: Some(frame_count::<W>),
xFrames: Some(frames::<W>),
xCheckpoint: Some(checkpoint::<W>),
Expand Down Expand Up @@ -295,6 +296,24 @@ pub unsafe extern "C" fn savepoint_undo<T: Wal>(wal: *mut wal_impl, wal_data: *m
}
}

pub unsafe extern "C" fn checkpoint_seq_count<T: Wal>(
wal: *mut wal_impl,
out: *mut c_uint,
) -> c_int {
let this = &mut (*(wal as *mut T));
match this.checkpoint_seq_count() {
Ok(n) => {
if !out.is_null() {
unsafe {
*out = n as _;
}
}
SQLITE_OK
}
Err(code) => code.extended_code,
}
}

pub unsafe extern "C" fn frame_count<T: Wal>(
wal: *mut wal_impl,
locked: i32,
Expand Down
2 changes: 2 additions & 0 deletions libsql-sys/src/wal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ pub trait Wal {
fn savepoint(&mut self, rollback_data: &mut [u32]);
fn savepoint_undo(&mut self, rollback_data: &mut [u32]) -> Result<()>;

fn checkpoint_seq_count(&self) -> Result<u32>;

fn frame_count(&self, locked: i32) -> Result<u32>;

/// Insert frames in the wal. On commit, returns the number of inserted frames for that
Expand Down
12 changes: 12 additions & 0 deletions libsql-sys/src/wal/sqlite3_wal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,18 @@ impl Wal for Sqlite3Wal {
}
}

fn checkpoint_seq_count(&self) -> Result<u32> {
let mut out: u32 = 0;
let rc = unsafe {
(self.inner.methods.xCheckpointSeqCount.unwrap())(self.inner.pData, &mut out)
};
if rc != 0 {
Err(Error::new(rc))
} else {
Ok(out)
}
}

fn frame_count(&self, locked: i32) -> Result<u32> {
let mut out: u32 = 0;
let rc = unsafe {
Expand Down
12 changes: 12 additions & 0 deletions libsql-sys/src/wal/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ impl<T: WrapWal<W>, W: Wal> Wal for WalRef<T, W> {
unsafe { (*self.wrapper).savepoint_undo(&mut *self.wrapped, rollback_data) }
}

fn checkpoint_seq_count(&self) -> super::Result<u32> {
unsafe { (*self.wrapper).checkpoint_seq_count(&*self.wrapped) }
}

fn frame_count(&self, locked: i32) -> super::Result<u32> {
unsafe { (*self.wrapper).frame_count(&*self.wrapped, locked) }
}
Expand Down Expand Up @@ -272,6 +276,10 @@ where
.savepoint_undo(&mut self.wrapped, rollback_data)
}

fn checkpoint_seq_count(&self) -> super::Result<u32> {
self.wrapper.checkpoint_seq_count(&self.wrapped)
}

fn frame_count(&self, locked: i32) -> super::Result<u32> {
self.wrapper.frame_count(&self.wrapped, locked)
}
Expand Down Expand Up @@ -409,6 +417,10 @@ pub trait WrapWal<W: Wal> {
wrapped.savepoint_undo(rollback_data)
}

fn checkpoint_seq_count(&self, wrapped: &W) -> super::Result<u32> {
wrapped.checkpoint_seq_count()
}

fn frame_count(&self, wrapped: &W, locked: i32) -> super::Result<u32> {
wrapped.frame_count(locked)
}
Expand Down
5 changes: 5 additions & 0 deletions libsql-wal/src/wal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ where
}
}

#[tracing::instrument(skip_all, fields(id = self.conn_id))]
fn checkpoint_seq_count(&self) -> libsql_sys::wal::Result<u32> {
Err(libsql_sys::wal::Error::new(10)) // SQLITE_IOERR
}

#[tracing::instrument(skip_all, fields(id = self.conn_id))]
fn frame_count(&self, _locked: i32) -> libsql_sys::wal::Result<u32> {
Err(libsql_sys::wal::Error::new(10)) // SQLITE_IOERR
Expand Down

0 comments on commit bb7f011

Please sign in to comment.