Skip to content

Commit

Permalink
first pass at IDictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
sdether committed May 7, 2010
1 parent 9e55f2a commit eed35c5
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 22 deletions.
1 change: 1 addition & 0 deletions Firkin.Test/Firkin.Test.csproj
Expand Up @@ -61,6 +61,7 @@
<Compile Include="TFirkinFile.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TFirkinStream.cs" />
<Compile Include="TMisc.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Firkin\Firkin.csproj">
Expand Down
39 changes: 39 additions & 0 deletions Firkin.Test/TMisc.cs
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace Droog.Firkin.Test {

[TestFixture]
public class TMisc {

[Test]
public void Dictionary_kvp_Contains_checks_key_and_value() {
IDictionary<int,string> dictionary = new Dictionary<int, string>();
dictionary[1] = "foo";
Assert.IsTrue(dictionary.Contains(new KeyValuePair<int, string>(1, "foo")));
Assert.IsFalse(dictionary.Contains(new KeyValuePair<int, string>(1, "bar")));
}

[Test]
public void Dictionary_kvp_Remove_checks_key_and_value() {
IDictionary<int, string> dictionary = new Dictionary<int, string>();
dictionary[1] = "foo";
Assert.IsFalse(dictionary.Remove(new KeyValuePair<int, string>(1, "bar")));
Assert.IsTrue(dictionary.Remove(new KeyValuePair<int, string>(1, "foo")));
}

[Test]
[ExpectedException(typeof(ArgumentException))]
public void Dictionary_CopyTo_requires_destination_of_sufficient_size() {
IDictionary<int, string> dictionary = new Dictionary<int, string>();
dictionary[1] = "foo";
dictionary[2] = "foo";
dictionary[3] = "foo";
var destination = new KeyValuePair<int, string>[2];
dictionary.CopyTo(destination, 0);
}
}
}
71 changes: 49 additions & 22 deletions Firkin/FirkinDictionary.cs
Expand Up @@ -20,9 +20,10 @@
using System.Collections.Generic;
using System.IO;
using Droog.Firkin.Serialization;
using System.Linq;

namespace Droog.Firkin {
public class FirkinDictionary<TKey,TValue> : IDictionary<TKey,TValue> {
public class FirkinDictionary<TKey, TValue> : IDictionary<TKey, TValue> {

//--- Fields ---
private readonly IFirkinHash<TKey> _hash;
Expand All @@ -40,7 +41,7 @@ public class FirkinDictionary<TKey,TValue> : IDictionary<TKey,TValue> {

public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
foreach(var pair in _hash) {
yield return new KeyValuePair<TKey, TValue>(pair.Key,_valueSerializer.Deserialize(pair.Value));
yield return new KeyValuePair<TKey, TValue>(pair.Key, _valueSerializer.Deserialize(pair.Value));
}
}

Expand All @@ -49,66 +50,92 @@ public class FirkinDictionary<TKey,TValue> : IDictionary<TKey,TValue> {
}

public void Add(KeyValuePair<TKey, TValue> item) {
var stream = GetStream(item);
_hash.Put(item.Key,stream,stream.Length);
Add(item.Key, item.Value);
}

public void Add(TKey key, TValue value) {

// Note: This behaves differently from normal dictionaries, as in it won't throw on collision
var stream = GetStream(value);
_hash.Put(key, stream, stream.Length);
}

public void Clear() {
_hash.Truncate();
}

public bool Contains(KeyValuePair<TKey, TValue> item) {
throw new NotImplementedException();
var stream = _hash.Get(item.Key);
return stream != null && item.Value.Equals(_valueSerializer.Deserialize(stream));
}

public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
throw new NotImplementedException();
try {
foreach(var kvp in this) {
array[arrayIndex] = kvp;
arrayIndex++;
}
} catch(IndexOutOfRangeException e) {
throw new ArgumentException("Destination array is too small", "array", e);
}
}

public bool Remove(KeyValuePair<TKey, TValue> item) {
throw new NotImplementedException();
// TODO: race condition.. have put a lock around all _hash accesses to avoid
return Contains(item) && _hash.Delete(item.Key);
}

public int Count {
get { throw new NotImplementedException(); }
get { return _hash.Count; }
}

public bool IsReadOnly {
get { throw new NotImplementedException(); }
get { return false; }
}

public bool ContainsKey(TKey key) {
throw new NotImplementedException();
}

public void Add(TKey key, TValue value) {
throw new NotImplementedException();
var stream = _hash.Get(key);
return stream != null;
}

public bool Remove(TKey key) {
throw new NotImplementedException();
return _hash.Delete(key);
}

public bool TryGetValue(TKey key, out TValue value) {
throw new NotImplementedException();
var stream = _hash.Get(key);
if(stream == null) {
value = default(TValue);
return false;
}
value = _valueSerializer.Deserialize(stream);
return true;
}

public TValue this[TKey key] {
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
get {
TValue value;
if(!TryGetValue(key, out value)) {
throw new KeyNotFoundException();
}
return value;
}
set {
Add(key, value);
}
}

public ICollection<TKey> Keys {
get { throw new NotImplementedException(); }
get { return _hash.Keys.ToList(); }
}

public ICollection<TValue> Values {
get { throw new NotImplementedException(); }
get { return this.Select(x => x.Value).ToList(); }
}

private MemoryStream GetStream(KeyValuePair<TKey, TValue> item) {
private MemoryStream GetStream(TValue value) {
var stream = new MemoryStream();
_valueSerializer.Serialize(stream,item.Value);
_valueSerializer.Serialize(stream, value);
stream.Position = 0;
return stream;
}
Expand Down
1 change: 1 addition & 0 deletions Firkin/FirkinHash.cs
Expand Up @@ -84,6 +84,7 @@ private class MergePair {

//--- Properties ---
public int Count { get { return _index.Count; } }
public IEnumerable<TKey> Keys { get { lock(_indexSyncRoot) { return _index.Keys.ToArray(); } } }

//--- Methods ---
public void Put(TKey key, Stream stream, uint length) {
Expand Down
2 changes: 2 additions & 0 deletions Firkin/IFirkinHash.cs
Expand Up @@ -28,5 +28,7 @@ public interface IFirkinHash<TKey> : IEnumerable<KeyValuePair<TKey, FirkinStream
bool Delete(TKey key);
void Merge();
void Truncate();
int Count { get; }
IEnumerable<TKey> Keys { get; }
}
}

0 comments on commit eed35c5

Please sign in to comment.