From 85200dbd458f3d241d576551c3ded01d64bbc407 Mon Sep 17 00:00:00 2001 From: halgari Date: Thu, 11 Jan 2024 11:11:03 -0700 Subject: [PATCH] Support serialization of accumulators --- .../ACollectionAttribute.cs | 17 +++++++++++++++++ .../IAccumulator.cs | 19 +++++++++++++++++++ .../MultiEntityAttributeDefinition.cs | 19 +++++++++++++++++++ .../ScalarAttribute.cs | 14 ++++++++++++++ .../Serialization/ISerializationRegistry.cs | 17 +++++++++++++++++ 5 files changed, 86 insertions(+) diff --git a/src/NexusMods.EventSourcing.Abstractions/ACollectionAttribute.cs b/src/NexusMods.EventSourcing.Abstractions/ACollectionAttribute.cs index ad9fe113..6d840310 100644 --- a/src/NexusMods.EventSourcing.Abstractions/ACollectionAttribute.cs +++ b/src/NexusMods.EventSourcing.Abstractions/ACollectionAttribute.cs @@ -1,5 +1,8 @@ using System; +using System.Buffers; using System.Collections.Generic; +using System.Linq; +using NexusMods.EventSourcing.Abstractions.Serialization; namespace NexusMods.EventSourcing.Abstractions; @@ -31,6 +34,20 @@ public object Get() { return _values; } + + /// + public void WriteTo(IBufferWriter writer, ISerializationRegistry registry) + { + registry.Serialize(writer, _values.ToArray()); + } + + /// + public int ReadFrom(ref ReadOnlySpan reader, ISerializationRegistry registry) + { + var read = registry.Deserialize(reader, out TType[] values); + _values = [..values]; + return read; + } } public Type Type => typeof(TType); diff --git a/src/NexusMods.EventSourcing.Abstractions/IAccumulator.cs b/src/NexusMods.EventSourcing.Abstractions/IAccumulator.cs index dfc12cde..e08cad23 100644 --- a/src/NexusMods.EventSourcing.Abstractions/IAccumulator.cs +++ b/src/NexusMods.EventSourcing.Abstractions/IAccumulator.cs @@ -1,3 +1,7 @@ +using System; +using System.Buffers; +using NexusMods.EventSourcing.Abstractions.Serialization; + namespace NexusMods.EventSourcing.Abstractions; /// @@ -5,4 +9,19 @@ namespace NexusMods.EventSourcing.Abstractions; /// public interface IAccumulator { + + /// + /// Writes the accumulator to the given buffer writer. + /// + /// + /// + public void WriteTo(IBufferWriter writer, ISerializationRegistry registry); + + /// + /// Reads the accumulator from the given buffer reader, the span may be larger than the accumulator. Returns the + /// number of bytes read. + /// + /// + /// + public int ReadFrom(ref ReadOnlySpan reader, ISerializationRegistry registry); } diff --git a/src/NexusMods.EventSourcing.Abstractions/MultiEntityAttributeDefinition.cs b/src/NexusMods.EventSourcing.Abstractions/MultiEntityAttributeDefinition.cs index 407c5271..a46e7014 100644 --- a/src/NexusMods.EventSourcing.Abstractions/MultiEntityAttributeDefinition.cs +++ b/src/NexusMods.EventSourcing.Abstractions/MultiEntityAttributeDefinition.cs @@ -1,7 +1,10 @@ using System; +using System.Buffers; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Linq; using NexusMods.EventSourcing.Abstractions.Collections; +using NexusMods.EventSourcing.Abstractions.Serialization; namespace NexusMods.EventSourcing.Abstractions; @@ -146,4 +149,20 @@ public void Remove(EntityId id) { Ids.Remove(id); } + + /// + public void WriteTo(IBufferWriter writer, ISerializationRegistry registry) + { + registry.Serialize(writer, Ids.ToArray()); + } + + /// + public int ReadFrom(ref ReadOnlySpan reader, ISerializationRegistry registry) + { + var read = registry.Deserialize(reader, out EntityId[] ids); + Ids.Clear(); + foreach (var id in ids) + Ids.Add(id); + return read; + } } diff --git a/src/NexusMods.EventSourcing.Abstractions/ScalarAttribute.cs b/src/NexusMods.EventSourcing.Abstractions/ScalarAttribute.cs index 74bc85d9..27fa13cd 100644 --- a/src/NexusMods.EventSourcing.Abstractions/ScalarAttribute.cs +++ b/src/NexusMods.EventSourcing.Abstractions/ScalarAttribute.cs @@ -1,4 +1,6 @@ using System; +using System.Buffers; +using NexusMods.EventSourcing.Abstractions.Serialization; namespace NexusMods.EventSourcing.Abstractions; @@ -74,4 +76,16 @@ public class ScalarAccumulator : IAccumulator /// The value of the accumulator /// public TVal Value = default! ; + + /// + public void WriteTo(IBufferWriter writer, ISerializationRegistry registry) + { + registry.Serialize(writer, Value); + } + + /// + public int ReadFrom(ref ReadOnlySpan span, ISerializationRegistry registry) + { + return registry.Deserialize(span, out Value); + } } diff --git a/src/NexusMods.EventSourcing.Abstractions/Serialization/ISerializationRegistry.cs b/src/NexusMods.EventSourcing.Abstractions/Serialization/ISerializationRegistry.cs index d9be3932..f6b91bd7 100644 --- a/src/NexusMods.EventSourcing.Abstractions/Serialization/ISerializationRegistry.cs +++ b/src/NexusMods.EventSourcing.Abstractions/Serialization/ISerializationRegistry.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers; namespace NexusMods.EventSourcing.Abstractions.Serialization; @@ -22,4 +23,20 @@ public interface ISerializationRegistry /// /// public void RegisterSerializer(Type serializedType, ISerializer serializer); + + /// + /// Serializes the given value into the given buffer writer. + /// + /// + /// + /// + public void Serialize(IBufferWriter writer, TVal value); + + /// + /// Deserializes the given bytes into the given type, returning the number of bytes read. + /// + /// + /// + /// + public int Deserialize(ReadOnlySpan bytes, out TVal value); }