Skip to content

Commit 8b1758a

Browse files
author
rstam
committed
Miscelaneous fixes.
1 parent 83d8891 commit 8b1758a

File tree

10 files changed

+66
-53
lines changed

10 files changed

+66
-53
lines changed

MongoDB.Bson/ObjectModel/BsonDocument.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ public virtual BsonDocument Add(string name, Func<BsonValue> valueFactory, bool
632632
}
633633
if (valueFactory == null)
634634
{
635-
throw new ArgumentNullException("value");
635+
throw new ArgumentNullException("valueFactory");
636636
}
637637
if (condition)
638638
{

MongoDB.Driver/AggregateArgs.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,26 @@ public AggregateOutputMode OutputMode
114114
public IEnumerable<BsonDocument> Pipeline
115115
{
116116
get { return _pipeline; }
117-
set { _pipeline = value.ToList(); }
117+
set { _pipeline = value; }
118+
}
119+
120+
// internal methods
121+
internal AggregateArgs WithMaterializedPipeline()
122+
{
123+
if (_pipeline is BsonDocument[] || _pipeline is List<BsonDocument>)
124+
{
125+
return this;
126+
}
127+
128+
var materializedPipeline = _pipeline.ToArray();
129+
return new AggregateArgs
130+
{
131+
AllowDiskUse = _allowDiskUse,
132+
BatchSize = _batchSize,
133+
MaxTime = _maxTime,
134+
OutputMode = _outputMode,
135+
Pipeline = materializedPipeline
136+
};
118137
}
119138
}
120139
}

MongoDB.Driver/BulkWriteUpsert.cs

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -65,39 +65,6 @@ public int Index
6565
get { return _index; }
6666
}
6767

