< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Repositories.Memory.ChannelMemoryRepository
Assembly: NLightning.Infrastructure.Repositories
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Repositories/Memory/ChannelMemoryRepository.cs
Tag: 36_15743069263
Line coverage
0%
Covered lines: 0
Uncovered lines: 65
Coverable lines: 65
Total lines: 114
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 12
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Repositories/Memory/ChannelMemoryRepository.cs

#LineLine coverage
 1using System.Collections.Concurrent;
 2using System.Diagnostics.CodeAnalysis;
 3
 4namespace NLightning.Infrastructure.Repositories.Memory;
 5
 6using Domain.Channels.Enums;
 7using Domain.Channels.Interfaces;
 8using Domain.Channels.Models;
 9using Domain.Channels.ValueObjects;
 10using Domain.Crypto.ValueObjects;
 11
 12public class ChannelMemoryRepository : IChannelMemoryRepository
 13{
 014    private readonly ConcurrentDictionary<ChannelId, ChannelModel> _channels = [];
 015    private readonly ConcurrentDictionary<ChannelId, ChannelState> _channelStates = [];
 016    private readonly ConcurrentDictionary<(CompactPubKey, ChannelId), ChannelModel> _temporaryChannels = [];
 017    private readonly ConcurrentDictionary<(CompactPubKey, ChannelId), ChannelState> _temporaryChannelStates = [];
 18
 19    public bool TryGetChannel(ChannelId channelId, [MaybeNullWhen(false)] out ChannelModel channel)
 020    {
 021        return _channels.TryGetValue(channelId, out channel);
 022    }
 23
 24    public List<ChannelModel> FindChannels(Func<ChannelModel, bool> predicate)
 025    {
 026        return _channels
 027              .Values
 028              .Where(predicate)
 029              .ToList();
 030    }
 31
 32    public bool TryGetChannelState(ChannelId channelId, out ChannelState channelState)
 033    {
 034        return _channelStates.TryGetValue(channelId, out channelState);
 035    }
 36
 37    public void AddChannel(ChannelModel channel)
 038    {
 039        ArgumentNullException.ThrowIfNull(channel);
 40
 041        if (!_channels.TryAdd(channel.ChannelId, channel))
 042            throw new InvalidOperationException($"Channel with Id {channel.ChannelId} already exists.");
 43
 044        _channelStates[channel.ChannelId] = channel.State;
 045    }
 46
 47    public void UpdateChannel(ChannelModel channel)
 048    {
 049        ArgumentNullException.ThrowIfNull(channel);
 50
 051        if (!_channels.ContainsKey(channel.ChannelId))
 052            throw new KeyNotFoundException($"Channel with Id {channel.ChannelId} does not exist.");
 53
 054        _channels[channel.ChannelId] = channel;
 055        _channelStates[channel.ChannelId] = channel.State;
 056    }
 57
 58    public void RemoveChannel(ChannelId channelId)
 059    {
 060        if (!_channels.TryRemove(channelId, out _))
 061            throw new KeyNotFoundException($"Channel with Id {channelId} does not exist.");
 62
 063        _channelStates.TryRemove(channelId, out _);
 064    }
 65
 66    public bool TryGetTemporaryChannel(CompactPubKey compactPubKey, ChannelId channelId,
 67                                       [MaybeNullWhen(false)] out ChannelModel channel)
 068    {
 069        return _temporaryChannels.TryGetValue((compactPubKey, channelId), out channel);
 070    }
 71
 72    public bool TryGetTemporaryChannelState(CompactPubKey compactPubKey, ChannelId channelId,
 73                                            out ChannelState channelState)
 074    {
 075        return _temporaryChannelStates.TryGetValue((compactPubKey, channelId), out channelState);
 076    }
 77
 78    public void AddTemporaryChannel(CompactPubKey compactPubKey, ChannelModel channel)
 079    {
 080        ArgumentNullException.ThrowIfNull(compactPubKey);
 081        ArgumentNullException.ThrowIfNull(channel.ChannelId);
 82
 083        if (!_temporaryChannels.TryAdd((compactPubKey, channel.ChannelId), channel))
 084            throw new InvalidOperationException(
 085                $"Temporary channel with Id {channel.ChannelId} for CompactPubKey {compactPubKey} already exists.");
 86
 087        _temporaryChannelStates[(compactPubKey, channel.ChannelId)] = channel.State;
 088    }
 89
 90    public void UpdateTemporaryChannel(CompactPubKey compactPubKey, ChannelModel channel)
 091    {
 092        ArgumentNullException.ThrowIfNull(compactPubKey);
 093        ArgumentNullException.ThrowIfNull(channel.ChannelId);
 94
 095        if (!_temporaryChannels.ContainsKey((compactPubKey, channel.ChannelId)))
 096            throw new KeyNotFoundException(
 097                $"Temporary channel with Id {channel.ChannelId} for CompactPubKey {compactPubKey} does not exist.");
 98
 099        _temporaryChannels[(compactPubKey, channel.ChannelId)] = channel;
 0100        _temporaryChannelStates[(compactPubKey, channel.ChannelId)] = channel.State;
 0101    }
 102
 103    public void RemoveTemporaryChannel(CompactPubKey compactPubKey, ChannelId channelId)
 0104    {
 0105        ArgumentNullException.ThrowIfNull(compactPubKey);
 0106        ArgumentNullException.ThrowIfNull(channelId);
 107
 0108        if (!_temporaryChannels.TryRemove((compactPubKey, channelId), out _))
 0109            throw new KeyNotFoundException(
 0110                $"Temporary channel with Id {channelId} for CompactPubKey {compactPubKey} does not exist.");
 111
 0112        _temporaryChannelStates.TryRemove((compactPubKey, channelId), out _);
 0113    }
 114}

