< Summary

Information
Class: NGql.Core.Features.MergeMemoScope
Assembly: NGql.Core
File(s): /home/runner/work/NGql/NGql/src/Core/Features/MergeMemoScope.cs
Line coverage
100%
Covered lines: 2
Uncovered lines: 0
Coverable lines: 2
Total lines: 48
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Version()100%11100%
Invalidate()100%11100%

File(s)

/home/runner/work/NGql/NGql/src/Core/Features/MergeMemoScope.cs

#LineLine coverage
 1namespace NGql.Core.Features;
 2
 3internal sealed class MergeMemoScope
 4{
 5    private long _version;
 6
 22567    internal long Version => Interlocked.Read(ref _version);
 8
 1689    internal void Invalidate() => Interlocked.Increment(ref _version);
 10}
 11
 12internal sealed class MergeMemoTracker
 13{
 14    private MergeMemoScope? _primary;
 15    private object? _gate;
 16    private volatile List<WeakReference<MergeMemoScope>>? _others;
 17
 18    internal void Attach(MergeMemoScope scope)
 19    {
 20        if (ReferenceEquals(Volatile.Read(ref _primary), scope)) return;
 21        var primary = Interlocked.CompareExchange(ref _primary, scope, null);
 22        if (primary is null || ReferenceEquals(primary, scope)) return;
 23        lock (LazyInitializer.EnsureInitialized(ref _gate))
 24        {
 25            _others ??= new();
 26            for (var i = _others.Count - 1; i >= 0; i--)
 27            {
 28                if (!_others[i].TryGetTarget(out var target)) _others.RemoveAt(i);
 29                else if (ReferenceEquals(target, scope)) return;
 30            }
 31            _others.Add(new WeakReference<MergeMemoScope>(scope));
 32        }
 33    }
 34
 35    internal void Invalidate()
 36    {
 37        Volatile.Read(ref _primary)?.Invalidate();
 38        if (_others is null) return;
 39        lock (_gate!)
 40        {
 41            for (var i = _others.Count - 1; i >= 0; i--)
 42            {
 43                if (_others[i].TryGetTarget(out var scope)) scope.Invalidate();
 44                else _others.RemoveAt(i);
 45            }
 46        }
 47    }
 48}

Methods/Properties

get_Version()
Invalidate()