-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rough design for extension api/draft extension
- Loading branch information
Showing
18 changed files
with
489 additions
and
348 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,41 @@ | ||
#[cfg(feature = "uuid")] | ||
mod uuid; | ||
use crate::{function::ExternalFunc, Database}; | ||
use std::sync::Arc; | ||
|
||
use extension_api::{AggregateFunction, ExtensionApi, Result, ScalarFunction, VirtualTable}; | ||
#[cfg(feature = "uuid")] | ||
pub use uuid::{exec_ts_from_uuid7, exec_uuid, exec_uuidblob, exec_uuidstr, UuidFunc}; | ||
|
||
impl ExtensionApi for Database { | ||
fn register_scalar_function( | ||
&self, | ||
name: &str, | ||
func: Arc<dyn ScalarFunction>, | ||
) -> extension_api::Result<()> { | ||
let ext_func = ExternalFunc::new(name, func.clone()); | ||
self.syms | ||
.borrow_mut() | ||
.functions | ||
.insert(name.to_string(), Arc::new(ext_func)); | ||
Ok(()) | ||
} | ||
|
||
fn register_aggregate_function( | ||
&self, | ||
_name: &str, | ||
_func: Arc<dyn AggregateFunction>, | ||
) -> Result<()> { | ||
todo!("implement aggregate function registration"); | ||
} | ||
|
||
fn register_virtual_table(&self, _name: &str, _table: Arc<dyn VirtualTable>) -> Result<()> { | ||
todo!("implement virtual table registration"); | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq)] | ||
pub enum ExtFunc { | ||
#[cfg(feature = "uuid")] | ||
Uuid(UuidFunc), | ||
use limbo_extension::{ExtensionApi, ResultCode, ScalarFunction, RESULT_ERROR, RESULT_OK}; | ||
pub use limbo_extension::{Value as ExtValue, ValueType as ExtValueType}; | ||
use std::{ | ||
ffi::{c_char, c_void, CStr}, | ||
rc::Rc, | ||
}; | ||
|
||
extern "C" fn register_scalar_function( | ||
ctx: *mut c_void, | ||
name: *const c_char, | ||
func: ScalarFunction, | ||
) -> ResultCode { | ||
let c_str = unsafe { CStr::from_ptr(name) }; | ||
let name_str = match c_str.to_str() { | ||
Ok(s) => s.to_string(), | ||
Err(_) => return RESULT_ERROR, | ||
}; | ||
let db = unsafe { &*(ctx as *const Database) }; | ||
db.register_scalar_function_impl(name_str, func) | ||
} | ||
|
||
#[allow(unreachable_patterns)] // TODO: remove when more extension funcs added | ||
impl std::fmt::Display for ExtFunc { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
#[cfg(feature = "uuid")] | ||
Self::Uuid(uuidfn) => write!(f, "{}", uuidfn), | ||
_ => write!(f, "unknown"), | ||
} | ||
impl Database { | ||
fn register_scalar_function_impl(&self, name: String, func: ScalarFunction) -> ResultCode { | ||
self.syms.borrow_mut().functions.insert( | ||
name.to_string(), | ||
Rc::new(ExternalFunc { | ||
name: name.to_string(), | ||
func, | ||
}), | ||
); | ||
RESULT_OK | ||
} | ||
} | ||
|
||
#[allow(unreachable_patterns)] | ||
impl ExtFunc { | ||
pub fn resolve_function(name: &str, num_args: usize) -> Option<ExtFunc> { | ||
match name { | ||
#[cfg(feature = "uuid")] | ||
name => UuidFunc::resolve_function(name, num_args), | ||
_ => None, | ||
pub fn build_limbo_extension(&self) -> ExtensionApi { | ||
ExtensionApi { | ||
ctx: self as *const _ as *mut c_void, | ||
register_scalar_function, | ||
} | ||
} | ||
} | ||
|
||
//pub fn init(db: &mut crate::Database) { | ||
// #[cfg(feature = "uuid")] | ||
// uuid::init(db); | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.