Skip to content

Commit 54b263d

Browse files
committed
CSHARP-1866: GridFS should check whether the indexes already exist before trying to create them.
1 parent e29a10b commit 54b263d

File tree

1 file changed

+84
-4
lines changed

1 file changed

+84
-4
lines changed

src/MongoDB.Driver.GridFS/GridFSBucket.cs

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
using System;
17+
using System.Collections.Generic;
1718
using System.Diagnostics.CodeAnalysis;
1819
using System.IO;
1920
using System.Linq;
@@ -515,6 +516,24 @@ public ImmutableGridFSBucketOptions Options
515516
}
516517

517518
// private methods
519+
private bool ChunksCollectionIndexesExist(List<BsonDocument> indexes)
520+
{
521+
var key = new BsonDocument { { "files_id", 1 }, { "n", 1 } };
522+
return IndexExists(indexes, key);
523+
}
524+
525+
private bool ChunksCollectionIndexesExist(IReadBindingHandle binding, CancellationToken cancellationToken)
526+
{
527+
var indexes = ListIndexes(binding, this.GetChunksCollectionNamespace(), cancellationToken);
528+
return ChunksCollectionIndexesExist(indexes);
529+
}
530+
531+
private async Task<bool> ChunksCollectionIndexesExistAsync(IReadBindingHandle binding, CancellationToken cancellationToken)
532+
{
533+
var indexes = await ListIndexesAsync(binding, this.GetChunksCollectionNamespace(), cancellationToken).ConfigureAwait(false);
534+
return ChunksCollectionIndexesExist(indexes);
535+
}
536+
518537
private void CreateChunksCollectionIndexes(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
519538
{
520539
var operation = CreateCreateChunksCollectionIndexesOperation();
@@ -682,6 +701,12 @@ private FindOperation<BsonDocument> CreateIsFilesCollectionEmptyOperation()
682701
};
683702
}
684703

704+
private ListIndexesOperation CreateListIndexesOperation(CollectionNamespace collectionNamespace)
705+
{
706+
var messageEncoderSettings = this.GetMessageEncoderSettings();
707+
return new ListIndexesOperation(collectionNamespace, messageEncoderSettings);
708+
}
709+
685710
private BulkMixedWriteOperation CreateRenameOperation(TFileId id, string newFilename)
686711
{
687712
var filesCollectionNamespace = this.GetFilesCollectionNamespace();
@@ -792,8 +817,14 @@ private void EnsureIndexes(IReadWriteBindingHandle binding, CancellationToken ca
792817
var isFilesCollectionEmpty = IsFilesCollectionEmpty(binding, cancellationToken);
793818
if (isFilesCollectionEmpty)
794819
{
795-
CreateFilesCollectionIndexes(binding, cancellationToken);
796-
CreateChunksCollectionIndexes(binding, cancellationToken);
820+
if (!FilesCollectionIndexesExist(binding, cancellationToken))
821+
{
822+
CreateFilesCollectionIndexes(binding, cancellationToken);
823+
}
824+
if (!ChunksCollectionIndexesExist(binding, cancellationToken))
825+
{
826+
CreateChunksCollectionIndexes(binding, cancellationToken);
827+
}
797828
}
798829

799830
_ensureIndexesDone = true;
@@ -815,8 +846,14 @@ private async Task EnsureIndexesAsync(IReadWriteBindingHandle binding, Cancellat
815846
var isFilesCollectionEmpty = await IsFilesCollectionEmptyAsync(binding, cancellationToken).ConfigureAwait(false);
816847
if (isFilesCollectionEmpty)
817848
{
818-
await CreateFilesCollectionIndexesAsync(binding, cancellationToken).ConfigureAwait(false);
819-
await CreateChunksCollectionIndexesAsync(binding, cancellationToken).ConfigureAwait(false);
849+
if (!(await FilesCollectionIndexesExistAsync(binding, cancellationToken).ConfigureAwait(false)))
850+
{
851+
await CreateFilesCollectionIndexesAsync(binding, cancellationToken).ConfigureAwait(false);
852+
}
853+
if (!(await ChunksCollectionIndexesExistAsync(binding, cancellationToken).ConfigureAwait(false)))
854+
{
855+
await CreateChunksCollectionIndexesAsync(binding, cancellationToken).ConfigureAwait(false);
856+
}
820857
}
821858

822859
_ensureIndexesDone = true;
@@ -828,6 +865,24 @@ private async Task EnsureIndexesAsync(IReadWriteBindingHandle binding, Cancellat
828865
}
829866
}
830867

868+
private bool FilesCollectionIndexesExist(List<BsonDocument> indexes)
869+
{
870+
var key = new BsonDocument { { "filename", 1 }, { "uploadDate", 1 } };
871+
return IndexExists(indexes, key);
872+
}
873+
874+
private bool FilesCollectionIndexesExist(IReadBindingHandle binding, CancellationToken cancellationToken)
875+
{
876+
var indexes = ListIndexes(binding, this.GetFilesCollectionNamespace(), cancellationToken);
877+
return FilesCollectionIndexesExist(indexes);
878+
}
879+
880+
private async Task<bool> FilesCollectionIndexesExistAsync(IReadBindingHandle binding, CancellationToken cancellationToken)
881+
{
882+
var indexes = await ListIndexesAsync(binding, this.GetFilesCollectionNamespace(), cancellationToken).ConfigureAwait(false);
883+
return FilesCollectionIndexesExist(indexes);
884+
}
885+
831886
private GridFSFileInfo<TFileId> GetFileInfo(IReadBindingHandle binding, TFileId id, CancellationToken cancellationToken)
832887
{
833888
var operation = CreateGetFileInfoOperation(id);
@@ -923,6 +978,18 @@ private async Task<IReadWriteBindingHandle> GetSingleServerReadWriteBindingAsync
923978
return new ReadWriteBindingHandle(binding);
924979
}
925980

981+
private bool IndexExists(List<BsonDocument> indexes, BsonDocument key)
982+
{
983+
foreach (var index in indexes)
984+
{
985+
if (index["key"].Equals(key))
986+
{
987+
return true;
988+
}
989+
}
990+
return false;
991+
}
992+
926993
private bool IsFilesCollectionEmpty(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
927994
{
928995
var operation = CreateIsFilesCollectionEmptyOperation();
@@ -942,5 +1009,18 @@ private async Task<bool> IsFilesCollectionEmptyAsync(IReadWriteBindingHandle bin
9421009
return firstOrDefault == null;
9431010
}
9441011
}
1012+
1013+
private List<BsonDocument> ListIndexes(IReadBinding binding, CollectionNamespace collectionNamespace, CancellationToken cancellationToken)
1014+
{
1015+
var operation = CreateListIndexesOperation(collectionNamespace);
1016+
return operation.Execute(binding, cancellationToken).ToList();
1017+
}
1018+
1019+
private async Task<List<BsonDocument>> ListIndexesAsync(IReadBinding binding, CollectionNamespace collectionNamespace, CancellationToken cancellationToken)
1020+
{
1021+
var operation = CreateListIndexesOperation(collectionNamespace);
1022+
var cursor = await operation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false);
1023+
return await cursor.ToListAsync(cancellationToken).ConfigureAwait(false);
1024+
}
9451025
}
9461026
}

0 commit comments

Comments
 (0)