Implemented InMemoryReportStorage

master
Inga 🏳‍🌈 7 years ago
parent 2184d49894
commit 9884fb963e
  1. 7
      EternalArrowBackup.sln
  2. 6
      Source/ReportStorage/Contracts/EternalArrowBackup.ReportStorage.Contracts.csproj
  3. 7
      Source/ReportStorage/Contracts/IReportInfo.cs
  4. 5
      Source/ReportStorage/Contracts/IReportStorage.cs
  5. 15
      Source/ReportStorage/InMemoryReportStorage/EternalArrowBackup.ReportStorage.InMemoryReportStorage.csproj
  6. 24
      Source/ReportStorage/InMemoryReportStorage/FileInfo.cs
  7. 84
      Source/ReportStorage/InMemoryReportStorage/ReportInfo.cs
  8. 57
      Source/ReportStorage/InMemoryReportStorage/ReportStorage.cs

@ -65,6 +65,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TargetMetadataStorage", "Ta
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EternalArrowBackup.TargetBinaryStorage.InMemoryBinaryStorage.Tests", "Tests\TargetMetadataStorage\InMemoryMetadataStorage\EternalArrowBackup.TargetBinaryStorage.InMemoryBinaryStorage.Tests.csproj", "{A2661315-0A4E-47AA-B0D8-200E98461004}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EternalArrowBackup.ReportStorage.InMemoryReportStorage", "Source\ReportStorage\InMemoryReportStorage\EternalArrowBackup.ReportStorage.InMemoryReportStorage.csproj", "{050B524C-451C-49F6-A9E5-60E40A40CFEE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -139,6 +141,10 @@ Global
{A2661315-0A4E-47AA-B0D8-200E98461004}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A2661315-0A4E-47AA-B0D8-200E98461004}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A2661315-0A4E-47AA-B0D8-200E98461004}.Release|Any CPU.Build.0 = Release|Any CPU
{050B524C-451C-49F6-A9E5-60E40A40CFEE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{050B524C-451C-49F6-A9E5-60E40A40CFEE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{050B524C-451C-49F6-A9E5-60E40A40CFEE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{050B524C-451C-49F6-A9E5-60E40A40CFEE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -173,5 +179,6 @@ Global
{AC7EA3E8-6469-4102-940F-31EF831E823F} = {99C42B64-99CF-4FB3-B270-079476B43FF7}
{F22F46A0-75BD-49CB-96F2-4E230B59601A} = {884B8E01-303A-40CF-8884-D62115F98683}
{A2661315-0A4E-47AA-B0D8-200E98461004} = {F22F46A0-75BD-49CB-96F2-4E230B59601A}
{050B524C-451C-49F6-A9E5-60E40A40CFEE} = {8FA27C30-856D-4895-87AD-BE71847B76EF}
EndGlobalSection
EndGlobal

@ -1,7 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Threading.Tasks.Dataflow" Version="4.7.0" />
</ItemGroup>
</Project>

@ -1,6 +1,9 @@
namespace EternalArrowBackup.ReportStorage.Contracts
{
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
public interface IReportInfo
{
@ -14,8 +17,8 @@
bool HasErrors { get; }
IObservable<string> GetAllErrors();
Task GetAllErrors(ActionBlock<string> actionBlock, CancellationToken ct);
IObservable<IFileInfo> GetAllFiles();
Task GetAllFiles(ActionBlock<IFileInfo> actionBlock, CancellationToken ct);
}
}

@ -1,7 +1,8 @@
namespace EternalArrowBackup.ReportStorage.Contracts
{
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
public interface IReportStorage
{
@ -13,6 +14,6 @@
Task AddFileInfo(string reportId, string directoryPath, string fileName, string hash, long size);
IObservable<IReportInfo> GetAllReports(bool includeEmpty);
Task GetAllReports(ActionBlock<IReportInfo> actionBlock, bool includeEmpty, CancellationToken ct);
}
}

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Threading.Tasks.Dataflow" Version="4.7.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Contracts\EternalArrowBackup.ReportStorage.Contracts.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,24 @@
namespace EternalArrowBackup.ReportStorage.InMemoryReportStorage
{
using System;
using EternalArrowBackup.ReportStorage.Contracts;
class FileInfo : IFileInfo
{
public FileInfo(string directoryPath, string filename, string hash, long size)
{
this.DirectoryPath = directoryPath;
this.Filename = filename;
this.Hash = hash;
this.Size = size;
}
public string DirectoryPath { get; }
public string Filename { get; }
public string Hash { get; }
public long Size { get; }
}
}

@ -0,0 +1,84 @@
namespace EternalArrowBackup.ReportStorage.InMemoryReportStorage
{
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using EternalArrowBackup.ReportStorage.Contracts;
internal class ReportInfo : IReportInfo
{
public ReportInfo(string reportId, DateTime dateTime)
{
}
public string ReportId { get; }
public DateTime DateTime { get; }
public bool IsCompleted { get; private set; } = false;
public bool IsEmpty { get; private set; }
public bool HasErrors { get; private set; }
private List<string> Errors { get; } = new List<string>();
private List<IFileInfo> Files { get; } = new List<IFileInfo>();
public Task GetAllErrors(ActionBlock<string> actionBlock, CancellationToken ct)
{
return Task.Run(() =>
{
foreach (var error in this.Errors)
{
if (ct.IsCancellationRequested)
{
break;
}
actionBlock.Post(error);
}
actionBlock.Complete();
});
}
public Task GetAllFiles(ActionBlock<IFileInfo> actionBlock, CancellationToken ct)
{
return Task.Run(() =>
{
foreach (var fileInfo in this.Files)
{
if (ct.IsCancellationRequested)
{
break;
}
actionBlock.Post(fileInfo);
}
actionBlock.Complete();
});
}
public void AddFile(IFileInfo fileInfo)
{
this.Files.Add(fileInfo);
}
public void AddError(string errorMessage)
{
this.Errors.Add(errorMessage);
}
public void Complete(bool isEmpty, bool hasErrors)
{
this.IsCompleted = true;
this.IsEmpty = isEmpty;
this.HasErrors = hasErrors;
}
}
}

@ -0,0 +1,57 @@
namespace EternalArrowBackup.ReportStorage.InMemoryReportStorage
{
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using EternalArrowBackup.ReportStorage.Contracts;
public class ReportStorage : IReportStorage
{
private Dictionary<string, ReportInfo> Reports { get; } = new Dictionary<string, ReportInfo>();
public Task AddError(string reportId, string errorMessage)
{
return Task.Run(() => this.Reports[reportId].AddError(errorMessage));
}
public Task AddFileInfo(string reportId, string directoryPath, string fileName, string hash, long size)
{
return Task.Run(() => this.Reports[reportId].AddFile(new FileInfo(directoryPath, fileName, hash, size)));
}
public Task CompleteReport(string reportId, bool isEmpty, bool hasErrors)
{
return Task.Run(() => this.Reports[reportId].Complete(isEmpty, hasErrors));
}
public Task GetAllReports(ActionBlock<IReportInfo> actionBlock, bool includeEmpty, CancellationToken ct)
{
return Task.Run(() =>
{
foreach (var report in this.Reports.Values)
{
if (ct.IsCancellationRequested)
{
break;
}
if (!includeEmpty && report.IsCompleted && !report.IsEmpty)
{
continue;
}
actionBlock.Post(report);
}
actionBlock.Complete();
});
}
public Task InitReport(string reportId)
{
return Task.Run(() => this.Reports[reportId] = new ReportInfo(reportId, DateTime.UtcNow));
}
}
}
Loading…
Cancel
Save