Skip to content

Commit be83c4a

Browse files
committed
Adapted most of the framework to the new index style
1 parent 32c0b8e commit be83c4a

File tree

4 files changed

+76
-9
lines changed

4 files changed

+76
-9
lines changed

src/NexusMods.EventSourcing.Abstractions/EntityIdDefinition.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace NexusMods.EventSourcing.Abstractions;
88

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

1717
/// <inheritdoc />
18-
public EntityDefinitionAccumulator CreateAccumulator()
18+
public EntityIdDefinitionAccumulator CreateAccumulator()
1919
{
20-
return new EntityDefinitionAccumulator();
20+
return new EntityIdDefinitionAccumulator();
2121
}
2222

2323
IAccumulator IAttribute.CreateAccumulator()
2424
{
25-
return new EntityDefinitionAccumulator();
25+
return new EntityIdDefinitionAccumulator();
2626
}
2727

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

5555
BinaryPrimitives.WriteUInt128BigEndian(span, entityDefinitionAccumulator.Id.Value);
5656
}
5757
}
5858

59-
public class EntityDefinitionAccumulator : IAccumulator
59+
public class EntityIdDefinitionAccumulator : IAccumulator
6060
{
6161
public EntityId Id;
6262

src/NexusMods.EventSourcing.Abstractions/IIndexableAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace NexusMods.EventSourcing.Abstractions;
1212
/// length values, hashing them and using the hash as the index is recommended. Once the entity is loaded with the help
1313
/// of the index, the actual value can be compared to ensure the values are correct.
1414
/// </summary>
15-
public interface IIndexableAttribute
15+
public interface IIndexableAttribute : IAttribute
1616
{
1717
/// <summary>
1818
/// The Id of the attribute definition index.

src/NexusMods.EventSourcing/EntityContext.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
4+
using DynamicData;
45
using NexusMods.EventSourcing.Abstractions;
56

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

7374
var ingester = new EntityContextIngester(values, id);
74-
store.EventsForEntity(id, ingester, snapshotTxId, asOf);
75+
store.EventsForIndex(IEntity.EntityIdAttribute, id, ingester, snapshotTxId, asOf);
7576

7677
if (ingester.ProcessedEvents > MaxEventsBeforeSnapshotting)
7778
{
@@ -109,7 +110,26 @@ public TransactionId Add<TEvent>(TEvent newEvent) where TEvent : IEvent
109110
lock (_lock)
110111
{
111112

112-
var newId = store.Add(newEvent);
113+
var indexerIngester = new IndexerIngester();
114+
indexerIngester.Ingest(TransactionId.Min, newEvent);
115+
116+
var lst = new List<(IIndexableAttribute, IAccumulator)>();
117+
118+
foreach (var entityId in indexerIngester.Ids)
119+
{
120+
lst.Add((IEntity.EntityIdAttribute, new EntityIdDefinitionAccumulator { Id = entityId }));
121+
}
122+
123+
foreach (var (attribute, accumulators) in indexerIngester.IndexedAttributes)
124+
{
125+
foreach (var accumulator in accumulators)
126+
{
127+
lst.Add((attribute, accumulator));
128+
}
129+
}
130+
131+
132+
var newId = store.Add(newEvent, lst.ToArray());
113133
asOf = newId;
114134

115135
var updatedAttributes = new HashSet<(EntityId, string)>();
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.Collections.Generic;
2+
using DynamicData;
3+
using NexusMods.EventSourcing.Abstractions;
4+
5+
namespace NexusMods.EventSourcing;
6+
7+
public class IndexerIngester : IEventIngester, IEventContext
8+
{
9+
public Dictionary<IIndexableAttribute, List<IAccumulator>> IndexedAttributes = new();
10+
11+
public HashSet<EntityId> Ids = new();
12+
13+
14+
public bool Ingest(TransactionId id, IEvent @event)
15+
{
16+
@event.Apply(this);
17+
return true;
18+
}
19+
20+
public bool GetAccumulator<TOwner, TAttribute, TAccumulator>(EntityId<TOwner> entityId, TAttribute attributeDefinition,
21+
out TAccumulator accumulator) where TOwner : IEntity where TAttribute : IAttribute<TAccumulator> where TAccumulator : IAccumulator
22+
{
23+
Ids.Add(entityId.Value);
24+
25+
if (attributeDefinition is IIndexableAttribute indexableAttribute)
26+
{
27+
var indexedAccumulator = indexableAttribute.CreateAccumulator();
28+
29+
if (IndexedAttributes.TryGetValue(indexableAttribute, out var found))
30+
{
31+
found.Add(indexedAccumulator);
32+
}
33+
else
34+
{
35+
var lst = new List<IAccumulator>();
36+
lst.Add(indexedAccumulator);
37+
IndexedAttributes[indexableAttribute] = lst;
38+
}
39+
40+
accumulator = (TAccumulator)indexedAccumulator;
41+
return true;
42+
}
43+
44+
accumulator = default!;
45+
return false;
46+
}
47+
}

0 commit comments

Comments
 (0)