mirror of
https://gitlab.com/veilid/veilidchat.git
synced 2025-01-23 22:11:19 -05:00
fix
This commit is contained in:
parent
bbe2d60c7f
commit
3c2c8827bb
@ -136,20 +136,28 @@ class DHTRecordPool with AsyncTableDBBacked<DHTRecordPoolAllocations> {
|
|||||||
await Future.wait(allFutures);
|
await Future.wait(allFutures);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _validateParent(TypedKey? parent, TypedKey child) {
|
||||||
|
final childJson = child.toJson();
|
||||||
|
if (parent == null) {
|
||||||
|
if (_state.parentByChild.containsKey(childJson)) {
|
||||||
|
throw StateError('Child is already parented: $child');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (_state.rootRecords.contains(child)) {
|
||||||
|
throw StateError('Child already added as root: $child');
|
||||||
|
}
|
||||||
|
if (_state.parentByChild[childJson] != parent) {
|
||||||
|
throw StateError('Child has two parents: $child <- $parent');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> _addDependency(TypedKey? parent, TypedKey child) async {
|
Future<void> _addDependency(TypedKey? parent, TypedKey child) async {
|
||||||
if (parent == null) {
|
if (parent == null) {
|
||||||
if (_state.rootRecords.contains(child)) {
|
if (_state.rootRecords.contains(child)) {
|
||||||
// Dependency already added
|
// Dependency already added
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (_state.parentByChild.containsKey(child.toJson())) {
|
|
||||||
throw StateError('Child is already parented: $child');
|
|
||||||
}
|
|
||||||
if (_state.childrenByParent.containsKey(child.toJson())) {
|
|
||||||
// dependencies should be opened after their parents
|
|
||||||
throw StateError('Child is not a leaf: $child');
|
|
||||||
}
|
|
||||||
|
|
||||||
_state = await store(
|
_state = await store(
|
||||||
_state.copyWith(rootRecords: _state.rootRecords.add(child)));
|
_state.copyWith(rootRecords: _state.rootRecords.add(child)));
|
||||||
} else {
|
} else {
|
||||||
@ -159,17 +167,6 @@ class DHTRecordPool with AsyncTableDBBacked<DHTRecordPoolAllocations> {
|
|||||||
// Dependency already added (consecutive opens, etc)
|
// Dependency already added (consecutive opens, etc)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (_state.rootRecords.contains(child)) {
|
|
||||||
throw StateError('Child already added as root: $child');
|
|
||||||
}
|
|
||||||
if (_state.parentByChild.containsKey(child.toJson())) {
|
|
||||||
throw StateError('Child has two parents: $child <- $parent');
|
|
||||||
}
|
|
||||||
if (_state.childrenByParent.containsKey(child.toJson())) {
|
|
||||||
// dependencies should be opened after their parents
|
|
||||||
throw StateError('Child is not a leaf: $child');
|
|
||||||
}
|
|
||||||
|
|
||||||
_state = await store(_state.copyWith(
|
_state = await store(_state.copyWith(
|
||||||
childrenByParent: _state.childrenByParent
|
childrenByParent: _state.childrenByParent
|
||||||
.add(parent.toJson(), childrenOfParent.add(child)),
|
.add(parent.toJson(), childrenOfParent.add(child)),
|
||||||
@ -223,10 +220,7 @@ class DHTRecordPool with AsyncTableDBBacked<DHTRecordPoolAllocations> {
|
|||||||
await DHTRecordCryptoPrivate.fromTypedKeyPair(
|
await DHTRecordCryptoPrivate.fromTypedKeyPair(
|
||||||
recordDescriptor.ownerTypedKeyPair()!));
|
recordDescriptor.ownerTypedKeyPair()!));
|
||||||
|
|
||||||
if (parent != null) {
|
await _addDependency(parent, rec.key);
|
||||||
await _addDependency(parent, rec.key);
|
|
||||||
}
|
|
||||||
|
|
||||||
await _recordOpened(rec.key);
|
await _recordOpened(rec.key);
|
||||||
|
|
||||||
return rec;
|
return rec;
|
||||||
@ -244,8 +238,7 @@ class DHTRecordPool with AsyncTableDBBacked<DHTRecordPoolAllocations> {
|
|||||||
try {
|
try {
|
||||||
// If we are opening a key that already exists
|
// If we are opening a key that already exists
|
||||||
// make sure we are using the same parent if one was specified
|
// make sure we are using the same parent if one was specified
|
||||||
final existingParent = _state.parentByChild[recordKey.toJson()];
|
_validateParent(parent, recordKey);
|
||||||
assert(existingParent == parent, 'wrong parent for opened key');
|
|
||||||
|
|
||||||
// Open from the veilid api
|
// Open from the veilid api
|
||||||
final dhtctx = routingContext ?? _routingContext;
|
final dhtctx = routingContext ?? _routingContext;
|
||||||
@ -256,10 +249,8 @@ class DHTRecordPool with AsyncTableDBBacked<DHTRecordPoolAllocations> {
|
|||||||
defaultSubkey: defaultSubkey,
|
defaultSubkey: defaultSubkey,
|
||||||
crypto: crypto ?? const DHTRecordCryptoPublic());
|
crypto: crypto ?? const DHTRecordCryptoPublic());
|
||||||
|
|
||||||
// Register the dependency if specified
|
// Register the dependency
|
||||||
if (parent != null) {
|
await _addDependency(parent, rec.key);
|
||||||
await _addDependency(parent, rec.key);
|
|
||||||
}
|
|
||||||
} on Exception catch (_) {
|
} on Exception catch (_) {
|
||||||
recordClosed(recordKey);
|
recordClosed(recordKey);
|
||||||
rethrow;
|
rethrow;
|
||||||
@ -283,8 +274,7 @@ class DHTRecordPool with AsyncTableDBBacked<DHTRecordPoolAllocations> {
|
|||||||
try {
|
try {
|
||||||
// If we are opening a key that already exists
|
// If we are opening a key that already exists
|
||||||
// make sure we are using the same parent if one was specified
|
// make sure we are using the same parent if one was specified
|
||||||
final existingParent = _state.parentByChild[recordKey.toJson()];
|
_validateParent(parent, recordKey);
|
||||||
assert(existingParent == parent, 'wrong parent for opened key');
|
|
||||||
|
|
||||||
// Open from the veilid api
|
// Open from the veilid api
|
||||||
final dhtctx = routingContext ?? _routingContext;
|
final dhtctx = routingContext ?? _routingContext;
|
||||||
@ -299,9 +289,7 @@ class DHTRecordPool with AsyncTableDBBacked<DHTRecordPoolAllocations> {
|
|||||||
TypedKeyPair.fromKeyPair(recordKey.kind, writer)));
|
TypedKeyPair.fromKeyPair(recordKey.kind, writer)));
|
||||||
|
|
||||||
// Register the dependency if specified
|
// Register the dependency if specified
|
||||||
if (parent != null) {
|
await _addDependency(parent, rec.key);
|
||||||
await _addDependency(parent, rec.key);
|
|
||||||
}
|
|
||||||
} on Exception catch (_) {
|
} on Exception catch (_) {
|
||||||
recordClosed(recordKey);
|
recordClosed(recordKey);
|
||||||
rethrow;
|
rethrow;
|
||||||
|
Loading…
Reference in New Issue
Block a user