Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

Commit

Permalink
Implemented a dirty flag to keep from sorting every time
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmohara committed Mar 31, 2010
1 parent 88ad794 commit ddc5add
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
26 changes: 17 additions & 9 deletions MongoDB.Net-Tests/TestDocument.cs
Expand Up @@ -6,20 +6,19 @@
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

using NUnit.Framework;

using MongoDB.Driver;
using System.Collections.Generic;

namespace MongoDB.Driver
{
[TestFixture]
public class TestDocument
{
[Test]
public void TestValuesAdded()
{
public void TestValuesAdded(){
Document d = new Document();
d["test"] = 1;
Assert.AreEqual(1, d["test"]);
Expand All @@ -37,6 +36,7 @@ public void TestValuesAdded()
cnt++;
}
}

[Test]
public void TestRemove(){
Document d = new Document();
Expand All @@ -46,8 +46,7 @@ public void TestValuesAdded()
}

[Test]
public void TestUseOfIComparerForKeys()
{
public void TestUseOfIComparerForKeys(){
var doc = new Document(new ReverseComparer());

doc.Append("a", 3);
Expand All @@ -58,8 +57,7 @@ public void TestUseOfIComparerForKeys()
}

[Test]
public void TestInsertMaintainsKeyOrder()
{
public void TestInsertMaintainsKeyOrder(){
Document d = new Document();
d["one"] = 1;
d.Insert("zero", 0, 0);
Expand All @@ -68,12 +66,22 @@ public void TestInsertMaintainsKeyOrder()
Assert.AreEqual(keysList.First(), "zero");
}

[Test]
public void TestMaintainsOrderUsingMultipleMethods(){
Document d = new Document(new ReverseComparer());
d["one"] = 1;
var test = d["one"];
d["zero"] = 0;

var keysList = d.Keys as IEnumerable<string>;
Assert.AreEqual(keysList.First(), "zero");
}

[Test]
[ExpectedException(ExceptionType = typeof(ArgumentException),
ExpectedMessage="Key already exists in Document",
MatchType=MessageMatch.Contains)]
public void TestInsertWillThrowArgumentExceptionIfKeyAlreadyExists()
{
public void TestInsertWillThrowArgumentExceptionIfKeyAlreadyExists(){
Document d = new Document();
d["one"] = 1;
d.Insert("one", 1, 0);
Expand Down
15 changes: 11 additions & 4 deletions MongoDBDriver/Document.cs
Expand Up @@ -9,6 +9,7 @@ namespace MongoDB.Driver
/// </summary>
public class Document : DictionaryBase
{
private bool _dirty;
private List<String> orderedKeys;
private IComparer<string> keyComparer;

Expand All @@ -26,14 +27,16 @@ public Document(IComparer<string> comparer)
public Object this[String key]
{
get { return Dictionary[key]; }
set { Dictionary[key] = value; }
set{
_dirty = true;
Dictionary[key] = value;
}
}

public IList<string> Keys
{
get
{
if (keyComparer != null)
get{
if (keyComparer != null && _dirty)
orderedKeys.Sort(keyComparer);
return orderedKeys;
}
Expand All @@ -46,6 +49,7 @@ public ICollection Values

public void Add(String key, Object value)
{
_dirty = true;
Dictionary.Add(key, value);
}

Expand Down Expand Up @@ -73,7 +77,9 @@ public void Insert(String key, Object value, int index)
if (!alreadyContains) orderedKeys.Remove(key);
throw;
}
_dirty = true;
}

public Document Prepend(String key, Object value)
{
this.Insert(key, value, 0);
Expand All @@ -98,6 +104,7 @@ public bool Contains(String key)

public void Remove(String key)
{
_dirty = true;
Dictionary.Remove(key);
}

Expand Down

0 comments on commit ddc5add

Please sign in to comment.