Skip to content

Commit 77bf751

Browse files
author
rstam
committed
CSHARP-736: Don't cache EnsureIndex and deprecate CreateIndex.
1 parent abd7b01 commit 77bf751

14 files changed

+40
-373
lines changed

MongoDB.Driver/GridFS/MongoGridFS.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -426,33 +426,13 @@ public void EnsureIndexes(int maxFiles)
426426
// 1. we might be reading from a secondary
427427
// 2. we might be authenticating as a read-only uaser
428428

429-
// avoid round trip to count files if possible
430-
var indexCache = _database.Server.IndexCache;
431-
if (indexCache.Contains(_files, "filename_1_uploadDate_1") &&
432-
indexCache.Contains(_chunks, "files_id_1_n_1"))
433-
{
434-
return;
435-
}
436-
437429
// only create indexes if number of GridFS files is still small (to avoid performance surprises)
438430
var count = _files.Count();
439431
if (count < maxFiles)
440432
{
441433
_files.EnsureIndex("filename", "uploadDate");
442434
_chunks.EnsureIndex(IndexKeys.Ascending("files_id", "n"), IndexOptions.SetUnique(true));
443435
}
444-
else
445-
{
446-
// at least check to see if the indexes exist so we can stop calling files.Count()
447-
if (_files.IndexExistsByName("filename_1_uploadDate_1"))
448-
{
449-
indexCache.Add(_files, "filename_1_uploadDate_1");
450-
}
451-
if (_chunks.IndexExistsByName("files_id_1_n_1"))
452-
{
453-
indexCache.Add(_chunks, "files_id_1_n_1");
454-
}
455-
}
456436
}
457437

458438
/// <summary>

MongoDB.Driver/IndexCache.cs

Lines changed: 0 additions & 201 deletions
This file was deleted.

