Skip to content

Commit 75cf839

Browse files
committed
CSHARP-1413: Add IndexOptionDefaults to CreateCollectionOptions and CreateCollectionOperation.
1 parent b81cc04 commit 75cf839

File tree

7 files changed

+113
-0
lines changed

7 files changed

+113
-0
lines changed

src/MongoDB.Driver.Core.Tests/Core/Operations/CreateCollectionOperationTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,25 @@ public void CreateCommand_should_return_expected_result_when_Capped_is_set(
149149
result.Should().Be(expectedResult);
150150
}
151151

152+
[Test]
153+
public void CreateCommand_should_return_expected_result_when_IndexOptionDefaults_is_set()
154+
{
155+
var value = new IndexOptionDefaults { StorageEngine = new BsonDocument("x", 1) };
156+
var subject = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings)
157+
{
158+
IndexOptionDefaults = value
159+
};
160+
var expectedResult = new BsonDocument
161+
{
162+
{ "create", _collectionNamespace.CollectionName },
163+
{ "indexOptionDefaults", new BsonDocument("storageEngine", new BsonDocument("x", 1)) }
164+
};
165+
166+
var result = subject.CreateCommand();
167+
168+
result.Should().Be(expectedResult);
169+
}
170+
152171
[Test]
153172
public void CreateCommand_should_return_expected_result_when_MaxDocuments_is_set(
154173
[Values(1, 2)]
@@ -449,6 +468,18 @@ public void Execute_should_create_collection_when_UsePowerOf2Sizes_is_set(
449468
}
450469
}
451470

471+
[Test]
472+
public void IndexOptionDefaults_should_work()
473+
{
474+
var subject = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings);
475+
var value = new IndexOptionDefaults { StorageEngine = new BsonDocument("x", 1) };
476+
subject.IndexOptionDefaults.Should().BeNull();
477+
478+
subject.IndexOptionDefaults = value;
479+
480+
subject.IndexOptionDefaults.Should().Be(value);
481+
}
482+
452483
[Test]
453484
public void MaxDocuments_should_work()
454485
{

src/MongoDB.Driver.Core/Core/Operations/CreateCollectionOperation.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class CreateCollectionOperation : IWriteOperation<BsonDocument>
3333
private bool? _autoIndexId;
3434
private bool? _capped;
3535
private readonly CollectionNamespace _collectionNamespace;
36+
private IndexOptionDefaults _indexOptionDefaults;
3637
private long? _maxDocuments;
3738
private long? _maxSize;
3839
private readonly MessageEncoderSettings _messageEncoderSettings;
@@ -92,6 +93,18 @@ public CollectionNamespace CollectionNamespace
9293
get { return _collectionNamespace; }
9394
}
9495

96+
/// <summary>
97+
/// Gets or sets the index option defaults.
98+
/// </summary>
99+
/// <value>
100+
/// The index option defaults.
101+
/// </value>
102+
public IndexOptionDefaults IndexOptionDefaults
103+
{
104+
get { return _indexOptionDefaults; }
105+
set { _indexOptionDefaults = value; }
106+
}
107+
95108
/// <summary>
96109
/// Gets or sets the maximum number of documents in a capped collection.
97110
/// </summary>
@@ -199,6 +212,7 @@ internal BsonDocument CreateCommand()
199212
{ "max", () => _maxDocuments.Value, _maxDocuments.HasValue },
200213
{ "flags", () => _usePowerOf2Sizes.Value ? 1 : 0, _usePowerOf2Sizes.HasValue},
201214
{ "storageEngine", () => _storageEngine, _storageEngine != null },
215+
{ "indexOptionDefaults", () => _indexOptionDefaults.ToBsonDocument(), _indexOptionDefaults != null },
202216
{ "validator", _validator, _validator != null },
203217
{ "validationAction", () => _validationAction.Value.ToString().ToLowerInvariant(), _validationAction.HasValue },
204218
{ "validationLevel", () => _validationLevel.Value.ToString().ToLowerInvariant(), _validationLevel.HasValue }
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* Copyright 2015 MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using MongoDB.Bson;
17+
18+
namespace MongoDB.Driver.Core.Operations
19+
{
20+
/// <summary>
21+
/// Represents index option defaults.
22+
/// </summary>
23+
public class IndexOptionDefaults
24+
{
25+
// private fields
26+
private BsonDocument _storageEngine;
27+
28+
// public properties
29+
/// <summary>
30+
/// Gets or sets the storage engine options.
31+
/// </summary>
32+
public BsonDocument StorageEngine
33+
{
34+
get { return _storageEngine; }
35+
set { _storageEngine = value; }
36+
}
37+
38+
// public methods
39+
/// <summary>
40+
/// Returns this instance represented as a BsonDocument.
41+
/// </summary>
42+
/// <returns>A BsonDocument.</returns>
43+
public BsonDocument ToBsonDocument()
44+
{
45+
return new BsonDocument
46+
{
47+
{ "storageEngine", _storageEngine, _storageEngine != null }
48+
};
49+
}
50+
}
51+
}

