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