using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; using MongoDB.Driver; using MongoDbGenericRepository.DataAccess.Base; using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository.DataAccess.Index { /// public class MongoDbIndexHandler : DataAccessBase, IMongoDbIndexHandler { /// /// The MongoDbIndexHandler constructor. /// /// The mongo db context public MongoDbIndexHandler(IMongoDbContext mongoDbContext) : base(mongoDbContext) { } /// public virtual async Task> GetIndexesNamesAsync(string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var indexCursor = await HandlePartitioned(partitionKey).Indexes.ListAsync(cancellationToken); var indexes = await indexCursor.ToListAsync(cancellationToken); return indexes.Select(e => e["name"].ToString()).ToList(); } /// public virtual async Task CreateTextIndexAsync( Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var model = new CreateIndexModel( Builders.IndexKeys.Text(field), indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions) ); return await HandlePartitioned(partitionKey).Indexes .CreateOneAsync( model, cancellationToken: cancellationToken); } /// public virtual async Task CreateAscendingIndexAsync( Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var collection = HandlePartitioned(partitionKey); var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions); var indexKey = Builders.IndexKeys; return await collection.Indexes.CreateOneAsync( new CreateIndexModel(indexKey.Ascending(field), createOptions), cancellationToken: cancellationToken); } /// public virtual async Task CreateDescendingIndexAsync( Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var collection = HandlePartitioned(partitionKey); var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions); var indexKey = Builders.IndexKeys; return await collection.Indexes .CreateOneAsync( new CreateIndexModel(indexKey.Descending(field), createOptions), cancellationToken: cancellationToken); } /// public virtual async Task CreateHashedIndexAsync( Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var collection = HandlePartitioned(partitionKey); var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions); var indexKey = Builders.IndexKeys; return await collection.Indexes .CreateOneAsync( new CreateIndexModel(indexKey.Hashed(field), createOptions), cancellationToken: cancellationToken); } /// public virtual async Task CreateCombinedTextIndexAsync( IEnumerable>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var collection = HandlePartitioned(partitionKey); var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions); var listOfDefs = new List>(); foreach (var field in fields) { listOfDefs.Add(Builders.IndexKeys.Text(field)); } return await collection.Indexes .CreateOneAsync( new CreateIndexModel(Builders.IndexKeys.Combine(listOfDefs), createOptions), cancellationToken: cancellationToken); } /// public virtual async Task DropIndexAsync(string indexName, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { await HandlePartitioned(partitionKey).Indexes.DropOneAsync(indexName, cancellationToken); } } }