checkpoint

This commit is contained in:
Christien Rioux 2023-07-19 00:09:57 -04:00
parent bf813d7d0f
commit 20bdf07f24
10 changed files with 329 additions and 218 deletions

View file

@ -1,3 +1,4 @@
import 'package:protobuf/protobuf.dart';
import 'package:veilid/veilid.dart';
import 'dart:typed_data';
import 'tools.dart';
@ -38,6 +39,49 @@ class DHTRecord {
int _subkey(int subkey) => (subkey == -1) ? _defaultSubkey : subkey;
TypedKey key() {
return _recordDescriptor.key;
}
PublicKey owner() {
return _recordDescriptor.owner;
}
KeyPair? ownerKeyPair() {
final ownerSecret = _recordDescriptor.ownerSecret;
if (ownerSecret == null) {
return null;
}
return KeyPair(key: _recordDescriptor.owner, secret: ownerSecret);
}
Future<void> close() async {
await _dhtctx.closeDHTRecord(_recordDescriptor.key);
}
Future<void> delete() async {
await _dhtctx.deleteDHTRecord(_recordDescriptor.key);
}
Future<T> scope<T>(Future<T> Function(DHTRecord) scopeFunction) async {
try {
return await scopeFunction(this);
} finally {
close();
}
}
Future<T> deleteScope<T>(Future<T> Function(DHTRecord) scopeFunction) async {
try {
return await scopeFunction(this);
} catch (_) {
delete();
rethrow;
} finally {
close();
}
}
Future<Uint8List?> get({int subkey = -1, bool forceRefresh = false}) async {
ValueData? valueData = await _dhtctx.getDHTValue(
_recordDescriptor.key, _subkey(subkey), false);
@ -101,9 +145,21 @@ class DHTRecord {
return eventualWriteBytes(jsonEncodeBytes(newValue), subkey: subkey);
}
Future<void> eventualWriteProtobuf<T extends GeneratedMessage>(T newValue,
{int subkey = -1}) {
return eventualWriteBytes(newValue.writeToBuffer(), subkey: subkey);
}
Future<void> eventualUpdateJson<T>(
T Function(Map<String, dynamic>) fromJson, Future<T> Function(T) update,
{int subkey = -1}) {
return eventualUpdateBytes(jsonUpdate(fromJson, update), subkey: subkey);
}
Future<void> eventualUpdateProtobuf<T extends GeneratedMessage>(
T Function(List<int>) fromBuffer, Future<T> Function(T) update,
{int subkey = -1}) {
return eventualUpdateBytes(protobufUpdate(fromBuffer, update),
subkey: subkey);
}
}

View file

@ -1,4 +1,3 @@
// import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:veilid/veilid.dart';
import 'dart:typed_data';
import 'dart:convert';
@ -28,3 +27,10 @@ Future<Uint8List> Function(Uint8List) jsonUpdate<T>(
return jsonUpdateBytes(fromJson, oldBytes, update);
};
}
T Function(Object?) genericFromJson<T>(
T Function(Map<String, dynamic>) fromJsonMap) {
return (Object? json) {
return fromJsonMap(json as Map<String, dynamic>);
};
}

View file

@ -0,0 +1,19 @@
import 'package:protobuf/protobuf.dart';
import 'dart:typed_data';
Future<Uint8List> protobufUpdateBytes<T extends GeneratedMessage>(
T Function(List<int>) fromBuffer,
Uint8List oldBytes,
Future<T> Function(T) update) async {
T oldObj = fromBuffer(oldBytes);
T newObj = await update(oldObj);
return Uint8List.fromList(newObj.writeToBuffer());
}
Future<Uint8List> Function(Uint8List)
protobufUpdate<T extends GeneratedMessage>(
T Function(List<int>) fromBuffer, Future<T> Function(T) update) {
return (Uint8List oldBytes) {
return protobufUpdateBytes(fromBuffer, oldBytes, update);
};
}

30
lib/tools/table_db.dart Normal file
View file

@ -0,0 +1,30 @@
import 'package:veilid/veilid.dart';
Future<T> tableScope<T>(
String name, Future<T> Function(VeilidTableDB tdb) callback,
{int columnCount = 1}) async {
VeilidTableDB tableDB = await Veilid.instance.openTableDB(name, columnCount);
try {
return await callback(tableDB);
} finally {
tableDB.close();
}
}
Future<T> transactionScope<T>(
VeilidTableDB tdb,
Future<T> Function(VeilidTableDBTransaction tdbt) callback,
) async {
VeilidTableDBTransaction tdbt = tdb.transact();
try {
final ret = await callback(tdbt);
if (!tdbt.isDone()) {
await tdbt.commit();
}
return ret;
} finally {
if (!tdbt.isDone()) {
await tdbt.rollback();
}
}
}

View file

@ -2,3 +2,5 @@ export 'external_stream_state.dart';
export 'dht_record.dart';
export 'json_tools.dart';
export 'phono_byte.dart';
export 'protobuf_tools.dart';
export 'table_db.dart';