@@ -55,7 +55,6 @@ public enum ReachabilityStatus
55
55
public static readonly Reachability Empty =
56
56
new Reachability ( ImmutableList . Create < Record > ( ) , ImmutableDictionary . Create < UniqueAddress , long > ( ) ) ;
57
57
58
- //TODO: Serialization should ignore
59
58
private readonly Lazy < Cache > _cache ;
60
59
61
60
/// <summary>
@@ -80,11 +79,6 @@ public Reachability(ImmutableList<Record> records, ImmutableDictionary<UniqueAdd
80
79
/// </summary>
81
80
public ImmutableDictionary < UniqueAddress , long > Versions { get ; }
82
81
83
- /*
84
- * def isReachable(observer: UniqueAddress, subject: UniqueAddress): Boolean =
85
- status(observer, subject) == Reachable
86
- */
87
-
88
82
/// <summary>
89
83
/// TBD
90
84
/// </summary>
@@ -178,7 +172,11 @@ private Reachability Change(UniqueAddress observer, UniqueAddress subject, Reach
178
172
var newVersions = Versions . SetItem ( observer , v ) ;
179
173
var newRecord = new Record ( observer , subject , status , v ) ;
180
174
var oldObserverRows = ObserverRows ( observer ) ;
175
+
176
+ // don't record Reachable observation if nothing has been noted so far
181
177
if ( oldObserverRows == null && status == ReachabilityStatus . Reachable ) return this ;
178
+
179
+ // otherwise, create new instance including this first observation
182
180
if ( oldObserverRows == null ) return new Reachability ( Records . Add ( newRecord ) , newVersions ) ;
183
181
184
182
if ( ! oldObserverRows . TryGetValue ( subject , out var oldRecord ) )
@@ -206,7 +204,7 @@ private Reachability Change(UniqueAddress observer, UniqueAddress subject, Reach
206
204
/// <param name="allowed">TBD</param>
207
205
/// <param name="other">TBD</param>
208
206
/// <returns>TBD</returns>
209
- public Reachability Merge ( IEnumerable < UniqueAddress > allowed , Reachability other )
207
+ public Reachability Merge ( IImmutableSet < UniqueAddress > allowed , Reachability other )
210
208
{
211
209
var recordBuilder = ImmutableList . CreateBuilder < Record > ( ) ;
212
210
//TODO: Size hint somehow?
@@ -337,7 +335,7 @@ public bool IsReachable(UniqueAddress observer, UniqueAddress subject)
337
335
public ImmutableHashSet < UniqueAddress > AllUnreachableFrom ( UniqueAddress observer )
338
336
{
339
337
var observerRows = ObserverRows ( observer ) ;
340
- if ( observerRows == null ) return ImmutableHashSet . Create < UniqueAddress > ( ) ;
338
+ if ( observerRows == null ) return ImmutableHashSet < UniqueAddress > . Empty ;
341
339
return
342
340
ImmutableHashSet . CreateRange (
343
341
observerRows . Where ( p => p . Value . Status == ReachabilityStatus . Unreachable ) . Select ( p => p . Key ) ) ;
@@ -351,16 +349,18 @@ public ImmutableHashSet<UniqueAddress> AllUnreachableFrom(UniqueAddress observer
351
349
public ImmutableList < Record > RecordsFrom ( UniqueAddress observer )
352
350
{
353
351
var rows = ObserverRows ( observer ) ;
354
- if ( rows == null ) return ImmutableList . Create < Record > ( ) ;
352
+ if ( rows == null ) return ImmutableList < Record > . Empty ;
355
353
return rows . Values . ToImmutableList ( ) ;
356
354
}
357
355
356
+ /// only used for testing
358
357
/// <inheritdoc />
359
358
public override int GetHashCode ( )
360
359
{
361
360
return Versions . GetHashCode ( ) ;
362
361
}
363
362
363
+ /// only used for testing
364
364
/// <inheritdoc />
365
365
public override bool Equals ( object obj )
366
366
{
@@ -467,10 +467,10 @@ public Cache(ImmutableList<Record> records)
467
467
{
468
468
if ( records . IsEmpty )
469
469
{
470
- ObserverRowMap = ImmutableDictionary
471
- . Create < UniqueAddress , ImmutableDictionary < UniqueAddress , Record > > ( ) ;
472
- AllTerminated = ImmutableHashSet . Create < UniqueAddress > ( ) ;
473
- AllUnreachable = ImmutableHashSet . Create < UniqueAddress > ( ) ;
470
+ ObserverRowMap = ImmutableDictionary < UniqueAddress , ImmutableDictionary < UniqueAddress , Record > >
471
+ . Empty ;
472
+ AllTerminated = ImmutableHashSet < UniqueAddress > . Empty ;
473
+ AllUnreachable = ImmutableHashSet < UniqueAddress > . Empty ;
474
474
}
475
475
else
476
476
{
@@ -480,16 +480,12 @@ public Cache(ImmutableList<Record> records)
480
480
481
481
foreach ( var r in records )
482
482
{
483
- ImmutableDictionary < UniqueAddress , Record > m = mapBuilder . TryGetValue ( r . Observer , out m )
484
- ? m . SetItem ( r . Subject , r )
485
- //TODO: Other collections take items for Create. Create unnecessary array here
486
- : ImmutableDictionary . CreateRange ( new [ ]
487
- {
488
- new KeyValuePair < UniqueAddress , Record > ( r . Subject , r )
489
- } ) ;
483
+ ImmutableDictionary < UniqueAddress , Record > m = mapBuilder . TryGetValue ( r . Observer , out var mR )
484
+ ? mR . SetItem ( r . Subject , r )
485
+ : ImmutableDictionary < UniqueAddress , Record > . Empty . Add ( r . Subject , r ) ;
490
486
491
487
492
- mapBuilder . AddOrSet ( r . Observer , m ) ;
488
+ mapBuilder [ r . Observer ] = m ;
493
489
494
490
if ( r . Status == ReachabilityStatus . Unreachable ) unreachableBuilder . Add ( r . Subject ) ;
495
491
else if ( r . Status == ReachabilityStatus . Terminated ) terminatedBuilder . Add ( r . Subject ) ;
@@ -514,12 +510,12 @@ public ImmutableDictionary<UniqueAddress, ImmutableDictionary<UniqueAddress, Rec
514
510
}
515
511
516
512
/// <summary>
517
- /// TBD
513
+ /// Contains all nodes that have been observed as Terminated by at least one other node.
518
514
/// </summary>
519
515
public ImmutableHashSet < UniqueAddress > AllTerminated { get ; }
520
516
521
517
/// <summary>
522
- /// TBD
518
+ /// Contains all nodes that have been observed as Unreachable by at least one other node.
523
519
/// </summary>
524
520
public ImmutableHashSet < UniqueAddress > AllUnreachable { get ; }
525
521
0 commit comments