Skip to content

Commit

Permalink
More code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
halgari committed Feb 21, 2024
1 parent e8fbc9e commit 6d58bea
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/NexusMods.EventSourcing.Abstractions/IDatomStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ namespace NexusMods.EventSourcing.Abstractions;
public interface IDatomStore : IDisposable
{

/// <summary>
/// Transacts (adds) the given datoms into the store.
/// </summary>
public TxId Transact(IEnumerable<IDatom> datoms);

/// <summary>
Expand Down
12 changes: 4 additions & 8 deletions src/NexusMods.EventSourcing.Abstractions/ITransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace NexusMods.EventSourcing.Abstractions;

/// <summary>
/// Represents a transaction, which is a set of proposed changes to the datom store
/// </summary>
public interface ITransaction
{
/// <summary>
Expand All @@ -10,12 +13,6 @@ public interface ITransaction
/// <returns></returns>
EntityId TempId();

/// <summary>
/// Adds a new datom to the transaction
/// </summary>
void Add(IDatom datom);


/// <summary>
/// Adds a new read model to the transaction, the datoms are extracted from the read model
/// as asserts for each property with the FromAttribute
Expand All @@ -29,10 +26,9 @@ void Add<TReadModel>(TReadModel model)
/// </summary>
/// <param name="entityId"></param>
/// <param name="val"></param>
/// <param name="isAssert"></param>
/// <typeparam name="TAttribute"></typeparam>
/// <typeparam name="TVal"></typeparam>
void Add<TAttribute, TVal>(EntityId entityId, TVal val, bool isAssert = true)
void Add<TAttribute, TVal>(EntityId entityId, TVal val)
where TAttribute : IAttribute<TVal>;

/// <summary>
Expand Down
9 changes: 9 additions & 0 deletions src/NexusMods.EventSourcing.Abstractions/Ids.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
namespace NexusMods.EventSourcing.Abstractions;

/// <summary>
/// A lot of ids in this system are 64 bit unsigned integers. This class provides a way to partition those ids
/// into different categories. 64 bits is a lot of space, so we can use the high 8 bits to store the partition,
/// and the low 56 bits to store the id. This allows us to have 256 different partitions, each with way more ids
/// than we would ever need.
/// </summary>
public static class Ids
{
/// <summary>
/// Known partitions
/// </summary>
public enum Partition
{
/// <summary>
Expand Down
11 changes: 11 additions & 0 deletions src/NexusMods.EventSourcing.Abstractions/ScalarAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ protected ScalarAttribute(Symbol symbol)
Id = symbol;
}

/// <inheritdoc />
public TValueType Read(ReadOnlySpan<byte> buffer)
{
_serializer.Read(buffer, out var val);
Expand All @@ -43,6 +44,7 @@ public static void Add(ITransaction tx, EntityId entity, TValueType value)
tx.Add<TAttribute, TValueType>(entity, value);
}

/// <inheritdoc />
public void SetSerializer(IValueSerializer serializer)
{
if (serializer is not IValueSerializer<TValueType> valueSerializer)
Expand All @@ -63,6 +65,9 @@ public void SetSerializer(IValueSerializer serializer)
/// <inheritdoc />
public Symbol Id { get; }

/// <summary>
/// Read a datom from a buffer
/// </summary>
public IDatom Read(ulong entity, ulong tx, bool isAssert, ReadOnlySpan<byte> buffer)
{
_serializer.Read(buffer, out var val);
Expand All @@ -71,6 +76,12 @@ public IDatom Read(ulong entity, ulong tx, bool isAssert, ReadOnlySpan<byte> buf
: throw new NotImplementedException();
}

/// <summary>
/// Create a new datom for an assert on this attribute, and return it
/// </summary>
/// <param name="e"></param>
/// <param name="v"></param>
/// <returns></returns>
public static IDatom Assert(ulong e, TValueType v)
{
return new AssertDatom<TAttribute, TValueType>(e, v);
Expand Down
3 changes: 3 additions & 0 deletions src/NexusMods.EventSourcing.Abstractions/TxId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace NexusMods.EventSourcing.Abstractions;

/// <summary>
/// A typed identifier for a transaction id, internally this is a ulong.
/// </summary>
[ValueObject<ulong>]
public readonly partial struct TxId
{
Expand Down

0 comments on commit 6d58bea

Please sign in to comment.