Skip to content

Commit

Permalink
Remove Document.Contains(key) in favor of the IDictionary<>.ContainsK…
Browse files Browse the repository at this point in the history
…ey. We dont not need both.
  • Loading branch information
lanwin committed Jul 8, 2010
1 parent 67e6c3b commit 97623f7
Show file tree
Hide file tree
Showing 16 changed files with 40 additions and 50 deletions.
8 changes: 4 additions & 4 deletions source/MongoDB.GridFS/GridFileInfo.cs
Expand Up @@ -68,14 +68,13 @@ public string FileName
/// <value>The aliases.</value>
public IList<String> Aliases{
get {
if(filedata.Contains("aliases") == false || filedata["aliases"] == null){
if(filedata.ContainsKey("aliases") == false || filedata["aliases"] == null){
return null;
}
if(filedata["aliases"] is IList<String>){
return (List<String>)filedata["aliases"];
}else{
return new List<String>();
}
return new List<String>();
}
set { filedata["aliases"] = value; }
}
Expand Down Expand Up @@ -280,7 +279,8 @@ public string FileName
/// Deletes all data in a file and sets the length to 0.
/// </summary>
public void Truncate(){
if(filedata.Contains("_id") == false) return;
if(filedata.ContainsKey("_id") == false)
return;
this.gridFile.Chunks.Remove(new Document().Add("files_id", filedata["_id"]));
this.Length = 0;
this.gridFile.Files.Save(filedata);
Expand Down
9 changes: 5 additions & 4 deletions source/MongoDB.GridFS/GridFileStream.cs
Expand Up @@ -256,10 +256,11 @@ public override void Flush ()
byte[] data = new byte[highestBuffPosition];
Array.Copy (buffer, data, highestBuffPosition);
chunk["data"] = new Binary (data);
}


if (chunk.Contains ("_id")) {
}


if(chunk.ContainsKey("_id"))
{
chunks.Save (chunk);
} else {
chunks.Insert (chunk);
Expand Down
2 changes: 1 addition & 1 deletion source/MongoDB.Tests/IntegrationTests/TestCollection.cs
Expand Up @@ -173,7 +173,7 @@ public void TestFindOneObjectContainingUKPound()
var query = new Document();
var result = DB["charreads"].FindOne(query);
Assert.IsNotNull(result);
Assert.IsTrue(result.Contains("test"));
Assert.IsTrue(result.ContainsKey("test"));
Assert.AreEqual("1234£56", result["test"]);
}

Expand Down
14 changes: 7 additions & 7 deletions source/MongoDB.Tests/IntegrationTests/TestCursor.cs
Expand Up @@ -99,9 +99,9 @@ public void TestCanReadSmall()
public void TestExplain()
{
var exp = DB["reads"].FindAll().Limit(5).Skip(5).Sort("x").Explain();
Assert.IsTrue(exp.Contains("cursor"));
Assert.IsTrue(exp.Contains("n"));
Assert.IsTrue(exp.Contains("nscanned"));
Assert.IsTrue(exp.ContainsKey("cursor"));
Assert.IsTrue(exp.ContainsKey("n"));
Assert.IsTrue(exp.ContainsKey("nscanned"));
}

[Test]
Expand All @@ -111,14 +111,14 @@ public void TestHint()
var hint = new Document().Add("x", IndexOrder.Ascending);

var exp = reads.FindAll().Hint(hint).Explain();
Assert.IsTrue(exp.Contains("$err"), "No error found");
Assert.IsTrue(exp.ContainsKey("$err"), "No error found");

reads.Metadata.CreateIndex("hintindex", hint, false);
exp = reads.FindAll().Hint(hint).Explain();

Assert.IsTrue(exp.Contains("cursor"));
Assert.IsTrue(exp.Contains("n"));
Assert.IsTrue(exp.Contains("nscanned"));
Assert.IsTrue(exp.ContainsKey("cursor"));
Assert.IsTrue(exp.ContainsKey("n"));
Assert.IsTrue(exp.ContainsKey("nscanned"));
}

[Test]
Expand Down
4 changes: 2 additions & 2 deletions source/MongoDB.Tests/IntegrationTests/TestDatabase.cs
Expand Up @@ -57,7 +57,7 @@ public class TestDatabase : MongoTestBase

