Skip to content

Commit

Permalink
Support serialization of accumulators
Browse files Browse the repository at this point in the history
  • Loading branch information
halgari committed Jan 11, 2024
1 parent 8d4006f commit 85200db
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/NexusMods.EventSourcing.Abstractions/ACollectionAttribute.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -31,6 +34,20 @@ public object Get()
{
return _values;
}

/// <inheritdoc />
public void WriteTo(IBufferWriter<byte> writer, ISerializationRegistry registry)
{
registry.Serialize(writer, _values.ToArray());
}

/// <inheritdoc />
public int ReadFrom(ref ReadOnlySpan<byte> reader, ISerializationRegistry registry)
{
var read = registry.Deserialize(reader, out TType[] values);
_values = [..values];
return read;
}
}

public Type Type => typeof(TType);
Expand Down
19 changes: 19 additions & 0 deletions src/NexusMods.EventSourcing.Abstractions/IAccumulator.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
using System;
using System.Buffers;
using NexusMods.EventSourcing.Abstractions.Serialization;

namespace NexusMods.EventSourcing.Abstractions;

/// <summary>
/// An accumulator is used to accumulate values from events, it is opaque to everything but the attribute definition that created it.
/// </summary>
public interface IAccumulator
{

/// <summary>
/// Writes the accumulator to the given buffer writer.
/// </summary>
/// <param name="writer"></param>
/// <param name="registry"></param>
public void WriteTo(IBufferWriter<byte> writer, ISerializationRegistry registry);

/// <summary>
/// Reads the accumulator from the given buffer reader, the span may be larger than the accumulator. Returns the
/// number of bytes read.
/// </summary>
/// <param name="reader"></param>
/// <param name="registry"></param>
public int ReadFrom(ref ReadOnlySpan<byte> reader, ISerializationRegistry registry);
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -146,4 +149,20 @@ public void Remove(EntityId<TType> id)
{
Ids.Remove(id);
}

/// <inheritdoc />
public void WriteTo(IBufferWriter<byte> writer, ISerializationRegistry registry)
{
registry.Serialize(writer, Ids.ToArray());
}

/// <inheritdoc />
public int ReadFrom(ref ReadOnlySpan<byte> reader, ISerializationRegistry registry)
{
var read = registry.Deserialize(reader, out EntityId<TType>[] ids);
Ids.Clear();
foreach (var id in ids)
Ids.Add(id);
return read;
}
}
14 changes: 14 additions & 0 deletions src/NexusMods.EventSourcing.Abstractions/ScalarAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Buffers;
using NexusMods.EventSourcing.Abstractions.Serialization;

namespace NexusMods.EventSourcing.Abstractions;

Expand Down Expand Up @@ -74,4 +76,16 @@ public class ScalarAccumulator<TVal> : IAccumulator
/// The value of the accumulator
/// </summary>
public TVal Value = default! ;

/// <inheritdoc />
public void WriteTo(IBufferWriter<byte> writer, ISerializationRegistry registry)
{
registry.Serialize(writer, Value);
}

/// <inheritdoc />
public int ReadFrom(ref ReadOnlySpan<byte> span, ISerializationRegistry registry)
{
return registry.Deserialize(span, out Value);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Buffers;

namespace NexusMods.EventSourcing.Abstractions.Serialization;

Expand All @@ -22,4 +23,20 @@ public interface ISerializationRegistry
/// <param name="serializedType"></param>
/// <param name="serializer"></param>
public void RegisterSerializer(Type serializedType, ISerializer serializer);

/// <summary>
/// Serializes the given value into the given buffer writer.
/// </summary>
/// <param name="writer"></param>
/// <param name="value"></param>
/// <typeparam name="TVal"></typeparam>
public void Serialize<TVal>(IBufferWriter<byte> writer, TVal value);

/// <summary>
/// Deserializes the given bytes into the given type, returning the number of bytes read.
/// </summary>
/// <param name="bytes"></param>
/// <typeparam name="TVal"></typeparam>
/// <returns></returns>
public int Deserialize<TVal>(ReadOnlySpan<byte> bytes, out TVal value);
}

0 comments on commit 85200db

Please sign in to comment.