Skip to content

Commit a88c99e

Browse files
committed
CSHARP-1108: support for storage options in create index and create collection.
1 parent 349e6d7 commit a88c99e

File tree

3 files changed

+101
-7
lines changed

3 files changed

+101
-7
lines changed

MongoDB.Driver/Builders/CollectionOptionsBuilder.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ public static CollectionOptionsBuilder SetMaxSize(long value)
7575
{
7676
return new CollectionOptionsBuilder().SetMaxSize(value);
7777
}
78+
79+
/// <summary>
80+
/// Sets the storage options.
81+
/// </summary>
82+
/// <param name="value">The value.</param>
83+
/// <returns>The builder (so method calls can be chained).</returns>
84+
public static CollectionOptionsBuilder SetStorageOptions(BsonDocument value)
85+
{
86+
return new CollectionOptionsBuilder().SetStorageOptions(value);
87+
}
7888
}
7989

8090
/// <summary>
@@ -140,6 +150,17 @@ public CollectionOptionsBuilder SetMaxSize(long value)
140150
return this;
141151
}
142152

153+
/// <summary>
154+
/// Sets the storage options.
155+
/// </summary>
156+
/// <param name="value">The value.</param>
157+
/// <returns>The builder (so method calls can be chained).</returns>
158+
public CollectionOptionsBuilder SetStorageOptions(BsonDocument value)
159+
{
160+
_document["storageOptions"] = value;
161+
return this;
162+
}
163+
143164
/// <summary>
144165
/// Returns the result of the builder as a BsonDocument.
145166
/// </summary>

MongoDB.Driver/Builders/IndexOptionsBuilder.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ public static IndexOptionsBuilder SetSparse(bool value)
109109
return new IndexOptionsBuilder().SetSparse(value);
110110
}
111111

112+
/// <summary>
113+
/// Sets the storage options.
114+
/// </summary>
115+
/// <param name="value">The value.</param>
116+
/// <returns>The builder (so method calls can be chained).</returns>
117+
public static IndexOptionsBuilder SetStorageOptions(BsonDocument value)
118+
{
119+
return new IndexOptionsBuilder().SetStorageOptions(value);
120+
}
121+
112122
/// <summary>
113123
/// Sets the default language for the text index.
114124
/// </summary>
@@ -261,6 +271,17 @@ public IndexOptionsBuilder SetSparse(bool value)
261271
return this;
262272
}
263273

274+
/// <summary>
275+
/// Sets the storage options.
276+
/// </summary>
277+
/// <param name="value">The value.</param>
278+
/// <returns>The builder (so method calls can be chained).</returns>
279+
public IndexOptionsBuilder SetStorageOptions(BsonDocument value)
280+
{
281+
_document["storageOptions"] = value;
282+
return this;
283+
}
284+
264285
/// <summary>
265286
/// Sets the default language for the text index.
266287
/// </summary>
@@ -434,6 +455,16 @@ public static IndexOptionsBuilder<TDocument> SetSparse(bool value)
434455
return new IndexOptionsBuilder<TDocument>().SetSparse(value);
435456
}
436457

458+
/// <summary>
459+
/// Sets the storage options.
460+
/// </summary>
461+
/// <param name="value">The value.</param>
462+
/// <returns>The builder (so method calls can be chained).</returns>
463+
public static IndexOptionsBuilder<TDocument> SetStorageOptions(BsonDocument value)
464+
{
465+
return new IndexOptionsBuilder<TDocument>().SetStorageOptions(value);
466+
}
467+
437468
/// <summary>
438469
/// Sets the default language for the text index.
439470
/// </summary>
@@ -591,6 +622,17 @@ public IndexOptionsBuilder<TDocument> SetSparse(bool value)
591622
return this;
592623
}
593624

625+
/// <summary>
626+
/// Sets the storage options.
627+
/// </summary>
628+
/// <param name="value">The value.</param>
629+
/// <returns>The builder (so method calls can be chained).</returns>
630+
public IndexOptionsBuilder<TDocument> SetStorageOptions(BsonDocument value)
631+
{
632+
_indexOptionsBuilder.SetStorageOptions(value);
633+
return this;
634+
}
635+
594636
/// <summary>
595637
/// Sets the default language for the text index.
596638
/// </summary>
@@ -676,6 +718,5 @@ protected override void Serialize(BsonWriter bsonWriter, Type nominalType, IBson
676718
{
677719
((IBsonSerializable)_indexOptionsBuilder).Serialize(bsonWriter, nominalType, options);
678720
}
679-
680721
}
681722
}