var target = DB.FollowReference(rf);
Assert.IsNotNull(target, "FollowReference returned null");
Assert.IsTrue(target.Contains("msg"));
Assert.IsTrue(target.ContainsKey("msg"));
Assert.AreEqual(msg, target["msg"]);
}

Expand Down Expand Up @@ -120,7 +120,7 @@ public class TestDatabase : MongoTestBase
var recv = DB.FollowReference(rf);

Assert.IsNotNull(recv);
Assert.IsTrue(recv.Contains("msg"));
Assert.IsTrue(recv.ContainsKey("msg"));
Assert.AreEqual(recv["_id"], (long)123);
}

Expand Down
10 changes: 5 additions & 5 deletions source/MongoDB.Tests/UnitTests/Bson/TestBsonReader.cs
Expand Up @@ -44,7 +44,7 @@ public class TestBsonReader : BsonTestBase
var doc = (Document)reader.ReadObject();
Assert.IsNotNull(doc, "Document was null");
Assert.AreEqual(buf.Length, reader.Position);
Assert.IsTrue(doc.Contains("a"));
Assert.IsTrue(doc.ContainsKey("a"));
}

[Test]
Expand Down Expand Up @@ -145,9 +145,9 @@ public class TestBsonReader : BsonTestBase
var doc = (Document)reader.ReadObject();

Assert.IsNotNull(doc, "Document was null");
Assert.IsTrue(doc.Contains("_id"));
Assert.IsTrue(doc.Contains("a"));
Assert.IsTrue(doc.Contains("b"));
Assert.IsTrue(doc.ContainsKey("_id"));
Assert.IsTrue(doc.ContainsKey("a"));
Assert.IsTrue(doc.ContainsKey("b"));
Assert.AreEqual("4a753ad8fac16ea58b290351", (doc["_id"]).ToString());
Assert.AreEqual(1, Convert.ToInt32(doc["a"]));
Assert.AreEqual("test", doc["b"]);
Expand All @@ -162,7 +162,7 @@ public class TestBsonReader : BsonTestBase
var doc = reader.Read();

Assert.IsNotNull(doc, "Document was null");
Assert.IsTrue(doc.Contains("test"));
Assert.IsTrue(doc.ContainsKey("test"));
Assert.AreEqual("test", doc["test"]);
}

Expand Down
6 changes: 3 additions & 3 deletions source/MongoDB.Tests/UnitTests/Bson/TestRoundTrips.cs
Expand Up @@ -32,9 +32,9 @@ public class TestRoundTrips
Document source = new Document();
source.Add("x", 1).Add("ref", new DBRef("refs", "ref1"));

Document copy = WriteAndRead(source);

Assert.IsTrue(copy.Contains("ref"));
Document copy = WriteAndRead(source);

Assert.IsTrue(copy.ContainsKey("ref"));
Assert.IsTrue(copy["ref"].GetType() == typeof(DBRef));

DBRef sref = (DBRef)source["ref"];
Expand Down
4 changes: 2 additions & 2 deletions source/MongoDB.Tests/UnitTests/TestDocument.cs
Expand Up @@ -42,7 +42,7 @@ public void TestClearRemovesAll()
d.Clear();
Assert.AreEqual(0, d.Count);
Assert.IsNull(d["one"]);
Assert.IsFalse(d.Contains("one"));
Assert.IsFalse(d.ContainsKey("one"));
}

[Test]
Expand Down Expand Up @@ -190,7 +190,7 @@ public void TestRemove()
var d = new Document();
d["one"] = 1;
d.Remove("one");
Assert.IsFalse(d.Contains("one"));
Assert.IsFalse(d.ContainsKey("one"));
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion source/MongoDB/CollectionMetadata.cs
Expand Up @@ -44,7 +44,7 @@ public Document Options
if(_options != null)
return _options;
var doc = _database["system.namespaces"].FindOne(new Document().Add("name", _fullName)) ?? new Document();
if(doc.Contains("create"))
if(doc.ContainsKey("create"))
doc.Remove("create");
//Not sure why this is here. The python driver has it.
_options = doc;
Expand Down
4 changes: 2 additions & 2 deletions source/MongoDB/Connections/Connection.cs
Expand Up @@ -225,9 +225,9 @@ public Document SendCommand(ISerializationFactory factory, string database, Type
if(!Convert.ToBoolean(result["ok"]))
{
var msg = string.Empty;
if(result.Contains("msg"))
if(result.ContainsKey("msg"))
msg = (string)result["msg"];
else if(result.Contains("errmsg"))
else if(result.ContainsKey("errmsg"))
msg = (string)result["errmsg"];
throw new MongoCommandException(msg, result, command);
}
Expand Down
4 changes: 2 additions & 2 deletions source/MongoDB/DBRef.cs
Expand Up @@ -42,7 +42,7 @@ public sealed class DBRef : IEquatable<DBRef>
_collectionName = (String)document[RefName];
_id = document[IdName];
_document = document;
if(document.Contains("metadata"))
if(document.ContainsKey("metadata"))
MetaData = (Document)document["metadata"];
}

