Skip to content

Commit d5b9f46

Browse files
committed
Decouple KeyValueEncoders from the rest, introduce IValueEncoding and ITypeSystem to tie everything together
- IValueEncoding add methods to get value encoders - ITypeSystem combines an IKeyEncoding and IValueEncoding - Rename all IKeyEncoding.GetEncoder() to GetKeyEncoder() to not conflict with value encoders - Refactor all sample layers to be more modern in the way they are configured.
1 parent eb00e3c commit d5b9f46

File tree

23 files changed

+434
-354
lines changed

23 files changed

+434
-354
lines changed

FoundationDB.Client/Shared/TypeSystem/Encoders/ICompositeKeyEncoder.cs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
2929
namespace Doxense.Serialization.Encoders
3030
{
3131
using System;
32+
using System.Runtime.CompilerServices;
3233
using Doxense.Memory;
3334

3435
public interface ICompositeKeyEncoder<T1, T2> : IKeyEncoder<(T1, T2)>
@@ -75,4 +76,116 @@ public interface ICompositeKeyEncoder<T1, T2, T3, T4, T5, T6> : IKeyEncoder<(T1,
7576
/// <summary>Read some or all parts of a composite key</summary>
7677
void ReadKeyPartsFrom(ref SliceReader reader, int count, out (T1, T2, T3, T4, T5, T6) items);
7778
}
79+
80+
/// <summary>Wrapper for encoding and decoding a pair with lambda functions</summary>
81+
public abstract class CompositeKeyEncoder<T1, T2> : ICompositeKeyEncoder<T1, T2>
82+
{
83+
84+
public abstract IKeyEncoding Encoding { get; }
85+
86+
public abstract void WriteKeyPartsTo(ref SliceWriter writer, int count, ref (T1, T2) items);
87+
88+
public abstract void ReadKeyPartsFrom(ref SliceReader reader, int count, out (T1, T2) items);
89+
90+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
91+
public void WriteKeyTo(ref SliceWriter writer, (T1, T2) items)
92+
{
93+
WriteKeyPartsTo(ref writer, 2, ref items);
94+
}
95+
96+
public void ReadKeyFrom(ref SliceReader reader, out (T1, T2) items)
97+
{
98+
ReadKeyPartsFrom(ref reader, 2, out items);
99+
}
100+
101+
}
102+
103+
/// <summary>Wrapper for encoding and decoding a triplet with lambda functions</summary>
104+
public abstract class CompositeKeyEncoder<T1, T2, T3> : ICompositeKeyEncoder<T1, T2, T3>
105+
{
106+
107+
public abstract IKeyEncoding Encoding { get; }
108+
109+
public abstract void WriteKeyPartsTo(ref SliceWriter writer, int count, ref (T1, T2, T3) items);
110+
111+
public abstract void ReadKeyPartsFrom(ref SliceReader reader, int count, out (T1, T2, T3) items);
112+
113+
public void WriteKeyTo(ref SliceWriter writer, (T1, T2, T3) items)
114+
{
115+
WriteKeyPartsTo(ref writer, 3, ref items);
116+
}
117+
118+
public void ReadKeyFrom(ref SliceReader reader, out (T1, T2, T3) items)
119+
{
120+
ReadKeyPartsFrom(ref reader, 3, out items);
121+
}
122+
123+
}
124+
125+
/// <summary>Wrapper for encoding and decoding a quad with lambda functions</summary>
126+
public abstract class CompositeKeyEncoder<T1, T2, T3, T4> : ICompositeKeyEncoder<T1, T2, T3, T4>
127+
{
128+
129+
public abstract IKeyEncoding Encoding { get; }
130+
131+
public abstract void WriteKeyPartsTo(ref SliceWriter writer, int count, ref (T1, T2, T3, T4) items);
132+
133+
public abstract void ReadKeyPartsFrom(ref SliceReader reader, int count, out (T1, T2, T3, T4) items);
134+
135+
public void WriteKeyTo(ref SliceWriter writer, (T1, T2, T3, T4) items)
136+
{
137+
WriteKeyPartsTo(ref writer, 4, ref items);
138+
}
139+
140+
public void ReadKeyFrom(ref SliceReader reader, out (T1, T2, T3, T4) items)
141+
{
142+
ReadKeyPartsFrom(ref reader, 4, out items);
143+
}
144+
145+
}
146+
147+
/// <summary>Wrapper for encoding and decoding five items with lambda functions</summary>
148+
public abstract class CompositeKeyEncoder<T1, T2, T3, T4, T5> : ICompositeKeyEncoder<T1, T2, T3, T4, T5>
149+
{
150+
151+
public abstract IKeyEncoding Encoding { get; }
152+
153+
public abstract void WriteKeyPartsTo(ref SliceWriter writer, int count, ref (T1, T2, T3, T4, T5) items);
154+
155+
public abstract void ReadKeyPartsFrom(ref SliceReader reader, int count, out (T1, T2, T3, T4, T5) items);
156+
157+
public void WriteKeyTo(ref SliceWriter writer, (T1, T2, T3, T4, T5) items)
158+
{
159+
WriteKeyPartsTo(ref writer, 5, ref items);
160+
}
161+
162+
public void ReadKeyFrom(ref SliceReader reader, out (T1, T2, T3, T4, T5) items)
163+
{
164+
ReadKeyPartsFrom(ref reader, 5, out items);
165+
}
166+
167+
}
168+
169+
/// <summary>Wrapper for encoding and decoding six items with lambda functions</summary>
170+
public abstract class CompositeKeyEncoder<T1, T2, T3, T4, T5, T6> : ICompositeKeyEncoder<T1, T2, T3, T4, T5, T6>
171+
{
172+
173+
public abstract IKeyEncoding Encoding { get; }
174+
175+
public abstract void WriteKeyPartsTo(ref SliceWriter writer, int count, ref (T1, T2, T3, T4, T5, T6) items);
176+
177+
public abstract void ReadKeyPartsFrom(ref SliceReader reader, int count, out (T1, T2, T3, T4, T5, T6) items);
178+
179+
public void WriteKeyTo(ref SliceWriter writer, (T1, T2, T3, T4, T5, T6) items)
180+
{
181+
WriteKeyPartsTo(ref writer, 6, ref items);
182+
}
183+
184+
public void ReadKeyFrom(ref SliceReader reader, out (T1, T2, T3, T4, T5, T6) items)
185+
{
186+
ReadKeyPartsFrom(ref reader, 6, out items);
187+
}
188+
189+
}
190+
78191
}

