Skip to content

Commit 5b69fd5

Browse files
committed
CSHARP-889: using new createIndexes commands when talking with a 2.6 server.
1 parent f58e6f6 commit 5b69fd5

File tree

5 files changed

+100
-57
lines changed

5 files changed

+100
-57
lines changed

MongoDB.Driver/Communication/FeatureDetection/FeatureSetDetector.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ internal class FeatureSetDetector
5050
// added in 2.5.5
5151
new FeatureSetDependency(
5252
new ServerVersionDependency(2, 5, 5),
53+
FeatureId.CreateIndexCommand,
5354
FeatureId.TextSearchQuery,
5455
FeatureId.WriteCommands),
5556
};

MongoDB.Driver/FeatureId.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public enum FeatureId
3737
/// </summary>
3838
AggregateOutputToCollection,
3939
/// <summary>
40+
/// The create index command feature.
41+
/// </summary>
42+
CreateIndexCommand,
43+
/// <summary>
4044
/// The max time feature.
4145
/// </summary>
4246
MaxTime,

MongoDB.Driver/GridFS/MongoGridFS.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,8 @@ public void EnsureIndexes(int maxFiles)
595595
var count = filesCollection.Count();
596596
if (count < maxFiles)
597597
{
598-
filesCollection.EnsureIndex("filename", "uploadDate");
599-
chunksCollection.EnsureIndex(IndexKeys.Ascending("files_id", "n"), IndexOptions.SetUnique(true));
598+
filesCollection.CreateIndex("filename", "uploadDate");
599+
chunksCollection.CreateIndex(IndexKeys.Ascending("files_id", "n"), IndexOptions.SetUnique(true));
600600
}
601601
}
602602

MongoDB.Driver/MongoCollection.cs

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -288,35 +288,37 @@ public virtual long Count(IMongoQuery query)
288288
/// <param name="keys">The indexed fields (usually an IndexKeysDocument or constructed using the IndexKeys builder).</param>
289289
/// <param name="options">The index options(usually an IndexOptionsDocument or created using the IndexOption builder).</param>
290290
/// <returns>A WriteConcernResult.</returns>
291-
[Obsolete("Use EnsureIndex instead.")]
292291
public virtual WriteConcernResult CreateIndex(IMongoIndexKeys keys, IMongoIndexOptions options)
293292
{
294-
var keysDocument = keys.ToBsonDocument();
295-
var optionsDocument = options.ToBsonDocument();
296-
var indexes = _database.GetCollection("system.indexes");
297-
var indexName = GetIndexName(keysDocument, optionsDocument);
298-
var index = new BsonDocument
293+
using (_database.RequestStart(ReadPreference.Primary))
299294
{
300-
{ "name", indexName },
301-
{ "ns", FullName },
302-
{ "key", keysDocument }
303-
};
304-
index.Merge(optionsDocument);
305-
var insertOptions = new MongoInsertOptions
306-
{
307-
CheckElementNames = false,
308-
WriteConcern = WriteConcern.Acknowledged
309-
};
310-
var result = indexes.Insert(index, insertOptions);
311-
return result;
295+
if (_server.RequestConnection.ServerInstance.Supports(FeatureId.CreateIndexCommand))
296+
{
297+
try
298+
{
299+
CreateIndexWithCommand(keys, options);
300+
var fakeResponse = new BsonDocument("ok", 1);
301+
return new WriteConcernResult(fakeResponse);
302+
}
303+
catch (MongoCommandException ex)
304+
{
305+
var translatedResult = new WriteConcernResult(ex.CommandResult.Response);
306+
translatedResult.Command = ex.CommandResult.Command;
307+
throw new WriteConcernException(ex.Message, translatedResult);
308+
}
309+
}
310+
else
311+
{
312+
return CreateIndexWithInsert(keys, options);
313+
}
314+
}
312315
}
313316

