namespace Mixtape.Raven; public class MixtapeStore : IMixtapeStore { public MixtapeStore(IDocumentStore raven, IMixtapeOptions options) : base() { Options = options; Raven = raven; //Database = null; } protected IMixtapeOptions Options { get; set; } protected Dictionary ScopedSessions { get; set; } = new(); private const string NullDb = "__default__"; /// public IDocumentStore Raven { get; } /// public IAsyncDocumentSession Session(MixtapeSessionResolution resolution = MixtapeSessionResolution.Reuse, SessionOptions options = null) { options ??= new SessionOptions(); if (resolution == MixtapeSessionResolution.Create) { return Raven.OpenAsyncSession(options); } if (!ScopedSessions.TryGetValue(Raven.Database, out IAsyncDocumentSession session)) { session = Raven.OpenAsyncSession(options); ScopedSessions.TryAdd(Raven.Database, session); } return session; } /// public async Task PurgeAsync(string querySuffix = null, Parameters parameters = null) { string collectionName = Raven.Conventions.FindCollectionName(typeof(T)); DeleteByQueryOperation operationQuery = new DeleteByQueryOperation(new IndexQuery() { Query = $"from {collectionName} c {querySuffix ?? string.Empty}", QueryParameters = parameters }, new QueryOperationOptions { AllowStale = true }); Operation operation = await Raven.Operations.SendAsync(operationQuery); await operation.WaitForCompletionAsync(); } /// public void Purge(string querySuffix = null, Parameters parameters = null) { string collectionName = Raven.Conventions.FindCollectionName(typeof(T)); DeleteByQueryOperation operationQuery = new DeleteByQueryOperation(new IndexQuery() { Query = $"from {collectionName} c {querySuffix ?? string.Empty}", QueryParameters = parameters }, new QueryOperationOptions { AllowStale = true }); Raven.Operations.Send(operationQuery); } } public enum MixtapeSessionResolution { Reuse = 0, Create = 1 } public interface IMixtapeStore { /// /// Get underlying raven document store /// IDocumentStore Raven { get; } /// /// Use a specific session /// IAsyncDocumentSession Session(MixtapeSessionResolution resolution = MixtapeSessionResolution.Reuse, SessionOptions options = null); /// /// Purges a collection /// Task PurgeAsync(string querySuffix = null, Parameters parameters = null); /// /// Purges a collection /// void Purge(string querySuffix = null, Parameters parameters = null); }