14
14
*/
15
15
16
16
using System ;
17
+ using System . Collections . Generic ;
17
18
using System . Diagnostics . CodeAnalysis ;
18
19
using System . IO ;
19
20
using System . Linq ;
@@ -515,6 +516,24 @@ public ImmutableGridFSBucketOptions Options
515
516
}
516
517
517
518
// 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
+
518
537
private void CreateChunksCollectionIndexes ( IReadWriteBindingHandle binding , CancellationToken cancellationToken )
519
538
{
520
539
var operation = CreateCreateChunksCollectionIndexesOperation ( ) ;
@@ -682,6 +701,12 @@ private FindOperation<BsonDocument> CreateIsFilesCollectionEmptyOperation()
682
701
} ;
683
702
}
684
703
704
+ private ListIndexesOperation CreateListIndexesOperation ( CollectionNamespace collectionNamespace )
705
+ {
706
+ var messageEncoderSettings = this . GetMessageEncoderSettings ( ) ;
707
+ return new ListIndexesOperation ( collectionNamespace , messageEncoderSettings ) ;
708
+ }
709
+
685
710
private BulkMixedWriteOperation CreateRenameOperation ( TFileId id , string newFilename )
686
711
{
687
712
var filesCollectionNamespace = this . GetFilesCollectionNamespace ( ) ;
@@ -792,8 +817,14 @@ private void EnsureIndexes(IReadWriteBindingHandle binding, CancellationToken ca
792
817
var isFilesCollectionEmpty = IsFilesCollectionEmpty ( binding , cancellationToken ) ;
793
818
if ( isFilesCollectionEmpty )
794
819
{
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
+ }
797
828
}
798
829
799
830
_ensureIndexesDone = true ;
@@ -815,8 +846,14 @@ private async Task EnsureIndexesAsync(IReadWriteBindingHandle binding, Cancellat
815
846
var isFilesCollectionEmpty = await IsFilesCollectionEmptyAsync ( binding , cancellationToken ) . ConfigureAwait ( false ) ;
816
847
if ( isFilesCollectionEmpty )
817
848
{
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
+ }
820
857
}
821
858
822
859
_ensureIndexesDone = true ;
@@ -828,6 +865,24 @@ private async Task EnsureIndexesAsync(IReadWriteBindingHandle binding, Cancellat
828
865
}
829
866
}
830
867
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
+
831
886
private GridFSFileInfo < TFileId > GetFileInfo ( IReadBindingHandle binding , TFileId id , CancellationToken cancellationToken )
832
887
{
833
888
var operation = CreateGetFileInfoOperation ( id ) ;
@@ -923,6 +978,18 @@ private async Task<IReadWriteBindingHandle> GetSingleServerReadWriteBindingAsync
923
978
return new ReadWriteBindingHandle ( binding ) ;
924
979
}
925
980
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
+
926
993
private bool IsFilesCollectionEmpty ( IReadWriteBindingHandle binding , CancellationToken cancellationToken )
927
994
{
928
995
var operation = CreateIsFilesCollectionEmptyOperation ( ) ;
@@ -942,5 +1009,18 @@ private async Task<bool> IsFilesCollectionEmptyAsync(IReadWriteBindingHandle bin
942
1009
return firstOrDefault == null ;
943
1010
}
944
1011
}
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
+ }
945
1025
}
946
1026
}
0 commit comments