Commit 819a608e authored by Nick Craver's avatar Nick Craver

Cleanup: CommandMap

parent 64d6f132
...@@ -64,7 +64,7 @@ internal CommandMap(byte[][] map) ...@@ -64,7 +64,7 @@ internal CommandMap(byte[][] map)
"get", "set", "del", "incr", "incrby", "mget", "mset", "keys", "getset", "setnx", "get", "set", "del", "incr", "incrby", "mget", "mset", "keys", "getset", "setnx",
"hget", "hset", "hdel", "hincrby", "hkeys", "hvals", "hmget", "hmset", "hlen", "hget", "hset", "hdel", "hincrby", "hkeys", "hvals", "hmget", "hmset", "hlen",
"zscore", "zadd", "zrem", "zrange", "zrangebyscore", "zincrby", "zdecrby", "zcard", "zscore", "zadd", "zrem", "zrange", "zrangebyscore", "zincrby", "zdecrby", "zcard",
"llen", "lpush", "rpush", "lpop", "rpop", "lrange", "lindex" "llen", "lpush", "rpush", "lpop", "rpop", "lrange", "lindex"
}, true); }, true);
/// <summary> /// <summary>
...@@ -78,6 +78,7 @@ internal CommandMap(byte[][] map) ...@@ -78,6 +78,7 @@ internal CommandMap(byte[][] map)
/// <summary> /// <summary>
/// Create a new CommandMap, customizing some commands /// Create a new CommandMap, customizing some commands
/// </summary> /// </summary>
/// <param name="overrides">The commands to override.</param>
public static CommandMap Create(Dictionary<string, string> overrides) public static CommandMap Create(Dictionary<string, string> overrides)
{ {
if (overrides == null || overrides.Count == 0) return Default; if (overrides == null || overrides.Count == 0) return Default;
...@@ -98,9 +99,10 @@ public static CommandMap Create(Dictionary<string, string> overrides) ...@@ -98,9 +99,10 @@ public static CommandMap Create(Dictionary<string, string> overrides)
/// <summary> /// <summary>
/// Creates a CommandMap by specifying which commands are available or unavailable /// Creates a CommandMap by specifying which commands are available or unavailable
/// </summary> /// </summary>
/// <param name="commands">The commands to specify.</param>
/// <param name="available">Whether the commands are available or excluded.</param>
public static CommandMap Create(HashSet<string> commands, bool available = true) public static CommandMap Create(HashSet<string> commands, bool available = true)
{ {
if (available) if (available)
{ {
var dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); var dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
...@@ -127,8 +129,7 @@ public static CommandMap Create(HashSet<string> commands, bool available = true) ...@@ -127,8 +129,7 @@ public static CommandMap Create(HashSet<string> commands, bool available = true)
// nix the things that are specified // nix the things that are specified
foreach (var command in commands) foreach (var command in commands)
{ {
RedisCommand parsed; if (Enum.TryParse(command, true, out RedisCommand parsed))
if (Enum.TryParse(command, true, out parsed))
{ {
(exclusions ?? (exclusions = new HashSet<RedisCommand>())).Add(parsed); (exclusions ?? (exclusions = new HashSet<RedisCommand>())).Add(parsed);
} }
...@@ -137,7 +138,6 @@ public static CommandMap Create(HashSet<string> commands, bool available = true) ...@@ -137,7 +138,6 @@ public static CommandMap Create(HashSet<string> commands, bool available = true)
if (exclusions == null || exclusions.Count == 0) return Default; if (exclusions == null || exclusions.Count == 0) return Default;
return CreateImpl(null, exclusions); return CreateImpl(null, exclusions);
} }
} }
/// <summary> /// <summary>
...@@ -169,10 +169,8 @@ internal void AssertAvailable(RedisCommand command) ...@@ -169,10 +169,8 @@ internal void AssertAvailable(RedisCommand command)
if (map[(int)command] == null) throw ExceptionFactory.CommandDisabled(false, command, null, null); if (map[(int)command] == null) throw ExceptionFactory.CommandDisabled(false, command, null, null);
} }
internal byte[] GetBytes(RedisCommand command) internal byte[] GetBytes(RedisCommand command) => map[(int)command];
{
return map[(int)command];
}
internal byte[] GetBytes(string command) internal byte[] GetBytes(string command)
{ {
if (command == null) return null; if (command == null) return null;
...@@ -195,37 +193,31 @@ internal byte[] GetBytes(string command) ...@@ -195,37 +193,31 @@ internal byte[] GetBytes(string command)
} }
return bytes; return bytes;
} }
static readonly Hashtable _unknownCommands = new Hashtable();
internal bool IsAvailable(RedisCommand command) private static readonly Hashtable _unknownCommands = new Hashtable();
{
return map[(int)command] != null; internal bool IsAvailable(RedisCommand command) => map[(int)command] != null;
}
private static CommandMap CreateImpl(Dictionary<string, string> caseInsensitiveOverrides, HashSet<RedisCommand> exclusions) private static CommandMap CreateImpl(Dictionary<string, string> caseInsensitiveOverrides, HashSet<RedisCommand> exclusions)
{ {
var commands = (RedisCommand[])Enum.GetValues(typeof(RedisCommand)); var commands = (RedisCommand[])Enum.GetValues(typeof(RedisCommand));
byte[][] map = new byte[commands.Length][]; var map = new byte[commands.Length][];
bool haveDelta = false; bool haveDelta = false;
for (int i = 0; i < commands.Length; i++) for (int i = 0; i < commands.Length; i++)
{ {
int idx = (int)commands[i]; int idx = (int)commands[i];
string name = commands[i].ToString(), value = name; string name = commands[i].ToString(), value = name;
if (exclusions != null && exclusions.Contains(commands[i])) if (exclusions?.Contains(commands[i]) == true)
{ {
map[idx] = null; map[idx] = null;
} }
else else
{ {
if (caseInsensitiveOverrides != null) if (caseInsensitiveOverrides != null && caseInsensitiveOverrides.TryGetValue(name, out string tmp))
{ {
string tmp; value = tmp;
if (caseInsensitiveOverrides.TryGetValue(name, out tmp))
{
value = tmp;
}
} }
if (value != name) haveDelta = true; if (value != name) haveDelta = true;
// TODO: bug? // TODO: bug?
......
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