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

View File

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

View File

@ -25,11 +25,11 @@ abstract class VeilidTableDBTransaction {
abstract class VeilidTableDB {
int getColumnCount();
List<Uint8List> getKeys(int col);
Future<List<Uint8List>> getKeys(int col);
VeilidTableDBTransaction transact();
Future<void> store(int col, Uint8List key, Uint8List value);
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,
{Object? Function(Object? nonEncodable)? toEncodable}) {
@ -56,4 +56,18 @@ abstract class VeilidTableDB {
{Object? Function(Object? key, Object? value)? 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]
pub extern "C" fn table_db_get_keys(id: u32, col: u32) -> *mut c_char {
let table_dbs = TABLE_DBS.lock();
let Some(table_db) = table_dbs.get(&id) else {
return std::ptr::null_mut();
}; xxx continue here and run all tests
let Ok(keys) = table_db.clone().get_keys(col) else {
return std::ptr::null_mut();
};
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()
pub extern "C" fn table_db_get_keys(port: i64, id: u32, col: u32) {
DartIsolateWrapper::new(port).spawn_result_json(async move {
let table_db = {
let table_dbs = TABLE_DBS.lock();
let Some(table_db) = table_dbs.get(&id) else {
return APIResult::Err(veilid_core::VeilidAPIError::invalid_argument("table_db_get_keys", "id", id));
};
table_db.clone()
};
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 {
@ -957,7 +960,7 @@ pub extern "C" fn table_db_load(port: i64, id: u32, col: u32, key: FfiStr) {
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));
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 = out.map(|x| data_encoding::BASE64URL_NOPAD.encode(&x));
APIResult::Ok(out)
});
}

View File

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