Methods/Properties

.ctor()
TryGetChannel(NLightning.Domain.Channels.ValueObjects.ChannelId,NLightning.Domain.Channels.Models.ChannelModel&)
TryGetChannel(NLightning.Domain.Channels.ValueObjects.ChannelId,NLightning.Domain.Channels.Models.ChannelModel&)
FindChannels(System.Func`2<NLightning.Domain.Channels.Models.ChannelModel,System.Boolean>)
FindChannels(System.Func`2<NLightning.Domain.Channels.Models.ChannelModel,System.Boolean>)
TryGetChannelState(NLightning.Domain.Channels.ValueObjects.ChannelId,NLightning.Domain.Channels.Enums.ChannelState&)
TryGetChannelState(NLightning.Domain.Channels.ValueObjects.ChannelId,NLightning.Domain.Channels.Enums.ChannelState&)
AddChannel(NLightning.Domain.Channels.Models.ChannelModel)
AddChannel(NLightning.Domain.Channels.Models.ChannelModel)
UpdateChannel(NLightning.Domain.Channels.Models.ChannelModel)
UpdateChannel(NLightning.Domain.Channels.Models.ChannelModel)
RemoveChannel(NLightning.Domain.Channels.ValueObjects.ChannelId)
RemoveChannel(NLightning.Domain.Channels.ValueObjects.ChannelId)
TryGetTemporaryChannel(NLightning.Domain.Crypto.ValueObjects.CompactPubKey,NLightning.Domain.Channels.ValueObjects.ChannelId,NLightning.Domain.Channels.Models.ChannelModel&)
TryGetTemporaryChannel(NLightning.Domain.Crypto.ValueObjects.CompactPubKey,NLightning.Domain.Channels.ValueObjects.ChannelId,NLightning.Domain.Channels.Models.ChannelModel&)
TryGetTemporaryChannelState(NLightning.Domain.Crypto.ValueObjects.CompactPubKey,NLightning.Domain.Channels.ValueObjects.ChannelId,NLightning.Domain.Channels.Enums.ChannelState&)
TryGetTemporaryChannelState(NLightning.Domain.Crypto.ValueObjects.CompactPubKey,NLightning.Domain.Channels.ValueObjects.ChannelId,NLightning.Domain.Channels.Enums.ChannelState&)
AddTemporaryChannel(NLightning.Domain.Crypto.ValueObjects.CompactPubKey,NLightning.Domain.Channels.Models.ChannelModel)
AddTemporaryChannel(NLightning.Domain.Crypto.ValueObjects.CompactPubKey,NLightning.Domain.Channels.Models.ChannelModel)
UpdateTemporaryChannel(NLightning.Domain.Crypto.ValueObjects.CompactPubKey,NLightning.Domain.Channels.Models.ChannelModel)
UpdateTemporaryChannel(NLightning.Domain.Crypto.ValueObjects.CompactPubKey,NLightning.Domain.Channels.Models.ChannelModel)
RemoveTemporaryChannel(NLightning.Domain.Crypto.ValueObjects.CompactPubKey,NLightning.Domain.Channels.ValueObjects.ChannelId)
RemoveTemporaryChannel(NLightning.Domain.Crypto.ValueObjects.CompactPubKey,NLightning.Domain.Channels.ValueObjects.ChannelId)