Skip to content

Commit

Permalink
Revert "Start of secondary indexing"
Browse files Browse the repository at this point in the history
This reverts commit 54ae924.
  • Loading branch information
halgari committed Jan 16, 2024
1 parent 54ae924 commit 51ce63f
Show file tree
Hide file tree
Showing 12 changed files with 13 additions and 131 deletions.
18 changes: 0 additions & 18 deletions docs/EventStore.md

This file was deleted.

33 changes: 0 additions & 33 deletions docs/SecondaryIndexes.md

This file was deleted.

11 changes: 0 additions & 11 deletions src/NexusMods.EventSourcing.Abstractions/AttributeDefinition.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ IAccumulator IAttribute.CreateAccumulator()
/// <returns></returns>
public Type Get<TCtx>(TCtx context, EntityId owner) where TCtx : IEntityContext
{
EntityStructureRegistry.Register(this);
if (context.GetReadOnlyAccumulator<IEntity, TypeAttributeDefinition, ScalarAccumulator<EntityDefinition>>(
new EntityId<IEntity>(owner), this, out var accumulator))
return accumulator.Value.Type;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Buffers.Binary;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -46,31 +45,12 @@ public static IServiceCollection AddEvent<T>(this IServiceCollection collection)
public static IServiceCollection AddEntity<T>(this IServiceCollection collection) where T : class, IEntity
{
var type = typeof(T);
var entityAttribute = type.GetCustomAttribute<EntityAttribute>();
if (entityAttribute is null)
var attribute = type.GetCustomAttribute<EntityAttribute>();
if (attribute is null)
{
throw new ArgumentException($"Entity type {type.Name} does not have an EntityAttribute.");
}

foreach (var staticAttribute in type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static))
{
if (staticAttribute.FieldType.IsAssignableTo(typeof(IAttribute)))
{
var attributeInstance = staticAttribute.GetValue(null) as IAttribute;
if (attributeInstance is null)
{
throw new InvalidOperationException($"Attribute {staticAttribute.Name} is null.");
}

var indexedAttribute = staticAttribute.GetCustomAttributes<IndexedAttribute>().FirstOrDefault();


var definition = new AttributeDefinition(attributeInstance, type, attributeInstance.Name, indexedAttribute is not null);
EntityStructureRegistry.Register(definition);
}
}

EntityStructureRegistry.Register(new EntityDefinition(type, entityAttribute.UUID, entityAttribute.Revision));
EntityStructureRegistry.Register(new EntityDefinition(type, attribute.UUID, attribute.Revision));
return collection;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public void Link<TContext>(TContext context, EntityId<TOwner> owner, EntityId<TO
{
if (context.GetAccumulator<TOwner, EntityAttributeDefinition<TOwner, TOther>, ScalarAccumulator<EntityId<TOther>>>(owner, this, out var accumulator))
accumulator.Value = value;
EntityStructureRegistry.Register(this);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace NexusMods.EventSourcing.Abstractions;
/// </summary>
public static class EntityStructureRegistry
{
private static readonly ConcurrentDictionary<Type, ConcurrentDictionary<string, AttributeDefinition>> _entityStructures = new();
private static readonly ConcurrentDictionary<Type, ConcurrentDictionary<string, IAttribute>> _entityStructures = new();


private static readonly ConcurrentDictionary<Type, EntityDefinition> _entityDefinitionsByType = new();
Expand All @@ -21,7 +21,7 @@ public static class EntityStructureRegistry
/// Register an attribute in the global registry.
/// </summary>
/// <param name="attribute"></param>
public static void Register(AttributeDefinition attribute)
public static void Register(IAttribute attribute)
{
TOP:
if (_entityStructures.TryGetValue(attribute.Owner, out var found))
Expand All @@ -30,7 +30,7 @@ public static void Register(AttributeDefinition attribute)
return;
}

var dict = new ConcurrentDictionary<string, AttributeDefinition>();
var dict = new ConcurrentDictionary<string, IAttribute>();
dict.TryAdd(attribute.Name, attribute);
if (!_entityStructures.TryAdd(attribute.Owner, dict))
{
Expand All @@ -55,7 +55,7 @@ public static void Register(EntityDefinition definition)
/// </summary>
/// <param name="owner"></param>
/// <returns></returns>
public static bool TryGetAttributes(Type owner, [NotNullWhen(true)] out ConcurrentDictionary<string, AttributeDefinition>? result)
public static bool TryGetAttributes(Type owner, [NotNullWhen(true)] out ConcurrentDictionary<string, IAttribute>? result)
{
if (_entityStructures.TryGetValue(owner, out var found ))
{
Expand Down
26 changes: 0 additions & 26 deletions src/NexusMods.EventSourcing.Abstractions/IIndexableAttribute.cs

This file was deleted.

14 changes: 0 additions & 14 deletions src/NexusMods.EventSourcing.Abstractions/IndexedAttribute.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class MultiEntityAttributeDefinition<TOwner, TOther> : IAttribute<MultiEn
public MultiEntityAttributeDefinition(string name)
{
_name = name;
EntityStructureRegistry.Register(this);
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void Set<TContext>(TContext context, EntityId<TOwner> owner, TType value)
{
if (context.GetAccumulator<TOwner, ScalarAttribute<TOwner, TType>, ScalarAccumulator<TType>>(owner, this, out var accumulator))
accumulator.Value = value;
EntityStructureRegistry.Register(this);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/NexusMods.EventSourcing/AEventStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ protected bool DeserializeSnapshot(out IAccumulator loadedDefinition,
if (!attributes.TryGetValue(attributeName, out var attribute))
throw new Exception("Entity definition does not match the current structure registry.");

var accumulator = attribute.Attribute.CreateAccumulator();
var accumulator = attribute.CreateAccumulator();

accumulator.ReadFrom(ref snapshot, _serializationRegistry);
snapshot = snapshot.SliceFast(read);

results[i] = (attribute.Attribute, accumulator);
results[i] = (attribute, accumulator);
}

loadedAttributes = results;
Expand Down

0 comments on commit 51ce63f

Please sign in to comment.