68-
// public methods
69-
/// <summary>
70-
/// Determines whether the specified <see cref="System.Object"></see>, is equal to this instance.
71-
/// </summary>
72-
/// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
73-
/// <returns>
74-
/// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
75-
/// </returns>
76-
public override bool Equals(object obj)
77-
{
78-
if (obj == null || obj.GetType() != typeof(BulkWriteUpsert))
79-
{
80-
return false;
81-
}
82-
83-
var rhs = (BulkWriteUpsert)obj;
84-
return _id == rhs._id && _index == rhs._index;
85-
}
86-
87-
/// <summary>
88-
/// Returns a hash code for this instance.
89-
/// </summary>
90-
/// <returns>
91-
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
92-
/// </returns>
93-
public override int GetHashCode()
94-
{
95-
return new Hasher()
96-
.Hash(_id)
97-
.Hash(_index)
98-
.GetHashCode();
99-
}
100-
10168
// internal methods
10269
internal BulkWriteUpsert WithMappedIndex(IndexMap indexMap)
10370
{

MongoDB.Driver/MongoCollection.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using System.Collections;
1818
using System.Collections.Generic;
19+
using System.Collections.ObjectModel;
1920
using System.Linq;
2021
using System.Reflection;
2122
using System.Text;
@@ -124,8 +125,8 @@ public virtual IEnumerable<BsonDocument> Aggregate(AggregateArgs args)
124125
if (args == null) { throw new ArgumentNullException("args"); }
125126
if (args.Pipeline == null) { throw new ArgumentException("Pipeline is null.", "args"); }
126127

127-
var pipeline = (List<BsonDocument>)args.Pipeline;
128-
var lastStage = pipeline.LastOrDefault();
128+
args = args.WithMaterializedPipeline(); // materialize the pipeline if necessary to prevent double enumeration side effects
129+
var lastStage = args.Pipeline.LastOrDefault();
129130

130131
string outputCollectionName = null;
131132
if (lastStage != null && lastStage.GetElement(0).Name == "$out")
@@ -1685,7 +1686,7 @@ public virtual MapReduceResult MapReduce(MapReduceArgs args)
16851686
/// <typeparam name="TDocument">The type of the document.</typeparam>
16861687
/// <param name="args">The args.</param>
16871688
/// <returns>Multiple enumerators, one for each cursor.</returns>
1688-
public List<IEnumerator<TDocument>> ParallelScanAs<TDocument>(ParallelScanArgs args)
1689+
public ReadOnlyCollection<IEnumerator<TDocument>> ParallelScanAs<TDocument>(ParallelScanArgs args)
16891690
{
16901691
var readPreference = args.ReadPreference ?? _settings.ReadPreference ?? ReadPreference.Primary;
16911692
var connection = _server.AcquireConnection(readPreference);
@@ -1718,13 +1719,13 @@ public List<IEnumerator<TDocument>> ParallelScanAs<TDocument>(ParallelScanArgs a
17181719
/// <param name="documentType">Type of the document.</param>
17191720
/// <param name="args">The args.</param>
17201721
/// <returns>Multiple enumerators, one for each cursor.</returns>
1721-
public List<IEnumerator> ParallelScanAs(Type documentType, ParallelScanArgs args)
1722+
public ReadOnlyCollection<IEnumerator> ParallelScanAs(Type documentType, ParallelScanArgs args)
17221723
{
17231724
var methodDefinition = GetType().GetMethod("ParallelScanAs", new Type[] { typeof(ParallelScanArgs) });
17241725
var methodInfo = methodDefinition.MakeGenericMethod(documentType);
17251726
try
17261727
{
1727-
return ((IEnumerable)methodInfo.Invoke(this, new object[] { args })).Cast<IEnumerator>().ToList();
1728+
return new ReadOnlyCollection<IEnumerator>(((IEnumerable)methodInfo.Invoke(this, new object[] { args })).Cast<IEnumerator>().ToList());
17281729
}
17291730
catch (TargetInvocationException ex)
17301731
{
@@ -2703,7 +2704,7 @@ public virtual IEnumerable<WriteConcernResult> InsertBatch(
27032704
/// </summary>
27042705
/// <param name="args">The args.</param>
27052706
/// <returns>Multiple enumerators, one for each cursor.</returns>
2706-
public virtual List<IEnumerator<TDefaultDocument>> ParallelScan(ParallelScanArgs args)
2707+
public virtual ReadOnlyCollection<IEnumerator<TDefaultDocument>> ParallelScan(ParallelScanArgs args)
27072708
{
27082709
return ParallelScanAs<TDefaultDocument>(args);
27092710
}

MongoDB.Driver/Operations/BulkInsertOperation.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,21 @@ protected override void SerializeRequest(BsonBinaryWriter bsonWriter, WriteReque
107107
}
108108

109109
var actualType = document.GetType();
110-
if (_cachedSerializerType != actualType)
110+
111+
IBsonSerializer serializer;
112+
if (actualType == insertRequest.NominalType && insertRequest.Serializer != null)
113+
{
114+
serializer = insertRequest.Serializer;
115+
}
116+
else
111117
{
112-
_cachedSerializer = BsonSerializer.LookupSerializer(actualType);
113-
_cachedSerializerType = actualType;
118+
if (_cachedSerializerType != actualType)
119+
{
120+
_cachedSerializer = BsonSerializer.LookupSerializer(actualType);
121+
_cachedSerializerType = actualType;
122+
}
123+
serializer = _cachedSerializer;
114124
}
115-
var serializer = _cachedSerializer;
116125
var serializationOptions = insertRequest.SerializationOptions ?? DocumentSerializationOptions.SerializeIdFirstInstance;
117126

118127
var savedCheckElementNames = bsonWriter.CheckElementNames;

MongoDB.Driver/Operations/BulkUpdateOperation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected override void SerializeRequest(BsonBinaryWriter bsonWriter, WriteReque
8181
{
8282
var updateRequest = (UpdateRequest)request;
8383

84-
bsonWriter.PushMaxDocumentSize(_args.MaxDocumentSize);
84+
bsonWriter.PushMaxDocumentSize(_args.MaxWireDocumentSize);
8585
bsonWriter.WriteStartDocument();
8686
bsonWriter.WriteName("q");
8787
BsonSerializer.Serialize(bsonWriter, updateRequest.Query ?? new QueryDocument());

MongoDB.Driver/Operations/BulkWriteArgs.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class BulkWriteArgs
2929
private bool? _isOrdered;
3030
private int? _maxBatchCount;
3131
private int? _maxBatchLength;
32-
private IEnumerable<WriteRequest> _requests = Enumerable.Empty<WriteRequest>();
32+
private IEnumerable<WriteRequest> _requests;
3333
private WriteConcern _writeConcern;
3434

3535
// public properties
@@ -96,7 +96,7 @@ public int? MaxBatchLength
9696
public IEnumerable<WriteRequest> Requests
9797
{
9898
get { return _requests; }
99-
set { _requests = value ?? Enumerable.Empty<WriteRequest>(); }
99+
set { _requests = value; }
100100
}
101101

102102
/// <summary>

MongoDB.Driver/Operations/BulkWriteBatchResultCombiner.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public BulkWriteBatchResultCombiner(IList<BulkWriteBatchResult> batchResults, bo
3434
}
3535

3636
// public methods
37-
public BulkWriteResult CreateResultOrThrowIfHasErrors(IEnumerable<WriteRequest> remainingRequest)
37+
public BulkWriteResult CreateResultOrThrowIfHasErrors(IEnumerable<WriteRequest> remainingRequests)
3838
{
3939
if (_batchResults.Any(r => r.HasWriteErrors || r.HasWriteConcernError))
4040
{
41-
throw CreateBulkWriteException(remainingRequest);
41+
throw CreateBulkWriteException(remainingRequests);
4242
}
4343

4444
return CreateBulkWriteResult();

MongoDB.Driver/Operations/ParallelScanOperation.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
using System.Collections.Generic;
17+
using System.Collections.ObjectModel;
1718
using System.Linq;
1819
using MongoDB.Bson;
1920
using MongoDB.Bson.IO;
@@ -59,7 +60,7 @@ public ParallelScanOperation(
5960
}
6061

6162
// public methods
62-
public List<IEnumerator<TDocument>> Execute(MongoConnection connection)
63+
public ReadOnlyCollection<IEnumerator<TDocument>> Execute(MongoConnection connection)
6364
{
6465
var command = new CommandDocument
6566
{
@@ -113,7 +114,7 @@ public List<IEnumerator<TDocument>> Execute(MongoConnection connection)
113114
enumerators.Add(enumerator);
114115
}
115116

116-
return enumerators;
117+
return new ReadOnlyCollection<IEnumerator<TDocument>>(enumerators);
117118
}
118119
}
119120
}

MongoDB.DriverUnitTests/Operations/BulkWriteBatchResultCombinerTests.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public void TestCombineUpserts(int count1, int count2)
125125
var result2 = CreateBatchResult(upserts: upserts2);
126126
var result = CombineResults(result1, result2);
127127
var expectedUpserts = upserts1.Concat(upserts2).ToArray();
128-
Assert.IsTrue(expectedUpserts.SequenceEqual(result.Upserts));
128+
Assert.IsTrue(expectedUpserts.SequenceEqual(result.Upserts, new BulkWriteUpsertComparer()));
129129
}
130130

131131
private BulkWriteResult CombineResults(params BulkWriteBatchResult[] batchResults)
@@ -163,5 +163,21 @@ private BulkWriteBatchResult CreateBatchResult(
163163
indexMap ?? IndexMap.IdentityMap,
164164
nextBatch);
165165
}
166+
167+
// nested types
168+
private class BulkWriteUpsertComparer : IEqualityComparer<BulkWriteUpsert>
169+
{
170+
public bool Equals(BulkWriteUpsert lhs, BulkWriteUpsert rhs)
171+
{
172+
if (object.ReferenceEquals(lhs, rhs)) { return true; }
173+
if (object.ReferenceEquals(lhs, null) || object.ReferenceEquals(rhs, null)) { return false; }
174+
return lhs.Index == rhs.Index && lhs.Id == rhs.Id;
175+
}
176+
177+
public int GetHashCode(BulkWriteUpsert obj)
178+
{
179+
return 0;
180+
}
181+
}
166182
}
167183
}

0 commit comments

Comments
 (0)