Skip to content

Commit

Permalink
change protocol for commands sending data to RESP (REdis Serial Proto…
Browse files Browse the repository at this point in the history
…col)
  • Loading branch information
rfranke committed Aug 16, 2014
1 parent 0fad4d2 commit 8d774bd
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions redis-sharp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void Set (string key, byte [] value)
if (value.Length > 1073741824)
throw new ArgumentException ("value exceeds 1G", "value");

if (!SendDataCommand (value, "SET {0} {1}\r\n", key, value.Length))
if (!SendDataCommand (value, ToDataRESP("SET", key, value.Length)))
throw new Exception ("Unable to connect");
ExpectSuccess ();
}
Expand All @@ -122,7 +122,7 @@ public bool SetNX (string key, byte [] value)
if (value.Length > 1073741824)
throw new ArgumentException ("value exceeds 1G", "value");

return SendDataExpectInt (value, "SETNX {0} {1}\r\n", key, value.Length) > 0 ? true : false;
return SendDataExpectInt (value, ToDataRESP("SETNX", key, value.Length)) > 0 ? true : false;
}

public void Set (IDictionary<string,string> dict)
Expand Down Expand Up @@ -184,7 +184,7 @@ public byte [] GetSet (string key, byte [] value)
if (value.Length > 1073741824)
throw new ArgumentException ("value exceeds 1G", "value");

if (!SendDataCommand (value, "GETSET {0} {1}\r\n", key, value.Length))
if (!SendDataCommand (value, ToDataRESP("GETSET", key, value.Length)))
throw new Exception ("Unable to connect");

return ReadData ();
Expand Down Expand Up @@ -284,7 +284,7 @@ bool SendCommand (string cmd, params object [] args)
[Conditional ("DEBUG")]
void Log (string fmt, params object [] args)
{
Console.WriteLine ("{0}", String.Format (fmt, args).Trim ());
Console.WriteLine("{0}", String.Format(fmt.Replace("\r\n", " "), args).Trim());
}

void ExpectSuccess ()
Expand Down Expand Up @@ -652,14 +652,14 @@ public byte[][] ListRange(string key, int start, int end)
return SendDataCommandExpectMultiBulkReply (null, "LRANGE {0} {1} {2}\r\n", key, start, end);
}

public void LeftPush(string key, string value)
{
SendExpectSuccess("LPUSH {0} {1}\r\n{2}\r\n", key, value.Length, value);
}
public void LeftPush(string key, string value)
{
SendExpectSuccess (ToDataRESP("LPUSH", key, value.Length) + "{0}\r\n", value);
}

public void RightPush(string key, string value)
{
SendExpectSuccess ("RPUSH {0} {1}\r\n{2}\r\n", key, value.Length, value);
SendExpectSuccess (ToDataRESP("RPUSH", key, value.Length) + "{0}\r\n", value);
}

public int ListLength (string key)
Expand All @@ -683,7 +683,7 @@ public byte[] LeftPop(string key)
#region Set commands
public bool AddToSet (string key, byte[] member)
{
return SendDataExpectInt(member, "SADD {0} {1}\r\n", key, member.Length) > 0;
return SendDataExpectInt(member, ToDataRESP("SADD", key, member.Length)) > 0;
}

public bool AddToSet (string key, string member)
Expand All @@ -698,7 +698,7 @@ public int CardinalityOfSet (string key)

public bool IsMemberOfSet (string key, byte[] member)
{
return SendDataExpectInt (member, "SISMEMBER {0} {1}\r\n", key, member.Length) > 0;
return SendDataExpectInt (member, ToDataRESP("SISMEMBER", key, member.Length)) > 0;
}

public bool IsMemberOfSet(string key, string member)
Expand All @@ -723,7 +723,7 @@ public byte[] PopRandomMemberOfSet (string key)

public bool RemoveFromSet (string key, byte[] member)
{
return SendDataExpectInt (member, "SREM {0} {1}\r\n", key, member.Length) > 0;
return SendDataExpectInt (member, ToDataRESP("SREM", key, member.Length)) > 0;
}

public bool RemoveFromSet (string key, string member)
Expand Down Expand Up @@ -787,10 +787,25 @@ public void StoreDifferenceOfSets (string destKey, params string[] keys)

public bool MoveMemberToSet (string srcKey, string destKey, byte[] member)
{
return SendDataExpectInt(member, "SMOVE {0} {1} {2}\r\n", srcKey, destKey, member.Length) > 0;
return SendDataExpectInt(member, ToDataRESP2("SMOVE", srcKey, destKey, member.Length)) > 0;
}
#endregion

string ToDataRESP (string command, string key, int dataLength)
{
return "*3\r\n$" + command.Length + "\r\n" + command + "\r\n"
+ "$" + key.Length + "\r\n" + key + "\r\n"
+ "$" + dataLength + "\r\n";
}

string ToDataRESP2 (string command, string key1, string key2, int dataLength)
{
return "*4\r\n$" + command.Length + "\r\n" + command + "\r\n"
+ "$" + key1.Length + "\r\n" + key1 + "\r\n"
+ "$" + key2.Length + "\r\n" + key2 + "\r\n"
+ "$" + dataLength + "\r\n";
}

public void Dispose ()
{
Dispose (true);
Expand Down

0 comments on commit 8d774bd

Please sign in to comment.