Skip to content

Commit

Permalink
Adapted most of the framework to the new index style
Browse files Browse the repository at this point in the history
  • Loading branch information
halgari committed Jan 17, 2024
1 parent 32c0b8e commit be83c4a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/NexusMods.EventSourcing.Abstractions/EntityIdDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace NexusMods.EventSourcing.Abstractions;

public class EntityIdDefinition : IAttribute<EntityDefinitionAccumulator>, IIndexableAttribute<EntityId>
public class EntityIdDefinition : IAttribute<EntityIdDefinitionAccumulator>, IIndexableAttribute<EntityId>
{
/// <inheritdoc />
public Type Owner => typeof(IEntity);
Expand All @@ -15,14 +15,14 @@ public class EntityIdDefinition : IAttribute<EntityDefinitionAccumulator>, IInde
public string Name => "Id";

/// <inheritdoc />
public EntityDefinitionAccumulator CreateAccumulator()
public EntityIdDefinitionAccumulator CreateAccumulator()
{
return new EntityDefinitionAccumulator();
return new EntityIdDefinitionAccumulator();
}

IAccumulator IAttribute.CreateAccumulator()
{
return new EntityDefinitionAccumulator();
return new EntityIdDefinitionAccumulator();
}

/// <inheritdoc />
Expand All @@ -49,14 +49,14 @@ public int SpanSize()
/// <inheritdoc />
public void WriteTo(Span<byte> span, IAccumulator accumulator)
{
if (accumulator is not EntityDefinitionAccumulator entityDefinitionAccumulator)
if (accumulator is not EntityIdDefinitionAccumulator entityDefinitionAccumulator)
throw new InvalidOperationException("Invalid accumulator type.");

BinaryPrimitives.WriteUInt128BigEndian(span, entityDefinitionAccumulator.Id.Value);
}
}

public class EntityDefinitionAccumulator : IAccumulator
public class EntityIdDefinitionAccumulator : IAccumulator
{
public EntityId Id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace NexusMods.EventSourcing.Abstractions;
/// length values, hashing them and using the hash as the index is recommended. Once the entity is loaded with the help
/// of the index, the actual value can be compared to ensure the values are correct.
/// </summary>
public interface IIndexableAttribute
public interface IIndexableAttribute : IAttribute
{
/// <summary>
/// The Id of the attribute definition index.
Expand Down
24 changes: 22 additions & 2 deletions src/NexusMods.EventSourcing/EntityContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using DynamicData;
using NexusMods.EventSourcing.Abstractions;

namespace NexusMods.EventSourcing;
Expand Down Expand Up @@ -71,7 +72,7 @@ private Dictionary<IAttribute, IAccumulator> LoadAccumulators(EntityId id)
}

var ingester = new EntityContextIngester(values, id);
store.EventsForEntity(id, ingester, snapshotTxId, asOf);
store.EventsForIndex(IEntity.EntityIdAttribute, id, ingester, snapshotTxId, asOf);

if (ingester.ProcessedEvents > MaxEventsBeforeSnapshotting)
{
Expand Down Expand Up @@ -109,7 +110,26 @@ public TransactionId Add<TEvent>(TEvent newEvent) where TEvent : IEvent
lock (_lock)
{

var newId = store.Add(newEvent);
var indexerIngester = new IndexerIngester();
indexerIngester.Ingest(TransactionId.Min, newEvent);

var lst = new List<(IIndexableAttribute, IAccumulator)>();

foreach (var entityId in indexerIngester.Ids)
{
lst.Add((IEntity.EntityIdAttribute, new EntityIdDefinitionAccumulator { Id = entityId }));
}

foreach (var (attribute, accumulators) in indexerIngester.IndexedAttributes)
{
foreach (var accumulator in accumulators)
{
lst.Add((attribute, accumulator));
}
}


var newId = store.Add(newEvent, lst.ToArray());
asOf = newId;

var updatedAttributes = new HashSet<(EntityId, string)>();
Expand Down
47 changes: 47 additions & 0 deletions src/NexusMods.EventSourcing/IndexerIngester.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Collections.Generic;
using DynamicData;
using NexusMods.EventSourcing.Abstractions;

namespace NexusMods.EventSourcing;

public class IndexerIngester : IEventIngester, IEventContext
{
public Dictionary<IIndexableAttribute, List<IAccumulator>> IndexedAttributes = new();

public HashSet<EntityId> Ids = new();


public bool Ingest(TransactionId id, IEvent @event)
{
@event.Apply(this);
return true;
}

public bool GetAccumulator<TOwner, TAttribute, TAccumulator>(EntityId<TOwner> entityId, TAttribute attributeDefinition,
out TAccumulator accumulator) where TOwner : IEntity where TAttribute : IAttribute<TAccumulator> where TAccumulator : IAccumulator
{
Ids.Add(entityId.Value);

if (attributeDefinition is IIndexableAttribute indexableAttribute)
{
var indexedAccumulator = indexableAttribute.CreateAccumulator();

if (IndexedAttributes.TryGetValue(indexableAttribute, out var found))
{
found.Add(indexedAccumulator);
}
else
{
var lst = new List<IAccumulator>();
lst.Add(indexedAccumulator);
IndexedAttributes[indexableAttribute] = lst;
}

accumulator = (TAccumulator)indexedAccumulator;
return true;
}

accumulator = default!;
return false;
}
}

0 comments on commit be83c4a

Please sign in to comment.