Skip to content

Commit ae13439

Browse files
authored
Merge pull request #103 from Nexus-Mods/serializer-cleanup
Serializer cleanup
2 parents 9d7ca56 + 57e2731 commit ae13439

File tree

68 files changed

+736
-1275
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+736
-1275
lines changed

benchmarks/NexusMods.MnemonicDB.Benchmarks/Benchmarks/IndexSegmentABenchmarks.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public IndexSegmentABenchmarks()
2121
for (int a = 1; a < 100; a++)
2222
{
2323
var prefix = new KeyPrefix(EntityId.From(42), AttributeId.From((ushort)a), TxId.From(42), false,
24-
ValueTags.Null);
24+
ValueTag.Null);
2525
builder.Add(new Datom(prefix, ReadOnlyMemory<byte>.Empty));
2626
}
2727

benchmarks/NexusMods.MnemonicDB.Benchmarks/Benchmarks/IndexSegmentEBenchmarks.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public IndexSegmentEBenchmarks()
2424
for (var a = 0; a < 20; a++)
2525
{
2626
builder.Add(new Datom(new KeyPrefix(EntityId.From((ulong)e), AttributeId.From((ushort)a), TxId.From((ulong)(e + a)), false,
27-
ValueTags.Null), ReadOnlyMemory<byte>.Empty));
27+
ValueTag.Null), ReadOnlyMemory<byte>.Empty));
2828
}
2929
}
3030

src/NexusMods.MnemonicDB.Abstractions/Attribute.cs

