Commit 70c9882f authored by Nick Craver's avatar Nick Craver

Cleanup: ServerSelectionStrategy

parent 7e06b342
......@@ -8,7 +8,7 @@ internal sealed class ServerSelectionStrategy
{
public const int NoSlot = -1, MultipleSlots = -2;
private const int RedisClusterSlotCount = 16384;
static readonly ushort[] crc16tab =
private static readonly ushort[] crc16tab =
{
0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,
......@@ -57,11 +57,12 @@ public ServerSelectionStrategy(ConnectionMultiplexer multiplexer)
}
public ServerType ServerType { get { return serverType; } set { serverType = value; } }
internal int TotalSlots => RedisClusterSlotCount;
internal int TotalSlots => RedisClusterSlotCount;
/// <summary>
/// Computes the hash-slot that would be used by the given key
/// </summary>
/// </summary>
/// <param name="key">The <see cref="RedisKey"/> to determine a slot ID for.</param>
public unsafe int HashSlot(RedisKey key)
{
//HASH_SLOT = CRC16(key) mod 16384
......@@ -101,7 +102,6 @@ public ServerEndPoint Select(Message message)
slot = message.GetHashSlot(this);
if (slot == MultipleSlots) throw ExceptionFactory.MultiSlot(multiplexer.IncludeDetailInExceptions, message);
break;
}
return Select(slot, message.Command, message.Flags);
}
......@@ -184,14 +184,16 @@ internal int CombineSlot(int oldSlot, int newSlot)
if (oldSlot == NoSlot) return newSlot;
return oldSlot == newSlot ? oldSlot : MultipleSlots;
}
internal int CombineSlot(int oldSlot, RedisKey key)
{
if (oldSlot == MultipleSlots || key.IsNull) return oldSlot;
int newSlot = HashSlot(key);
int newSlot = HashSlot(key);
if (oldSlot == NoSlot) return newSlot;
return oldSlot == newSlot ? oldSlot : MultipleSlots;
}
internal int CountCoveredSlots()
{
var arr = map;
......@@ -211,7 +213,7 @@ internal void UpdateClusterRange(int fromInclusive, int toInclusive, ServerEndPo
}
}
static unsafe int IndexOf(byte* ptr, byte value, int start, int end)
private static unsafe int IndexOf(byte* ptr, byte value, int start, int end)
{
for (int offset = start; offset < end; offset++)
if (ptr[offset] == value) return offset;
......@@ -273,7 +275,7 @@ private ServerEndPoint Select(int slot, RedisCommand command, CommandFlags flags
ServerEndPoint endpoint = arr[slot], testing;
// but: ^^^ is the MASTER slots; if we want a slave, we need to do some thinking
if (endpoint != null)
{
switch (flags)
......
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