FoundationDB.Client/Shared/TypeSystem/Encoders/KeyValueEncoders.Ordered.cs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ namespace Doxense.Serialization.Encoders
3030
{
3131
using JetBrains.Annotations;
3232
using System;
33-
using Doxense.Collections.Tuples;
3433
using Doxense.Diagnostics.Contracts;
3534
using Doxense.Memory;
3635

@@ -85,19 +84,19 @@ public void ReadKeyFrom(ref SliceReader reader, out T key)
8584

8685
#region IKeyEncoding...
8786

88-
IDynamicKeyEncoder IKeyEncoding.GetDynamicEncoder() => throw new NotSupportedException();
87+
IDynamicKeyEncoder IKeyEncoding.GetDynamicKeyEncoder() => throw new NotSupportedException();
8988

90-
IKeyEncoder<T1> IKeyEncoding.GetEncoder<T1>()
89+
IKeyEncoder<T1> IKeyEncoding.GetKeyEncoder<T1>()
9190
{
9291
if (typeof(T1) != typeof(T)) throw new NotSupportedException();
9392
return (IKeyEncoder<T1>) (object) this;
9493
}
9594

96-
ICompositeKeyEncoder<T1, T2> IKeyEncoding.GetEncoder<T1, T2>() => throw new NotSupportedException();
95+
ICompositeKeyEncoder<T1, T2> IKeyEncoding.GetKeyEncoder<T1, T2>() => throw new NotSupportedException();
9796

98-
ICompositeKeyEncoder<T1, T2, T3> IKeyEncoding.GetEncoder<T1, T2, T3>() => throw new NotSupportedException();
97+
ICompositeKeyEncoder<T1, T2, T3> IKeyEncoding.GetKeyEncoder<T1, T2, T3>() => throw new NotSupportedException();
9998

100-
ICompositeKeyEncoder<T1, T2, T3, T4> IKeyEncoding.GetEncoder<T1, T2, T3, T4>() => throw new NotSupportedException();
99+
ICompositeKeyEncoder<T1, T2, T3, T4> IKeyEncoding.GetKeyEncoder<T1, T2, T3, T4>() => throw new NotSupportedException();
101100

102101
#endregion
103102

@@ -134,19 +133,19 @@ public override void ReadKeyPartsFrom(ref SliceReader reader, int count, out (T1
134133

135134
#region IKeyEncoding...
136135

137-
IDynamicKeyEncoder IKeyEncoding.GetDynamicEncoder() => throw new NotSupportedException();
136+
IDynamicKeyEncoder IKeyEncoding.GetDynamicKeyEncoder() => throw new NotSupportedException();
138137

139-
IKeyEncoder<T1B> IKeyEncoding.GetEncoder<T1B>() => throw new NotSupportedException();
138+
IKeyEncoder<T1B> IKeyEncoding.GetKeyEncoder<T1B>() => throw new NotSupportedException();
140139

141-
ICompositeKeyEncoder<T1B, T2B> IKeyEncoding.GetEncoder<T1B, T2B>()
140+
ICompositeKeyEncoder<T1B, T2B> IKeyEncoding.GetKeyEncoder<T1B, T2B>()
142141
{
143142
if (typeof(T1B) != typeof(T1) && typeof(T2B) != typeof(T2)) throw new NotSupportedException();
144143
return (ICompositeKeyEncoder<T1B, T2B>) (object) this;
145144
}
146145

147-
ICompositeKeyEncoder<T1B, T2B, T3> IKeyEncoding.GetEncoder<T1B, T2B, T3>() => throw new NotSupportedException();
146+
ICompositeKeyEncoder<T1B, T2B, T3> IKeyEncoding.GetKeyEncoder<T1B, T2B, T3>() => throw new NotSupportedException();
148147

149-
ICompositeKeyEncoder<T1B, T2B, T3, T4> IKeyEncoding.GetEncoder<T1B, T2B, T3, T4>() => throw new NotSupportedException();
148+
ICompositeKeyEncoder<T1B, T2B, T3, T4> IKeyEncoding.GetKeyEncoder<T1B, T2B, T3, T4>() => throw new NotSupportedException();
150149

151150
#endregion
152151

@@ -187,19 +186,19 @@ public override void ReadKeyPartsFrom(ref SliceReader reader, int count, out (T1
187186

188187
#region IKeyEncoding...
189188

190-
IDynamicKeyEncoder IKeyEncoding.GetDynamicEncoder() => throw new NotSupportedException();
189+
IDynamicKeyEncoder IKeyEncoding.GetDynamicKeyEncoder() => throw new NotSupportedException();
191190

192-
IKeyEncoder<T1B> IKeyEncoding.GetEncoder<T1B>() => throw new NotSupportedException();
191+
IKeyEncoder<T1B> IKeyEncoding.GetKeyEncoder<T1B>() => throw new NotSupportedException();
193192

194-
ICompositeKeyEncoder<T1B, T2B> IKeyEncoding.GetEncoder<T1B, T2B>() => throw new NotSupportedException();
193+
ICompositeKeyEncoder<T1B, T2B> IKeyEncoding.GetKeyEncoder<T1B, T2B>() => throw new NotSupportedException();
195194

196-
ICompositeKeyEncoder<T1B, T2B, T3B> IKeyEncoding.GetEncoder<T1B, T2B, T3B>()
195+
ICompositeKeyEncoder<T1B, T2B, T3B> IKeyEncoding.GetKeyEncoder<T1B, T2B, T3B>()
197196
{
198197
if (typeof(T1B) != typeof(T1) && typeof(T2B) != typeof(T2) && typeof(T3B) != typeof(T3)) throw new NotSupportedException();
199198
return (ICompositeKeyEncoder<T1B, T2B, T3B>) (object) this;
200199
}
201200

202-
ICompositeKeyEncoder<T1B, T2B, T3B, T4> IKeyEncoding.GetEncoder<T1B, T2B, T3B, T4>() => throw new NotSupportedException();
201+
ICompositeKeyEncoder<T1B, T2B, T3B, T4> IKeyEncoding.GetKeyEncoder<T1B, T2B, T3B, T4>() => throw new NotSupportedException();
203202

204203
#endregion
205204
}
@@ -242,15 +241,15 @@ public override void ReadKeyPartsFrom(ref SliceReader reader, int count, out (T1
242241

243242
#region IKeyEncoding...
244243

245-
IDynamicKeyEncoder IKeyEncoding.GetDynamicEncoder() => throw new NotSupportedException();
244+
IDynamicKeyEncoder IKeyEncoding.GetDynamicKeyEncoder() => throw new NotSupportedException();
246245

247-
IKeyEncoder<T1B> IKeyEncoding.GetEncoder<T1B>() => throw new NotSupportedException();
246+
IKeyEncoder<T1B> IKeyEncoding.GetKeyEncoder<T1B>() => throw new NotSupportedException();
248247

249-
ICompositeKeyEncoder<T1B, T2B> IKeyEncoding.GetEncoder<T1B, T2B>() => throw new NotSupportedException();
248+
ICompositeKeyEncoder<T1B, T2B> IKeyEncoding.GetKeyEncoder<T1B, T2B>() => throw new NotSupportedException();
250249

251-
ICompositeKeyEncoder<T1B, T2B, T3B> IKeyEncoding.GetEncoder<T1B, T2B, T3B>() => throw new NotSupportedException();
250+
ICompositeKeyEncoder<T1B, T2B, T3B> IKeyEncoding.GetKeyEncoder<T1B, T2B, T3B>() => throw new NotSupportedException();
252251

253-
ICompositeKeyEncoder<T1B, T2B, T3B, T4B> IKeyEncoding.GetEncoder<T1B, T2B, T3B, T4B>()
252+
ICompositeKeyEncoder<T1B, T2B, T3B, T4B> IKeyEncoding.GetKeyEncoder<T1B, T2B, T3B, T4B>()
254253
{
255254
if (typeof(T1B) != typeof(T1) && typeof(T2B) != typeof(T2) && typeof(T3B) != typeof(T3) && typeof(T4B) != typeof(T4)) throw new NotSupportedException();
256255
return (ICompositeKeyEncoder<T1B, T2B, T3B, T4B>) (object) this;

FoundationDB.Client/Shared/TypeSystem/Encoders/KeyValueEncoders.Tuples.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
2929
namespace Doxense.Serialization.Encoders
3030
{
3131
using System;
32-
using Doxense.Collections.Tuples;
3332
using Doxense.Collections.Tuples.Encoding;
34-
using Doxense.Diagnostics.Contracts;
35-
using Doxense.Memory;
3633
using JetBrains.Annotations;
3734

3835
/// <summary>Helper class for all key/value encoders</summary>
@@ -49,7 +46,7 @@ public static class Tuples
4946
[NotNull]
5047
public static IKeyEncoder<T1> Key<T1>()
5148
{
52-
return TupleEncoder.KeyEncoder<T1>.Default;
49+
return TupleEncoder.Encoder<T1>.Default;
5350
}
5451

5552
[NotNull]
@@ -77,7 +74,7 @@ public static ICompositeKeyEncoder<T1, T2, T3, T4> CompositeKey<T1, T2, T3, T4>(
7774
[NotNull]
7875
public static IValueEncoder<T> Value<T>()
7976
{
80-
return TupleEncoder.KeyEncoder<T>.Default;
77+
return TupleEncoder.Encoder<T>.Default;
8178
}
8279

8380
#endregion

0 commit comments

Comments
 (0)