Skip to content

Commit 32c0b8e

Browse files
committed
latest work
1 parent a56c678 commit 32c0b8e

File tree

6 files changed

+54
-17
lines changed

6 files changed

+54
-17
lines changed

src/NexusMods.EventSourcing.Abstractions/EntityIdDefinition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void WriteTo(Span<byte> span, IAccumulator accumulator)
5858

5959
public class EntityDefinitionAccumulator : IAccumulator
6060
{
61-
internal EntityId Id;
61+
public EntityId Id;
6262

6363
public void WriteTo(IBufferWriter<byte> writer, ISerializationRegistry registry)
6464
{

src/NexusMods.EventSourcing.Abstractions/EntityStructureRegistry.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,24 @@ public static void Register(IAttribute attribute)
3838
}
3939
}
4040

41+
/// <summary>
42+
/// Returns all indexed attributes.
43+
/// </summary>
44+
/// <returns></returns>
45+
public static IEnumerable<IIndexableAttribute> AllIndexableAttributes()
46+
{
47+
foreach (var (_, attributes) in _entityStructures)
48+
{
49+
foreach (var (_, attribute) in attributes)
50+
{
51+
if (attribute is IIndexableAttribute indexableAttribute && indexableAttribute.IndexedAttributeId != UInt128.Zero)
52+
{
53+
yield return indexableAttribute;
54+
}
55+
}
56+
}
57+
}
58+
4159
/// <summary>
4260
/// Registers an entity type in the global registry.
4361
/// </summary>

src/NexusMods.EventSourcing.Abstractions/IEventStore.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,37 @@ public interface IEventStore
1414
/// <param name="eventEntity"></param>
1515
/// <typeparam name="T"></typeparam>
1616
/// <returns></returns>
17-
public TransactionId Add<T>(T eventEntity) where T : IEvent;
17+
public TransactionId Add<T>(T eventEntity, (IIndexableAttribute, IAccumulator)[] indexed) where T : IEvent;
18+
1819

1920
/// <summary>
20-
/// For each event within the given range (inclusive), for the given entity id, call the ingester.
21+
/// Gets all events where the given attribute appears in the index with the given value. Transactions
22+
/// will be between fromTx and toTx (inclusive on both ends)
2123
/// </summary>
22-
/// <param name="entityId">The Entity Id to playback events for</param>
23-
/// <param name="ingester">The ingester to handle the events</param>
24-
/// <param name="reverse">If true, plays the events in reverse</param>
24+
/// <param name="attr"></param>
25+
/// <param name="value"></param>
26+
/// <param name="ingester"></param>
27+
/// <param name="fromTx"></param>
28+
/// <param name="toTx"></param>
2529
/// <typeparam name="TIngester"></typeparam>
26-
public void EventsForEntity<TIngester>(EntityId entityId, TIngester ingester, TransactionId fromId, TransactionId toId)
30+
/// <typeparam name="TVal"></typeparam>
31+
public void EventsForIndex<TIngester, TVal>(IIndexableAttribute<TVal> attr, TVal value, TIngester ingester,
32+
TransactionId fromTx, TransactionId toTx)
2733
where TIngester : IEventIngester;
2834

2935
/// <summary>
3036
/// For each event for the given entity id, call the ingester.
3137
/// </summary>
32-
/// <param name="entityId">The Entity Id to playback events for</param>
38+
/// <param name="value"></param>
3339
/// <param name="ingester">The ingester to handle the events</param>
34-
/// <param name="reverse">If true, plays the events in reverse</param>
40+
/// <param name="attr"></param>
3541
/// <typeparam name="TIngester"></typeparam>
36-
public void EventsForEntity<TIngester>(EntityId entityId, TIngester ingester)
42+
/// <typeparam name="TVal"></typeparam>
43+
public void EventsForIndex<TIngester, TVal>(IIndexableAttribute<TVal> attr, TVal value, TIngester ingester)
3744
where TIngester : IEventIngester
3845

