using LogosStorageClient; namespace StoragePlugin.OverwatchSupport { public class IdentityMap { private readonly List nodes = new List(); private readonly Dictionary nameIndexMap = new Dictionary(); private readonly Dictionary shortToLong = new Dictionary(); public void Add(string name, string peerId, string nodeId) { Add(new StorageNodeIdentity { Name = name, PeerId = peerId, NodeId = nodeId }); nameIndexMap.Add(name, nameIndexMap.Count); } public void Add(StorageNodeIdentity identity) { if (string.IsNullOrWhiteSpace(identity.Name)) throw new Exception("Name required"); if (string.IsNullOrWhiteSpace(identity.PeerId) || identity.PeerId.Length < 11) throw new Exception("PeerId invalid"); if (string.IsNullOrWhiteSpace(identity.NodeId) || identity.NodeId.Length < 11) throw new Exception("NodeId invalid"); nodes.Add(identity); shortToLong.Add(LogosStorageUtils.ToShortId(identity.PeerId), identity.PeerId); shortToLong.Add(LogosStorageUtils.ToNodeIdShortId(identity.NodeId), identity.NodeId); } public StorageNodeIdentity[] Get() { return nodes.ToArray(); } public int GetIndex(string name) { return nameIndexMap[name]; } public StorageNodeIdentity GetId(string name) { return nodes.Single(n => n.Name == name); } public string ReplaceShortIds(string value) { var result = value; foreach (var pair in shortToLong) { result = result.Replace(pair.Key, pair.Value); } return result; } public int Size { get { return nodes.Count; } } } }