tabledb work

This commit is contained in:
John Smith 2023-05-21 12:57:37 +01:00
parent 135b66298c
commit f31044e8a3
6 changed files with 71 additions and 49 deletions

View File

@ -14,6 +14,12 @@ Uint8List base64UrlNoPadDecode(String source) {
return base64.decode(source); return base64.decode(source);
} }
Uint8List base64UrlNoPadDecodeDynamic(dynamic source) {
source = source as String;
source = base64.normalize(source);
return base64.decode(source);
}
abstract class EncodedString { abstract class EncodedString {
late String contents; late String contents;
EncodedString(String s) { EncodedString(String s) {

View File

@ -148,9 +148,9 @@ typedef _DeleteTableDbDart = void Function(int, Pointer<Utf8>);
// fn table_db_get_column_count(id: u32) -> u32 // fn table_db_get_column_count(id: u32) -> u32
typedef _TableDbGetColumnCountC = Uint32 Function(Uint32); typedef _TableDbGetColumnCountC = Uint32 Function(Uint32);
typedef _TableDbGetColumnCountDart = int Function(int); typedef _TableDbGetColumnCountDart = int Function(int);
// fn table_db_get_keys(id: u32, col: u32) -> *mut c_char // fn table_db_get_keys(port: i64, id: u32, col: u32)
typedef _TableDbGetKeysC = Pointer<Utf8> Function(Uint32, Uint32); typedef _TableDbGetKeysC = Pointer<Utf8> Function(Uint64, Uint32, Uint32);
typedef _TableDbGetKeysDart = Pointer<Utf8> Function(int, int); typedef _TableDbGetKeysDart = Pointer<Utf8> Function(int, int, int);
// fn table_db_store(port: i64, id: u32, col: u32, key: FfiStr, value: FfiStr) // fn table_db_store(port: i64, id: u32, col: u32, key: FfiStr, value: FfiStr)
typedef _TableDbStoreC = Void Function( typedef _TableDbStoreC = Void Function(
Int64, Uint32, Uint32, Pointer<Utf8>, Pointer<Utf8>); Int64, Uint32, Uint32, Pointer<Utf8>, Pointer<Utf8>);
@ -834,15 +834,15 @@ class VeilidTableDBFFI extends VeilidTableDB {
} }
@override @override
List<Uint8List> getKeys(int col) { Future<List<Uint8List>> getKeys(int col) {
final s = _tdb.ffi._tableDbGetKeys(_tdb.id, col); final recvPort = ReceivePort("veilid_table_db_get_keys");
if (s.address == nullptr.address) { final sendPort = recvPort.sendPort;
throw VeilidAPIExceptionInternal("No db for id");
} _tdb.ffi._tableDbGetKeys(sendPort.nativePort, _tdb.id, col);
String ja = s.toDartString();
_tdb.ffi._freeString(s); return processFutureJson(
List<dynamic> jarr = jsonDecode(ja); jsonListConstructor<Uint8List>(base64UrlNoPadDecodeDynamic),
return jarr.map((e) => base64UrlNoPadDecode(e)).toList(); recvPort.first);
} }
@override @override
@ -888,7 +888,7 @@ class VeilidTableDBFFI extends VeilidTableDB {
} }
@override @override
Future<bool> delete(int col, Uint8List key) { Future<Uint8List?> delete(int col, Uint8List key) {
final nativeEncodedKey = base64UrlNoPadEncode(key).toNativeUtf8(); final nativeEncodedKey = base64UrlNoPadEncode(key).toNativeUtf8();
final recvPort = ReceivePort("veilid_table_db_delete"); final recvPort = ReceivePort("veilid_table_db_delete");

View File

@ -399,13 +399,9 @@ class VeilidTableDBJS extends VeilidTableDB {
} }
@override @override
List<Uint8List> getKeys(int col) { Future<List<Uint8List>> getKeys(int col) async {
String? s = js_util.callMethod(wasm, "table_db_get_keys", [_tdb.id, col]); return jsonListConstructor(base64UrlNoPadDecodeDynamic)(jsonDecode(
if (s == null) { await js_util.callMethod(wasm, "table_db_get_keys", [_tdb.id, col])));
throw VeilidAPIExceptionInternal("No db for id");
}
List<dynamic> jarr = jsonDecode(s);
return jarr.map((e) => base64UrlNoPadDecode(e)).toList();
} }
@override @override
@ -437,7 +433,7 @@ class VeilidTableDBJS extends VeilidTableDB {
} }
@override @override
Future<bool> delete(int col, Uint8List key) { Future<Uint8List?> delete(int col, Uint8List key) {
final encodedKey = base64UrlNoPadEncode(key); final encodedKey = base64UrlNoPadEncode(key);
return _wrapApiPromise(js_util return _wrapApiPromise(js_util

View File

@ -25,11 +25,11 @@ abstract class VeilidTableDBTransaction {
abstract class VeilidTableDB { abstract class VeilidTableDB {
int getColumnCount(); int getColumnCount();
List<Uint8List> getKeys(int col); Future<List<Uint8List>> getKeys(int col);
VeilidTableDBTransaction transact(); VeilidTableDBTransaction transact();
Future<void> store(int col, Uint8List key, Uint8List value); Future<void> store(int col, Uint8List key, Uint8List value);
Future<Uint8List?> load(int col, Uint8List key); Future<Uint8List?> load(int col, Uint8List key);
Future<bool> delete(int col, Uint8List key); Future<Uint8List?> delete(int col, Uint8List key);
Future<void> storeJson(int col, Uint8List key, Object? object, Future<void> storeJson(int col, Uint8List key, Object? object,
{Object? Function(Object? nonEncodable)? toEncodable}) { {Object? Function(Object? nonEncodable)? toEncodable}) {
@ -56,4 +56,18 @@ abstract class VeilidTableDB {
{Object? Function(Object? key, Object? value)? reviver}) { {Object? Function(Object? key, Object? value)? reviver}) {
return loadJson(col, utf8.encoder.convert(key), reviver: reviver); return loadJson(col, utf8.encoder.convert(key), reviver: reviver);
} }
Future<Object?> deleteJson(int col, Uint8List key,
{Object? Function(Object? key, Object? value)? reviver}) async {
var s = await delete(col, key);
if (s == null) {
return null;
}
return jsonDecode(utf8.decode(s, allowMalformed: false), reviver: reviver);
}
Future<Object?> deleteStringJson(int col, String key,
{Object? Function(Object? key, Object? value)? reviver}) {
return deleteJson(col, utf8.encoder.convert(key), reviver: reviver);
}
} }

View File

@ -781,17 +781,20 @@ pub extern "C" fn table_db_get_column_count(id: u32) -> u32 {
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn table_db_get_keys(id: u32, col: u32) -> *mut c_char { pub extern "C" fn table_db_get_keys(port: i64, id: u32, col: u32) {
let table_dbs = TABLE_DBS.lock(); DartIsolateWrapper::new(port).spawn_result_json(async move {
let Some(table_db) = table_dbs.get(&id) else { let table_db = {
return std::ptr::null_mut(); let table_dbs = TABLE_DBS.lock();
}; xxx continue here and run all tests let Some(table_db) = table_dbs.get(&id) else {
let Ok(keys) = table_db.clone().get_keys(col) else { return APIResult::Err(veilid_core::VeilidAPIError::invalid_argument("table_db_get_keys", "id", id));
return std::ptr::null_mut(); };
}; table_db.clone()
let keys: Vec<String> = keys.into_iter().map(|k| BASE64URL_NOPAD.encode(&k)).collect(); };
let out = veilid_core::serialize_json(keys);
out.into_ffi_value() let keys = table_db.get_keys(col).await.map_err(veilid_core::VeilidAPIError::generic)?;
let out: Vec<String> = keys.into_iter().map(|k| BASE64URL_NOPAD.encode(&k)).collect();
APIResult::Ok(out)
});
} }
fn add_table_db_transaction(tdbt: veilid_core::TableDBTransaction) -> u32 { fn add_table_db_transaction(tdbt: veilid_core::TableDBTransaction) -> u32 {
@ -957,7 +960,7 @@ pub extern "C" fn table_db_load(port: i64, id: u32, col: u32, key: FfiStr) {
table_db.clone() table_db.clone()
}; };
let out = table_db.load(col, &key).map_err(veilid_core::VeilidAPIError::generic)?; let out = table_db.load(col, &key).await.map_err(veilid_core::VeilidAPIError::generic)?;
let out = out.map(|x| data_encoding::BASE64URL_NOPAD.encode(&x)); let out = out.map(|x| data_encoding::BASE64URL_NOPAD.encode(&x));
APIResult::Ok(out) APIResult::Ok(out)
}); });
@ -982,6 +985,7 @@ pub extern "C" fn table_db_delete(port: i64, id: u32, col: u32, key: FfiStr) {
}; };
let out = table_db.delete(col, &key).await.map_err(veilid_core::VeilidAPIError::generic)?; let out = table_db.delete(col, &key).await.map_err(veilid_core::VeilidAPIError::generic)?;
let out = out.map(|x| data_encoding::BASE64URL_NOPAD.encode(&x));
APIResult::Ok(out) APIResult::Ok(out)
}); });
} }

