using MongoDbGenericRepository.DataAccess.Index;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq.Expressions;
using MongoDbGenericRepository.Models;
namespace MongoDbGenericRepository
{
///
/// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation.
/// Its constructor must be given a connection string and a database name.
///
public abstract partial class BaseMongoRepository : IBaseMongoRepository_Index
{
private volatile IMongoDbIndexHandler _mongoDbIndexHandler;
protected virtual IMongoDbIndexHandler MongoDbIndexHandler
{
get
{
if (_mongoDbIndexHandler != null) { return _mongoDbIndexHandler; }
lock (_initLock)
{
if (_mongoDbIndexHandler == null)
{
_mongoDbIndexHandler = new MongoDbIndexHandler(MongoDbContext);
}
}
return _mongoDbIndexHandler;
}
set { _mongoDbIndexHandler = value; }
}
///
/// Returns the names of the indexes present on a collection.
///
/// The type representing a Document.
/// The type of the primary key for a Document.
/// An optional partition key
/// A list containing the names of the indexes on on the concerned collection.
public async Task> GetIndexesNamesAsync(string partitionKey = null)
where TDocument : IDocument
{
return await MongoDbIndexHandler.GetIndexesNamesAsync(partitionKey);
}
///
/// Returns the names of the indexes present on a collection.
///
/// The type representing a Document.
/// An optional partition key
/// A list containing the names of the indexes on on the concerned collection.
public async virtual Task> GetIndexesNamesAsync(string partitionKey = null)
where TDocument : IDocument
where TKey : IEquatable
{
return await MongoDbIndexHandler.GetIndexesNamesAsync(partitionKey);
}
///
/// Create a text index on the given field.
/// IndexCreationOptions can be supplied to further specify
/// how the creation should be done.
///
/// The type representing a Document.
/// The field we want to index.
/// Options for creating an index.
/// An optional partition key.
/// The result of the create index operation.
public async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument
{
return await MongoDbIndexHandler.CreateTextIndexAsync(field, indexCreationOptions, partitionKey);
}
///
/// Create a text index on the given field.
/// IndexCreationOptions can be supplied to further specify
/// how the creation should be done.
///
/// The type representing a Document.
/// The type of the primary key for a Document.
/// The field we want to index.
/// Options for creating an index.
/// An optional partition key.
/// The result of the create index operation.
public async virtual Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument
where TKey : IEquatable
{
return await MongoDbIndexHandler.CreateTextIndexAsync(field, indexCreationOptions, partitionKey);
}
///
/// Creates an index on the given field in ascending order.
/// IndexCreationOptions can be supplied to further specify
/// how the creation should be done.
///
/// The type representing a Document.
/// The field we want to index.
/// Options for creating an index.
/// An optional partition key.
/// The result of the create index operation.
public async Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument
{
return await MongoDbIndexHandler.CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey);
}
///
/// Creates an index on the given field in ascending order.
/// IndexCreationOptions can be supplied to further specify
/// how the creation should be done.
///
/// The type representing a Document.
/// The type of the primary key for a Document.
/// The field we want to index.
/// Options for creating an index.
/// An optional partition key.
/// The result of the create index operation.
public async virtual Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument
where TKey : IEquatable
{
return await MongoDbIndexHandler.CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey);
}
///
/// Creates an index on the given field in descending order.
/// IndexCreationOptions can be supplied to further specify
/// how the creation should be done.
///
/// The type representing a Document.
/// The field we want to index.
/// Options for creating an index.
/// An optional partition key.
/// The result of the create index operation.
public async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument
{
return await MongoDbIndexHandler.CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey);
}
///
public async virtual Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument
where TKey : IEquatable
{
return await MongoDbIndexHandler.CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey);
}
///
/// Creates a hashed index on the given field.
/// IndexCreationOptions can be supplied to further specify
/// how the creation should be done.
///
/// The type representing a Document.
/// The field we want to index.
/// Options for creating an index.
/// An optional partition key.
/// The result of the create index operation.
public async Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument
{
return await MongoDbIndexHandler.CreateHashedIndexAsync(field, indexCreationOptions, partitionKey);
}
///
public async virtual Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument
where TKey : IEquatable
{
return await MongoDbIndexHandler.CreateHashedIndexAsync(field, indexCreationOptions, partitionKey);
}
///
/// Creates a combined text index.
/// IndexCreationOptions can be supplied to further specify
/// how the creation should be done.
///
/// The type representing a Document.
/// The fields we want to index.
/// Options for creating an index.
/// An optional partition key.
/// The result of the create index operation.
public async Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument
{
return await MongoDbIndexHandler.CreateCombinedTextIndexAsync(fields, indexCreationOptions, partitionKey);
}
///
public async virtual Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument
where TKey : IEquatable
{
return await MongoDbIndexHandler.CreateCombinedTextIndexAsync(fields, indexCreationOptions, partitionKey);
}
///
/// Drops the index given a field name
///
/// The type representing a Document.
/// The name of the index
/// An optional partition key
public async Task DropIndexAsync(string indexName, string partitionKey = null)
where TDocument : IDocument
{
await MongoDbIndexHandler.DropIndexAsync(indexName, partitionKey);
}
///
public async virtual Task DropIndexAsync(string indexName, string partitionKey = null)
where TDocument : IDocument
where TKey : IEquatable
{
await MongoDbIndexHandler.DropIndexAsync(indexName, partitionKey);
}
}
}