Files
natsdotnet/src/NATS.Server/LeafNodes/LeafHubSpokeMapper.cs
2026-02-23 13:08:52 -05:00

31 lines
1.0 KiB
C#

namespace NATS.Server.LeafNodes;
public enum LeafMapDirection
{
Inbound,
Outbound,
}
public sealed record LeafMappingResult(string Account, string Subject);
public sealed class LeafHubSpokeMapper
{
private readonly IReadOnlyDictionary<string, string> _hubToSpoke;
private readonly IReadOnlyDictionary<string, string> _spokeToHub;
public LeafHubSpokeMapper(IReadOnlyDictionary<string, string> hubToSpoke)
{
_hubToSpoke = hubToSpoke;
_spokeToHub = hubToSpoke.ToDictionary(static p => p.Value, static p => p.Key, StringComparer.Ordinal);
}
public LeafMappingResult Map(string account, string subject, LeafMapDirection direction)
{
if (direction == LeafMapDirection.Outbound && _hubToSpoke.TryGetValue(account, out var spoke))
return new LeafMappingResult(spoke, subject);
if (direction == LeafMapDirection.Inbound && _spokeToHub.TryGetValue(account, out var hub))
return new LeafMappingResult(hub, subject);
return new LeafMappingResult(account, subject);
}
}