diff --git a/csharp/Platform.Data.Doublets.Tests/ConsoleLoggingDecoratorTests.cs b/csharp/Platform.Data.Doublets.Tests/ConsoleLoggingDecoratorTests.cs new file mode 100644 index 000000000..7c7e67573 --- /dev/null +++ b/csharp/Platform.Data.Doublets.Tests/ConsoleLoggingDecoratorTests.cs @@ -0,0 +1,57 @@ +using System; +using System.IO; +using System.Numerics; +using Platform.Data.Doublets.Decorators; +using Xunit; + +using Platform.Memory; + +using Platform.Data.Doublets.Memory.United.Generic; + +namespace Platform.Data.Doublets.Tests +{ + public static class ConsoleLoggingDecoratorTests + { + [Fact] + public static void BasicConsoleLoggingTest() + { + Using(links => + { + // Create a link + var link = links.Create(); + + // Update it + links.Update(link, link, link); + + // Count links + var count = links.Count(); + + // Delete the link + links.Delete(link); + }); + } + + [Fact] + public static void ConsoleLoggingWithRestrictionsTest() + { + Using(links => + { + var link1 = links.Create(); + var link2 = links.Create(); + + // Test with restrictions + var count = links.Count(new[] { links.Constants.Any, link1, links.Constants.Any }); + + links.Delete(link1); + links.Delete(link2); + }); + } + + private static void Using(Action> action) where TLinkAddress : IUnsignedNumber, IShiftOperators, IBitwiseOperators, IMinMaxValue, IComparisonOperators + { + var unitedMemoryLinks = new UnitedMemoryLinks(new HeapResizableDirectMemory()); + ConsoleLoggingDecorator decoratedStorage = new(unitedMemoryLinks); + action(decoratedStorage); + } + } +} \ No newline at end of file diff --git a/csharp/Platform.Data.Doublets/Decorators/ConsoleLoggingDecorator.cs b/csharp/Platform.Data.Doublets/Decorators/ConsoleLoggingDecorator.cs new file mode 100644 index 000000000..a4f41ea39 --- /dev/null +++ b/csharp/Platform.Data.Doublets/Decorators/ConsoleLoggingDecorator.cs @@ -0,0 +1,53 @@ +using System.Collections.Generic; +using System.Numerics; +using Platform.Delegates; + +namespace Platform.Data.Doublets.Decorators; + +public class ConsoleLoggingDecorator : LinksDecoratorBase where TLinkAddress : IUnsignedNumber +{ + public ConsoleLoggingDecorator(ILinks links) : base(links: links) + { + } + + public override TLinkAddress Create(IList? substitution, WriteHandler? handler) + { + WriteHandlerState handlerState = new(@continue: _constants.Continue, @break: _constants.Break, handler: handler); + return base.Create(substitution: substitution, handler: (before, after) => + { + handlerState.Handle(before: before, after: after); + System.Console.WriteLine(value: $"Create. Before: {new Link(values: before)}. After: {new Link(values: after)}"); + return _constants.Continue; + }); + } + + public override TLinkAddress Update(IList? restriction, IList? substitution, WriteHandler? handler) + { + WriteHandlerState handlerState = new(@continue: _constants.Continue, @break: _constants.Break, handler: handler); + return base.Update(restriction: restriction, substitution: substitution, handler: (before, after) => + { + handlerState.Handle(before: before, after: after); + System.Console.WriteLine(value: $"Update. Before: {new Link(values: before)}. After: {new Link(values: after)}"); + return _constants.Continue; + }); + } + + public override TLinkAddress Delete(IList? restriction, WriteHandler? handler) + { + WriteHandlerState handlerState = new(@continue: _constants.Continue, @break: _constants.Break, handler: handler); + return base.Delete(restriction: restriction, handler: (before, after) => + { + handlerState.Handle(before: before, after: after); + System.Console.WriteLine(value: $"Delete. Before: {new Link(values: before)}. After: {new Link(values: after)}"); + return _constants.Continue; + }); + } + + public override TLinkAddress Count(IList? restriction) + { + var result = base.Count(restriction: restriction); + var restrictionStr = restriction == null ? "null" : $"[{string.Join(", ", restriction)}]"; + System.Console.WriteLine(value: $"Count. Restriction: {restrictionStr}. Result: {result}"); + return result; + } +} \ No newline at end of file