NonAckedCountCache.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Abc.Zebus.Util.Extensions;
  4. namespace Abc.Zebus.Persistence.Storage
  5. {
  6. public class NonAckedCountCache
  7. {
  8. private readonly Dictionary<PeerId, NonAckedCount> _nonAckedCounts = new Dictionary<PeerId, NonAckedCount>();
  9. public IEnumerable<NonAckedCount> GetForUpdatedPeers(ICollection<(PeerId, int)> allPeerStates)
  10. {
  11. var updatedPeers = (from peerState in allPeerStates
  12. let count = _nonAckedCounts.GetValueOrDefault(peerState.Item1, id => new NonAckedCount(id, -42))
  13. where count.Count != peerState.Item2
  14. select new NonAckedCount(peerState.Item1, peerState.Item2)).ToList();
  15. foreach (var peerState in allPeerStates)
  16. {
  17. _nonAckedCounts[peerState.Item1] = new NonAckedCount(peerState.Item1, peerState.Item2);
  18. }
  19. return updatedPeers;
  20. }
  21. }
  22. public readonly struct NonAckedCount
  23. {
  24. public readonly PeerId PeerId;
  25. public readonly int Count;
  26. public NonAckedCount(PeerId peerId, int count)
  27. {
  28. PeerId = peerId;
  29. Count = count;
  30. }
  31. }
  32. }