Commit 64d6f132 authored by Nick Craver's avatar Nick Craver

Cleanup: ClusterConfiguration

parent 2855c627
......@@ -17,6 +17,8 @@ public struct SlotRange : IEquatable<SlotRange>, IComparable<SlotRange>, ICompar
/// <summary>
/// Create a new SlotRange value
/// </summary>
/// <param name="from">The slot ID to start at.</param>
/// <param name="to">The slot ID to end at.</param>
public SlotRange(int from, int to)
{
checked
......@@ -44,22 +46,22 @@ private SlotRange(short from, short to)
/// <summary>
/// Indicates whether two ranges are not equal
/// </summary>
public static bool operator !=(SlotRange x, SlotRange y)
{
return x.from != y.from || x.to != y.to;
}
/// <param name="x">The first slot range.</param>
/// <param name="y">The second slot range.</param>
public static bool operator !=(SlotRange x, SlotRange y) => x.from != y.from || x.to != y.to;
/// <summary>
/// Indicates whether two ranges are equal
/// Indicates whether two ranges are equal.
/// </summary>
public static bool operator ==(SlotRange x, SlotRange y)
{
return x.from == y.from && x.to == y.to;
}
/// <param name="x">The first slot range.</param>
/// <param name="y">The second slot range.</param>
public static bool operator ==(SlotRange x, SlotRange y) => x.from == y.from && x.to == y.to;
/// <summary>
/// Try to parse a string as a range
/// Try to parse a string as a range.
/// </summary>
/// <param name="range">The range string to parse, e.g."1-12".</param>
/// <param name="value">The parsed <see cref="SlotRange"/>, if successful.</param>
public static bool TryParse(string range, out SlotRange value)
{
if (string.IsNullOrWhiteSpace(range))
......@@ -68,7 +70,7 @@ public static bool TryParse(string range, out SlotRange value)
return false;
}
int i = range.IndexOf('-');
short from, to;
short from;
if (i < 0)
{
if (TryParseInt16(range, 0, range.Length, out from))
......@@ -79,7 +81,7 @@ public static bool TryParse(string range, out SlotRange value)
}
else
{
if (TryParseInt16(range, 0, i++, out from) && TryParseInt16(range, i, range.Length - i, out to))
if (TryParseInt16(range, 0, i++, out from) && TryParseInt16(range, i, range.Length - i, out short to))
{
value = new SlotRange(from, to);
return true;
......@@ -92,6 +94,7 @@ public static bool TryParse(string range, out SlotRange value)
/// <summary>
/// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
/// </summary>
/// <param name="other">The other slot range to compare to.</param>
public int CompareTo(SlotRange other)
{
int delta = (int)this.from - (int)other.from;
......@@ -101,21 +104,14 @@ public int CompareTo(SlotRange other)
/// <summary>
/// See Object.Equals
/// </summary>
public override bool Equals(object obj)
{
if (obj is SlotRange)
{
return Equals((SlotRange)obj);
}
return false;
}
/// <param name="obj">The other slot range to compare to.</param>
public override bool Equals(object obj) => obj is SlotRange sRange && Equals(sRange);
/// <summary>
/// Indicates whether two ranges are equal
/// </summary>
public bool Equals(SlotRange other)
{
return other.from == this.from && other.to == this.to;
}
/// <param name="other">The other slot range to compare to.</param>
public bool Equals(SlotRange other) => other.from == from && other.to == to;
/// <summary>
/// See Object.GetHashCode()
......@@ -129,16 +125,11 @@ public override int GetHashCode()
/// <summary>
/// See Object.ToString()
/// </summary>
public override string ToString()
{
return from == to ? from.ToString() : (from + "-" + to);
}
internal bool Includes(int hashSlot)
{
return hashSlot >= from && hashSlot <= to;
}
public override string ToString() => from == to ? from.ToString() : (from + "-" + to);
internal bool Includes(int hashSlot) => hashSlot >= from && hashSlot <= to;
static bool TryParseInt16(string s, int offset, int count, out short value)
private static bool TryParseInt16(string s, int offset, int count, out short value)
{
checked
{
......@@ -155,10 +146,7 @@ static bool TryParseInt16(string s, int offset, int count, out short value)
}
}
int IComparable.CompareTo(object obj)
{
return obj is SlotRange ? CompareTo((SlotRange)obj) : -1;
}
int IComparable.CompareTo(object obj) => obj is SlotRange sRange ? CompareTo(sRange) : -1;
}
/// <summary>
......@@ -236,15 +224,10 @@ internal ClusterConfiguration(ServerSelectionStrategy serverSelectionStrategy, s
/// <summary>
/// Obtain the node relating to a specified endpoint
/// </summary>
public ClusterNode this[EndPoint endpoint]
{
get
{
ClusterNode result;
return endpoint == null ? null
: nodeLookup.TryGetValue(endpoint, out result) ? result : null;
}
}
/// <param name="endpoint">The endpoint to get a cluster node from.</param>
public ClusterNode this[EndPoint endpoint] => endpoint == null
? null
: nodeLookup.TryGetValue(endpoint, out ClusterNode result) ? result : null;
internal ClusterNode this[string nodeId]
{
......@@ -260,8 +243,9 @@ internal ClusterConfiguration(ServerSelectionStrategy serverSelectionStrategy, s
}
/// <summary>
/// Gets the node that serves the specified slot
/// Gets the node that serves the specified slot.
/// </summary>
/// <param name="slot">The slot ID to get a node by.</param>
public ClusterNode GetBySlot(int slot)
{
foreach(var node in Nodes)
......@@ -270,18 +254,16 @@ public ClusterNode GetBySlot(int slot)
}
return null;
}
/// <summary>
/// Gets the node that serves the specified slot
/// Gets the node that serves the specified key's slot.
/// </summary>
public ClusterNode GetBySlot(RedisKey key)
{
return GetBySlot(serverSelectionStrategy.HashSlot(key));
/// <param name="key">The key to identify a node by.</param>
public ClusterNode GetBySlot(RedisKey key) => GetBySlot(serverSelectionStrategy.HashSlot(key));
}
}
/// <summary>
/// Represents the configuration of a single node in a cluster configuration
/// Represents the configuration of a single node in a cluster configuration.
/// </summary>
public sealed class ClusterNode : IEquatable<ClusterNode>, IComparable<ClusterNode>, IComparable
{
......@@ -335,15 +317,14 @@ internal ClusterNode(ClusterConfiguration configuration, string raw, EndPoint or
for (int i = 8; i < parts.Length; i++)
{
SlotRange range;
if (SlotRange.TryParse(parts[i], out range))
if (SlotRange.TryParse(parts[i], out SlotRange range))
{
if(slots == null) slots = new List<SlotRange>(parts.Length - i);
if (slots == null) slots = new List<SlotRange>(parts.Length - i);
slots.Add(range);
}
}
this.Slots = slots?.AsReadOnly() ?? NoSlots;
this.IsConnected = parts[7] == "connected"; // Can be "connected" or "disconnected"
Slots = slots?.AsReadOnly() ?? NoSlots;
IsConnected = parts[7] == "connected"; // Can be "connected" or "disconnected"
}
/// <summary>
/// Gets all child nodes of the current node
......@@ -430,6 +411,7 @@ public ClusterNode Parent
/// <summary>
/// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
/// </summary>
/// <param name="other">The <see cref="ClusterNode"/> to compare to.</param>
public int CompareTo(ClusterNode other)
{
if (other == null) return -1;
......@@ -442,34 +424,29 @@ public int CompareTo(ClusterNode other)
if (i != 0) return i;
}
return string.CompareOrdinal(this.NodeId, other.NodeId);
}
/// <summary>
/// See Object.Equals
/// </summary>
public override bool Equals(object obj)
{
return Equals(obj as ClusterNode);
}
/// <param name="obj">The <see cref="ClusterNode"/> to compare to.</param>
public override bool Equals(object obj) => Equals(obj as ClusterNode);
/// <summary>
/// Indicates whether two ClusterNode instances are equivalent
/// </summary>
/// <param name="other">The <see cref="ClusterNode"/> to compare to.</param>
public bool Equals(ClusterNode other)
{
if (other == null) return false;
return this.ToString() == other.ToString(); // lazy, but effective - plus only computes once
return ToString() == other.ToString(); // lazy, but effective - plus only computes once
}
/// <summary>
/// See object.GetHashCode()
/// </summary>
public override int GetHashCode()
{
return ToString().GetHashCode();
}
public override int GetHashCode() => ToString().GetHashCode();
/// <summary>
/// See Object.ToString()
......@@ -498,10 +475,11 @@ public override string ToString()
{
sb.Append(slot).Append(' ');
}
sb.Length -= 1; // remove tailing space
sb.Length--; // remove tailing space
}
return toString = sb.ToString();
}
internal bool ServesSlot(int hashSlot)
{
foreach (var slot in Slots)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment