@@ -18,15 +18,10 @@ namespace NexusMods.MnemonicDB.Abstractions;
18
18
/// Interface for a specific attribute
19
19
/// </summary>
20
20
/// <typeparam name="TValueType"></typeparam>
21
- public abstract partial class Attribute < TValueType , TLowLevelType > : IAttribute < TValueType >
21
+ public abstract class Attribute < TValueType , TLowLevelType > : IAttribute < TValueType >
22
22
{
23
- private const int MaxStackAlloc = 128 ;
24
- private static Encoding AsciiEncoding = Encoding . ASCII ;
25
-
26
- private static Encoding Utf8Encoding = Encoding . UTF8 ;
27
-
28
23
protected Attribute (
29
- ValueTags lowLevelType ,
24
+ ValueTag lowLevelType ,
30
25
string ns ,
31
26
string name ,
32
27
bool isIndexed = false ,
@@ -44,116 +39,14 @@ protected Attribute(
44
39
/// Converts a high-level value to a low-level value
45
40
/// </summary>
46
41
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
+
147
43
/// <summary>
148
- /// Converts a low -level value to a high -level value
44
+ /// Converts a high -level value to a low -level value
149
45
/// </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
+
155
48
/// <inheritdoc />
156
- public ValueTags LowLevelType { get ; }
49
+ public ValueTag LowLevelType { get ; }
157
50
158
51
/// <inheritdoc />
159
52
public Symbol Id { get ; }
@@ -174,7 +67,7 @@ protected virtual TValueType FromLowLevel(double value, ValueTags tags, Attribut
174
67
public Type ValueType => typeof ( TValueType ) ;
175
68
176
69
/// <inheritdoc />
177
- public bool IsReference => LowLevelType == ValueTags . Reference ;
70
+ public bool IsReference => LowLevelType == ValueTag . Reference ;
178
71
179
72
/// <inheritdoc />
180
73
IReadDatom IAttribute . Resolve ( in KeyPrefix prefix , ReadOnlySpan < byte > valueSpan , AttributeResolver resolver )
@@ -198,6 +91,27 @@ public ReadDatom Resolve(in Datom datom, AttributeResolver resolver)
198
91
var prefix = datom . Prefix ;
199
92
return new ReadDatom ( in prefix , ReadValue ( datom . ValueSpan , datom . Prefix . ValueTag , resolver ) , this ) ;
200
93
}
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
+ }
201
115
202
116
/// <summary>
203
117
/// Returns true if the attribute is present on the entity
@@ -216,23 +130,7 @@ public bool IsIn<T>(T entity)
216
130
{
217
131
return entity . IndexSegment . Contains ( this ) ;
218
132
}
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
+
236
134
/// <summary>
237
135
/// Adds a datom to the active transaction for this entity that adds the given value to this attribute
238
136
/// </summary>
@@ -274,6 +172,9 @@ public override string ToString()
274
172
/// </summary>
275
173
public readonly record struct ReadDatom : IReadDatom
276
174
{
175
+ /// <summary>
176
+ /// The key prefix for this datom, contains the E, A, T, IsRetract and ValueTag values for this datom
177
+ /// </summary>
277
178
public readonly KeyPrefix Prefix ;
278
179
279
180
/// <summary>
0 commit comments