Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: penfold/mongodb-csharp
base: master
...
head fork: abolibibelot/mongodb-csharp
compare: 6397a0f032f24227894afa6ec37e92dd18dd0b3f
  • 4 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
View
102 source/MongoDB.Tests/IntegrationTests/TestCollection.cs
@@ -192,57 +192,57 @@ public void TestFindWhereEquivalency()
Assert.AreEqual(4, CountDocs(col.Find(explicitWhere)), "Explicit where didn't return 4 docs");
Assert.AreEqual(4, CountDocs(col.Find(funcDoc)), "Function where didn't return 4 docs");
}
-
- [Test]
- public void TestFindAndModifyReturnsOldDocument() {
- IMongoCollection collection = DB["find_and_modify"];
- Document person = new Document().Append("First", "Sally").Append("Last", "Simmons");
- collection.Insert(person);
-
- Document spec = new Document().Append("_id", person["_id"]);
- Document loaded = collection.FindAndModify(new Document().Append("First", "Jane"), spec);
-
- Assert.AreEqual("Sally", loaded["First"]);
- }
-
- [Test]
- public void TestFindAndModifyReturnsNewDocument() {
- IMongoCollection collection = DB["find_and_modify"];
- Document person = new Document().Append("First", "Susie").Append("Last", "O'Hara");
- collection.Insert(person);
-
- Document spec = new Document().Append("_id", person["_id"]);
- Document loaded = collection.FindAndModify(new Document().Append("First", "Darlene"), spec, true);
-
- Assert.AreEqual("Darlene", loaded["First"]);
- }
-
- [Test]
- public void TestFindAndModifySortsResults() {
- IMongoCollection collection = DB["find_and_modify"];
- Document doc1 = new Document().Append("handled", false).Append("priority", 1).Append("value", "Test 1");
- Document doc2 = new Document().Append("handled", false).Append("priority", 2).Append("value", "Test 2");
- collection.Insert(doc1);
- collection.Insert(doc2);
-
- Document update = new Document().Append("handled", true);
- Document spec = new Document().Append("handled", false);
- Document sort = new Document().Append("priority", -1);
- Document loaded = collection.FindAndModify(update, spec, sort, true);
-
- Assert.AreEqual(true, loaded["handled"]);
- Assert.AreEqual(doc2["priority"], loaded["priority"]);
- Assert.AreEqual(doc2["value"], loaded["value"]);
- }
-
- [Test]
- public void TestFindAndModifyReturnNullForNoRecordFound() {
- IMongoCollection collection = DB["find_and_modify"];
- Document spec = new Document().Append("FirstName", "Noone");
- Document loaded = collection.FindAndModify(new Document().Append("First", "Darlene"), spec, true);
-
- Assert.IsNull(loaded, "Should return null for no document found");
- }
+
+ [Test]
+ public void TestFindAndModifyReturnsOldDocument() {
+ IMongoCollection collection = DB["find_and_modify"];
+ Document person = new Document().Append("First", "Sally").Append("Last", "Simmons");
+ collection.Insert(person);
+
+ Document spec = new Document().Append("_id", person["_id"]);
+ Document loaded = collection.FindAndModify(new Document().Append("First", "Jane"), spec);
+
+ Assert.AreEqual("Sally", loaded["First"]);
+ }
+
+ [Test]
+ public void TestFindAndModifyReturnsNewDocument() {
+ IMongoCollection collection = DB["find_and_modify"];
+ Document person = new Document().Append("First", "Susie").Append("Last", "O'Hara");
+ collection.Insert(person);
+
+ Document spec = new Document().Append("_id", person["_id"]);
+ Document loaded = collection.FindAndModify(new Document().Append("First", "Darlene"), spec, true);
+
+ Assert.AreEqual("Darlene", loaded["First"]);
+ }
+
+ [Test]
+ public void TestFindAndModifySortsResults() {
+ IMongoCollection collection = DB["find_and_modify"];
+ Document doc1 = new Document().Append("handled", false).Append("priority", 1).Append("value", "Test 1");
+ Document doc2 = new Document().Append("handled", false).Append("priority", 2).Append("value", "Test 2");
+ collection.Insert(doc1);
+ collection.Insert(doc2);
+
+ Document update = new Document().Append("handled", true);
+ Document spec = new Document().Append("handled", false);
+ Document sort = new Document().Append("priority", -1);
+ Document loaded = collection.FindAndModify(update, spec, sort, true);
+
+ Assert.AreEqual(true, loaded["handled"]);
+ Assert.AreEqual(doc2["priority"], loaded["priority"]);
+ Assert.AreEqual(doc2["value"], loaded["value"]);
+ }
+
+ [Test]
+ public void TestFindAndModifyReturnNullForNoRecordFound() {
+ IMongoCollection collection = DB["find_and_modify"];
+ Document spec = new Document().Append("FirstName", "Noone");
+ Document loaded = collection.FindAndModify(new Document().Append("First", "Darlene"), spec, true);
+
+ Assert.IsNull(loaded, "Should return null for no document found");
+ }
[Test]
public void TestInsertBulkLargerThan4MBOfDocuments()
View
45 source/MongoDB/Configuration/Mapping/AutoMappingStore.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Threading;
using MongoDB.Configuration.Mapping.Auto;
using MongoDB.Configuration.Mapping.Model;
@@ -11,6 +12,7 @@ public class AutoMappingStore : IMappingStore
{
private readonly IAutoMapper _autoMapper;
private readonly Dictionary<Type, IClassMap> _autoMaps;
+ private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
private readonly IMappingStore _wrappedMappingStore;
/// <summary>
@@ -71,23 +73,42 @@ public AutoMappingStore(IAutoMapper autoMapper, IMappingStore mappingStore)
/// <returns></returns>
public IClassMap GetClassMap(Type classType)
{
- IClassMap classMap;
-
- if(_autoMaps.TryGetValue(classType, out classMap))
- return classMap;
-
- if(_wrappedMappingStore != null)
+ try
{
- classMap = _wrappedMappingStore.GetClassMap(classType);
- if(classMap != null)
+ _lock.EnterUpgradeableReadLock();
+
+ IClassMap classMap;
+ if(_autoMaps.TryGetValue(classType, out classMap))
return classMap;
- }
- classMap = _autoMapper.CreateClassMap(classType, GetClassMap);
+ if(_wrappedMappingStore != null)
+ {
+ classMap = _wrappedMappingStore.GetClassMap(classType);
+ if(classMap != null)
+ return classMap;
+ }
+
+ classMap = _autoMapper.CreateClassMap(classType, GetClassMap);
- _autoMaps.Add(classType, classMap);
+ try
+ {
+ _lock.EnterWriteLock();
- return classMap;
+ _autoMaps.Add(classType, classMap);
+
+ return classMap;
+ }
+ finally
+ {
+ if(_lock.IsWriteLockHeld)
+ _lock.ExitWriteLock();
+ }
+ }
+ finally
+ {
+ if(_lock.IsUpgradeableReadLockHeld)
+ _lock.ExitUpgradeableReadLock();
+ }
}
}
}
View
17 source/MongoDB/IMongoCollection_1.cs
@@ -148,9 +148,12 @@ public interface IMongoCollection<T>
/// <param name="selector">The selector.</param>
/// <param name="sort"><see cref="Document"/> containing the names of columns to sort on with the values being the
/// <see cref="IndexOrder"/></param>
+ /// <param name="fields">The fields.</param>
+ /// <param name="remove">if set to <c>true</c> [remove].</param>
/// <param name="returnNew">if set to <c>true</c> [return new].</param>
+ /// <param name="upsert">if set to <c>true</c> [upsert].</param>
/// <returns>A <see cref="Document"/></returns>
- T FindAndModify(object document, object selector, object sort, bool returnNew);
+ T FindAndModify(object document, object selector, object sort, object fields, bool remove, bool returnNew, bool upsert);
/// <summary>
/// Entrypoint into executing a map/reduce query against the collection.
@@ -338,5 +341,17 @@ public interface IMongoCollection<T>
/// <param name = "document">The document.</param>
/// <param name = "safemode">if set to <c>true</c> [safemode].</param>
void Save(object document, bool safemode);
+
+ /// <summary>
+ /// Executes a query and atomically applies a modifier operation to the first document returning the original document
+ /// by default.
+ /// </summary>
+ /// <param name="document">The document.</param>
+ /// <param name="spec"><see cref="Document"/> to find the document.</param>
+ /// <param name="sort"><see cref="Document"/> containing the names of columns to sort on with the values being the
+ /// <see cref="IndexOrder"/></param>
+ /// <param name="returnNew">if set to <c>true</c> [return new].</param>
+ /// <returns>A <see cref="Document"/></returns>
+ T FindAndModify(object document, object spec, object sort, bool returnNew);
}
}
View
41 source/MongoDB/MongoCollection_1.cs
@@ -7,7 +7,6 @@
using MongoDB.Protocol;
using MongoDB.Results;
using MongoDB.Util;
-using MongoDB.Configuration.Mapping.Model;
namespace MongoDB
{
@@ -182,8 +181,9 @@ public T FindOne(string javascriptWhere)
/// <param name="sort"><see cref="Document"/> containing the names of columns to sort on with the values being the</param>
/// <returns>A <see cref="Document"/></returns>
/// <see cref="IndexOrder"/>
- public T FindAndModify(object document, object spec, object sort){
- return FindAndModify(document, spec, sort);
+ public T FindAndModify(object document, object spec, object sort)
+ {
+ return FindAndModify(document, spec, sort, null, false, false, false);
}
/// <summary>
@@ -194,8 +194,23 @@ public T FindOne(string javascriptWhere)
/// <param name="spec"><see cref="Document"/> to find the document.</param>
/// <param name="returnNew">if set to <c>true</c> [return new].</param>
/// <returns>A <see cref="Document"/></returns>
- public T FindAndModify(object document, object spec, bool returnNew){
- return FindAndModify(document, spec, new Document(), returnNew);
+ public T FindAndModify(object document, object spec, bool returnNew)
+ {
+ return FindAndModify(document, spec, null, null, false, returnNew, false);
+ }
+ /// <summary>
+ /// Executes a query and atomically applies a modifier operation to the first document returning the original document
+ /// by default.
+ /// </summary>
+ /// <param name="document">The document.</param>
+ /// <param name="spec"><see cref="Document"/> to find the document.</param>
+ /// <param name="sort"><see cref="Document"/> containing the names of columns to sort on with the values being the
+ /// <see cref="IndexOrder"/></param>
+ /// <param name="returnNew">if set to <c>true</c> [return new].</param>
+ /// <returns>A <see cref="Document"/></returns>
+ public T FindAndModify(object document, object spec, object sort, bool returnNew)
+ {
+ return FindAndModify(document, spec, sort, null, false, returnNew, false);
}
/// <summary>
@@ -206,9 +221,13 @@ public T FindOne(string javascriptWhere)
/// <param name="spec"><see cref="Document"/> to find the document.</param>
/// <param name="sort"><see cref="Document"/> containing the names of columns to sort on with the values being the
/// <see cref="IndexOrder"/></param>
+ /// <param name="fields">The fields.</param>
+ /// <param name="remove">if set to <c>true</c> [remove].</param>
/// <param name="returnNew">if set to <c>true</c> [return new].</param>
+ /// <param name="upsert">if set to <c>true</c> [upsert].</param>
/// <returns>A <see cref="Document"/></returns>
- public T FindAndModify(object document, object spec, object sort, bool returnNew){
+ public T FindAndModify(object document, object spec, object sort, object fields, bool remove, bool returnNew, bool upsert)
+ {
try
{
var command = new Document
@@ -216,10 +235,16 @@ public T FindOne(string javascriptWhere)
{"findandmodify", Name},
{"query", spec},
{"update", EnsureUpdateDocument(document)},
- {"sort", sort},
- {"new", returnNew}
+ {"new", returnNew},
+ {"remove", remove},
+ {"upsert", upsert}
};
+ if(sort != null)
+ command.Add("sort", sort);
+ if(fields != null)
+ command.Add("fields", fields);
+
var response = _connection.SendCommand<FindAndModifyResult<T>>(_configuration.SerializationFactory,
DatabaseName,
typeof(T),
View
21 source/MongoDB/Obsolete/IMongoCollection.cs
@@ -30,7 +30,7 @@ public interface IMongoCollection
/// Gets the database.
/// </summary>
/// <value>The database.</value>
- IMongoDatabase Database { get; }
+ IMongoDatabase Database { get; }
/// <summary>
/// Gets the meta data.
@@ -125,7 +125,22 @@ public interface IMongoCollection
/// <param name="returnNew">if set to <c>true</c> [return new].</param>
/// <returns>A <see cref="Document"/></returns>
Document FindAndModify(Document document, Document selector, Document sort, bool returnNew);
-
+
+ /// <summary>
+ /// Executes a query and atomically applies a modifier operation to the first document returning the original document
+ /// by default.
+ /// </summary>
+ /// <param name="document">The document.</param>
+ /// <param name="spec"><see cref="Document"/> to find the document.</param>
+ /// <param name="sort"><see cref="Document"/> containing the names of columns to sort on with the values being the
+ /// <see cref="IndexOrder"/></param>
+ /// <param name="fields">The fields.</param>
+ /// <param name="remove">if set to <c>true</c> [remove].</param>
+ /// <param name="returnNew">if set to <c>true</c> [return new].</param>
+ /// <param name="upsert">if set to <c>true</c> [upsert].</param>
+ /// <returns>A <see cref="Document"/></returns>
+ Document FindAndModify(Document document, Document spec, Document sort, Document fields, bool remove, bool returnNew, bool upsert);
+
/// <summary>
/// Maps the reduce.
/// </summary>
@@ -272,6 +287,6 @@ public interface IMongoCollection
/// </summary>
/// <param name = "document">The document.</param>
/// <param name = "safemode">if set to <c>true</c> [safemode].</param>
- void Save(Document document, bool safemode);
+ void Save(Document document, bool safemode);
}
}
View
38 source/MongoDB/Obsolete/MongoCollection.cs
@@ -173,18 +173,36 @@ public Document FindAndModify(Document document, Document spec, bool returnNew)
}
/// <summary>
- /// Executes a query and atomically applies a modifier operation to the first document returning the original document
- /// by default.
- /// </summary>
- /// <param name = "document">The document.</param>
- /// <param name = "spec"><see cref = "Document" /> to find the document.</param>
- /// <param name = "sort"><see cref = "Document" /> containing the names of columns to sort on with the values being the
- /// <see cref = "IndexOrder" /></param>
- /// <param name = "returnNew">if set to <c>true</c> [return new].</param>
- /// <returns>A <see cref = "Document" /></returns>
+ /// Executes a query and atomically applies a modifier operation to the first document returning the original document
+ /// by default.
+ /// </summary>
+ /// <param name="document">The document.</param>
+ /// <param name="spec"><see cref="Document"/> to find the document.</param>
+ /// <param name="sort"><see cref="Document"/> containing the names of columns to sort on with the values being the
+ /// <see cref="IndexOrder"/></param>
+ /// <param name="returnNew">if set to <c>true</c> [return new].</param>
+ /// <returns>A <see cref="Document"/></returns>
public Document FindAndModify(Document document, Document spec, Document sort, bool returnNew)
{
- return _collection.FindAndModify(document, spec, sort, returnNew);
+ return _collection.FindAndModify(document, spec, sort, null, false, returnNew, false);
+ }
+
+ /// <summary>
+ /// Executes a query and atomically applies a modifier operation to the first document returning the original document
+ /// by default.
+ /// </summary>
+ /// <param name="document">The document.</param>
+ /// <param name="spec"><see cref="Document"/> to find the document.</param>
+ /// <param name="sort"><see cref="Document"/> containing the names of columns to sort on with the values being the
+ /// <see cref="IndexOrder"/></param>
+ /// <param name="fields">The fields.</param>
+ /// <param name="remove">if set to <c>true</c> [remove].</param>
+ /// <param name="returnNew">if set to <c>true</c> [return new].</param>
+ /// <param name="upsert">if set to <c>true</c> [upsert].</param>
+ /// <returns>A <see cref="Document"/></returns>
+ public Document FindAndModify(Document document, Document spec, Document sort, Document fields, bool remove, bool returnNew, bool upsert)
+ {
+ return _collection.FindAndModify(document, spec, sort, fields, remove, returnNew, upsert);
}
/// <summary>

No commit comments for this range

Something went wrong with that request. Please try again.