parent fix

This commit is contained in:
Christien Rioux 2023-08-06 10:15:34 -04:00
parent 4a554841d1
commit bbe2d60c7f
4 changed files with 102 additions and 44 deletions

View File

@ -70,7 +70,6 @@ Future<AcceptedOrRejectedContact?> checkAcceptRejectContact(
final signedContactResponse = await contactRequestInbox
.getProtobuf(SignedContactResponse.fromBuffer, forceRefresh: true);
if (signedContactResponse == null) {
log.error('failed to get signed contact response');
return null;
}

View File

@ -16,6 +16,7 @@ class DHTRecordPoolAllocations with _$DHTRecordPoolAllocations {
childrenByParent, // String key due to IMap<> json unsupported in key
required IMap<String, TypedKey>
parentByChild, // String key due to IMap<> json unsupported in key
required ISet<TypedKey> rootRecords,
}) = _DHTRecordPoolAllocations;
factory DHTRecordPoolAllocations.fromJson(dynamic json) =>
@ -38,7 +39,9 @@ class OwnedDHTRecordPointer with _$OwnedDHTRecordPointer {
class DHTRecordPool with AsyncTableDBBacked<DHTRecordPoolAllocations> {
DHTRecordPool._(Veilid veilid, VeilidRoutingContext routingContext)
: _state = DHTRecordPoolAllocations(
childrenByParent: IMap(), parentByChild: IMap()),
childrenByParent: IMap(),
parentByChild: IMap(),
rootRecords: ISet()),
_opened = <TypedKey, Mutex>{},
_routingContext = routingContext,
_veilid = veilid;
@ -64,7 +67,7 @@ class DHTRecordPool with AsyncTableDBBacked<DHTRecordPoolAllocations> {
DHTRecordPoolAllocations valueFromJson(Object? obj) => obj != null
? DHTRecordPoolAllocations.fromJson(obj)
: DHTRecordPoolAllocations(
childrenByParent: IMap(), parentByChild: IMap());
childrenByParent: IMap(), parentByChild: IMap(), rootRecords: ISet());
@override
Object? valueToJson(DHTRecordPoolAllocations val) => val.toJson();
@ -133,45 +136,70 @@ class DHTRecordPool with AsyncTableDBBacked<DHTRecordPoolAllocations> {
await Future.wait(allFutures);
}
Future<void> _addDependency(TypedKey parent, TypedKey child) async {
final childrenOfParent =
_state.childrenByParent[parent.toJson()] ?? ISet<TypedKey>();
if (childrenOfParent.contains(child)) {
// Dependency already added (consecutive opens, etc)
return;
}
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');
}
Future<void> _addDependency(TypedKey? parent, TypedKey child) async {
if (parent == null) {
if (_state.rootRecords.contains(child)) {
// Dependency already added
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.copyWith(
childrenByParent: _state.childrenByParent
.add(parent.toJson(), childrenOfParent.add(child)),
parentByChild: _state.parentByChild.add(child.toJson(), parent)));
_state = await store(
_state.copyWith(rootRecords: _state.rootRecords.add(child)));
} else {
final childrenOfParent =
_state.childrenByParent[parent.toJson()] ?? ISet<TypedKey>();
if (childrenOfParent.contains(child)) {
// Dependency already added (consecutive opens, etc)
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(
childrenByParent: _state.childrenByParent
.add(parent.toJson(), childrenOfParent.add(child)),
parentByChild: _state.parentByChild.add(child.toJson(), parent)));
}
}
Future<void> _removeDependency(TypedKey child) async {
final parent = _state.parentByChild[child.toJson()];
if (parent == null) {
return;
}
final children = _state.childrenByParent[parent.toJson()]!.remove(child);
late final DHTRecordPoolAllocations newState;
if (children.isEmpty) {
newState = _state.copyWith(
childrenByParent: _state.childrenByParent.remove(parent.toJson()),
parentByChild: _state.parentByChild.remove(child.toJson()));
if (_state.rootRecords.contains(child)) {
_state = await store(
_state.copyWith(rootRecords: _state.rootRecords.remove(child)));
} else {
newState = _state.copyWith(
childrenByParent:
_state.childrenByParent.add(parent.toJson(), children),
parentByChild: _state.parentByChild.remove(child.toJson()));
final parent = _state.parentByChild[child.toJson()];
if (parent == null) {
return;
}
final children = _state.childrenByParent[parent.toJson()]!.remove(child);
late final DHTRecordPoolAllocations newState;
if (children.isEmpty) {
newState = _state.copyWith(
childrenByParent: _state.childrenByParent.remove(parent.toJson()),
parentByChild: _state.parentByChild.remove(child.toJson()));
} else {
newState = _state.copyWith(
childrenByParent:
_state.childrenByParent.add(parent.toJson(), children),
parentByChild: _state.parentByChild.remove(child.toJson()));
}
_state = await store(newState);
}
_state = await store(newState);
}
///////////////////////////////////////////////////////////////////////

View File

@ -24,6 +24,8 @@ mixin _$DHTRecordPoolAllocations {
IMap<String, ISet<Typed<FixedEncodedString43>>> get childrenByParent =>
throw _privateConstructorUsedError; // String key due to IMap<> json unsupported in key
IMap<String, Typed<FixedEncodedString43>> get parentByChild =>
throw _privateConstructorUsedError; // String key due to IMap<> json unsupported in key
ISet<Typed<FixedEncodedString43>> get rootRecords =>
throw _privateConstructorUsedError;
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
@ -40,7 +42,8 @@ abstract class $DHTRecordPoolAllocationsCopyWith<$Res> {
@useResult
$Res call(
{IMap<String, ISet<Typed<FixedEncodedString43>>> childrenByParent,
IMap<String, Typed<FixedEncodedString43>> parentByChild});
IMap<String, Typed<FixedEncodedString43>> parentByChild,
ISet<Typed<FixedEncodedString43>> rootRecords});
}
/// @nodoc
@ -59,6 +62,7 @@ class _$DHTRecordPoolAllocationsCopyWithImpl<$Res,
$Res call({
Object? childrenByParent = null,
Object? parentByChild = null,
Object? rootRecords = null,
}) {
return _then(_value.copyWith(
childrenByParent: null == childrenByParent
@ -69,6 +73,10 @@ class _$DHTRecordPoolAllocationsCopyWithImpl<$Res,
? _value.parentByChild
: parentByChild // ignore: cast_nullable_to_non_nullable
as IMap<String, Typed<FixedEncodedString43>>,
rootRecords: null == rootRecords
? _value.rootRecords
: rootRecords // ignore: cast_nullable_to_non_nullable
as ISet<Typed<FixedEncodedString43>>,
) as $Val);
}
}
@ -84,7 +92,8 @@ abstract class _$$_DHTRecordPoolAllocationsCopyWith<$Res>
@useResult
$Res call(
{IMap<String, ISet<Typed<FixedEncodedString43>>> childrenByParent,
IMap<String, Typed<FixedEncodedString43>> parentByChild});
IMap<String, Typed<FixedEncodedString43>> parentByChild,
ISet<Typed<FixedEncodedString43>> rootRecords});
}
/// @nodoc
@ -101,6 +110,7 @@ class __$$_DHTRecordPoolAllocationsCopyWithImpl<$Res>
$Res call({
Object? childrenByParent = null,
Object? parentByChild = null,
Object? rootRecords = null,
}) {
return _then(_$_DHTRecordPoolAllocations(
childrenByParent: null == childrenByParent
@ -111,6 +121,10 @@ class __$$_DHTRecordPoolAllocationsCopyWithImpl<$Res>
? _value.parentByChild
: parentByChild // ignore: cast_nullable_to_non_nullable
as IMap<String, Typed<FixedEncodedString43>>,
rootRecords: null == rootRecords
? _value.rootRecords
: rootRecords // ignore: cast_nullable_to_non_nullable
as ISet<Typed<FixedEncodedString43>>,
));
}
}
@ -119,7 +133,9 @@ class __$$_DHTRecordPoolAllocationsCopyWithImpl<$Res>
@JsonSerializable()
class _$_DHTRecordPoolAllocations implements _DHTRecordPoolAllocations {
const _$_DHTRecordPoolAllocations(
{required this.childrenByParent, required this.parentByChild});
{required this.childrenByParent,
required this.parentByChild,
required this.rootRecords});
factory _$_DHTRecordPoolAllocations.fromJson(Map<String, dynamic> json) =>
_$$_DHTRecordPoolAllocationsFromJson(json);
@ -129,10 +145,13 @@ class _$_DHTRecordPoolAllocations implements _DHTRecordPoolAllocations {
// String key due to IMap<> json unsupported in key
@override
final IMap<String, Typed<FixedEncodedString43>> parentByChild;
// String key due to IMap<> json unsupported in key
@override
final ISet<Typed<FixedEncodedString43>> rootRecords;
@override
String toString() {
return 'DHTRecordPoolAllocations(childrenByParent: $childrenByParent, parentByChild: $parentByChild)';
return 'DHTRecordPoolAllocations(childrenByParent: $childrenByParent, parentByChild: $parentByChild, rootRecords: $rootRecords)';
}
@override
@ -143,12 +162,15 @@ class _$_DHTRecordPoolAllocations implements _DHTRecordPoolAllocations {
(identical(other.childrenByParent, childrenByParent) ||
other.childrenByParent == childrenByParent) &&
(identical(other.parentByChild, parentByChild) ||
other.parentByChild == parentByChild));
other.parentByChild == parentByChild) &&
const DeepCollectionEquality()
.equals(other.rootRecords, rootRecords));
}
@JsonKey(ignore: true)
@override
int get hashCode => Object.hash(runtimeType, childrenByParent, parentByChild);
int get hashCode => Object.hash(runtimeType, childrenByParent, parentByChild,
const DeepCollectionEquality().hash(rootRecords));
@JsonKey(ignore: true)
@override
@ -169,8 +191,9 @@ abstract class _DHTRecordPoolAllocations implements DHTRecordPoolAllocations {
const factory _DHTRecordPoolAllocations(
{required final IMap<String, ISet<Typed<FixedEncodedString43>>>
childrenByParent,
required final IMap<String, Typed<FixedEncodedString43>>
parentByChild}) = _$_DHTRecordPoolAllocations;
required final IMap<String, Typed<FixedEncodedString43>> parentByChild,
required final ISet<Typed<FixedEncodedString43>>
rootRecords}) = _$_DHTRecordPoolAllocations;
factory _DHTRecordPoolAllocations.fromJson(Map<String, dynamic> json) =
_$_DHTRecordPoolAllocations.fromJson;
@ -179,6 +202,8 @@ abstract class _DHTRecordPoolAllocations implements DHTRecordPoolAllocations {
IMap<String, ISet<Typed<FixedEncodedString43>>> get childrenByParent;
@override // String key due to IMap<> json unsupported in key
IMap<String, Typed<FixedEncodedString43>> get parentByChild;
@override // String key due to IMap<> json unsupported in key
ISet<Typed<FixedEncodedString43>> get rootRecords;
@override
@JsonKey(ignore: true)
_$$_DHTRecordPoolAllocationsCopyWith<_$_DHTRecordPoolAllocations>

View File

@ -19,6 +19,9 @@ _$_DHTRecordPoolAllocations _$$_DHTRecordPoolAllocationsFromJson(
json['parent_by_child'] as Map<String, dynamic>,
(value) => value as String,
(value) => Typed<FixedEncodedString43>.fromJson(value)),
rootRecords: ISet<Typed<FixedEncodedString43>>.fromJson(
json['root_records'],
(value) => Typed<FixedEncodedString43>.fromJson(value)),
);
Map<String, dynamic> _$$_DHTRecordPoolAllocationsToJson(
@ -34,6 +37,9 @@ Map<String, dynamic> _$$_DHTRecordPoolAllocationsToJson(
(value) => value,
(value) => value.toJson(),
),
'root_records': instance.rootRecords.toJson(
(value) => value.toJson(),
),
};
_$_OwnedDHTRecordPointer _$$_OwnedDHTRecordPointerFromJson(