Skip to content

Commit

Permalink
Usings updated. Error Handling improved.
Browse files Browse the repository at this point in the history
  • Loading branch information
crusjiberatwork committed Dec 18, 2023
1 parent c7a1d59 commit 8bb6eac
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

using BoCode.RedoDB.Builder;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace BoCode.RedoDB.DependencyInjection;

Expand Down
50 changes: 37 additions & 13 deletions BoCode.RedoDB/Builder/RedoDBEngineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
using BoCode.RedoDB.Persistence.Snapshots;
using BoCode.RedoDB.RedoableData;
using ImpromptuInterface;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace BoCode.RedoDB.Builder
{
Expand All @@ -19,7 +24,7 @@ public class RedoDBEngineBuilder<T, I> : IWithDataPath
where I : class
{
private IInterceptions _interceptions;
private readonly Func<T> _creator;
private readonly Func<T>? _creator;
private ICommandAdapter? _commandAdapter;
private ISnapshotAdapter<T>? _snapshotAdapter;
private bool _withNoPersistence;
Expand All @@ -37,16 +42,13 @@ public class RedoDBEngineBuilder<T, I> : IWithDataPath
/// <summary>
/// The parameterless constructor uses and empty InterceptInstructions and the default CommandManagager.
/// </summary>
public RedoDBEngineBuilder(Func<T> creator = null)
public RedoDBEngineBuilder(Func<T>? creator = null)
{
_interceptions = new InterceptionsManager();
_creator = creator ?? Activator.CreateInstance<T>;
}

public RedoDBEngineBuilder(IInterceptions interceptionManager)
{
_interceptions = interceptionManager;
}
public RedoDBEngineBuilder(IInterceptions interceptionManager) => _interceptions = interceptionManager;

public RedoDBEngineBuilder<T, I> WithNoPersistence()
{
Expand Down Expand Up @@ -158,8 +160,24 @@ private void PrepareComponents()
private void ActivateCompensationManager()
{
if (_withCommandlogOnly) throw new RedoDBEngineBuilderException("Compensation requires a snapshot adapter and is not compatible with the option 'WithCommandlogOnly'!");
_compensationManager = new CompensationManager<T>(_creator);
_compensationManager.SetSnapshotAdapter(_snapshotAdapter);
if (_creator is null)
{
throw new RedoDBEngineBuilderException("Creator can't be null while activating compensation manager!");
}
else
{
_compensationManager = new CompensationManager<T>(_creator);
}
if (_snapshotAdapter is null)
{

throw new RedoDBEngineBuilderException("Snapshot Adapter can't be null while actiating compensation manager!");
}

else
{
_compensationManager.SetSnapshotAdapter(_snapshotAdapter);
}
}

private void CreateNoPersistenceAdapters()
Expand Down Expand Up @@ -222,6 +240,7 @@ private I CreateEngine(T recovered)
{
if (_commandAdapter is null) throw new RedoDBEngineException("_commandAdapter is null!");
if (_snapshotAdapter is null) throw new RedoDBEngineException("_snapshotAdapter is null!");
if (_creator is null) throw new RedoDBEngineException("_creator is null!");
I redoable;

if (recovered != null)
Expand Down Expand Up @@ -257,10 +276,10 @@ private void StartEngine(I redoable)
//deserialize and return T
private async Task<T> RecoverRedoableAsync()
{
if (_creator is null) throw new InvalidOperationException(nameof(_creator));
if (_withNoPersistence) return _creator();

if (_commandAdapter is null) throw new ArgumentNullException(nameof(_commandAdapter));
if (_snapshotAdapter is null) throw new ArgumentNullException(nameof(_snapshotAdapter));
if (_commandAdapter is null) throw new InvalidOperationException(nameof(_commandAdapter));
if (_snapshotAdapter is null) throw new InvalidOperationException(nameof(_snapshotAdapter));

T recovered = await _snapshotAdapter.DeserializeAsync() ?? _creator();

Expand All @@ -271,6 +290,10 @@ private async Task<T> RecoverRedoableAsync()

private void RecoverFromLogs(T? recovered)
{
if (recovered is null) throw new ArgumentNullException(nameof(recovered));
if (_commandAdapter is null) throw new InvalidOperationException("CommandAdapter is null while recovering from logs!");
if (_snapshotAdapter is null) throw new InvalidOperationException("SnapshotAdapter is null while recoving from logs!");

HandleRedoableDependencies(recovered);

_commandAdapter.LastSnapshotName = _snapshotAdapter.LastSnapshot == null ? string.Empty : new FileInfo(_snapshotAdapter.LastSnapshot).Name;
Expand All @@ -290,10 +313,11 @@ private void RedoCommands(T recovered)

private T RecoverRedoable()
{
if (_creator is null) throw new InvalidOperationException("Creator is null while recovering data!");
if (_withCommandlogOnly || _withNoPersistence) return _creator();

if (_commandAdapter is null) throw new ArgumentNullException(nameof(_commandAdapter));
if (_snapshotAdapter is null) throw new ArgumentNullException(nameof(_snapshotAdapter));
if (_commandAdapter is null) throw new InvalidOperationException(nameof(_commandAdapter));
if (_snapshotAdapter is null) throw new InvalidOperationException(nameof(_snapshotAdapter));

T recovered = _snapshotAdapter.Deserialize() ?? _creator();

Expand Down
1 change: 1 addition & 0 deletions BoCode.RedoDB/Persistence/Commands/CommandsManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BoCode.RedoDB.Compensation;
using System.Collections.Generic;

namespace BoCode.RedoDB.Persistence.Commands
{
Expand Down

0 comments on commit 8bb6eac

Please sign in to comment.