3946
{
40-
EventsForEntity(entityId, ingester, TransactionId.Min, TransactionId.Max);
47+
EventsForIndex(attr, value, ingester, TransactionId.Min, TransactionId.Max);
4148
}
4249

4350
/// <summary>
@@ -48,7 +55,7 @@ public void EventsForEntity<TIngester>(EntityId entityId, TIngester ingester)
4855
/// </summary>
4956
/// <param name="asOf"></param>
5057
/// <param name="entityId"></param>
51-
/// <param name="revision"></param>
58+
/// <param name="loadedDefinition"></param>
5259
/// <param name="loadedAttributes"></param>
5360
/// <returns></returns>
5461
public TransactionId GetSnapshot(TransactionId asOf, EntityId entityId,

src/NexusMods.EventSourcing/AEventStore.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ protected ReadOnlySpan<byte> SerializeSnapshot(EntityId id, IDictionary<IAttribu
102102
public abstract TransactionId Add<T>(T eventEntity) where T : IEvent;
103103
public abstract void EventsForEntity<TIngester>(EntityId entityId, TIngester ingester, TransactionId fromId, TransactionId toId) where TIngester : IEventIngester;
104104

105+
public abstract TransactionId Add<T>(T eventEntity, (IIndexableAttribute, IAccumulator)[] indexed) where T : IEvent;
106+
107+
public abstract void EventsForIndex<TIngester, TVal>(IIndexableAttribute<TVal> attr, TVal value, TIngester ingester, TransactionId fromTx,
108+
TransactionId toTx) where TIngester : IEventIngester;
109+
105110
public abstract TransactionId GetSnapshot(TransactionId asOf, EntityId entityId, out IAccumulator loadedDefinition,
106111
out (IAttribute Attribute, IAccumulator Accumulator)[] loadedAttributes);
107112

src/NexusMods.EventSourcing/EntityContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public TransactionId Add<TEvent>(TEvent newEvent) where TEvent : IEvent
108108
{
109109
lock (_lock)
110110
{
111+
111112
var newId = store.Add(newEvent);
112113
asOf = newId;
113114

tests/NexusMods.EventSourcing.Tests/AEventStoreTest.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,20 @@ public AEventStoreTest(T store)
2121
public void CanGetAndReturnEvents()
2222
{
2323
var enityId = EntityId<Loadout>.NewId();
24-
Store.Add(new CreateLoadout(enityId, "Test"));
24+
var entityIdAccumulator = IEntity.EntityIdAttribute.CreateAccumulator();
25+
entityIdAccumulator.Id = enityId.Value;
26+
27+
var indexArray = new (IIndexableAttribute, IAccumulator)[] { (IEntity.EntityIdAttribute, entityIdAccumulator) };
28+
29+
Store.Add(new CreateLoadout(enityId, "Test"), indexArray);
30+
2531
for (var i = 0; i < 10; i++)
2632
{
27-
Store.Add(new RenameLoadout(enityId, $"Test {i}"));
33+
Store.Add(new RenameLoadout(enityId, $"Test {i}"), indexArray);
2834
}
2935

30-
var accumulator = new EventAccumulator();
31-
Store.EventsForEntity(enityId.Value, accumulator);
36+
var accumulator = new EventIngester();
37+
Store.EventsForIndex(IEntity.EntityIdAttribute, enityId.Value, accumulator);
3238
accumulator.Events.Count.Should().Be(11);
3339
accumulator.Events[0].Should().BeEquivalentTo(new CreateLoadout(enityId, "Test"));
3440
for (var i = 1; i < 11; i++)
@@ -69,7 +75,7 @@ public void CanGetSnapshots()
6975

7076
}
7177

72-
private class EventAccumulator : IEventIngester
78+
private class EventIngester : IEventIngester
7379
{
7480
public List<IEvent> Events { get; } = new();
7581
public bool Ingest(TransactionId _, IEvent @event)

0 commit comments

Comments
 (0)