1- using System ;
1+ using System ;
2+ using System . Collections . Concurrent ;
23using System . Collections . Generic ;
34using System . Linq ;
45using System . Linq . Expressions ;
56using System . Threading . Tasks ;
7+ using Foundatio . AsyncEx ;
68using Foundatio . Parsers . ElasticQueries ;
79using Foundatio . Parsers . ElasticQueries . Extensions ;
810using Foundatio . Parsers . LuceneQueries . Visitors ;
@@ -26,8 +28,9 @@ public class Index : IIndex
2628 private readonly Lazy < ElasticQueryParser > _queryParser ;
2729 private readonly Lazy < ElasticMappingResolver > _mappingResolver ;
2830 private readonly Lazy < QueryFieldResolver > _fieldResolver ;
31+ private readonly ConcurrentDictionary < string , ICustomFieldType > _customFieldTypes = new ( ) ;
32+ private readonly AsyncLock _lock = new ( ) ;
2933 protected readonly ILogger _logger ;
30- private readonly IDictionary < string , ICustomFieldType > _customFieldTypes = new Dictionary < string , ICustomFieldType > ( ) ;
3134
3235 public Index ( IElasticConfiguration configuration , string name = null )
3336 {
@@ -149,14 +152,14 @@ public virtual async Task EnsureIndexAsync(object target)
149152 if ( _isEnsured )
150153 return ;
151154
152- bool existsResult = await IndexExistsAsync ( Name ) . AnyContext ( ) ;
153- if ( existsResult )
155+ using ( await _lock . LockAsync ( ) . AnyContext ( ) )
154156 {
157+ if ( _isEnsured )
158+ return ;
159+
160+ await ConfigureAsync ( ) . AnyContext ( ) ;
155161 _isEnsured = true ;
156- return ;
157162 }
158-
159- await ConfigureAsync ( ) . AnyContext ( ) ;
160163 }
161164
162165 public virtual Task MaintainAsync ( bool includeOptionalTasks = true )
@@ -178,6 +181,7 @@ public virtual IPromise<IAliases> ConfigureIndexAliases(AliasesDescriptor aliase
178181
179182 public virtual Task DeleteAsync ( )
180183 {
184+ _isEnsured = false ;
181185 return DeleteIndexAsync ( Name ) ;
182186 }
183187
0 commit comments