Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Significant Updates #4

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions Collections/RedisGenericBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// RedisGenericBase.cs : Generic methods for Abstracted Collections
//
// Authors:
// Miguel de Icaza ([email protected])
// Jonathan R. Steele ([email protected])
//
// Copyright 2010 Novell, Inc.
//
// Licensed under the same terms of reddis: new BSD license.
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace RedisSharp.Collections {
public abstract class RedisGenericBase<T> : RedisBase
{
internal RedisGenericBase(string key, string host, int port) : base (host, port) { Key = key; }

public string Key {
get;
private set;
}

protected T DeSerialize(byte[] data)
{
IFormatter bFormatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream(data);

T result = (T)bFormatter.Deserialize(ms);

ms.Dispose();
ms.Close();

return result;

}

protected byte[] Serialize(T data)
{
IFormatter bFormatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream();

bFormatter.Serialize(ms, data);

ms.Position = 0;

byte[] result = ms.ToArray();

ms.Dispose();
ms.Close();

return result;
}


}
}

206 changes: 206 additions & 0 deletions Collections/RedisList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
// RedisList.cs : Abstracted List Implementations for Redis
//
// Author:
// Jonathan R. Steele ([email protected])
//
// Copyright 2010 Novell, Inc.
//
// Licensed under the same terms of reddis: new BSD license.
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace RedisSharp.Collections {

public class RedisList<T> : RedisGenericBase<T>, IList<T>, IEnumerable, IEnumerable<T>
{
internal RedisList(string key, string host, int port) : base(key, host, port)
{}


public T this[int index] {
get {
SendCommand ("LINDEX {0} {1}\r\n", Key, index);
byte[] data = ReadData();

return DeSerialize(data);
}
set {
byte[] data = Serialize(value);
SendDataCommand(data, "LSET {0} {1} {2}\r\n", Key, index, data.Length);
ExpectSuccess();

}

}

public int Count {
get {
return SendExpectInt("LLEN {0}\r\n", Key);
}
}

public bool IsFixedSize {
get { return false; }
}

public bool IsReadOnly {
get { return false; }
}

public bool IsSynchronized {
get { return false; }
}

public Object SyncRoot {
get { return this; }
}

public void Add(T item)
{
byte[] data = Serialize(item);
SendDataExpectInt(data, "RPUSH {0} {1}\r\n", Key, data.Length);
}



public void Insert(int index, T item)
{
int len = Count;
if (index < len && index > 0) {
Add(item); // Add it first to expand the list

for (int idx = Count - 1; idx > index; idx--) {
this[idx] = this[idx-1];
}
this[index] = item;
}
}

public bool Contains(T input)
{
return (IndexOf(input) >= 0);
}

public int IndexOf(T input)
{
for (int i = 0; i < Count; i++) {
if ( input.Equals(this[i]) ) return i;
}

return -1;
}

public T[] GetRange(int start, int end)
{
List<T> data = new List<T>();

byte[][] result = SendDataCommandExpectMultiBulkReply(null, "LRANGE {0} {1} {2}\r\n", Key, start, end);

foreach (byte[] itm in result)
data.Add(DeSerialize(itm));

result = null;

return data.ToArray();

}

public void CopyTo(T[] array, int index)
{
int j = index;
for (int i=0; i < Count; i++) {
array.SetValue(this[i], j);
j++;
}
}


public bool Remove(T item)
{
byte[] data = Serialize(item);
return (SendDataExpectInt(data, "LREM {0} 1 {1}\r\n", Key, data.Length) > 0);
}

public void RemoveAt(int index)
{
T itm = this[index];

if (itm.Equals(default(T))) return;

Remove(itm);

}

public void Clear()
{
SendExpectInt("DEL {0}\r\n", Key);

}


public IEnumerator<T> GetEnumerator ()
{
return new RedisList<T>.Enumerator(GetRange(0, Count - 1));
}



IEnumerator IEnumerable.GetEnumerator ()
{
return new RedisList<T>.Enumerator(GetRange(0, Count - 1));
}


public struct Enumerator : IEnumerator<T>, IEnumerator {

T[] data;
int position;

internal Enumerator(T[] input)
{
data = input;
position = -1;
}

public bool MoveNext()
{
position++;
return (position < data.Length);
}

public void Reset()
{
position = -1;
}

object System.Collections.IEnumerator.Current {
get {
return Current;
}
}


public T Current {
get {
try {
return data[position];
} catch (IndexOutOfRangeException) {
throw new InvalidOperationException();
}
}
}

public void Dispose() { }

}

}



}
111 changes: 111 additions & 0 deletions Collections/RedisSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// RedisSet.cs : Abstracted Set commands for Redis
//
// Authors:
// Miguel de Icaza ([email protected])
// Jonathan R. Steele ([email protected])
//
// Copyright 2010 Novell, Inc.
//
// Licensed under the same terms of reddis: new BSD license.
//
using System;
using RedisSharp;

namespace RedisSharp.Collections {

public class RedisSet<T> : RedisGenericBase<T>
{
internal RedisSet (string key, string host, int port) : base (key, host, port) { }

public int Count {
get {
return SendDataExpectInt (null, "SCARD {0}\r\n", Key);
}
}

public bool Add (T item)
{
byte[] data = Serialize (item);
return SendDataExpectInt (data, "SADD {0} {1}\r\n", Key, data.Length) > 0;
}

public bool Remove (T item)
{
byte[] data = Serialize (item);
return SendDataExpectInt (data, "SREM {0} {1}\r\n", Key, data.Length) > 0;
}

public T PopRandomItem ()
{
byte[] result = SendExpectData (null,"SPOP {0}\r\n", Key);
return DeSerialize (result);
}

public T RandomItem ()
{
byte[] result = SendExpectData (null,"SRANDMEMBER {0}\r\n", Key);
return DeSerialize (result);

}

protected T[] SetCommand (string command, params string[] keys)
{
if (keys == null)
throw new ArgumentNullException ();

System.Collections.Generic.List<T> items = new System.Collections.Generic.List<T> ();

byte[][] result = SendDataCommandExpectMultiBulkReply (null, command + " " + Key + " " + string.Join (" ", keys) + "\r\n");
foreach (byte[] b in result) {
items.Add ( DeSerialize (b) );
}


return items.ToArray ();

}

protected RedisSet<T> StoreSetCommand (string command, string destKey, params string[] keys)
{
if (keys == null)
throw new ArgumentNullException ();

SendExpectSuccess (command + " " + destKey + " " + Key + " " + string.Join (" ", keys) + "\r\n");
return new RedisSet<T> (destKey, Host, Port);
}

public T[] Union (params string[] keys)
{
return SetCommand ("SUNION",keys);
}

public RedisSet<T> StoreUnion (string destKey, params string[] keys)
{
return StoreSetCommand ("SUNIONSTORE", destKey , keys);
}

public T[] Intersect (params string[] keys)
{
return SetCommand ("SINTER", keys);
}

public RedisSet<T> StoreIntersect (string destKey, params string[] keys)
{
return StoreSetCommand ("SINTERSTORE",destKey, keys);
}

public T[] Difference (params string[] keys)
{
return SetCommand ("SDIFF", keys);
}

public RedisSet<T> StoreDifference (string destKey, params string[] keys)
{
return StoreSetCommand ("SDIFFSTORE", destKey, keys);
}


}

}

Loading