MongoDB.DriverUnitTests/MongoCollectionTests.cs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,8 @@ public void TestBulkWriteUnordered()
422422
_collection.Drop();
423423
_collection.BulkWrite(new BulkWriteArgs
424424
{
425-
WriteConcern = WriteConcern.Acknowledged,
426-
IsOrdered = false,
425+
WriteConcern = WriteConcern.Acknowledged,
426+
IsOrdered = false,
427427
Requests = new WriteRequest[]
428428
{
429429
new UpdateRequest(Query.EQ("x", 1), Update.Set("y", 1)) { IsUpsert = true },
@@ -493,7 +493,7 @@ public void TestCountWithQuery()
493493
}
494494

495495
[Test]
496-
[RequiresServer(MinimumVersion="2.6.0")]
496+
[RequiresServer(MinimumVersion = "2.6.0")]
497497
public void TestCountWithHint()
498498
{
499499
_collection.RemoveAll();
@@ -602,6 +602,21 @@ public void TestCreateCollectionSetCappedSetMaxSize()
602602
collection.Drop();
603603
}
604604

605+
[Test]
606+
[RequiresServer(StorageEngines = "wiredtiger")]
607+
public void TestCreateCollectionSetStorageOptions()
608+
{
609+
var collection = _database.GetCollection("cappedcollection");
610+
collection.Drop();
611+
Assert.IsFalse(collection.Exists());
612+
var options = CollectionOptions.SetStorageOptions(
613+
new BsonDocument("wiredtiger", new BsonDocument("configString", "block_compressor=zlib")));
614+
_database.CreateCollection(collection.Name, options);
615+
Assert.IsTrue(collection.Exists());
616+
var stats = collection.GetStats();
617+
collection.Drop();
618+
}
619+
605620
[Test]
606621
public void TestCreateIndex()
607622
{
@@ -677,6 +692,23 @@ public void TestCreateIndex()
677692
}
678693
}
679694

695+
[Test]
696+
[RequiresServer(StorageEngines = "wiredtiger")]
697+
public void TestCreateIndexWithStorageOptions()
698+
{
699+
_collection.Drop();
700+
_collection.Insert(new BsonDocument("x", 1));
701+
_collection.DropAllIndexes(); // doesn't drop the index on _id
702+
703+
_collection.CreateIndex(
704+
IndexKeys.Ascending("x"),
705+
IndexOptions.SetStorageOptions(
706+
new BsonDocument("wiredtiger", new BsonDocument("configString", "block_compressor=zlib"))));
707+
708+
var result = _collection.GetIndexes();
709+
Assert.AreEqual(2, result.Count);
710+
}
711+
680712
[Test]
681713
public void TestDistinct()
682714
{
@@ -1758,9 +1790,9 @@ public void TestGeoSphericalIndex()
17581790
if (_server.BuildInfo.Version >= new Version(2, 4, 0))
17591791
{
17601792
if (_collection.Exists()) { _collection.Drop(); }
1761-
_collection.Insert(new PlaceGeoJson { Location = GeoJson.Point(GeoJson.Geographic(-74.0, 40.74)), Name = "10gen" , Type = "Office" });
1762-
_collection.Insert(new PlaceGeoJson { Location = GeoJson.Point(GeoJson.Geographic(-74.0, 41.73)), Name = "Three" , Type = "Coffee" });
1763-
_collection.Insert(new PlaceGeoJson { Location = GeoJson.Point(GeoJson.Geographic(-75.0, 40.74)), Name = "Two" , Type = "Coffee" });
1793+
_collection.Insert(new PlaceGeoJson { Location = GeoJson.Point(GeoJson.Geographic(-74.0, 40.74)), Name = "10gen", Type = "Office" });
1794+
_collection.Insert(new PlaceGeoJson { Location = GeoJson.Point(GeoJson.Geographic(-74.0, 41.73)), Name = "Three", Type = "Coffee" });
1795+
_collection.Insert(new PlaceGeoJson { Location = GeoJson.Point(GeoJson.Geographic(-75.0, 40.74)), Name = "Two", Type = "Coffee" });
17641796
_collection.CreateIndex(IndexKeys.GeoSpatialSpherical("Location"));
17651797

17661798
// TODO: add Query builder support for 2dsphere queries

0 commit comments

Comments
 (0)