Expand Down Expand Up @@ -174,7 +174,7 @@ public sealed class DBRef : IEquatable<DBRef>
/// <c>true</c> if [is document DB ref] [the specified document]; otherwise, <c>false</c>.
/// </returns>
public static bool IsDocumentDBRef(Document document){
return document != null && document.Contains(RefName) && document.Contains(IdName);
return document != null && document.ContainsKey(RefName) && document.ContainsKey(IdName);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions source/MongoDB/DatabaseMetadata.cs
Expand Up @@ -72,7 +72,7 @@ public bool DropCollection(MongoCollection collection)
public bool DropCollection(string name)
{
var result = _database.SendCommand(new Document().Add("drop", name));
return result.Contains("ok") && Convert.ToBoolean(result["ok"]);
return result.ContainsKey("ok") && Convert.ToBoolean(result["ok"]);
}

/// <summary>
Expand All @@ -82,7 +82,7 @@ public bool DropCollection(string name)
public bool DropDatabase()
{
var result = _database.SendCommand("dropDatabase");
return result.Contains("ok") && Convert.ToBoolean(result["ok"]);
return result.ContainsKey("ok") && Convert.ToBoolean(result["ok"]);
}

/// <summary>
Expand Down
13 changes: 1 addition & 12 deletions source/MongoDB/Document.cs
Expand Up @@ -293,17 +293,6 @@ public Document Merge(Document source)
return this;
}

/// <summary>
/// Determines whether [contains] [the specified key].
/// </summary>
/// <param name="key">The key.</param>
/// <returns>
/// <c>true</c> if [contains] [the specified key]; otherwise, <c>false</c>.
/// </returns>
public bool Contains(String key){
return (_orderedKeys.Contains(key));
}

/// <summary>
/// Removes the specified key.
/// </summary>
Expand Down Expand Up @@ -449,7 +438,7 @@ void IDictionary.Remove(object key)
//Todo: Fix any accidental reordering issues.

foreach(var key in _orderedKeys){
if(destinationDocument.Contains(key))
if(destinationDocument.ContainsKey(key))
destinationDocument.Remove(key);
destinationDocument[key] = this[key];
}
Expand Down
2 changes: 1 addition & 1 deletion source/MongoDB/MapReduce.cs
Expand Up @@ -233,7 +233,7 @@ public MapReduce Scope(Document scope)
/// </summary>
internal void RetrieveData()
{
if(Command.Command.Contains("map") == false || Command.Command.Contains("reduce") == false)
if(Command.Command.ContainsKey("map") == false || Command.Command.ContainsKey("reduce") == false)
throw new InvalidOperationException("Cannot execute without a map and reduce function");

IsModifiable = false;
Expand Down
2 changes: 1 addition & 1 deletion source/MongoDB/Results/MapReduceResult.cs
Expand Up @@ -99,7 +99,7 @@ public String ErrorMessage
{
get
{
if(_result.Contains("msg"))
if(_result.ContainsKey("msg"))
return (String)_result["msg"];
return String.Empty;
}
Expand Down
2 changes: 1 addition & 1 deletion source/MongoDB/Util/ErrorTranslator.cs
Expand Up @@ -30,7 +30,7 @@ internal class ErrorTranslator
/// <c>true</c> if the specified document is error; otherwise, <c>false</c>.
/// </returns>
public static bool IsError(Document document){
if(document.Contains("err") && document["err"] != null)
if(document.ContainsKey("err") && document["err"] != null)
return true;
return false;
}
Expand Down

0 comments on commit 97623f7

Please sign in to comment.