add destroy function to vtab

This commit is contained in:
pedrocarlo 2025-04-08 21:29:31 -03:00
parent c0747e8064
commit 2181de79de
4 changed files with 48 additions and 2 deletions

View file

@ -754,6 +754,19 @@ impl VirtualTable {
_ => Err(LimboError::ExtensionError(rc.to_string())),
}
}
pub fn destroy(&self) -> Result<()> {
let implementation = self.implementation.as_ref();
let rc = unsafe {
(self.implementation.destroy)(
implementation as *const VTabModuleImpl as *const std::ffi::c_void,
)
};
match rc {
ResultCode::OK => Ok(()),
_ => Err(LimboError::ExtensionError(rc.to_string())),
}
}
}
pub(crate) struct SymbolTable {

View file

@ -1151,7 +1151,12 @@ pub fn op_vdestroy(
};
{
conn.syms.borrow_mut().vtabs.remove(table_name);
let Some(vtab) = conn.syms.borrow_mut().vtabs.remove(table_name) else {
return Err(crate::LimboError::InternalError(
"Could not find Virtual Table to Destroy".to_string(),
));
};
vtab.destroy()?;
}
state.pc += 1;

View file

@ -21,6 +21,7 @@ pub struct VTabModuleImpl {
pub eof: VtabFnEof,
pub update: VtabFnUpdate,
pub rowid: VtabRowIDFn,
pub destroy: VtabFnDestroy,
}
#[cfg(feature = "core_only")]
@ -60,6 +61,8 @@ pub type VtabFnUpdate = unsafe extern "C" fn(
p_out_rowid: *mut i64,
) -> ResultCode;
pub type VtabFnDestroy = unsafe extern "C" fn(vtab: *const c_void) -> ResultCode;
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum VTabKind {
@ -88,6 +91,9 @@ pub trait VTabModule: 'static {
fn delete(&mut self, _rowid: i64) -> Result<(), Self::Error> {
Ok(())
}
fn destroy(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}
pub trait VTabCursor: Sized {

View file

@ -404,7 +404,11 @@ pub fn derive_agg_func(input: TokenStream) -> TokenStream {
/// /// Delete the row with the provided rowid
/// fn delete(&mut self, rowid: i64) -> Result<(), Self::Error> {
/// Ok(())
/// }
/// }
/// /// Destroy the virtual table. Any cleanup logic for when the table is deleted comes heres
/// fn destroy(&mut self) -> Result<(), Self::Error> {
/// Ok(())
/// }
///
/// #[derive(Debug)]
/// struct CsvCursor {
@ -450,6 +454,7 @@ pub fn derive_vtab_module(input: TokenStream) -> TokenStream {
let eof_fn_name = format_ident!("eof_{}", struct_name);
let update_fn_name = format_ident!("update_{}", struct_name);
let rowid_fn_name = format_ident!("rowid_{}", struct_name);
let destroy_fn_name = format_ident!("destroy_{}", struct_name);
let expanded = quote! {
impl #struct_name {
@ -592,6 +597,22 @@ pub fn derive_vtab_module(input: TokenStream) -> TokenStream {
<<#struct_name as ::limbo_ext::VTabModule>::VCursor as ::limbo_ext::VTabCursor>::rowid(cursor)
}
#[no_mangle]
unsafe extern "C" fn #destroy_fn_name(
vtab: *const ::std::ffi::c_void,
) -> ::limbo_ext::ResultCode {
if vtab.is_null() {
return ::limbo_ext::ResultCode::Error;
}
let vtab = &mut *(vtab as *mut #struct_name);
if <#struct_name as VTabModule>::destroy(vtab).is_err() {
return ::limbo_ext::ResultCode::Error;
}
return ::limbo_ext::ResultCode::OK;
}
#[no_mangle]
pub unsafe extern "C" fn #register_fn_name(
api: *const ::limbo_ext::ExtensionApi
@ -614,6 +635,7 @@ pub fn derive_vtab_module(input: TokenStream) -> TokenStream {
eof: Self::#eof_fn_name,
update: Self::#update_fn_name,
rowid: Self::#rowid_fn_name,
destroy: Self::#destroy_fn_name,
};
(api.register_vtab_module)(api.ctx, name_c, module, <#struct_name as ::limbo_ext::VTabModule>::VTAB_KIND)
}