src/MongoDB.Driver.Core/MongoDB.Driver.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
<Compile Include="Core\Operations\CreateIndexesUsingInsertOperation.cs" />
140140
<Compile Include="Core\Operations\DocumentValidationAction.cs" />
141141
<Compile Include="Core\Operations\DocumentValidationLevel.cs" />
142+
<Compile Include="Core\Operations\IndexOptionDefaults.cs" />
142143
<Compile Include="Core\Operations\ListCollectionsUsingCommandOperation.cs" />
143144
<Compile Include="Core\Operations\ListCollectionsUsingQueryOperation.cs" />
144145
<Compile Include="Core\Operations\ListIndexesUsingCommandOperation.cs" />

src/MongoDB.Driver.Tests/MongoDatabaseImplTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public async Task CreateCollectionAsync_should_execute_the_CreateCollectionOpera
7575
{
7676
AutoIndexId = false,
7777
Capped = true,
78+
IndexOptionDefaults = new IndexOptionDefaults { StorageEngine = new BsonDocument("x", 1) },
7879
MaxDocuments = 10,
7980
MaxSize = 11,
8081
StorageEngine = storageEngine,
@@ -92,6 +93,7 @@ public async Task CreateCollectionAsync_should_execute_the_CreateCollectionOpera
9293
op.CollectionNamespace.Should().Be(new CollectionNamespace(new DatabaseNamespace("foo"), "bar"));
9394
op.AutoIndexId.Should().Be(options.AutoIndexId);
9495
op.Capped.Should().Be(options.Capped);
96+
op.IndexOptionDefaults.ToBsonDocument().Should().Be(options.IndexOptionDefaults.ToBsonDocument());
9597
op.MaxDocuments.Should().Be(options.MaxDocuments);
9698
op.MaxSize.Should().Be(options.MaxSize);
9799
op.StorageEngine.Should().Be(storageEngine);

src/MongoDB.Driver/CreateCollectionOptions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class CreateCollectionOptions
2727
// fields
2828
private bool? _autoIndexId;
2929
private bool? _capped;
30+
private IndexOptionDefaults _indexOptionDefaults;
3031
private long? _maxDocuments;
3132
private long? _maxSize;
3233
private BsonDocument _storageEngine;
@@ -54,6 +55,18 @@ public bool? Capped
5455
set { _capped = value; }
5556
}
5657

58+
/// <summary>
59+
/// Gets or sets the index option defaults.
60+
/// </summary>
61+
/// <value>
62+
/// The index option defaults.
63+
/// </value>
64+
public IndexOptionDefaults IndexOptionDefaults
65+
{
66+
get { return _indexOptionDefaults; }
67+
set { _indexOptionDefaults = value; }
68+
}
69+
5770
/// <summary>
5871
/// Gets or sets the maximum number of documents (used with capped collections).
5972
/// </summary>

src/MongoDB.Driver/MongoDatabaseImpl.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ private Task CreateCollectionHelperAsync<TDocument>(string name, CreateCollectio
162162
{
163163
AutoIndexId = options.AutoIndexId,
164164
Capped = options.Capped,
165+
IndexOptionDefaults = options.IndexOptionDefaults,
165166
MaxDocuments = options.MaxDocuments,
166167
MaxSize = options.MaxSize,
167168
StorageEngine = options.StorageEngine,

0 commit comments

Comments
 (0)