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

Cleanup: CommandMap

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