Lines changed: 33 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,10 @@ namespace NexusMods.MnemonicDB.Abstractions;
1818
/// Interface for a specific attribute
1919
/// </summary>
2020
/// <typeparam name="TValueType"></typeparam>
21-
public abstract partial class Attribute<TValueType, TLowLevelType> : IAttribute<TValueType>
21+
public abstract class Attribute<TValueType, TLowLevelType> : IAttribute<TValueType>
2222
{
23-
private const int MaxStackAlloc = 128;
24-
private static Encoding AsciiEncoding = Encoding.ASCII;
25-
26-
private static Encoding Utf8Encoding = Encoding.UTF8;
27-
2823
protected Attribute(
29-
ValueTags lowLevelType,
24+
ValueTag lowLevelType,
3025
string ns,
3126
string name,
3227
bool isIndexed = false,
@@ -44,116 +39,14 @@ protected Attribute(
4439
/// Converts a high-level value to a low-level value
4540
/// </summary>
4641
protected abstract TLowLevelType ToLowLevel(TValueType value);
47-
48-
/// <summary>
49-
/// Converts a low-level value to a high-level value
50-
/// </summary>
51-
protected virtual TValueType FromLowLevel(byte value, ValueTags tags, AttributeResolver resolver)
52-
{
53-
throw new NotSupportedException("Unsupported low-level type " + value + " on attribute " + Id);
54-
}
55-
56-
/// <summary>
57-
/// Converts a low-level value to a high-level value
58-
/// </summary>
59-
protected virtual TValueType FromLowLevel(ushort value, ValueTags tags, AttributeResolver resolver)
60-
{
61-
throw new NotSupportedException("Unsupported low-level type " + value + " on attribute " + Id);
62-
}
63-
64-
/// <summary>
65-
/// Converts a low-level value to a high-level value
66-
/// </summary>
67-
protected virtual TValueType FromLowLevel(uint value, ValueTags tags, AttributeResolver resolver)
68-
{
69-
throw new NotSupportedException("Unsupported low-level type " + value + " on attribute " + Id);
70-
}
71-
72-
73-
/// <summary>
74-
/// Converts a low-level value to a high-level value
75-
/// </summary>
76-
protected virtual TValueType FromLowLevel(string value, ValueTags tag, AttributeResolver resolver)
77-
{
78-
throw new NotSupportedException("Unsupported low-level type " + value + " on attribute " + Id);
79-
}
80-
81-
/// <summary>
82-
/// Converts a low-level value to a high-level value
83-
/// </summary>
84-
protected virtual TValueType FromLowLevel(ReadOnlySpan<byte> value, ValueTags tag, AttributeResolver resolver)
85-
{
86-
throw new NotSupportedException("Unsupported low-level type " + tag + " on attribute " + Id);
87-
}
88-
89-
90-
/// <summary>
91-
/// Converts a low-level value to a high-level value
92-
/// </summary>
93-
protected virtual TValueType FromLowLevel(ulong value, ValueTags tags, AttributeResolver resolver)
94-
{
95-
throw new NotSupportedException("Unsupported low-level type " + value + " on attribute " + Id);
96-
}
97-
98-
99-
/// <summary>
100-
/// Converts a low-level value to a high-level value
101-
/// </summary>
102-
protected virtual TValueType FromLowLevel(UInt128 value, ValueTags tags, AttributeResolver resolver)
103-
{
104-
throw new NotSupportedException("Unsupported low-level type " + value + " on attribute " + Id);
105-
}
106-
107-
/// <summary>
108-
/// Converts a low-level value to a high-level value
109-
/// </summary>
110-
protected virtual TValueType FromLowLevel(short value, ValueTags tags, AttributeResolver resolver)
111-
{
112-
throw new NotSupportedException("Unsupported low-level type " + value + " on attribute " + Id);
113-
}
114-
115-
/// <summary>
116-
/// Converts a low-level value to a high-level value
117-
/// </summary>
118-
protected virtual TValueType FromLowLevel(int value, ValueTags tags, AttributeResolver resolver)
119-
{
120-
throw new NotSupportedException("Unsupported low-level type " + value + " on attribute " + Id);
121-
}
122-
123-
/// <summary>
124-
/// Converts a low-level value to a high-level value
125-
/// </summary>
126-
protected virtual TValueType FromLowLevel(long value, ValueTags tags, AttributeResolver resolver)
127-
{
128-
throw new NotSupportedException("Unsupported low-level type " + value + " on attribute " + Id);
129-
}
130-
131-
/// <summary>
132-
/// Converts a low-level value to a high-level value
133-
/// </summary>
134-
protected virtual TValueType FromLowLevel(Int128 value, ValueTags tags, AttributeResolver resolver)
135-
{
136-
throw new NotSupportedException("Unsupported low-level type " + value + " on attribute " + Id);
137-
}
138-
139-
/// <summary>
140-
/// Converts a low-level value to a high-level value
141-
/// </summary>
142-
protected virtual TValueType FromLowLevel(float value, ValueTags tags, AttributeResolver resolver)
143-
{
144-
throw new NotSupportedException("Unsupported low-level type " + value + " on attribute " + Id);
145-
}
146-
42+
14743
/// <summary>
148-
/// Converts a low-level value to a high-level value
44+
/// Converts a high-level value to a low-level value
14945
/// </summary>
150-
protected virtual TValueType FromLowLevel(double value, ValueTags tags, AttributeResolver resolver)
151-
{
152-
throw new NotSupportedException("Unsupported low-level type " + value + " on attribute " + Id);
153-
}
154-
46+
protected abstract TValueType FromLowLevel(TLowLevelType value, AttributeResolver resolver);
47+
15548
/// <inheritdoc />
156-
public ValueTags LowLevelType { get; }
49+
public ValueTag LowLevelType { get; }
15750

15851
/// <inheritdoc />
15952
public Symbol Id { get; }
@@ -174,7 +67,7 @@ protected virtual TValueType FromLowLevel(double value, ValueTags tags, Attribut
17467
public Type ValueType => typeof(TValueType);
17568

17669
/// <inheritdoc />
177-
public bool IsReference => LowLevelType == ValueTags.Reference;
70+
public bool IsReference => LowLevelType == ValueTag.Reference;
17871

17972
/// <inheritdoc />
18073
IReadDatom IAttribute.Resolve(in KeyPrefix prefix, ReadOnlySpan<byte> valueSpan, AttributeResolver resolver)
@@ -198,6 +91,27 @@ public ReadDatom Resolve(in Datom datom, AttributeResolver resolver)
19891
var prefix = datom.Prefix;
19992
return new ReadDatom(in prefix, ReadValue(datom.ValueSpan, datom.Prefix.ValueTag, resolver), this);
20093
}
94+
95+
/// <summary>
96+
/// Reads the high level value from the given span
97+
/// </summary>
98+
public TValueType ReadValue(ReadOnlySpan<byte> span, ValueTag tag, AttributeResolver resolver)
99+
{
100+
return FromLowLevel(tag.Read<TLowLevelType>(span), resolver);
101+
}
102+
103+
/// <summary>
104+
/// Write a datom for this attribute to the given writer
105+
/// </summary>
106+
public void Write<TWriter>(EntityId entityId, AttributeCache cache, TValueType value, TxId txId, bool isRetract, TWriter writer)
107+
where TWriter : IBufferWriter<byte>
108+
{
109+
var prefix = new KeyPrefix(entityId, cache.GetAttributeId(Id), txId, isRetract, LowLevelType);
110+
var span = writer.GetSpan(KeyPrefix.Size);
111+
MemoryMarshal.Write(span, prefix);
112+
writer.Advance(KeyPrefix.Size);
113+
LowLevelType.Write(ToLowLevel(value), writer);
114+
}
201115

202116
/// <summary>
203117
/// Returns true if the attribute is present on the entity
@@ -216,23 +130,7 @@ public bool IsIn<T>(T entity)
216130
{
217131
return entity.IndexSegment.Contains(this);
218132
}
219-
220-
/// <inheritdoc />
221-
public virtual void Remap(Func<EntityId, EntityId> remapper, Span<byte> valueSpan)
222-
{
223-
if (LowLevelType == ValueTags.Reference)
224-
{
225-
var id = MemoryMarshal.Read<EntityId>(valueSpan);
226-
var newId = remapper(id);
227-
MemoryMarshal.Write(valueSpan, newId);
228-
}
229-
}
230-
231-
private void ThrowKeyNotFoundException(EntityId id)
232-
{
233-
throw new KeyNotFoundException($"Attribute {Id} not found on entity {id}");
234-
}
235-
133+
236134
/// <summary>
237135
/// Adds a datom to the active transaction for this entity that adds the given value to this attribute
238136
/// </summary>
@@ -274,6 +172,9 @@ public override string ToString()
274172
/// </summary>
275173
public readonly record struct ReadDatom : IReadDatom
276174
{
175+
/// <summary>
176+
/// The key prefix for this datom, contains the E, A, T, IsRetract and ValueTag values for this datom
177+
/// </summary>
277178
public readonly KeyPrefix Prefix;
278179

279180
/// <summary>

src/NexusMods.MnemonicDB.Abstractions/AttributeCache.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ public void Reset(IDb db)
6464
_symbols = newSymbols;
6565

6666
var types = db.Datoms(AttributeDefinition.ValueType);
67-
var newTypes = new ValueTags[maxIndex];
67+
var newTypes = new ValueTag[maxIndex];
6868
var newIsReference = new BitArray(maxIndex);
6969
foreach (var datom in types)
7070
{
7171
var id = datom.E.Value;
7272
var type = AttributeDefinition.ValueType.ReadValue(datom.ValueSpan, datom.Prefix.ValueTag, null!);
7373
newTypes[id] = type;
74-
newIsReference[(int)id] = type == ValueTags.Reference;
74+
newIsReference[(int)id] = type == ValueTag.Reference;
7575
}
7676
_isReference = newIsReference;
7777

src/NexusMods.MnemonicDB.Abstractions/Attributes/BlobAttribute.cs

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/NexusMods.MnemonicDB.Abstractions/Attributes/CardinalityAttribute.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,11 @@ namespace NexusMods.MnemonicDB.Abstractions.Attributes;
55
/// <summary>
66
/// Used to mark the cardinality of an attribute in the database
77
/// </summary>
8-
public class CardinalityAttribute(string ns, string name) : ScalarAttribute<Cardinality, byte>(ValueTags.UInt8, ns, name)
8+
public sealed class CardinalityAttribute(string ns, string name) : ScalarAttribute<Cardinality, byte>(ValueTag.UInt8, ns, name)
99
{
1010
/// <inheritdoc />
11-
protected override byte ToLowLevel(Cardinality value)
12-
{
13-
return (byte)value;
14-
}
11+
protected override byte ToLowLevel(Cardinality value) => (byte)value;
1512

1613
/// <inheritdoc />
17-
protected override Cardinality FromLowLevel(byte value, ValueTags tags, AttributeResolver resolver)
18-
{
19-
return (Cardinality)value;
20-
}
14+
protected override Cardinality FromLowLevel(byte value, AttributeResolver resolver) => (Cardinality)value;
2115
}

src/NexusMods.MnemonicDB.Abstractions/Attributes/CollectionAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace NexusMods.MnemonicDB.Abstractions.Attributes;
99
/// <summary>
1010
/// An attribute that represents a collection of values
1111
/// </summary>
12-
public abstract class CollectionAttribute<TValue, TLowLevel>(ValueTags tag, string ns, string name)
12+
public abstract class CollectionAttribute<TValue, TLowLevel>(ValueTag tag, string ns, string name)
1313
: Attribute<TValue, TLowLevel>(tag, ns, name, cardinality: Cardinality.Many)
1414
{
1515

src/NexusMods.MnemonicDB.Abstractions/Attributes/HashedBlobAttribute.cs

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/NexusMods.MnemonicDB.Abstractions/Attributes/MarkerAttribute.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ namespace NexusMods.MnemonicDB.Abstractions.Attributes;
99
/// </summary>
1010
/// <param name="ns"></param>
1111
/// <param name="name"></param>
12-
public class MarkerAttribute(string ns, string name) : Attribute<Null, Null>(ValueTags.Null, ns, name)
12+
public class MarkerAttribute(string ns, string name) : Attribute<Null, Null>(ValueTag.Null, ns, name)
1313
{
1414
/// <inheritdoc />
15-
protected override Null ToLowLevel(Null value)
16-
{
17-
return value;
18-
}
15+
protected override Null ToLowLevel(Null value) => value;
16+
17+
/// <inheritdoc />
18+
protected override Null FromLowLevel(Null value, AttributeResolver resolver) => value;
1919

2020
/// <summary>
2121
/// Returns true if the entity contains the attribute.

0 commit comments

Comments
 (0)