View File

@ -717,20 +717,21 @@ pub fn table_db_get_column_count(id: u32) -> u32 {
} }
#[wasm_bindgen()] #[wasm_bindgen()]
pub fn table_db_get_keys(id: u32, col: u32) -> Option<String> { pub fn table_db_get_keys(id: u32, col: u32) -> Promise {
let table_dbs = (*TABLE_DBS).borrow(); wrap_api_future_json(async move {
let Some(table_db) = table_dbs.get(&id) else { let table_dbs = (*TABLE_DBS).borrow();
return None; let Some(table_db) = table_dbs.get(&id) else {
}; return None;
let Ok(keys) = table_db.clone().get_keys(col) else { };
return None; let Ok(keys) = table_db.clone().get_keys(col) else {
}; return None;
let keys: Vec<String> = keys };
.into_iter() let out: Vec<String> = keys
.map(|k| data_encoding::BASE64URL_NOPAD.encode(&k)) .into_iter()
.collect(); .map(|k| data_encoding::BASE64URL_NOPAD.encode(&k))
let out = veilid_core::serialize_json(keys); .collect();
Some(out) APIResult::Ok(Some(out))
});
} }
fn add_table_db_transaction(tdbt: veilid_core::TableDBTransaction) -> u32 { fn add_table_db_transaction(tdbt: veilid_core::TableDBTransaction) -> u32 {
@ -903,6 +904,7 @@ pub fn table_db_delete(id: u32, col: u32, key: String) -> Promise {
.delete(col, &key) .delete(col, &key)
.await .await
.map_err(veilid_core::VeilidAPIError::generic)?; .map_err(veilid_core::VeilidAPIError::generic)?;
let out = out.map(|x| data_encoding::BASE64URL_NOPAD.encode(&x));
APIResult::Ok(out) APIResult::Ok(out)
}) })
} }