Skip to content

Commit

Permalink
CSHARP-2123: Round up to the next highest integral maxTimeMS instead …
Browse files Browse the repository at this point in the history
…of truncating.
  • Loading branch information
rstam committed Jan 7, 2018
1 parent ef9872a commit 50c119f
Show file tree
Hide file tree
Showing 64 changed files with 646 additions and 219 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -12,7 +12,7 @@ TestResults
project.lock.json
.vscode

#JetBrains Rider artifcats
# JetBrains Rider artifacts
.idea

# CodeReview
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -104,6 +104,7 @@ Please see our [guidelines](CONTRIBUTING.md) for contributing to the driver.
* Jacob Jewell jacobjewell@eflexsystems.com
* Danny Kendrick https://github.com/dkendrick
* Brian Knight brianknight10@gmail.com
* Andrey Kondratyev https://github.com/byTimo
* Anatoly Koperin https://github.com/ExM
* Nik Kolev nkolev@gmail.com
* Oleg Kosmakov https://github.com/kosmakoff
Expand Down
Expand Up @@ -124,7 +124,7 @@ public BsonValue Hint
public TimeSpan? MaxTime
{
get { return _maxTime; }
set { _maxTime = value; }
set { _maxTime = Ensure.IsNullOrInfiniteOrGreaterThanOrEqualToZero(value, nameof(value)); }
}

/// <summary>
Expand Down Expand Up @@ -160,7 +160,7 @@ internal BsonDocument CreateCommand(SemanticVersion serverVersion)
{ "explain", true },
{ "pipeline", new BsonArray(_pipeline) },
{ "allowDiskUse", () => _allowDiskUse.Value, _allowDiskUse.HasValue },
{ "maxTimeMS", () => (int)_maxTime.Value.TotalMilliseconds, _maxTime.HasValue },
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
{ "collation", () => _collation.ToBsonDocument(), _collation != null },
{ "hint", () => _hint, _hint != null },
{ "comment", () => _comment, _comment != null }
Expand Down
4 changes: 2 additions & 2 deletions src/MongoDB.Driver.Core/Core/Operations/AggregateOperation.cs
Expand Up @@ -157,7 +157,7 @@ public BsonValue Hint
public TimeSpan? MaxTime
{
get { return _maxTime; }
set { _maxTime = value; }
set { _maxTime = Ensure.IsNullOrInfiniteOrGreaterThanOrEqualToZero(value, nameof(value)); }
}

/// <summary>
Expand Down Expand Up @@ -277,7 +277,7 @@ internal BsonDocument CreateCommand(ConnectionDescription connectionDescription,
{ "aggregate", _collectionNamespace.CollectionName },
{ "pipeline", new BsonArray(_pipeline) },
{ "allowDiskUse", () => _allowDiskUse.Value, _allowDiskUse.HasValue },
{ "maxTimeMS", () => (int)_maxTime.Value.TotalMilliseconds, _maxTime.HasValue },
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
{ "collation", () => _collation.ToBsonDocument(), _collation != null },
{ "hint", () => _hint, _hint != null },
{ "comment", () => _comment, _comment != null }
Expand Down
Expand Up @@ -140,7 +140,7 @@ public BsonValue Hint
public TimeSpan? MaxTime
{
get { return _maxTime; }
set { _maxTime = value; }
set { _maxTime = Ensure.IsNullOrInfiniteOrGreaterThanOrEqualToZero(value, nameof(value)); }
}

/// <summary>
Expand Down Expand Up @@ -220,7 +220,7 @@ internal BsonDocument CreateCommand(SemanticVersion serverVersion)
{ "pipeline", new BsonArray(_pipeline) },
{ "allowDiskUse", () => _allowDiskUse.Value, _allowDiskUse.HasValue },
{ "bypassDocumentValidation", () => _bypassDocumentValidation.Value, _bypassDocumentValidation.HasValue && Feature.BypassDocumentValidation.IsSupported(serverVersion) },
{ "maxTimeMS", () => (int)_maxTime.Value.TotalMilliseconds, _maxTime.HasValue },
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
{ "collation", () => _collation.ToBsonDocument(), _collation != null },
{ "writeConcern", () => _writeConcern.ToBsonDocument(), Feature.CommandsThatWriteAcceptWriteConcern.ShouldSendWriteConcern(serverVersion, _writeConcern) },
{ "cursor", new BsonDocument(), serverVersion >= new SemanticVersion(3, 5, 0) },
Expand Down
2 changes: 1 addition & 1 deletion src/MongoDB.Driver.Core/Core/Operations/AsyncCursor.cs
Expand Up @@ -157,7 +157,7 @@ private BsonDocument CreateGetMoreCommand()
{ "getMore", _cursorId },
{ "collection", _collectionNamespace.CollectionName },
{ "batchSize", () => _batchSize.Value, _batchSize > 0 },
{ "maxTimeMS", () => (int)_maxTime.Value.TotalMilliseconds, _maxTime.HasValue }
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue }
};

