Files
mixtape/zero.Core/Database/ZeroStore.cs
T

163 lines
4.2 KiB
C#
Raw Normal View History

2020-11-16 01:07:07 +01:00
using Raven.Client;
using Raven.Client.Documents;
2020-11-15 00:59:51 +01:00
using Raven.Client.Documents.BulkInsert;
using Raven.Client.Documents.Operations;
2020-11-16 01:07:07 +01:00
using Raven.Client.Documents.Queries;
2020-11-15 00:59:51 +01:00
using Raven.Client.Documents.Session;
2020-11-16 01:07:07 +01:00
using System;
2020-11-15 00:59:51 +01:00
using System.Threading;
using System.Threading.Tasks;
using zero.Core.Options;
2020-11-15 00:59:51 +01:00
namespace zero.Core.Database
{
2020-11-15 16:29:22 +01:00
public class ZeroStore : DocumentStore, IZeroStore
2020-11-15 00:59:51 +01:00
{
public ZeroStore(IZeroOptions options) : base()
2020-11-15 00:59:51 +01:00
{
Options = options;
Database = null;
2020-11-15 00:59:51 +01:00
}
2020-11-16 01:07:07 +01:00
protected IZeroOptions Options { get; set; }
2020-11-16 01:07:07 +01:00
/// <inheritdoc />
public string ResolvedDatabase { get; set; }
/// <inheritdoc />
public IDocumentStore Raven => this;
/// <inheritdoc />
public OperationExecutor GetOperationExecutor(string database = null)
{
2020-11-16 01:07:07 +01:00
return Operations.ForDatabase(database ?? ResolvedDatabase);
}
/// <inheritdoc />
public IAsyncDocumentSession OpenCoreSession()
{
return OpenAsyncSession(Options.Raven.Database);
}
/// <inheritdoc />
public override IAsyncDocumentSession OpenAsyncSession(string database)
{
2020-12-09 13:42:23 +01:00
return OpenAsyncSession(new SessionOptions()
{
Database = database
});
2020-11-16 01:07:07 +01:00
}
/// <inheritdoc />
public override IAsyncDocumentSession OpenAsyncSession(SessionOptions options)
{
options.Database = options.Database ?? ResolvedDatabase;
2020-12-09 13:42:23 +01:00
AssertInitialized();
EnsureNotClosed();
var sessionId = Guid.NewGuid();
var session = new ZeroDocumentSession(this, sessionId, options);
RegisterEvents(session);
AfterSessionCreated(session);
return session;
2020-11-16 01:07:07 +01:00
}
/// <inheritdoc />
public override IAsyncDocumentSession OpenAsyncSession()
{
2020-12-09 13:42:23 +01:00
return OpenAsyncSession(new SessionOptions()
{
Database = ResolvedDatabase
});
2020-11-16 01:07:07 +01:00
}
/// <inheritdoc />
public override IDocumentSession OpenSession(SessionOptions options)
{
options.Database = options.Database ?? ResolvedDatabase;
2020-12-09 13:42:23 +01:00
AssertInitialized();
EnsureNotClosed();
var sessionId = Guid.NewGuid();
var session = new DocumentSession(this, sessionId, options);
RegisterEvents(session);
AfterSessionCreated(session);
return session;
2020-11-16 01:07:07 +01:00
}
/// <inheritdoc />
public override IDocumentSession OpenSession(string database)
{
2020-12-09 13:42:23 +01:00
return OpenSession(new SessionOptions()
{
Database = database
});
2020-11-16 01:07:07 +01:00
}
/// <inheritdoc />
public override IDocumentSession OpenSession()
{
2020-12-09 13:42:23 +01:00
return OpenSession(new SessionOptions()
{
Database = ResolvedDatabase
});
2020-11-16 01:07:07 +01:00
}
/// <inheritdoc />
public override BulkInsertOperation BulkInsert(string database = null, CancellationToken token = default)
{
return base.BulkInsert(database ?? ResolvedDatabase, token);
}
/// <inheritdoc />
public async Task PurgeAsync<T>(string database = null, string querySuffix = null, Parameters parameters = null)
{
var collectionName = Conventions.FindCollectionName(typeof(T));
var operationQuery = new DeleteByQueryOperation(new IndexQuery()
{
2020-11-16 01:07:07 +01:00
Query = $"from {collectionName} c {querySuffix ?? String.Empty}",
QueryParameters = parameters
}, new QueryOperationOptions { AllowStale = true });
Operation operation = await GetOperationExecutor(database).SendAsync(operationQuery);
await operation.WaitForCompletionAsync();
}
}
public interface IZeroStore : IDocumentStore
{
2020-11-16 01:07:07 +01:00
/// <summary>
/// The database which has been resolved from the current application
/// </summary>
string ResolvedDatabase { get; set; }
2020-11-15 00:59:51 +01:00
/// <summary>
/// Get underlying raven document store
/// </summary>
IDocumentStore Raven { get; }
2020-11-15 17:07:27 +01:00
/// <summary>
/// Get operation executor
2020-11-15 17:07:27 +01:00
/// </summary>
2020-11-16 01:07:07 +01:00
OperationExecutor GetOperationExecutor(string database = null);
/// <summary>
/// Purges a collection
/// </summary>
Task PurgeAsync<T>(string database = null, string querySuffix = null, Parameters parameters = null);
/// <summary>
/// Opens a session for the core database
/// </summary>
IAsyncDocumentSession OpenCoreSession();
2020-11-15 00:59:51 +01:00
}
}