use std::ffi::c_void;
use std::panic::{AssertUnwindSafe, catch_unwind};
use crate::{Connection, Error, Result, ffi};
type Callback = dyn Fn(DoltPushProgressEvent) + Send + Sync + 'static;
#[doc(hidden)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DoltPushProgressEvent {
Plan {
missing_chunk_count: u64,
missing_payload_bytes: u64,
},
Uploaded {
acknowledged_chunk_count: u64,
acknowledged_payload_bytes: u64,
},
}
struct CallbackState {
callback: Box<Callback>,
}
#[doc(hidden)]
#[must_use = "dropping the guard removes Dolt push progress notifications"]
pub struct DoltPushProgressGuard<'connection> {
connection: &'connection Connection,
context: *mut c_void,
state: Option<Box<CallbackState>>,
}
impl Drop for DoltPushProgressGuard<'_> {
fn drop(&mut self) {
let db = unsafe { self.connection.handle() };
let rc = unsafe { ffi::sqlite3_doltlite_clear_push_progress_callback(db, self.context) };
if rc != ffi::SQLITE_OK {
if let Some(state) = self.state.take() {
let _ = Box::into_raw(state);
}
}
}
}
impl Connection {
#[doc(hidden)]
pub fn dolt_push_progress_callback<F>(&self, callback: F) -> Result<DoltPushProgressGuard<'_>>
where
F: Fn(DoltPushProgressEvent) + Send + Sync + 'static,
{
let mut state = Box::new(CallbackState {
callback: Box::new(callback),
});
let context = (&mut *state as *mut CallbackState).cast::<c_void>();
let db = unsafe { self.handle() };
let rc = unsafe {
ffi::sqlite3_doltlite_set_push_progress_callback(db, context, Some(progress_trampoline))
};
if rc != ffi::SQLITE_OK {
return Err(Error::SqliteFailure(ffi::Error::new(rc), None));
}
Ok(DoltPushProgressGuard {
connection: self,
context,
state: Some(state),
})
}
}
unsafe extern "C" fn progress_trampoline(
context: *mut c_void,
event: std::ffi::c_int,
count: ffi::sqlite3_int64,
bytes: ffi::sqlite3_int64,
) {
if context.is_null() || count < 0 || bytes < 0 {
return;
}
let event = match event {
ffi::DOLTLITE_PUSH_PROGRESS_PLAN => DoltPushProgressEvent::Plan {
missing_chunk_count: count as u64,
missing_payload_bytes: bytes as u64,
},
ffi::DOLTLITE_PUSH_PROGRESS_UPLOADED => DoltPushProgressEvent::Uploaded {
acknowledged_chunk_count: count as u64,
acknowledged_payload_bytes: bytes as u64,
},
_ => return,
};
let _ = catch_unwind(AssertUnwindSafe(|| {
let state = unsafe { &*(context.cast::<CallbackState>()) };
(state.callback)(event);
}));
}
type CloneCallback = dyn Fn(DoltCloneProgressEvent) + Send + Sync + 'static;
struct CloneCallbackState {
callback: Box<CloneCallback>,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DoltCloneProgressEvent {
Started,
Progress {
downloaded_chunk_count: u64,
downloaded_payload_bytes: u64,
reused_chunk_count: u64,
reused_payload_bytes: u64,
},
Finalizing,
Completed,
}
#[must_use = "dropping the guard removes Dolt clone progress notifications"]
pub struct DoltCloneProgressGuard<'connection> {
connection: &'connection Connection,
context: *mut c_void,
state: Option<Box<CloneCallbackState>>,
}
impl Drop for DoltCloneProgressGuard<'_> {
fn drop(&mut self) {
let db = unsafe { self.connection.handle() };
let rc = unsafe { ffi::sqlite3_doltlite_clear_clone_progress_callback(db, self.context) };
if rc != ffi::SQLITE_OK {
if let Some(state) = self.state.take() {
let _ = Box::into_raw(state);
}
}
}
}
impl Connection {
pub fn dolt_clone_progress_callback<F>(&self, callback: F) -> Result<DoltCloneProgressGuard<'_>>
where
F: Fn(DoltCloneProgressEvent) + Send + Sync + 'static,
{
let mut state = Box::new(CloneCallbackState {
callback: Box::new(callback),
});
let context = (&mut *state as *mut CloneCallbackState).cast::<c_void>();
let db = unsafe { self.handle() };
let rc = unsafe {
ffi::sqlite3_doltlite_set_clone_progress_callback(
db,
context,
Some(clone_progress_trampoline),
)
};
if rc != ffi::SQLITE_OK {
return Err(Error::SqliteFailure(ffi::Error::new(rc), None));
}
Ok(DoltCloneProgressGuard {
connection: self,
context,
state: Some(state),
})
}
}
unsafe extern "C" fn clone_progress_trampoline(
context: *mut c_void,
event: std::ffi::c_int,
count: ffi::sqlite3_int64,
bytes: ffi::sqlite3_int64,
reused_count: ffi::sqlite3_int64,
reused_bytes: ffi::sqlite3_int64,
) {
if context.is_null() || count < 0 || bytes < 0 || reused_count < 0 || reused_bytes < 0 {
return;
}
let event = match event {
ffi::DOLTLITE_CLONE_PROGRESS_STARTED
if count == 0 && bytes == 0 && reused_count == 0 && reused_bytes == 0 =>
{
DoltCloneProgressEvent::Started
}
ffi::DOLTLITE_CLONE_PROGRESS_PROGRESS => DoltCloneProgressEvent::Progress {
downloaded_chunk_count: count as u64,
downloaded_payload_bytes: bytes as u64,
reused_chunk_count: reused_count as u64,
reused_payload_bytes: reused_bytes as u64,
},
ffi::DOLTLITE_CLONE_PROGRESS_FINALIZING
if count == 0 && bytes == 0 && reused_count == 0 && reused_bytes == 0 =>
{
DoltCloneProgressEvent::Finalizing
}
ffi::DOLTLITE_CLONE_PROGRESS_COMPLETED
if count == 0 && bytes == 0 && reused_count == 0 && reused_bytes == 0 =>
{
DoltCloneProgressEvent::Completed
}
_ => return,
};
let _ = catch_unwind(AssertUnwindSafe(|| {
let state = unsafe { &*(context.cast::<CloneCallbackState>()) };
(state.callback)(event);
}));
}