return command;
Expand Down
2 changes: 1 addition & 1 deletion src/MongoDB.Driver.Core/Core/Operations/CountOperation.cs
Expand Up @@ -172,7 +172,7 @@ internal BsonDocument CreateCommand(ConnectionDescription connectionDescription,
{ "limit", () => _limit.Value, _limit.HasValue },
{ "skip", () => _skip.Value, _skip.HasValue },
{ "hint", _hint, _hint != null },
{ "maxTimeMS", () => (int)_maxTime.Value.TotalMilliseconds, _maxTime.HasValue },
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
{ "collation", () => _collation.ToBsonDocument(), _collation != null }
};

Expand Down
Expand Up @@ -191,7 +191,7 @@ internal BsonDocument CreateCommand(ConnectionDescription connectionDescription,
{ "distinct", _collectionNamespace.CollectionName },
{ "key", _fieldName },
{ "query", _filter, _filter != null },
{ "maxTimeMS", () => (int)_maxTime.Value.TotalMilliseconds, _maxTime.HasValue },
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
{ "collation", () => _collation.ToBsonDocument(), _collation != null }
};

Expand Down
2 changes: 1 addition & 1 deletion src/MongoDB.Driver.Core/Core/Operations/EvalOperation.cs
Expand Up @@ -152,7 +152,7 @@ internal BsonDocument CreateCommand()
{ "$eval", _function },
{ "args", () => new BsonArray(_args), _args != null },
{ "nolock", () => _noLock.Value, _noLock.HasValue },
{ "maxTimeMS", () => (int)_maxTime.Value.TotalMilliseconds, _maxTime.HasValue }
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue }
};
}

Expand Down
Expand Up @@ -255,7 +255,7 @@ public BsonDocument Max
public TimeSpan? MaxTime
{
get { return _maxTime; }
set { _maxTime = Ensure.IsNullOrGreaterThanZero(value, nameof(value)); }
set { _maxTime = Ensure.IsNullOrInfiniteOrGreaterThanOrEqualToZero(value, nameof(value)); }
}