314317
/// <summary>
315318
/// Creates an index for this collection.
316319
/// </summary>
317320
/// <param name="keys">The indexed fields (usually an IndexKeysDocument or constructed using the IndexKeys builder).</param>
318321
/// <returns>A WriteConcernResult.</returns>
319-
[Obsolete("Use EnsureIndex instead.")]
320322
public virtual WriteConcernResult CreateIndex(IMongoIndexKeys keys)
321323
{
322324
return CreateIndex(keys, IndexOptions.Null);
@@ -327,7 +329,6 @@ public virtual WriteConcernResult CreateIndex(IMongoIndexKeys keys)
327329
/// </summary>
328330
/// <param name="keyNames">The names of the indexed fields.</param>
329331
/// <returns>A WriteConcernResult.</returns>
330-
[Obsolete("Use EnsureIndex instead.")]
331332
public virtual WriteConcernResult CreateIndex(params string[] keyNames)
332333
{
333334
return CreateIndex(IndexKeys.Ascending(keyNames));
@@ -487,33 +488,30 @@ public virtual CommandResult DropIndexByName(string indexName)
487488
/// </summary>
488489
/// <param name="keys">The indexed fields (usually an IndexKeysDocument or constructed using the IndexKeys builder).</param>
489490
/// <param name="options">The index options(usually an IndexOptionsDocument or created using the IndexOption builder).</param>
491+
[Obsolete("Use CreateIndex instead.")]
490492
public virtual void EnsureIndex(IMongoIndexKeys keys, IMongoIndexOptions options)
491493
{
492-
#pragma warning disable 618
493494
CreateIndex(keys, options);
494-
#pragma warning restore
495495
}
496496

497497
/// <summary>
498498
/// Ensures that the desired index exists and creates it if it does not.
499499
/// </summary>
500500
/// <param name="keys">The indexed fields (usually an IndexKeysDocument or constructed using the IndexKeys builder).</param>
501+
[Obsolete("Use CreateIndex instead.")]
501502
public virtual void EnsureIndex(IMongoIndexKeys keys)
502503
{
503-
#pragma warning disable 618
504504
CreateIndex(keys);
505-
#pragma warning restore
506505
}
507506

508507
/// <summary>
509508
/// Ensures that the desired index exists and creates it if it does not.
510509
/// </summary>
511510
/// <param name="keyNames">The names of the indexed fields.</param>
511+
[Obsolete("Use CreateIndex instead.")]
512512
public virtual void EnsureIndex(params string[] keyNames)
513513
{
514-
#pragma warning disable 618
515514
CreateIndex(keyNames);
516-
#pragma warning restore
517515
}
518516

519517
/// <summary>
@@ -2159,6 +2157,30 @@ private void AssignId(InsertRequest request)
21592157
}
21602158
}
21612159

2160+
private CommandResult CreateIndexWithCommand(IMongoIndexKeys keys, IMongoIndexOptions options)
2161+
{
2162+
var command = new CommandDocument
2163+
{
2164+
{ "createIndexes", Name },
2165+
{ "indexes", new BsonArray { GetIndexDocument(keys, options) } }
2166+
};
2167+
2168+
return RunCommandAs<CommandResult>(command);
2169+
}
2170+
2171+
private WriteConcernResult CreateIndexWithInsert(IMongoIndexKeys keys, IMongoIndexOptions options)
2172+
{
2173+
var index = GetIndexDocument(keys, options);
2174+
var insertOptions = new MongoInsertOptions
2175+
{
2176+
CheckElementNames = false,
2177+
WriteConcern = WriteConcern.Acknowledged
2178+
};
2179+
var indexes = _database.GetCollection("system.indexes");
2180+
var result = indexes.Insert(index, insertOptions);
2181+
return result;
2182+
}
2183+
21622184
private MongoCursor FindAs(Type documentType, IMongoQuery query, IBsonSerializer serializer, IBsonSerializationOptions serializationOptions)
21632185
{
21642186
return MongoCursor.Create(documentType, this, query, _settings.ReadPreference, serializer, serializationOptions);
@@ -2197,6 +2219,22 @@ private BsonBinaryWriterSettings GetBinaryWriterSettings()
21972219
};
21982220
}
21992221

2222+
private BsonDocument GetIndexDocument(IMongoIndexKeys keys, IMongoIndexOptions options)
2223+
{
2224+
var keysDocument = keys.ToBsonDocument();
2225+
var optionsDocument = options.ToBsonDocument();
2226+
var indexName = GetIndexName(keysDocument, optionsDocument);
2227+
var index = new BsonDocument
2228+
{
2229+
{ "ns", FullName },
2230+
{ "name", indexName },
2231+
{ "key", keysDocument }
2232+
};
2233+
index.Merge(optionsDocument);
2234+
2235+
return index;
2236+
}
2237+
22002238
private string GetIndexName(BsonDocument keys, BsonDocument options)
22012239
{
22022240
if (options != null)

0 commit comments

Comments
 (0)