| | 1 | | using System.Collections.Concurrent; |
| | 2 | | using System.Diagnostics.CodeAnalysis; |
| | 3 | |
|
| | 4 | | namespace NLightning.Infrastructure.Repositories.Memory; |
| | 5 | |
|
| | 6 | | using Domain.Channels.Enums; |
| | 7 | | using Domain.Channels.Interfaces; |
| | 8 | | using Domain.Channels.Models; |
| | 9 | | using Domain.Channels.ValueObjects; |
| | 10 | | using Domain.Crypto.ValueObjects; |
| | 11 | |
|
| | 12 | | public class ChannelMemoryRepository : IChannelMemoryRepository |
| | 13 | | { |
| 0 | 14 | | private readonly ConcurrentDictionary<ChannelId, ChannelModel> _channels = []; |
| 0 | 15 | | private readonly ConcurrentDictionary<ChannelId, ChannelState> _channelStates = []; |
| 0 | 16 | | private readonly ConcurrentDictionary<(CompactPubKey, ChannelId), ChannelModel> _temporaryChannels = []; |
| 0 | 17 | | private readonly ConcurrentDictionary<(CompactPubKey, ChannelId), ChannelState> _temporaryChannelStates = []; |
| | 18 | |
|
| | 19 | | public bool TryGetChannel(ChannelId channelId, [MaybeNullWhen(false)] out ChannelModel channel) |
| 0 | 20 | | { |
| 0 | 21 | | return _channels.TryGetValue(channelId, out channel); |
| 0 | 22 | | } |
| | 23 | |
|
| | 24 | | public List<ChannelModel> FindChannels(Func<ChannelModel, bool> predicate) |
| 0 | 25 | | { |
| 0 | 26 | | return _channels |
| 0 | 27 | | .Values |
| 0 | 28 | | .Where(predicate) |
| 0 | 29 | | .ToList(); |
| 0 | 30 | | } |
| | 31 | |
|
| | 32 | | public bool TryGetChannelState(ChannelId channelId, out ChannelState channelState) |
| 0 | 33 | | { |
| 0 | 34 | | return _channelStates.TryGetValue(channelId, out channelState); |
| 0 | 35 | | } |
| | 36 | |
|
| | 37 | | public void AddChannel(ChannelModel channel) |
| 0 | 38 | | { |
| 0 | 39 | | ArgumentNullException.ThrowIfNull(channel); |
| | 40 | |
|
| 0 | 41 | | if (!_channels.TryAdd(channel.ChannelId, channel)) |
| 0 | 42 | | throw new InvalidOperationException($"Channel with Id {channel.ChannelId} already exists."); |
| | 43 | |
|
| 0 | 44 | | _channelStates[channel.ChannelId] = channel.State; |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | public void UpdateChannel(ChannelModel channel) |
| 0 | 48 | | { |
| 0 | 49 | | ArgumentNullException.ThrowIfNull(channel); |
| | 50 | |
|
| 0 | 51 | | if (!_channels.ContainsKey(channel.ChannelId)) |
| 0 | 52 | | throw new KeyNotFoundException($"Channel with Id {channel.ChannelId} does not exist."); |
| | 53 | |
|
| 0 | 54 | | _channels[channel.ChannelId] = channel; |
| 0 | 55 | | _channelStates[channel.ChannelId] = channel.State; |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | public void RemoveChannel(ChannelId channelId) |
| 0 | 59 | | { |
| 0 | 60 | | if (!_channels.TryRemove(channelId, out _)) |
| 0 | 61 | | throw new KeyNotFoundException($"Channel with Id {channelId} does not exist."); |
| | 62 | |
|
| 0 | 63 | | _channelStates.TryRemove(channelId, out _); |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | public bool TryGetTemporaryChannel(CompactPubKey compactPubKey, ChannelId channelId, |
| | 67 | | [MaybeNullWhen(false)] out ChannelModel channel) |
| 0 | 68 | | { |
| 0 | 69 | | return _temporaryChannels.TryGetValue((compactPubKey, channelId), out channel); |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | public bool TryGetTemporaryChannelState(CompactPubKey compactPubKey, ChannelId channelId, |
| | 73 | | out ChannelState channelState) |
| 0 | 74 | | { |
| 0 | 75 | | return _temporaryChannelStates.TryGetValue((compactPubKey, channelId), out channelState); |
| 0 | 76 | | } |
| | 77 | |
|
| | 78 | | public void AddTemporaryChannel(CompactPubKey compactPubKey, ChannelModel channel) |
| 0 | 79 | | { |
| 0 | 80 | | ArgumentNullException.ThrowIfNull(compactPubKey); |
| 0 | 81 | | ArgumentNullException.ThrowIfNull(channel.ChannelId); |
| | 82 | |
|
| 0 | 83 | | if (!_temporaryChannels.TryAdd((compactPubKey, channel.ChannelId), channel)) |
| 0 | 84 | | throw new InvalidOperationException( |
| 0 | 85 | | $"Temporary channel with Id {channel.ChannelId} for CompactPubKey {compactPubKey} already exists."); |
| | 86 | |
|
| 0 | 87 | | _temporaryChannelStates[(compactPubKey, channel.ChannelId)] = channel.State; |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | public void UpdateTemporaryChannel(CompactPubKey compactPubKey, ChannelModel channel) |
| 0 | 91 | | { |
| 0 | 92 | | ArgumentNullException.ThrowIfNull(compactPubKey); |
| 0 | 93 | | ArgumentNullException.ThrowIfNull(channel.ChannelId); |
| | 94 | |
|
| 0 | 95 | | if (!_temporaryChannels.ContainsKey((compactPubKey, channel.ChannelId))) |
| 0 | 96 | | throw new KeyNotFoundException( |
| 0 | 97 | | $"Temporary channel with Id {channel.ChannelId} for CompactPubKey {compactPubKey} does not exist."); |
| | 98 | |
|
| 0 | 99 | | _temporaryChannels[(compactPubKey, channel.ChannelId)] = channel; |
| 0 | 100 | | _temporaryChannelStates[(compactPubKey, channel.ChannelId)] = channel.State; |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | public void RemoveTemporaryChannel(CompactPubKey compactPubKey, ChannelId channelId) |
| 0 | 104 | | { |
| 0 | 105 | | ArgumentNullException.ThrowIfNull(compactPubKey); |
| 0 | 106 | | ArgumentNullException.ThrowIfNull(channelId); |
| | 107 | |
|
| 0 | 108 | | if (!_temporaryChannels.TryRemove((compactPubKey, channelId), out _)) |
| 0 | 109 | | throw new KeyNotFoundException( |
| 0 | 110 | | $"Temporary channel with Id {channelId} for CompactPubKey {compactPubKey} does not exist."); |
| | 111 | |
|
| 0 | 112 | | _temporaryChannelStates.TryRemove((compactPubKey, channelId), out _); |
| 0 | 113 | | } |
| | 114 | | } |