mirror of
https://github.com/aljazceru/turso.git
synced 2026-02-14 12:34:20 +01:00
Merge pull request #36 from penberg/readimpl
bindings: expose reading from the database
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
typedef enum {
|
||||
MVCC_OK = 0,
|
||||
MVCC_IO_ERROR_READ = 266,
|
||||
MVCC_IO_ERROR_WRITE = 778,
|
||||
} MVCCError;
|
||||
|
||||
@@ -22,6 +23,10 @@ void MVCCDatabaseClose(MVCCDatabaseRef db);
|
||||
|
||||
MVCCError MVCCDatabaseInsert(MVCCDatabaseRef db, uint64_t id, const void *value_ptr, uintptr_t value_len);
|
||||
|
||||
MVCCError MVCCDatabaseRead(MVCCDatabaseRef db, uint64_t id, char **value_ptr, int64_t *value_len);
|
||||
|
||||
void MVCCFreeStr(void *ptr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif // __cplusplus
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#[repr(C)]
|
||||
pub enum MVCCError {
|
||||
MVCC_OK = 0,
|
||||
MVCC_IO_ERROR_READ = 266,
|
||||
MVCC_IO_ERROR_WRITE = 778,
|
||||
}
|
||||
|
||||
@@ -93,3 +93,53 @@ pub unsafe extern "C" fn MVCCDatabaseInsert(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn MVCCDatabaseRead(
|
||||
db: MVCCDatabaseRef,
|
||||
id: u64,
|
||||
value_ptr: *mut *mut std::ffi::c_char,
|
||||
value_len: *mut i64,
|
||||
) -> MVCCError {
|
||||
let db = db.get_ref();
|
||||
let (db, runtime) = (&db.db, &db.runtime);
|
||||
|
||||
match runtime.block_on(async move {
|
||||
let tx = db.begin_tx().await;
|
||||
let maybe_row = db.read(tx, id).await?;
|
||||
match maybe_row {
|
||||
Some(row) => {
|
||||
tracing::debug!("Found row {row:?}");
|
||||
let str_len = row.data.len() + 1;
|
||||
let value = std::ffi::CString::new(row.data.as_bytes()).map_err(|e| {
|
||||
mvcc_rs::errors::DatabaseError::Io(format!(
|
||||
"Failed to transform read data into CString: {e}"
|
||||
))
|
||||
})?;
|
||||
unsafe {
|
||||
*value_ptr = value.into_raw();
|
||||
*value_len = str_len as i64;
|
||||
}
|
||||
}
|
||||
None => unsafe { *value_len = -1 },
|
||||
};
|
||||
Ok::<(), mvcc_rs::errors::DatabaseError>(())
|
||||
}) {
|
||||
Ok(_) => {
|
||||
tracing::debug!("MVCCDatabaseRead: success");
|
||||
MVCCError::MVCC_OK
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("MVCCDatabaseRead: {e}");
|
||||
MVCCError::MVCC_IO_ERROR_READ
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn MVCCFreeStr(ptr: *mut std::ffi::c_void) {
|
||||
if ptr.is_null() {
|
||||
return;
|
||||
}
|
||||
let _ = std::ffi::CString::from_raw(ptr as *mut std::ffi::c_char);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user