Commit d5ca30fa authored by Marc Gravell's avatar Marc Gravell

The .Assert() names were amibguous (unclear of intent), and I didn't like the stack-copy semantic

parent e7a3485e
......@@ -563,7 +563,8 @@ internal abstract class CommandChannelBase : Message
public CommandChannelBase(int db, CommandFlags flags, RedisCommand command, RedisChannel channel) : base(db, flags, command)
{
this.Channel = channel.Assert();
channel.AssertNotNull();
this.Channel = channel;
}
public override string CommandAndKey { get { return Command + " " + Channel; } }
......@@ -575,7 +576,8 @@ internal abstract class CommandKeyBase : Message
public CommandKeyBase(int db, CommandFlags flags, RedisCommand command, RedisKey key) : base(db, flags, command)
{
this.Key = key.Assert();
key.AssertNotNull();
this.Key = key;
}
public override string CommandAndKey { get { return Command + " " + Key; } }
......@@ -600,7 +602,8 @@ sealed class CommandChannelValueMessage : CommandChannelBase
private readonly RedisValue value;
public CommandChannelValueMessage(int db, CommandFlags flags, RedisCommand command, RedisChannel channel, RedisValue value) : base(db, flags, command, channel)
{
this.value = value.Assert();
value.AssertNotNull();
this.value = value;
}
internal override void WriteImpl(PhysicalConnection physical)
{
......@@ -615,8 +618,10 @@ sealed class CommandKeyKeyKeyMessage : CommandKeyBase
private readonly RedisKey key1, key2;
public CommandKeyKeyKeyMessage(int db, CommandFlags flags, RedisCommand command, RedisKey key0, RedisKey key1, RedisKey key2) : base(db, flags, command, key0)
{
this.key1 = key1.Assert();
this.key2 = key2.Assert();
key1.AssertNotNull();
key2.AssertNotNull();
this.key1 = key1;
this.key2 = key2;
}
public override int GetHashSlot(ServerSelectionStrategy serverSelectionStrategy)
{
......@@ -640,7 +645,8 @@ class CommandKeyKeyMessage : CommandKeyBase
protected readonly RedisKey key1;
public CommandKeyKeyMessage(int db, CommandFlags flags, RedisCommand command, RedisKey key0, RedisKey key1) : base(db, flags, command, key0)
{
this.key1 = key1.Assert();
key1.AssertNotNull();
this.key1 = key1;
}
public override int GetHashSlot(ServerSelectionStrategy serverSelectionStrategy)
{
......@@ -663,7 +669,7 @@ public CommandKeyKeysMessage(int db, CommandFlags flags, RedisCommand command, R
{
for (int i = 0; i < keys.Length; i++)
{
keys[i].Assert();
keys[i].AssertNotNull();
}
this.keys = keys;
}
......@@ -692,7 +698,8 @@ sealed class CommandKeyKeyValueMessage : CommandKeyKeyMessage
private readonly RedisValue value;
public CommandKeyKeyValueMessage(int db, CommandFlags flags, RedisCommand command, RedisKey key0, RedisKey key1, RedisValue value) : base(db, flags, command, key0, key1)
{
this.value = value.Assert();
value.AssertNotNull();
this.value = value;
}
internal override void WriteImpl(PhysicalConnection physical)
......@@ -720,7 +727,7 @@ public CommandKeysMessage(int db, CommandFlags flags, RedisCommand command, Redi
{
for (int i = 0; i < keys.Length; i++)
{
keys[i].Assert();
keys[i].AssertNotNull();
}
this.keys = keys;
}
......@@ -750,7 +757,8 @@ sealed class CommandKeyValueMessage : CommandKeyBase
private readonly RedisValue value;
public CommandKeyValueMessage(int db, CommandFlags flags, RedisCommand command, RedisKey key, RedisValue value) : base(db, flags, command, key)
{
this.value = value.Assert();
value.AssertNotNull();
this.value = value;
}
internal override void WriteImpl(PhysicalConnection physical)
{
......@@ -767,10 +775,11 @@ public CommandKeyValuesKeyMessage(int db, CommandFlags flags, RedisCommand comma
{
for (int i = 0; i < values.Length; i++)
{
values[i].Assert();
values[i].AssertNotNull();
}
this.values = values;
this.key1 = key1.Assert();
key1.AssertNotNull();
this.key1 = key1;
}
public override int GetHashSlot(ServerSelectionStrategy serverSelectionStrategy)
{
......@@ -794,7 +803,7 @@ public CommandKeyValuesMessage(int db, CommandFlags flags, RedisCommand command,
{
for (int i = 0; i < values.Length; i++)
{
values[i].Assert();
values[i].AssertNotNull();
}
this.values = values;
}
......@@ -811,8 +820,10 @@ sealed class CommandKeyValueValueMessage : CommandKeyBase
private readonly RedisValue value0, value1;
public CommandKeyValueValueMessage(int db, CommandFlags flags, RedisCommand command, RedisKey key, RedisValue value0, RedisValue value1) : base(db, flags, command, key)
{
this.value0 = value0.Assert();
this.value1 = value1.Assert();
value0.AssertNotNull();
value1.AssertNotNull();
this.value0 = value0;
this.value1 = value1;
}
internal override void WriteImpl(PhysicalConnection physical)
{
......@@ -828,9 +839,12 @@ sealed class CommandKeyValueValueValueMessage : CommandKeyBase
private readonly RedisValue value0, value1, value2;
public CommandKeyValueValueValueMessage(int db, CommandFlags flags, RedisCommand command, RedisKey key, RedisValue value0, RedisValue value1, RedisValue value2) : base(db, flags, command, key)
{
this.value0 = value0.Assert();
this.value1 = value1.Assert();
this.value2 = value2.Assert();
value0.AssertNotNull();
value1.AssertNotNull();
value2.AssertNotNull();
this.value0 = value0;
this.value1 = value1;
this.value2 = value2;
}
internal override void WriteImpl(PhysicalConnection physical)
{
......@@ -847,10 +861,14 @@ sealed class CommandKeyValueValueValueValueMessage : CommandKeyBase
private readonly RedisValue value0, value1, value2, value3;
public CommandKeyValueValueValueValueMessage(int db, CommandFlags flags, RedisCommand command, RedisKey key, RedisValue value0, RedisValue value1, RedisValue value2, RedisValue value3) : base(db, flags, command, key)
{
this.value0 = value0.Assert();
this.value1 = value1.Assert();
this.value2 = value2.Assert();
this.value3 = value3.Assert();
value0.AssertNotNull();
value1.AssertNotNull();
value2.AssertNotNull();
value3.AssertNotNull();
this.value0 = value0;
this.value1 = value1;
this.value2 = value2;
this.value3 = value3;
}
internal override void WriteImpl(PhysicalConnection physical)
{
......@@ -883,7 +901,7 @@ public CommandSlotValuesMessage(int db, int slot, CommandFlags flags, RedisComma
this.slot = slot;
for (int i = 0; i < values.Length; i++)
{
values[i].Assert();
values[i].AssertNotNull();
}
this.values = values;
}
......@@ -906,7 +924,8 @@ sealed class CommandValueChannelMessage : CommandChannelBase
private readonly RedisValue value;
public CommandValueChannelMessage(int db, CommandFlags flags, RedisCommand command, RedisValue value, RedisChannel channel) : base(db, flags, command, channel)
{
this.value = value.Assert();
value.AssertNotNull();
this.value = value;
}
internal override void WriteImpl(PhysicalConnection physical)
{
......@@ -921,7 +940,8 @@ sealed class CommandValueKeyMessage : CommandKeyBase
public CommandValueKeyMessage(int db, CommandFlags flags, RedisCommand command, RedisValue value, RedisKey key) : base(db, flags, command, key)
{
this.value = value.Assert();
value.AssertNotNull();
this.value = value;
}
public override void AppendStormLog(StringBuilder sb)
......@@ -942,7 +962,8 @@ sealed class CommandValueMessage : Message
private readonly RedisValue value;
public CommandValueMessage(int db, CommandFlags flags, RedisCommand command, RedisValue value) : base(db, flags, command)
{
this.value = value.Assert();
value.AssertNotNull();
this.value = value;
}
internal override void WriteImpl(PhysicalConnection physical)
{
......@@ -956,8 +977,10 @@ sealed class CommandValueValueMessage : Message
private readonly RedisValue value0, value1;
public CommandValueValueMessage(int db, CommandFlags flags, RedisCommand command, RedisValue value0, RedisValue value1) : base(db, flags, command)
{
this.value0 = value0.Assert();
this.value1 = value1.Assert();
value0.AssertNotNull();
value1.AssertNotNull();
this.value0 = value0;
this.value1 = value1;
}
internal override void WriteImpl(PhysicalConnection physical)
{
......@@ -972,9 +995,12 @@ sealed class CommandValueValueValueMessage : Message
private readonly RedisValue value0, value1, value2;
public CommandValueValueValueMessage(int db, CommandFlags flags, RedisCommand command, RedisValue value0, RedisValue value1, RedisValue value2) : base(db, flags, command)
{
this.value0 = value0.Assert();
this.value1 = value1.Assert();
this.value2 = value2.Assert();
value0.AssertNotNull();
value1.AssertNotNull();
value2.AssertNotNull();
this.value0 = value0;
this.value1 = value1;
this.value2 = value2;
}
internal override void WriteImpl(PhysicalConnection physical)
{
......@@ -990,11 +1016,16 @@ sealed class CommandValueValueValueValueValueMessage : Message
private readonly RedisValue value0, value1, value2, value3, value4;
public CommandValueValueValueValueValueMessage(int db, CommandFlags flags, RedisCommand command, RedisValue value0, RedisValue value1, RedisValue value2, RedisValue value3, RedisValue value4) : base(db, flags, command)
{
this.value0 = value0.Assert();
this.value1 = value1.Assert();
this.value2 = value2.Assert();
this.value3 = value3.Assert();
this.value4 = value4.Assert();
value0.AssertNotNull();
value1.AssertNotNull();
value2.AssertNotNull();
value3.AssertNotNull();
value4.AssertNotNull();
this.value0 = value0;
this.value1 = value1;
this.value2 = value2;
this.value3 = value3;
this.value4 = value4;
}
internal override void WriteImpl(PhysicalConnection physical)
{
......
......@@ -588,16 +588,16 @@ void MatchResult(RawResult result)
if (connectionType == ConnectionType.Subscription && result.Type == ResultType.MultiBulk)
{ // out of band message does not match to a queued message
var items = result.GetItems();
if (items.Length >= 3 && items[0].Assert(message))
if (items.Length >= 3 && items[0].IsEqual(message))
{
// special-case the configuration change broadcasts (we don't keep that in the usual pub/sub registry)
var configChanged = multiplexer.ConfigurationChangedChannel;
if (configChanged != null && items[1].Assert(configChanged))
if (configChanged != null && items[1].IsEqual(configChanged))
{
EndPoint blame = null;
try
{
if (!items[2].Assert(RedisLiterals.ByteWildcard))
if (!items[2].IsEqual(RedisLiterals.ByteWildcard))
{
blame = Format.TryParseEndPoint(items[2].GetString());
}
......@@ -616,7 +616,7 @@ void MatchResult(RawResult result)
}
return; // AND STOP PROCESSING!
}
else if (items.Length >= 4 && items[0].Assert(pmessage))
else if (items.Length >= 4 && items[0].IsEqual(pmessage))
{
var channel = items[2].AsRedisChannel(ChannelPrefix);
multiplexer.Trace("PMESSAGE: " + channel, physicalName);
......
......@@ -117,7 +117,7 @@ internal RedisValue AsRedisValue()
throw new InvalidCastException("Cannot convert to RedisValue: " + resultType);
}
internal unsafe bool Assert(byte[] expected)
internal unsafe bool IsEqual(byte[] expected)
{
if (expected == null) throw new ArgumentNullException("expected");
if (expected.Length != count) return false;
......
......@@ -169,10 +169,9 @@ internal static bool AssertStarts(byte[] value, byte[] expected)
return true;
}
internal RedisChannel Assert()
internal void AssertNotNull()
{
if (IsNull) throw new ArgumentException("A null key is not valid in this context");
return this;
}
internal RedisChannel Clone()
......
......@@ -1958,10 +1958,10 @@ public ScriptEvalMessage(int db, CommandFlags flags, RedisCommand command, strin
if (script == null) throw new ArgumentNullException("script");
this.script = script;
for (int i = 0; i < keys.Length; i++)
keys[i].Assert();
keys[i].AssertNotNull();
this.keys = keys;
for (int i = 0; i < values.Length; i++)
values[i].Assert();
values[i].AssertNotNull();
this.values = values;
}
public override int GetHashSlot(ServerSelectionStrategy serverSelectionStrategy)
......@@ -2021,10 +2021,10 @@ sealed class SortedSetCombineAndStoreCommandMessage : Message.CommandKeyBase //
public SortedSetCombineAndStoreCommandMessage(int db, CommandFlags flags, RedisCommand command, RedisKey destination, RedisKey[] keys, RedisValue[] values) : base(db, flags, command, destination)
{
for (int i = 0; i < keys.Length; i++)
keys[i].Assert();
keys[i].AssertNotNull();
this.keys = keys;
for (int i = 0; i < values.Length; i++)
values[i].Assert();
values[i].AssertNotNull();
this.values = values;
}
public override int GetHashSlot(ServerSelectionStrategy serverSelectionStrategy)
......
......@@ -151,10 +151,9 @@ internal RedisValue AsRedisValue()
return value;
}
internal RedisKey Assert()
internal void AssertNotNull()
{
if (IsNull) throw new ArgumentException("A null key is not valid in this context");
return this;
}
/// <summary>
......
......@@ -143,7 +143,7 @@ class QueuedProcessor : ResultProcessor<bool>
static readonly byte[] QUEUED = Encoding.UTF8.GetBytes("QUEUED");
protected override bool SetResultCore(PhysicalConnection connection, Message message, RawResult result)
{
if(result.Type == ResultType.SimpleString && result.Assert(QUEUED))
if(result.Type == ResultType.SimpleString && result.IsEqual(QUEUED))
{
var q = message as QueuedMessage;
if (q != null) q.WasQueued = true;
......@@ -394,7 +394,7 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes
switch (result.Type)
{
case ResultType.SimpleString:
if (tran.IsAborted && result.Assert(RedisLiterals.BytesOK))
if (tran.IsAborted && result.IsEqual(RedisLiterals.BytesOK))
{
connection.Multiplexer.Trace("Acknowledging UNWATCH (aborted electively)");
SetResult(message, false);
......
......@@ -221,10 +221,9 @@ internal static bool TryParseInt64(byte[] value, int offset, int count, out long
}
}
internal RedisValue Assert()
internal void AssertNotNull()
{
if (IsNull) throw new ArgumentException("A null value is not valid in this context");
return this;
}
/// <summary>
/// Creates a new RedisValue from an Int32
......
......@@ -301,8 +301,8 @@ public static bool TryGet(RawResult result, out bool value)
case ResultType.Integer:
case ResultType.SimpleString:
case ResultType.BulkString:
if (result.Assert(one)) { value = true; return true; }
else if (result.Assert(zero)) { value = false; return true; }
if (result.IsEqual(one)) { value = true; return true; }
else if (result.IsEqual(zero)) { value = false; return true; }
break;
}
value = false;
......@@ -507,7 +507,7 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes
for (int i = 0; i < count; i++)
{
var key = arr[i * 2];
if (key.Assert(timeout) && arr[(i * 2) + 1].TryGetInt64(out i64))
if (key.IsEqual(timeout) && arr[(i * 2) + 1].TryGetInt64(out i64))
{
// note the configuration is in seconds
int timeoutSeconds = checked((int)i64), targetSeconds;
......@@ -525,21 +525,21 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes
server.WriteEverySeconds = targetSeconds;
}
}
else if (key.Assert(databases) && arr[(i * 2) + 1].TryGetInt64(out i64))
else if (key.IsEqual(databases) && arr[(i * 2) + 1].TryGetInt64(out i64))
{
int dbCount = checked((int)i64);
server.Multiplexer.Trace("Auto-configured databases: " + dbCount);
server.Databases = dbCount;
}
else if (key.Assert(slave_read_only))
else if (key.IsEqual(slave_read_only))
{
var val = arr[(i * 2) + 1];
if (val.Assert(yes))
if (val.IsEqual(yes))
{
server.SlaveReadOnly = true;
server.Multiplexer.Trace("Auto-configured slave-read-only: true");
}
else if (val.Assert(no))
else if (val.IsEqual(no))
{
server.SlaveReadOnly = false;
server.Multiplexer.Trace("Auto-configured slave-read-only: false");
......@@ -572,7 +572,7 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes
switch (result.Type)
{
case ResultType.SimpleString:
if (result.Assert(RedisLiterals.BytesOK))
if (result.IsEqual(RedisLiterals.BytesOK))
{
SetResult(message, true);
}
......@@ -749,7 +749,7 @@ public ExpectBasicStringProcessor(byte[] value)
}
protected override bool SetResultCore(PhysicalConnection connection, Message message, RawResult result)
{
if (result.Assert(expected))
if (result.IsEqual(expected))
{
SetResult(message, true);
return true;
......@@ -1042,7 +1042,7 @@ public override bool SetResult(PhysicalConnection connection, Message message, R
var final = base.SetResult(connection, message, result);
if (result.IsError)
{
if (result.Assert(authFail))
if (result.IsEqual(authFail))
{
connection.RecordConnectionFailed(ConnectionFailureType.AuthenticationFailure);
}
......@@ -1064,10 +1064,10 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes
switch(message.Command)
{
case RedisCommand.ECHO:
happy = result.Type == ResultType.BulkString && (!establishConnection || result.Assert(connection.Multiplexer.UniqueId));
happy = result.Type == ResultType.BulkString && (!establishConnection || result.IsEqual(connection.Multiplexer.UniqueId));
break;
case RedisCommand.PING:
happy = result.Type == ResultType.SimpleString && result.Assert(RedisLiterals.BytesPONG);
happy = result.Type == ResultType.SimpleString && result.IsEqual(RedisLiterals.BytesPONG);
break;
case RedisCommand.TIME:
happy = result.Type == ResultType.MultiBulk && result.GetItems().Length == 2;
......
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