/// <summary>
Expand Down Expand Up @@ -434,7 +434,7 @@ internal BsonDocument CreateCommand(ConnectionDescription connectionDescription,
{ "singleBatch", () => _limit < 0 || _singleBatch.Value, _limit < 0 || _singleBatch.HasValue },
{ "comment", _comment, _comment != null },
{ "maxScan", () => _maxScan.Value, _maxScan.HasValue },
{ "maxTimeMS", () => (int)_maxTime.Value.TotalMilliseconds, _maxTime.HasValue },
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
{ "max", _max, _max != null },
{ "min", _min, _min != null },
{ "returnKey", () => _returnKey.Value, _returnKey.HasValue },
Expand Down
Expand Up @@ -69,7 +69,7 @@ public BsonDocument Filter
public TimeSpan? MaxTime
{
get { return _maxTime; }
set { _maxTime = value; }
set { _maxTime = Ensure.IsNullOrInfiniteOrGreaterThanOrEqualToZero(value, nameof(value)); }
}

/// <summary>
Expand Down Expand Up @@ -108,7 +108,7 @@ internal override BsonDocument CreateCommand(SemanticVersion serverVersion, long
{ "remove", true },
{ "sort", _sort, _sort != null },
{ "fields", _projection, _projection != null },
{ "maxTimeMS", () => (int)_maxTime.Value.TotalMilliseconds, _maxTime.HasValue },
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
{ "writeConcern", () => WriteConcern.ToBsonDocument(), WriteConcern != null && !WriteConcern.IsServerDefault && Feature.FindAndModifyWriteConcern.IsSupported(serverVersion) },
{ "collation", () => Collation.ToBsonDocument(), Collation != null },
{ "txnNumber", () => transactionNumber, transactionNumber.HasValue }
Expand Down
Expand Up @@ -106,7 +106,7 @@ public bool IsUpsert
public TimeSpan? MaxTime
{
get { return _maxTime; }
set { _maxTime = value; }
set { _maxTime = Ensure.IsNullOrInfiniteOrGreaterThanOrEqualToZero(value, nameof(value)); }
}

/// <summary>
Expand Down Expand Up @@ -170,7 +170,7 @@ internal override BsonDocument CreateCommand(SemanticVersion serverVersion, long
{ "sort", _sort, _sort != null },
{ "fields", _projection, _projection != null },
{ "upsert", true, _isUpsert },
{ "maxTimeMS", () => (int)_maxTime.Value.TotalMilliseconds, _maxTime.HasValue },
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
{ "writeConcern", () => WriteConcern.ToBsonDocument(), WriteConcern != null && !WriteConcern.IsServerDefault && Feature.FindAndModifyWriteConcern.IsSupported(serverVersion) },
{ "bypassDocumentValidation", () => _bypassDocumentValidation.Value, _bypassDocumentValidation.HasValue && Feature.BypassDocumentValidation.IsSupported(serverVersion) },
{ "collation", () => Collation.ToBsonDocument(), Collation != null },
Expand Down
Expand Up @@ -123,7 +123,7 @@ public bool IsUpsert
public TimeSpan? MaxTime
{
get { return _maxTime; }
set { _maxTime = value; }
set { _maxTime = Ensure.IsNullOrInfiniteOrGreaterThanOrEqualToZero(value, nameof(value)); }
}

/// <summary>
Expand Down Expand Up @@ -187,7 +187,7 @@ internal override BsonDocument CreateCommand(SemanticVersion serverVersion, long
{ "sort", _sort, _sort != null },
{ "fields", _projection, _projection != null },
{ "upsert", true, _isUpsert },
{ "maxTimeMS", () => (int)_maxTime.Value.TotalMilliseconds, _maxTime.HasValue },
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
{ "writeConcern", () => WriteConcern.ToBsonDocument(), WriteConcern != null && !WriteConcern.IsServerDefault && Feature.FindAndModifyWriteConcern.IsSupported(serverVersion) },
{ "bypassDocumentValidation", () => _bypassDocumentValidation.Value, _bypassDocumentValidation.HasValue && Feature.BypassDocumentValidation.IsSupported(serverVersion) },
{ "collation", () => Collation.ToBsonDocument(), Collation != null },
Expand Down
@@ -1,4 +1,4 @@
/* Copyright 2015-present MongoDB Inc.
/* Copyright 2015-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -220,7 +220,7 @@ public BsonDocument Max
public TimeSpan? MaxTime
{
get { return _maxTime; }
set { _maxTime = Ensure.IsNullOrGreaterThanZero(value, nameof(value)); }
set { _maxTime = Ensure.IsNullOrInfiniteOrGreaterThanOrEqualToZero(value, nameof(value)); }
}

/// <summary>
Expand Down Expand Up @@ -408,7 +408,7 @@ internal BsonDocument CreateWrappedQuery(ServerType serverType, ReadPreference r
{ "$readPreference", readPreferenceDocument, readPreferenceDocument != null },
{ "$orderby", _sort, _sort != null },
{ "$comment", _comment, _comment != null },
{ "$maxTimeMS", () => (int)_maxTime.Value.TotalMilliseconds, _maxTime.HasValue },
{ "$maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
{ "$hint", _hint, _hint != null },
{ "$max", _max, _max != null },
{ "$maxScan", () => _maxScan.Value, _maxScan.HasValue },
Expand Down
2 changes: 1 addition & 1 deletion src/MongoDB.Driver.Core/Core/Operations/FindOperation.cs
Expand Up @@ -243,7 +243,7 @@ public BsonDocument Max
public TimeSpan? MaxTime
{
get { return _maxTime; }
set { _maxTime = value; }
set { _maxTime = Ensure.IsNullOrInfiniteOrGreaterThanOrEqualToZero(value, nameof(value)); }
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/MongoDB.Driver.Core/Core/Operations/GeoNearOperation.cs
Expand Up @@ -129,7 +129,7 @@ public BsonDocument Filter
public TimeSpan? MaxTime
{
get { return _maxTime; }
set { _maxTime = value; }
set { _maxTime = Ensure.IsNullOrInfiniteOrGreaterThanOrEqualToZero(value, nameof(value)); }
}

/// <summary>
Expand Down Expand Up @@ -225,7 +225,7 @@ internal BsonDocument CreateCommand(ConnectionDescription connectionDescription,
{ "distanceMultiplier", () => _distanceMultiplier.Value, _distanceMultiplier.HasValue },
{ "includeLocs", () => _includeLocs.Value, _includeLocs.HasValue },
{ "uniqueDocs", () => _uniqueDocs.Value, _uniqueDocs.HasValue },
{ "maxTimeMS", () => (int)_maxTime.Value.TotalMilliseconds, _maxTime.HasValue },
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
{ "collation", () => _collation.ToBsonDocument(), _collation != null }
};

Expand Down
4 changes: 2 additions & 2 deletions src/MongoDB.Driver.Core/Core/Operations/GeoSearchOperation.cs
Expand Up @@ -88,7 +88,7 @@ public CollectionNamespace CollectionNamespace
public TimeSpan? MaxTime
{
get { return _maxTime; }
set { _maxTime = value; }
set { _maxTime = Ensure.IsNullOrInfiniteOrGreaterThanOrEqualToZero(value, nameof(value)); }
}

/// <summary>
Expand Down Expand Up @@ -170,7 +170,7 @@ internal BsonDocument CreateCommand(ConnectionDescription connectionDescription,
{ "limit", () => _limit.Value, _limit.HasValue },
{ "maxDistance", () => _maxDistance.Value, _maxDistance.HasValue },
{ "search", _search, _search != null },
{ "maxTimeMS", () => (int)_maxTime.Value.TotalMilliseconds, _maxTime.HasValue },
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue }
};

ReadConcernHelper.AppendReadConcern(command, _readConcern, connectionDescription, session);
Expand Down
4 changes: 2 additions & 2 deletions src/MongoDB.Driver.Core/Core/Operations/GroupOperation.cs
Expand Up @@ -173,7 +173,7 @@ public BsonJavaScript KeyFunction
public TimeSpan? MaxTime
{
get { return _maxTime; }
set { _maxTime = value; }
set { _maxTime = Ensure.IsNullOrInfiniteOrGreaterThanOrEqualToZero(value, nameof(value)); }
}

/// <summary>
Expand Down Expand Up @@ -256,7 +256,7 @@ internal BsonDocument CreateCommand(SemanticVersion serverVersion)
{ "collation", () => _collation.ToBsonDocument(), _collation != null }
}
},
{ "maxTimeMS", () => (int)_maxTime.Value.TotalMilliseconds, _maxTime.HasValue }
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue }
};
}

Expand Down
Expand Up @@ -157,7 +157,7 @@ public BsonJavaScript MapFunction
public TimeSpan? MaxTime
{
get { return _maxTime; }
set { _maxTime = Ensure.IsNullOrGreaterThanZero(value, nameof(value)); }
set { _maxTime = Ensure.IsNullOrInfiniteOrGreaterThanOrEqualToZero(value, nameof(value)); }
}

/// <summary>
Expand Down Expand Up @@ -245,7 +245,7 @@ protected internal virtual BsonDocument CreateCommand(ConnectionDescription conn
{ "scope", _scope, _scope != null },
{ "jsMode", () => _javaScriptMode.Value, _javaScriptMode.HasValue },
{ "verbose", () => _verbose.Value, _verbose.HasValue },
{ "maxTimeMS", () => (int)_maxTime.Value.TotalMilliseconds, _maxTime.HasValue },
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
{ "collation", () => _collation.ToBsonDocument(), _collation != null }
};
}
Expand Down
47 changes: 47 additions & 0 deletions src/MongoDB.Driver.Core/Core/Operations/MaxTimeHelper.cs
@@ -0,0 +1,47 @@
/* Copyright 2018-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.Threading;

namespace MongoDB.Driver.Core.Operations
{
/// <summary>
/// Helper methods for MaxTime.
/// </summary>
public static class MaxTimeHelper
{
/// <summary>
/// Converts a TimeSpan to the appropriate integral number of milliseconds to send to the server as the maxTimeMS parameter.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The integral milliseconds.</returns>
public static int ToMaxTimeMS(TimeSpan value)
{
if (value == Timeout.InfiniteTimeSpan)
{
return 0;
}
else if (value < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(value));
}
else
{
return (int)Math.Ceiling(value.TotalMilliseconds);
}
}
}
}
1 change: 1 addition & 0 deletions src/MongoDB.Driver.Core/MongoDB.Driver.Core.csproj
Expand Up @@ -119,6 +119,7 @@
<Compile Include="Core\Operations\CreateViewOperation.cs" />
<Compile Include="Core\Clusters\IClusterClock.cs" />
<Compile Include="Core\Operations\DelayedEvaluationWriteConcernSerializer.cs" />
<Compile Include="Core\Operations\MaxTimeHelper.cs" />
<Compile Include="Core\Operations\RetryableDeleteCommandOperation.cs" />
<Compile Include="Core\Operations\RetryableInsertCommandOperation.cs" />
<Compile Include="Core\Operations\IOperationClock.cs" />
Expand Down
2 changes: 1 addition & 1 deletion src/MongoDB.Driver.GridFS/GridFSFindOptions.cs
Expand Up @@ -66,7 +66,7 @@ public class GridFSFindOptions<TFileId>
public TimeSpan? MaxTime
{
get { return _maxTime; }
set { _maxTime = Ensure.IsNullOrGreaterThanZero(value, nameof(value)); }
set { _maxTime = Ensure.IsNullOrInfiniteOrGreaterThanOrEqualToZero(value, nameof(value)); }
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion src/MongoDB.Driver.Legacy/AggregateArgs.cs
Expand Up @@ -17,6 +17,7 @@
using System.Collections.Generic;
using System.Linq;
using MongoDB.Bson;
using MongoDB.Driver.Core.Misc;

namespace MongoDB.Driver
{
Expand Down Expand Up @@ -105,7 +106,7 @@ public Collation Collation
public TimeSpan? MaxTime
{
get { return _maxTime; }
set { _maxTime = value; }
set { _maxTime = Ensure.IsNullOrInfiniteOrGreaterThanOrEqualToZero(value, nameof(value)); }
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion src/MongoDB.Driver.Legacy/CountArgs.cs
Expand Up @@ -15,6 +15,7 @@

using System;
using MongoDB.Bson;
using MongoDB.Driver.Core.Misc;

namespace MongoDB.Driver
{
Expand Down Expand Up @@ -64,7 +65,7 @@ public BsonValue Hint
public TimeSpan? MaxTime
{
get { return _maxTime; }
set { _maxTime = value; }
set { _maxTime = Ensure.IsNullOrInfiniteOrGreaterThanOrEqualToZero(value, nameof(value)); }
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion src/MongoDB.Driver.Legacy/DistinctArgs.cs
Expand Up @@ -15,6 +15,7 @@

using System;
using MongoDB.Bson.Serialization;
using MongoDB.Driver.Core.Misc;

namespace MongoDB.Driver
{
Expand Down Expand Up @@ -55,7 +56,7 @@ public string Key
public TimeSpan? MaxTime
{
get { return _maxTime; }
set { _maxTime = value; }
set { _maxTime = Ensure.IsNullOrInfiniteOrGreaterThanOrEqualToZero(value, nameof(value)); }
}

/// <summary>
Expand Down

0 comments on commit 50c119f

Please sign in to comment.