MongoDB.Driver/MongoCollection.cs

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ public virtual long Count(IMongoQuery query)
173173
/// <param name="keys">The indexed fields (usually an IndexKeysDocument or constructed using the IndexKeys builder).</param>
174174
/// <param name="options">The index options(usually an IndexOptionsDocument or created using the IndexOption builder).</param>
175175
/// <returns>A WriteConcernResult.</returns>
176+
[Obsolete("Use EnsureIndex instead.")]
176177
public virtual WriteConcernResult CreateIndex(IMongoIndexKeys keys, IMongoIndexOptions options)
177178
{
178179
var keysDocument = keys.ToBsonDocument();
@@ -200,6 +201,7 @@ public virtual WriteConcernResult CreateIndex(IMongoIndexKeys keys, IMongoIndexO
200201
/// </summary>
201202
/// <param name="keys">The indexed fields (usually an IndexKeysDocument or constructed using the IndexKeys builder).</param>
202203
/// <returns>A WriteConcernResult.</returns>
204+
[Obsolete("Use EnsureIndex instead.")]
203205
public virtual WriteConcernResult CreateIndex(IMongoIndexKeys keys)
204206
{
205207
return CreateIndex(keys, IndexOptions.Null);
@@ -210,6 +212,7 @@ public virtual WriteConcernResult CreateIndex(IMongoIndexKeys keys)
210212
/// </summary>
211213
/// <param name="keyNames">The names of the indexed fields.</param>
212214
/// <returns>A WriteConcernResult.</returns>
215+
[Obsolete("Use EnsureIndex instead.")]
213216
public virtual WriteConcernResult CreateIndex(params string[] keyNames)
214217
{
215218
return CreateIndex(IndexKeys.Ascending(keyNames));
@@ -307,15 +310,6 @@ public virtual CommandResult DropIndex(params string[] keyNames)
307310
/// <returns>A <see cref="CommandResult"/>.</returns>
308311
public virtual CommandResult DropIndexByName(string indexName)
309312
{
310-
// remove from cache first (even if command ends up failing)
311-
if (indexName == "*")
312-
{
313-
_server.IndexCache.Reset(this);
314-
}
315-
else
316-
{
317-
_server.IndexCache.Remove(this, indexName);
318-
}
319313
var command = new CommandDocument
320314
{
321315
{ "deleteIndexes", _name }, // not FullName
@@ -342,14 +336,9 @@ public virtual CommandResult DropIndexByName(string indexName)
342336
/// <param name="options">The index options(usually an IndexOptionsDocument or created using the IndexOption builder).</param>
343337
public virtual void EnsureIndex(IMongoIndexKeys keys, IMongoIndexOptions options)
344338
{
345-
var keysDocument = keys.ToBsonDocument();
346-
var optionsDocument = options.ToBsonDocument();
347-
var indexName = GetIndexName(keysDocument, optionsDocument);
348-
if (!_server.IndexCache.Contains(this, indexName))
349-
{
350-
CreateIndex(keys, options);
351-
_server.IndexCache.Add(this, indexName);
352-
}
339+
#pragma warning disable 618
340+
CreateIndex(keys, options);
341+
#pragma warning restore
353342
}
354343

355344
/// <summary>
@@ -358,7 +347,9 @@ public virtual void EnsureIndex(IMongoIndexKeys keys, IMongoIndexOptions options
358347
/// <param name="keys">The indexed fields (usually an IndexKeysDocument or constructed using the IndexKeys builder).</param>
359348
public virtual void EnsureIndex(IMongoIndexKeys keys)
360349
{
361-
EnsureIndex(keys, IndexOptions.Null);
350+
#pragma warning disable 618
351+
CreateIndex(keys);
352+
#pragma warning restore
362353
}
363354

364355
/// <summary>
@@ -367,12 +358,9 @@ public virtual void EnsureIndex(IMongoIndexKeys keys)
367358
/// <param name="keyNames">The names of the indexed fields.</param>
368359
public virtual void EnsureIndex(params string[] keyNames)
369360
{
370-
string indexName = GetIndexName(keyNames);
371-
if (!_server.IndexCache.Contains(this, indexName))
372-
{
373-
CreateIndex(IndexKeys.Ascending(keyNames), IndexOptions.SetName(indexName));
374-
_server.IndexCache.Add(this, indexName);
375-
}
361+
#pragma warning disable 618
362+
CreateIndex(keyNames);
363+
#pragma warning restore
376364
}
377365

378366
/// <summary>
@@ -1343,16 +1331,6 @@ public virtual WriteConcernResult RemoveAll(WriteConcern writeConcern)
13431331
return Remove(Query.Null, RemoveFlags.None, writeConcern);
13441332
}
13451333

1346-
/// <summary>
1347-
/// Removes all entries for this collection in the index cache used by EnsureIndex. Call this method
1348-
/// when you know (or suspect) that a process other than this one may have dropped one or
1349-
/// more indexes.
1350-
/// </summary>
1351-
public virtual void ResetIndexCache()
1352-
{
1353-
_server.IndexCache.Reset(this);
1354-
}
1355-
13561334
/// <summary>
13571335
/// Saves a document to this collection. The document must have an identifiable Id field. Based on the value
13581336
/// of the Id field Save will perform either an Insert or an Update.

MongoDB.Driver/MongoDB.Driver.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@
303303
<Compile Include="Communication\Proxies\MongoServerProxyFactory.cs" />
304304
<Compile Include="Communication\Proxies\DiscoveringMongoServerProxy.cs" />
305305
<Compile Include="Communication\Proxies\IMongoServerProxy.cs" />
306-
<Compile Include="IndexCache.cs" />
307306
<Compile Include="MongoCursorEnumerator.cs" />
308307
<Compile Include="Communication\Messages\MongoKillCursorsMessage.cs" />
309308
<Compile Include="Communication\Proxies\BlockingQueue.cs" />

MongoDB.Driver/MongoDatabase.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,6 @@ public virtual CommandResult DropCollection(string collectionName)
360360
{
361361
var command = new CommandDocument("drop", collectionName);
362362
var result = RunCommandAs<CommandResult>(command);
363-
_server.IndexCache.Reset(_name, collectionName);
364363
return result;
365364
}
366365
catch (MongoCommandException ex)
@@ -879,16 +878,6 @@ public virtual IDisposable RequestStart(ReadPreference readPreference)
879878

880879
// TODO: mongo shell has ResetError at the database level
881880

882-
/// <summary>
883-
/// Removes all entries for this database in the index cache used by EnsureIndex. Call this method
884-
/// when you know (or suspect) that a process other than this one may have dropped one or
885-
/// more indexes.
886-
/// </summary>
887-
public virtual void ResetIndexCache()
888-
{
889-
_server.IndexCache.Reset(this);
890-
}
891-
892881
/// <summary>
893882
/// Runs a command on this database.
894883
/// </summary>

0 commit comments

Comments
 (0)