mirror of
https://github.com/tursodatabase/limbo.git
synced 2025-08-04 18:18:03 +00:00
Improve generated C bindings
Before: ```c typedef LocalClock Clock; typedef JsonOnDisk Storage; typedef DatabaseInner<Clock, Storage> Inner; typedef Database<Clock, Storage, Mutex<Inner>> Db; typedef struct { Db db; Runtime runtime; } DbContext; extern "C" { DbContext *mvccrs_new_database(const char *path); void mvccrs_free_database(Db *db); int32_t mvccrs_insert(DbContext *db, uint64_t id, const uint8_t *value_ptr, uintptr_t value_len); } // extern "C" ``` After: ```c typedef struct DbContext DbContext; typedef const DbContext *MVCCDatabaseRef; extern "C" { MVCCDatabaseRef MVCCDatabaseOpen(const char *path); void MVCCDatabaseClose(MVCCDatabaseRef db); int32_t MVCCDatabaseInsert(MVCCDatabaseRef db, uint64_t id, const uint8_t *value_ptr, uintptr_t value_len); } // extern "C" ```
This commit is contained in:
parent
124446f17c
commit
34a4f1a269
2 changed files with 78 additions and 19 deletions
|
@ -1,25 +1,31 @@
|
|||
#![allow(non_camel_case_types)]
|
||||
|
||||
mod types;
|
||||
|
||||
use types::{MVCCDatabaseRef, DbContext};
|
||||
use mvcc_rs::*;
|
||||
|
||||
/// cbindgen:ignore
|
||||
type Clock = clock::LocalClock;
|
||||
|
||||
/// cbindgen:ignore
|
||||
type Storage = persistent_storage::JsonOnDisk;
|
||||
|
||||
/// cbindgen:ignore
|
||||
type Inner = database::DatabaseInner<Clock, Storage>;
|
||||
|
||||
/// cbindgen:ignore
|
||||
type Db = database::Database<Clock, Storage, tokio::sync::Mutex<Inner>>;
|
||||
|
||||
static INIT_RUST_LOG: std::sync::Once = std::sync::Once::new();
|
||||
|
||||
#[repr(C)]
|
||||
pub struct DbContext {
|
||||
db: Db,
|
||||
runtime: tokio::runtime::Runtime,
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn mvccrs_new_database(path: *const std::ffi::c_char) -> *mut DbContext {
|
||||
pub extern "C" fn MVCCDatabaseOpen(path: *const std::ffi::c_char) -> MVCCDatabaseRef {
|
||||
INIT_RUST_LOG.call_once(|| {
|
||||
tracing_subscriber::fmt::init();
|
||||
});
|
||||
|
||||
tracing::debug!("mvccrs_new_database");
|
||||
tracing::debug!("MVCCDatabaseOpen");
|
||||
|
||||
let clock = clock::LocalClock::new();
|
||||
let path = unsafe { std::ffi::CStr::from_ptr(path) };
|
||||
|
@ -27,29 +33,35 @@ pub extern "C" fn mvccrs_new_database(path: *const std::ffi::c_char) -> *mut DbC
|
|||
Ok(path) => path,
|
||||
Err(_) => {
|
||||
tracing::error!("Invalid UTF-8 path");
|
||||
return std::ptr::null_mut();
|
||||
return MVCCDatabaseRef::null();
|
||||
}
|
||||
};
|
||||
tracing::debug!("mvccrs: opening persistent storage at {path}");
|
||||
let storage = crate::persistent_storage::JsonOnDisk::new(path);
|
||||
let db = Db::new(clock, storage);
|
||||
let runtime = tokio::runtime::Runtime::new().unwrap();
|
||||
Box::into_raw(Box::new(DbContext { db, runtime }))
|
||||
let ctx = DbContext { db, runtime };
|
||||
let ctx = Box::leak(Box::new(ctx));
|
||||
MVCCDatabaseRef::from(ctx)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn mvccrs_free_database(db: *mut Db) {
|
||||
tracing::debug!("mvccrs_free_database");
|
||||
let _ = Box::from_raw(db);
|
||||
pub unsafe extern "C" fn MVCCDatabaseClose(db: MVCCDatabaseRef) {
|
||||
tracing::debug!("MVCCDatabaseClose");
|
||||
if db.is_null() {
|
||||
return;
|
||||
}
|
||||
let _ = unsafe { Box::from_raw(db.get_ref_mut()) };
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn mvccrs_insert(
|
||||
db: *mut DbContext,
|
||||
pub unsafe extern "C" fn MVCCDatabaseInsert(
|
||||
db: MVCCDatabaseRef,
|
||||
id: u64,
|
||||
value_ptr: *const u8,
|
||||
value_len: usize,
|
||||
) -> i32 {
|
||||
let db = db.get_ref();
|
||||
let value = std::slice::from_raw_parts(value_ptr, value_len);
|
||||
let data = match std::str::from_utf8(value) {
|
||||
Ok(value) => value.to_string(),
|
||||
|
@ -59,20 +71,20 @@ pub unsafe extern "C" fn mvccrs_insert(
|
|||
general_purpose::STANDARD.encode(value)
|
||||
}
|
||||
};
|
||||
let DbContext { db, runtime } = unsafe { &mut *db };
|
||||
let (db, runtime) = (&db.db, &db.runtime);
|
||||
let row = database::Row { id, data };
|
||||
tracing::debug!("mvccrs_insert: {row:?}");
|
||||
tracing::debug!("MVCCDatabaseInsert: {row:?}");
|
||||
match runtime.block_on(async move {
|
||||
let tx = db.begin_tx().await;
|
||||
db.insert(tx, row).await?;
|
||||
db.commit_tx(tx).await
|
||||
}) {
|
||||
Ok(_) => {
|
||||
tracing::debug!("mvccrs_insert: success");
|
||||
tracing::debug!("MVCCDatabaseInsert: success");
|
||||
0 // SQLITE_OK
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("mvccrs_insert: {e}");
|
||||
tracing::error!("MVCCDatabaseInsert: {e}");
|
||||
778 // SQLITE_IOERR_WRITE
|
||||
}
|
||||
}
|
||||
|
|
47
core/mvcc/bindings/c/src/types.rs
Normal file
47
core/mvcc/bindings/c/src/types.rs
Normal file
|
@ -0,0 +1,47 @@
|
|||
use crate::Db;
|
||||
|
||||
#[repr(transparent)]
|
||||
pub struct MVCCDatabaseRef {
|
||||
ptr: *const DbContext,
|
||||
}
|
||||
|
||||
impl MVCCDatabaseRef {
|
||||
pub fn null() -> MVCCDatabaseRef {
|
||||
MVCCDatabaseRef {
|
||||
ptr: std::ptr::null(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_null(&self) -> bool {
|
||||
self.ptr.is_null()
|
||||
}
|
||||
|
||||
pub fn get_ref(&self) -> &DbContext {
|
||||
unsafe { &*(self.ptr) }
|
||||
}
|
||||
|
||||
#[allow(clippy::mut_from_ref)]
|
||||
pub fn get_ref_mut(&self) -> &mut DbContext {
|
||||
let ptr_mut = self.ptr as *mut DbContext;
|
||||
unsafe { &mut (*ptr_mut) }
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::from_over_into)]
|
||||
impl From<&DbContext> for MVCCDatabaseRef {
|
||||
fn from(value: &DbContext) -> Self {
|
||||
Self { ptr: value }
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::from_over_into)]
|
||||
impl From<&mut DbContext> for MVCCDatabaseRef {
|
||||
fn from(value: &mut DbContext) -> Self {
|
||||
Self { ptr: value }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DbContext {
|
||||
pub(crate) db: Db,
|
||||
pub(crate) runtime: tokio::runtime::Runtime,
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue