Skip to content

Commit 3e85505

Browse files
committed
CSHARP-1832: Support NoPadding create collection option.
1 parent 788d933 commit 3e85505

File tree

8 files changed

+173
-5
lines changed

8 files changed

+173
-5
lines changed

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class CreateCollectionOperation : IWriteOperation<BsonDocument>
3939
private long? _maxDocuments;
4040
private long? _maxSize;
4141
private readonly MessageEncoderSettings _messageEncoderSettings;
42+
private bool? _noPadding;
4243
private BsonDocument _storageEngine;
4344
private bool? _usePowerOf2Sizes;
4445
private DocumentValidationAction? _validationAction;
@@ -155,6 +156,15 @@ public MessageEncoderSettings MessageEncoderSettings
155156
get { return _messageEncoderSettings; }
156157
}
157158

159+
/// <summary>
160+
/// Gets or sets whether padding should not be used.
161+
/// </summary>
162+
public bool? NoPadding
163+
{
164+
get { return _noPadding; }
165+
set { _noPadding = value; }
166+
}
167+
158168
/// <summary>
159169
/// Gets or sets the storage engine options.
160170
/// </summary>
@@ -232,14 +242,15 @@ internal BsonDocument CreateCommand(SemanticVersion serverVersion)
232242
{
233243
Feature.Collation.ThrowIfNotSupported(serverVersion, _collation);
234244

245+
var flags = GetFlags();
235246
return new BsonDocument
236247
{
237248
{ "create", _collectionNamespace.CollectionName },
238249
{ "capped", () => _capped.Value, _capped.HasValue },
239250
{ "autoIndexId", () => _autoIndexId.Value, _autoIndexId.HasValue },
240251
{ "size", () => _maxSize.Value, _maxSize.HasValue },
241252
{ "max", () => _maxDocuments.Value, _maxDocuments.HasValue },
242-
{ "flags", () => _usePowerOf2Sizes.Value ? 1 : 0, _usePowerOf2Sizes.HasValue },
253+
{ "flags", () => (int)flags.Value, flags.HasValue },
243254
{ "storageEngine", () => _storageEngine, _storageEngine != null },
244255
{ "indexOptionDefaults", _indexOptionDefaults, _indexOptionDefaults != null },
245256
{ "validator", _validator, _validator != null },
@@ -250,6 +261,27 @@ internal BsonDocument CreateCommand(SemanticVersion serverVersion)
250261
};
251262
}
252263

264+
private CreateCollectionFlags? GetFlags()
265+
{
266+
if (_usePowerOf2Sizes.HasValue || _noPadding.HasValue)
267+
{
268+
var flags = CreateCollectionFlags.None;
269+
if (_usePowerOf2Sizes.HasValue && _usePowerOf2Sizes.Value)
270+
{
271+
flags |= CreateCollectionFlags.UsePowerOf2Sizes;
272+
}
273+
if (_noPadding.HasValue && _noPadding.Value)
274+
{
275+
flags |= CreateCollectionFlags.NoPadding;
276+
}
277+
return flags;
278+
}
279+
else
280+
{
281+
return null;
282+
}
283+
}
284+
253285
/// <inheritdoc/>
254286
public BsonDocument Execute(IWriteBinding binding, CancellationToken cancellationToken)
255287
{
@@ -287,5 +319,13 @@ private WriteCommandOperation<BsonDocument> CreateOperation(SemanticVersion serv
287319
var command = CreateCommand(serverVersion);
288320
return new WriteCommandOperation<BsonDocument>(_collectionNamespace.DatabaseNamespace, command, BsonDocumentSerializer.Instance, _messageEncoderSettings);
289321
}
322+
323+
[Flags]
324+
private enum CreateCollectionFlags
325+
{
326+
None = 0,
327+
UsePowerOf2Sizes = 1,
328+
NoPadding = 2
329+
}
290330
}
291331
}

src/MongoDB.Driver.Legacy/CommandResults/CollectionStatsResult.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ public enum CollectionUserFlags
5050
/// <summary>
5151
/// User power of 2 size.
5252
/// </summary>
53-
UsePowerOf2Sizes = 1
53+
UsePowerOf2Sizes = 1,
54+
/// <summary>
55+
/// Whether padding should not be used.
56+
/// </summary>
57+
NoPadding = 2
5458
}
5559

5660
/// <summary>

src/MongoDB.Driver.Legacy/MongoDatabase.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ public virtual CommandResult CreateCollection(string collectionName, IMongoColle
212212
BsonDocument indexOptionDefaults = null;
213213
int? maxDocuments = null;
214214
long? maxSize = null;
215+
bool? noPadding = null;
215216
BsonDocument storageEngine = null;
216217
bool? usePowerOf2Sizes = null;
217218
DocumentValidationAction? validationAction = null;
@@ -243,6 +244,10 @@ public virtual CommandResult CreateCollection(string collectionName, IMongoColle
243244
{
244245
maxDocuments = value.ToInt32();
245246
}
247+
if (optionsDocument.TryGetValue("flags", out value))
248+
{
249+
noPadding = ((CollectionUserFlags)value.ToInt32() & CollectionUserFlags.NoPadding) != 0;
250+
}
246251
if (optionsDocument.TryGetValue("size", out value))
247252
{
248253
maxSize = value.ToInt64();
@@ -253,7 +258,7 @@ public virtual CommandResult CreateCollection(string collectionName, IMongoColle
253258
}
254259
if (optionsDocument.TryGetValue("flags", out value))
255260
{
256-
usePowerOf2Sizes = value.ToInt32() == 1;
261+
usePowerOf2Sizes = ((CollectionUserFlags)value.ToInt32() & CollectionUserFlags.UsePowerOf2Sizes) != 0;
257262
}
258263
if (optionsDocument.TryGetValue("validationAction", out value))
259264
{
@@ -277,6 +282,7 @@ public virtual CommandResult CreateCollection(string collectionName, IMongoColle
277282
IndexOptionDefaults = indexOptionDefaults,
278283
MaxDocuments = maxDocuments,
279284
MaxSize = maxSize,
285+
NoPadding = noPadding,
280286
StorageEngine = storageEngine,
281287
UsePowerOf2Sizes = usePowerOf2Sizes,
282288
ValidationAction = validationAction,

src/MongoDB.Driver/CreateCollectionOptions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class CreateCollectionOptions
3030
private IndexOptionDefaults _indexOptionDefaults;
3131
private long? _maxDocuments;
3232
private long? _maxSize;
33+
private bool? _noPadding;
3334
private BsonDocument _storageEngine;
3435
private bool? _usePowerOf2Sizes;
3536
private IBsonSerializerRegistry _serializerRegistry;
@@ -94,6 +95,15 @@ public long? MaxSize
9495
set { _maxSize = value; }
9596
}
9697

98+
/// <summary>
99+
/// Gets or sets whether padding should not be used.
100+
/// </summary>
101+
public bool? NoPadding
102+
{
103+
get { return _noPadding; }
104+
set { _noPadding = value; }
105+
}
106+
97107
/// <summary>
98108
/// Gets or sets the serializer registry.
99109
/// </summary>
@@ -176,6 +186,7 @@ internal static CreateCollectionOptions<TDocument> CoercedFrom(CreateCollectionO
176186
IndexOptionDefaults = options.IndexOptionDefaults,
177187
MaxDocuments = options.MaxDocuments,
178188
MaxSize = options.MaxSize,
189+
NoPadding = options.NoPadding,
179190
SerializerRegistry = options.SerializerRegistry,
180191
StorageEngine = options.StorageEngine,
181192
UsePowerOf2Sizes = options.UsePowerOf2Sizes,

src/MongoDB.Driver/MongoDatabaseImpl.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ private CreateCollectionOperation CreateCreateCollectionOperation(string name, C
262262
Capped = options.Capped,
263263
MaxDocuments = options.MaxDocuments,
264264
MaxSize = options.MaxSize,
265+
NoPadding = options.NoPadding,
265266
StorageEngine = options.StorageEngine,
266267
UsePowerOf2Sizes = options.UsePowerOf2Sizes,
267268
WriteConcern = _settings.WriteConcern
@@ -287,6 +288,7 @@ private CreateCollectionOperation CreateCreateCollectionOperation<TDocument>(str
287288
IndexOptionDefaults = options.IndexOptionDefaults?.ToBsonDocument(),
288289
MaxDocuments = options.MaxDocuments,
289290
MaxSize = options.MaxSize,
291+
NoPadding = options.NoPadding,
290292
StorageEngine = options.StorageEngine,
291293
UsePowerOf2Sizes = options.UsePowerOf2Sizes,
292294
ValidationAction = options.ValidationAction,

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public void constructor_should_initialize_subject()
9797
subject.IndexOptionDefaults.Should().BeNull();
9898
subject.MaxDocuments.Should().NotHaveValue();
9999
subject.MaxSize.Should().NotHaveValue();
100+
subject.NoPadding.Should().NotHaveValue();
100101
subject.StorageEngine.Should().BeNull();
101102
subject.UsePowerOf2Sizes.Should().NotHaveValue();
102103
subject.ValidationAction.Should().BeNull();
@@ -269,6 +270,27 @@ public void CreateCommand_should_return_expected_result_when_MaxSize_is_set(
269270
result.Should().Be(expectedResult);
270271
}
271272

273+
[Theory]
274+
[ParameterAttributeData]
275+
public void CreateCommand_should_return_expected_result_when_NoPadding_is_set(
276+
[Values(null, false, true)]
277+
bool? noPadding)
278+
{
279+
var subject = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings)
280+
{
281+
NoPadding = noPadding
282+
};
283+
284+
var result = subject.CreateCommand(null);
285+
286+
var expectedResult = new BsonDocument
287+
{
288+
{ "create", _collectionNamespace.CollectionName },
289+
{ "flags", () => noPadding.Value ? 2 : 0, noPadding != null }
290+
};
291+
result.Should().Be(expectedResult);
292+
}
293+
272294
[Theory]
273295
[ParameterAttributeData]
274296
public void CreateCommand_should_return_expected_result_when_StorageEngine_is_set(
@@ -580,6 +602,31 @@ public void Execute_should_create_collection_when_MaxSize_is_set(
580602
info["options"]["size"].ToInt64().Should().BeGreaterOrEqualTo(maxSize); // server rounds maxSize up
581603
}
582604

605+
[SkippableTheory]
606+
[ParameterAttributeData]
607+
public void Execute_should_create_collection_when_NoPadding_is_set(
608+
[Values(false, true)]
609+
bool noPadding,
610+
[Values(false, true)]
611+
bool async)
612+
{
613+
RequireServer.Check().VersionGreaterThanOrEqualTo("3.0").ClusterTypes(ClusterType.Standalone, ClusterType.ReplicaSet).StorageEngine("mmapv1");
614+
DropCollection();
615+
var subject = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings)
616+
{
617+
NoPadding = noPadding
618+
};
619+
620+
BsonDocument info;
621+
using (var binding = CoreTestConfiguration.GetReadWriteBinding())
622+
{
623+
ExecuteOperation(subject, binding, async);
624+
info = GetCollectionInfo(binding);
625+
}
626+
627+
info["options"]["flags"].Should().Be(noPadding ? 2 : 0);
628+
}
629+
583630
[SkippableTheory]
584631
[ParameterAttributeData]
585632
public void Execute_should_create_collection_when_StorageEngine_is_set(
@@ -779,6 +826,20 @@ public void MaxSize_set_should_throw_when_value_is_invalid(
779826
argumentOutOfRangeException.ParamName.Should().Be("value");
780827
}
781828

829+
[Theory]
830+
[ParameterAttributeData]
831+
public void NoPadding_get_and_set_should_work(
832+
[Values(null, false, true)]
833+
bool? value)
834+
{
835+
var subject = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings);
836+
837+
subject.NoPadding = value;
838+
var result = subject.NoPadding;
839+
840+
result.Should().Be(value);
841+
}
842+
782843
[Theory]
783844
[ParameterAttributeData]
784845
public void StorageEngine_get_and_set_should_work(

tests/MongoDB.Driver.Legacy.Tests/MongoCollectionTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,27 @@ public void TestCreateCollectionSetCappedSetMaxSize()
595595
collection.Drop();
596596
}
597597

598+
[SkippableTheory]
599+
[ParameterAttributeData]
600+
public void TestCreateCollectionSetNoPadding(
601+
[Values(false, true)]
602+
bool noPadding)
603+
{
604+
RequireServer.Check().VersionGreaterThanOrEqualTo("3.0").ClusterTypes(ClusterType.Standalone, ClusterType.ReplicaSet).StorageEngine("mmapv1");
605+
var collection = _database.GetCollection("cappedcollection");
606+
collection.Drop();
607+
var userFlags = noPadding ? CollectionUserFlags.NoPadding : CollectionUserFlags.None;
608+
var options = new CollectionOptionsDocument
609+
{
610+
{ "flags", (int)userFlags }
611+
};
612+
613+
_database.CreateCollection(collection.Name, options);
614+
615+
var stats = collection.GetStats();
616+
Assert.Equal(userFlags, stats.UserFlags);
617+
}
618+
598619
[SkippableTheory]
599620
[ParameterAttributeData]
600621
public void TestCreateCollectionSetUsePowerOf2Sizes(
@@ -3069,6 +3090,24 @@ public void TestGetStats()
30693090
_collection.GetStats();
30703091
}
30713092

3093+
[SkippableFact]
3094+
public void TestGetStatsNoPadding()
3095+
{
3096+
RequireServer.Check().VersionGreaterThanOrEqualTo("3.0").ClusterTypes(ClusterType.Standalone, ClusterType.ReplicaSet).StorageEngine("mmapv1");
3097+
_collection.Drop();
3098+
_database.CreateCollection(_collection.Name); // collMod command only works if collection exists
3099+
3100+
var command = new CommandDocument
3101+
{
3102+
{ "collMod", _collection.Name },
3103+
{ "noPadding", true }
3104+
};
3105+
_database.RunCommand(command);
3106+
3107+
var stats = _collection.GetStats();
3108+
Assert.True((stats.UserFlags & CollectionUserFlags.NoPadding) != 0);
3109+
}
3110+
30723111
[SkippableFact]
30733112
public void TestGetStatsUsePowerOf2Sizes()
30743113
{

tests/MongoDB.Driver.Tests/MongoDatabaseImplTests.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ public void CreateCollection_should_execute_the_CreateCollectionOperation_when_o
7676
IndexOptionDefaults = new IndexOptionDefaults { StorageEngine = new BsonDocument("x", 1) },
7777
MaxDocuments = 10,
7878
MaxSize = 11,
79+
NoPadding = true,
7980
StorageEngine = storageEngine,
80-
UsePowerOf2Sizes = false,
81+
UsePowerOf2Sizes = true,
8182
ValidationAction = DocumentValidationAction.Warn,
8283
ValidationLevel = DocumentValidationLevel.Off,
8384
Validator = new BsonDocument("x", 1)
@@ -105,6 +106,7 @@ public void CreateCollection_should_execute_the_CreateCollectionOperation_when_o
105106
op.IndexOptionDefaults.ToBsonDocument().Should().Be(options.IndexOptionDefaults.ToBsonDocument());
106107
op.MaxDocuments.Should().Be(options.MaxDocuments);
107108
op.MaxSize.Should().Be(options.MaxSize);
109+
op.NoPadding.Should().Be(options.NoPadding);
108110
op.StorageEngine.Should().Be(storageEngine);
109111
op.UsePowerOf2Sizes.Should().Be(options.UsePowerOf2Sizes);
110112
op.ValidationAction.Should().Be(options.ValidationAction);
@@ -130,8 +132,9 @@ public void CreateCollection_should_execute_the_CreateCollectionOperation_when_o
130132
IndexOptionDefaults = new IndexOptionDefaults { StorageEngine = new BsonDocument("x", 1) },
131133
MaxDocuments = 10,
132134
MaxSize = 11,
135+
NoPadding = true,
133136
StorageEngine = storageEngine,
134-
UsePowerOf2Sizes = false,
137+
UsePowerOf2Sizes = true,
135138
ValidationAction = DocumentValidationAction.Warn,
136139
ValidationLevel = DocumentValidationLevel.Off
137140
};
@@ -158,6 +161,7 @@ public void CreateCollection_should_execute_the_CreateCollectionOperation_when_o
158161
op.IndexOptionDefaults.ToBsonDocument().Should().Be(options.IndexOptionDefaults.ToBsonDocument());
159162
op.MaxDocuments.Should().Be(options.MaxDocuments);
160163
op.MaxSize.Should().Be(options.MaxSize);
164+
op.NoPadding.Should().Be(options.NoPadding);
161165
op.StorageEngine.Should().Be(storageEngine);
162166
op.UsePowerOf2Sizes.Should().Be(options.UsePowerOf2Sizes);
163167
op.ValidationAction.Should().Be(options.ValidationAction);
@@ -193,6 +197,7 @@ public void CreateCollection_should_execute_the_CreateCollectionOperation_when_o
193197
op.IndexOptionDefaults.Should().BeNull();
194198
op.MaxDocuments.Should().NotHaveValue();
195199
op.MaxSize.Should().NotHaveValue();
200+
op.NoPadding.Should().NotHaveValue();
196201
op.StorageEngine.Should().BeNull();
197202
op.UsePowerOf2Sizes.Should().NotHaveValue();
198203
op.ValidationAction.Should().BeNull();

0 commit comments

Comments
 (0)