Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove-warning-and-add-di #83

Merged
merged 3 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class IndexSegmentABenchmarks

public IndexSegmentABenchmarks()
{
var registry = new AttributeRegistry([]);
var registry = new AttributeRegistry(null!, []);
using var builder = new IndexSegmentBuilder(registry);

for (int a = 1; a < 100; a++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class IndexSegmentEBenchmarks

public IndexSegmentEBenchmarks()
{
var registry = new AttributeRegistry([]);
var registry = new AttributeRegistry(null!, []);
using var builder = new IndexSegmentBuilder(registry);

for (var e = 1; e < 100; e++)
Expand Down
107 changes: 62 additions & 45 deletions src/NexusMods.MnemonicDB.Abstractions/Attribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,23 @@ protected Attribute(
/// <summary>
/// Converts a low-level value to a high-level value
/// </summary>
protected virtual TValueType FromLowLevel(byte value, ValueTags tags)
protected virtual TValueType FromLowLevel(byte value, ValueTags tags, RegistryId registryId)
{
throw new NotSupportedException("Unsupported low-level type " + value + " on attribute " + Id);
}

/// <summary>
/// Converts a low-level value to a high-level value
/// </summary>
protected virtual TValueType FromLowLevel(ushort value, ValueTags tags)
protected virtual TValueType FromLowLevel(ushort value, ValueTags tags, RegistryId registryId)
{
throw new NotSupportedException("Unsupported low-level type " + value + " on attribute " + Id);
}

/// <summary>
/// Converts a low-level value to a high-level value
/// </summary>
protected virtual TValueType FromLowLevel(uint value, ValueTags tags)
protected virtual TValueType FromLowLevel(uint value, ValueTags tags, RegistryId registryId)
{
throw new NotSupportedException("Unsupported low-level type " + value + " on attribute " + Id);
}
Expand All @@ -75,15 +75,15 @@ protected virtual TValueType FromLowLevel(uint value, ValueTags tags)
/// <summary>
/// Converts a low-level value to a high-level value
/// </summary>
protected virtual TValueType FromLowLevel(string value, ValueTags tag)
protected virtual TValueType FromLowLevel(string value, ValueTags tag, RegistryId registryId)
{
throw new NotSupportedException("Unsupported low-level type " + tag + " on attribute " + Id);
throw new NotSupportedException("Unsupported low-level type " + value + " on attribute " + Id);
}

/// <summary>
/// Converts a low-level value to a high-level value
/// </summary>
protected virtual TValueType FromLowLevel(ReadOnlySpan<byte> value, ValueTags tag)
protected virtual TValueType FromLowLevel(ReadOnlySpan<byte> value, ValueTags tag, RegistryId registryId)
{
throw new NotSupportedException("Unsupported low-level type " + tag + " on attribute " + Id);
}
Expand All @@ -92,7 +92,7 @@ protected virtual TValueType FromLowLevel(ReadOnlySpan<byte> value, ValueTags ta
/// <summary>
/// Converts a low-level value to a high-level value
/// </summary>
protected virtual TValueType FromLowLevel(ulong value, ValueTags tags)
protected virtual TValueType FromLowLevel(ulong value, ValueTags tags, RegistryId registryId)
{
throw new NotSupportedException("Unsupported low-level type " + value + " on attribute " + Id);
}
Expand All @@ -101,55 +101,55 @@ protected virtual TValueType FromLowLevel(ulong value, ValueTags tags)
/// <summary>
/// Converts a low-level value to a high-level value
/// </summary>
protected virtual TValueType FromLowLevel(UInt128 value, ValueTags tags)
protected virtual TValueType FromLowLevel(UInt128 value, ValueTags tags, RegistryId registryId)
{
throw new NotSupportedException("Unsupported low-level type " + value + " on attribute " + Id);
}

/// <summary>
/// Converts a low-level value to a high-level value
/// </summary>
protected virtual TValueType FromLowLevel(short value, ValueTags tags)
protected virtual TValueType FromLowLevel(short value, ValueTags tags, RegistryId registryId)
{
throw new NotSupportedException("Unsupported low-level type " + value + " on attribute " + Id);
}

/// <summary>
/// Converts a low-level value to a high-level value
/// </summary>
protected virtual TValueType FromLowLevel(int value, ValueTags tags)
protected virtual TValueType FromLowLevel(int value, ValueTags tags, RegistryId registryId)
{
throw new NotSupportedException("Unsupported low-level type " + value + " on attribute " + Id);
}

/// <summary>
/// Converts a low-level value to a high-level value
/// </summary>
protected virtual TValueType FromLowLevel(long value, ValueTags tags)
protected virtual TValueType FromLowLevel(long value, ValueTags tags, RegistryId registryId)
{
throw new NotSupportedException("Unsupported low-level type " + value + " on attribute " + Id);
}

/// <summary>
/// Converts a low-level value to a high-level value
/// </summary>
protected virtual TValueType FromLowLevel(Int128 value, ValueTags tags)
protected virtual TValueType FromLowLevel(Int128 value, ValueTags tags, RegistryId registryId)
{
throw new NotSupportedException("Unsupported low-level type " + value + " on attribute " + Id);
}

/// <summary>
/// Converts a low-level value to a high-level value
/// </summary>
protected virtual TValueType FromLowLevel(float value, ValueTags tags)
protected virtual TValueType FromLowLevel(float value, ValueTags tags, RegistryId registryId)
{
throw new NotSupportedException("Unsupported low-level type " + value + " on attribute " + Id);
}

/// <summary>
/// Converts a low-level value to a high-level value
/// </summary>
protected virtual TValueType FromLowLevel(double value, ValueTags tags)
protected virtual TValueType FromLowLevel(double value, ValueTags tags, RegistryId registryId)
{
throw new NotSupportedException("Unsupported low-level type " + value + " on attribute " + Id);
}
Expand Down Expand Up @@ -194,17 +194,9 @@ public AttributeId GetDbId(RegistryId id)
public bool IsReference => LowLevelType == ValueTags.Reference;

/// <inheritdoc />
public IReadDatom Resolve(EntityId entityId, AttributeId attributeId, ReadOnlySpan<byte> value, TxId tx,
bool isRetract, ValueTags valueTag)
public IReadDatom Resolve(in KeyPrefix prefix, ReadOnlySpan<byte> valueSpan, RegistryId registryId)
{
return new ReadDatom
{
E = entityId,
A = this,
V = ReadValue(value, valueTag),
T = tx,
IsRetract = isRetract
};
return new ReadDatom(in prefix, ReadValue(valueSpan, prefix.ValueTag, registryId), this);
}

/// <summary>
Expand Down Expand Up @@ -268,44 +260,67 @@ public override string ToString()
return Id.ToString();
}

/// <summary>
/// Gets a service provider thats specific to this registry defined by the registry id
/// </summary>
protected IServiceProvider GetServiceProvider(RegistryId registryId)
{
return AttributeRegistryRegistry.Registries[registryId.Value]!.ServiceProvider;
}

/// <summary>
/// Gets the registry for the given registry id
/// </summary>
protected IAttributeRegistry GetRegistry(RegistryId registryId)
{
return AttributeRegistryRegistry.Registries[registryId.Value]!;
}

/// <summary>
/// Typed datom for this attribute
/// </summary>
public readonly record struct ReadDatom : IReadDatom
{
private readonly ulong _tx;
public readonly KeyPrefix Prefix;

/// <summary>
/// The value for this datom
/// Typed datom for this attribute
/// </summary>
public required TValueType V { get; init; }
public ReadDatom(in KeyPrefix prefix, TValueType v, Attribute<TValueType, TLowLevelType> a)
{
Prefix = prefix;
TypedAttribute = a;
V = v;
}

/// <summary>
/// The attribute for this datom
/// The typed attribute for this datom
/// </summary>
public readonly Attribute<TValueType, TLowLevelType> TypedAttribute;

/// <summary>
/// The abstract attribute for this datom
/// </summary>
public required IAttribute A { get; init; }
public IAttribute A => TypedAttribute;

/// <summary>
/// The value for this datom
/// </summary>
public readonly TValueType V;

/// <summary>
/// The entity id for this datom
/// </summary>
public required EntityId E { get; init; }
public EntityId E => Prefix.E;

/// <summary>
/// The transaction id for this datom
/// </summary>
public TxId T
{
get => TxId.From(_tx >> 1);
init => _tx = (_tx & 1) | (value.Value << 1);
}
public TxId T => Prefix.T;

/// <inheritdoc />
public bool IsRetract
{
get => (_tx & 1) == 1;
init => _tx = (_tx & ~1UL) | (value ? 1UL : 0);
}

public bool IsRetract => Prefix.IsRetract;

/// <inheritdoc />
public void Retract(ITransaction tx)
{
Expand All @@ -318,6 +333,8 @@ public void Retract(ITransaction tx)
/// <inheritdoc />
public Type ValueType => typeof(TValueType);

public KeyPrefix prefix { get; init; }

/// <inheritdoc />
public override string ToString()
{
Expand All @@ -338,9 +355,9 @@ public int HashCodeByValue()
return HashCode.Combine(A, E, V);
}


public void Deconstruct(out KeyPrefix prefix)
{
prefix = this.prefix;
}
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected override byte[] ToLowLevel(TValue value)
/// <summary>
/// Overwrite this method to read the value from the reader
/// </summary>
protected abstract override TValue FromLowLevel(ReadOnlySpan<byte> value, ValueTags tag);
protected abstract override TValue FromLowLevel(ReadOnlySpan<byte> value, ValueTags tags, RegistryId registryId);

/// <summary>
/// Overwrite this method to write the value to the writer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protected override byte ToLowLevel(Cardinality value)
}

/// <inheritdoc />
protected override Cardinality FromLowLevel(byte value, ValueTags tags)
protected override Cardinality FromLowLevel(byte value, ValueTags tags, RegistryId registryId)
{
return (Cardinality)value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected override byte[] ToLowLevel(TValue value)
/// <summary>
/// Overwrite this method to read the value from the reader
/// </summary>
protected abstract override TValue FromLowLevel(ReadOnlySpan<byte> value, ValueTags tag);
protected abstract override TValue FromLowLevel(ReadOnlySpan<byte> value, ValueTags tags, RegistryId registryId);

/// <summary>
/// Overwrite this method to write the value to the writer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public class ReferenceAttribute(string ns, string name) : ScalarAttribute<Entity
protected override ulong ToLowLevel(EntityId value) => value.Value;

/// <inheritdoc />
protected override EntityId FromLowLevel(ulong lowLevelType, ValueTags tags) => EntityId.From(lowLevelType);
protected override EntityId FromLowLevel(ulong lowLevelType, ValueTags tags, RegistryId registryId)
=> EntityId.From(lowLevelType);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public class ReferencesAttribute(string ns, string name) : CollectionAttribute<E
protected override ulong ToLowLevel(EntityId value) => value.Value;

/// <inheritdoc />
protected override EntityId FromLowLevel(ulong value, ValueTags tags) => EntityId.From(value);
protected override EntityId FromLowLevel(ulong value, ValueTags tags, RegistryId registryId)
=> EntityId.From(value);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public TValue Get(IHasEntityIdAndDb entity)
{
var datom = segment[i];
if (datom.A != dbId) continue;
return ReadValue(datom.ValueSpan, datom.Prefix.ValueTag);
return ReadValue(datom.ValueSpan, datom.Prefix.ValueTag, segment.RegistryId);
}

if (DefaultValue is not null)
Expand Down Expand Up @@ -64,7 +64,7 @@ public bool TryGet(IHasEntityIdAndDb entity, out TValue value)
{
var datom = segment[i];
if (datom.A != dbId) continue;
value = ReadValue(datom.ValueSpan, datom.Prefix.ValueTag);
value = ReadValue(datom.ValueSpan, datom.Prefix.ValueTag, segment.RegistryId);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public class StringAttribute(string ns, string name) : ScalarAttribute<string, s
protected override string ToLowLevel(string value) => value;

/// <inheritdoc />
protected override string FromLowLevel(string lowLevelType, ValueTags tags) => lowLevelType;
protected override string FromLowLevel(string lowLevelType, ValueTags tags, RegistryId registryId)
=> lowLevelType;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class SymbolAttribute(string ns, string name) : ScalarAttribute<Symbol, s
protected override string ToLowLevel(Symbol value) => value.Id;

/// <inheritdoc />
protected override Symbol FromLowLevel(string value, ValueTags tags) => Symbol.InternPreSanitized(value);
protected override Symbol FromLowLevel(string value, ValueTags tags, RegistryId registryId)
=> Symbol.InternPreSanitized(value);
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class TimestampAttribute(string ns, string name) : ScalarAttribute<DateTi
protected override long ToLowLevel(DateTime value) => value.ToFileTimeUtc();

/// <inheritdoc />
protected override DateTime FromLowLevel(long value, ValueTags tags)
protected override DateTime FromLowLevel(long value, ValueTags tags, RegistryId registryId)
{
return DateTime.FromFileTimeUtc(value);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Runtime.InteropServices;
using NexusMods.MnemonicDB.Abstractions.ElementComparers;
using NexusMods.MnemonicDB.Abstractions.Internals;
using Reloaded.Memory.Extensions;

namespace NexusMods.MnemonicDB.Abstractions.Attributes;
Expand All @@ -21,7 +22,7 @@ public TupleAttribute(ValueTags tag1, ValueTags tag2, string ns, string name) :
}

/// <inheritdoc />
public override (T1HighLevel, T2HighLevel) ReadValue(ReadOnlySpan<byte> span, ValueTags tag)
public override (T1HighLevel, T2HighLevel) ReadValue(ReadOnlySpan<byte> span, ValueTags tag, RegistryId registryId)
{
if (tag != ValueTags.Tuple2)
throw new ArgumentException($"Expected tag {ValueTags.Tuple2}, but got {tag}");
Expand All @@ -32,8 +33,8 @@ public override (T1HighLevel, T2HighLevel) ReadValue(ReadOnlySpan<byte> span, Va
if (type1 != (byte)_tag1 || type2 != (byte)_tag2)
throw new ArgumentException($"Expected tag {_tag1} and {_tag2}, but got {type1} and {type2}");

var valA = ReadValue<T1LowLevel>(span.SliceFast(2), _tag1, out var sizeA);
var valB = ReadValue<T2LowLevel>(span.SliceFast(2 + sizeA), _tag2, out _);
var valA = ReadValue<T1LowLevel>(span.SliceFast(2), _tag1, registryId, out var sizeA);
var valB = ReadValue<T2LowLevel>(span.SliceFast(2 + sizeA), _tag2, registryId, out _);

return FromLowLevel((valA, valB));
}
Expand Down
Loading
Loading