bindings: expose reading from the database

The results are returned as a CString for now.
This commit is contained in:
Piotr Sarna
2023-05-10 12:09:06 +02:00
parent 3944a4e987
commit d047a24a32
3 changed files with 56 additions and 0 deletions

View File

@@ -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

View File

@@ -1,5 +1,6 @@
#[repr(C)]
pub enum MVCCError {
MVCC_OK = 0,
MVCC_IO_ERROR_READ = 266,
MVCC_IO_ERROR_WRITE = 778,
}

View File

@@ -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);
}