Skip to content

Commit 8192bb7

Browse files
committed
Add bascic support for VersionStamps to Tuple Encoder
- Support both 80-bits and 96-bits variants - BUGBUG: cannot recognized complete/incomplete stamps yet when parsing.
1 parent 2b6737c commit 8192bb7

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

FoundationDB.Client/Layers/Tuples/Encoding/TuplePackers.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ namespace Doxense.Collections.Tuples.Encoding
4040
using Doxense.Collections.Tuples;
4141
using Doxense.Diagnostics.Contracts;
4242
using Doxense.Runtime.Converters;
43+
using FoundationDB.Client;
4344
using JetBrains.Annotations;
4445

4546
/// <summary>Helper methods used during serialization of values to the tuple binary format</summary>
@@ -553,6 +554,11 @@ public static void SerializeTo(ref TupleWriter writer, Uuid64 value)
553554
TupleParser.WriteUuid64(ref writer, value);
554555
}
555556

557+
public static void SerializeTo(ref TupleWriter writer, VersionStamp value)
558+
{
559+
TupleParser.WriteVersionStamp(ref writer, value);
560+
}
561+
556562
/// <summary>Writes an IPaddress as a 32-bit (IPv4) or 128-bit (IPv6) byte array</summary>
557563
public static void SerializeTo(ref TupleWriter writer, System.Net.IPAddress value)
558564
{
@@ -680,6 +686,7 @@ private static Dictionary<Type, Delegate> InitializeDefaultUnpackers()
680686
[typeof(TimeSpan)] = new Func<Slice, TimeSpan>(TuplePackers.DeserializeTimeSpan),
681687
[typeof(DateTime)] = new Func<Slice, DateTime>(TuplePackers.DeserializeDateTime),
682688
[typeof(System.Net.IPAddress)] = new Func<Slice, System.Net.IPAddress>(TuplePackers.DeserializeIPAddress),
689+
[typeof(VersionStamp)] = new Func<Slice, VersionStamp>(TuplePackers.DeserializeVersionStamp),
683690
[typeof(ITuple)] = new Func<Slice, ITuple>(TuplePackers.DeserializeTuple),
684691
};
685692

@@ -1694,6 +1701,24 @@ public static Uuid64 DeserializeUuid64(Slice slice)
16941701
throw new FormatException($"Cannot convert tuple segment of type 0x{type:X} into an Uuid64");
16951702
}
16961703

1704+
public static VersionStamp DeserializeVersionStamp(Slice slice)
1705+
{
1706+
if (slice.IsNullOrEmpty) return default(VersionStamp);
1707+
1708+
int type = slice[0];
1709+
1710+
if (type == TupleTypes.VersionStamp80 || type == TupleTypes.VersionStamp96)
1711+
{
1712+
if (VersionStamp.TryParse(slice.Substring(1), out var stamp))
1713+
{
1714+
return stamp;
1715+
}
1716+
throw new FormatException("Cannot convert malformed tuple segment into a VersionStamp");
1717+
}
1718+
1719+
throw new FormatException($"Cannot convert tuple segment of type 0x{type:X} into a VersionStamp");
1720+
}
1721+
16971722
/// <summary>Deserialize a tuple segment into Guid</summary>
16981723
/// <param name="slice">Slice that contains a single packed element</param>
16991724
[CanBeNull]

FoundationDB.Client/Layers/Tuples/Encoding/TupleParser.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace Doxense.Collections.Tuples.Encoding
3434
using Doxense.Collections.Tuples;
3535
using Doxense.Diagnostics.Contracts;
3636
using Doxense.Memory;
37+
using FoundationDB.Client;
3738
using JetBrains.Annotations;
3839

3940
/// <summary>Helper class that contains low-level encoders for the tuple binary format</summary>
@@ -758,6 +759,28 @@ public static void WriteUuid64(ref TupleWriter writer, Uuid64? value)
758759
if (!value.HasValue) WriteNil(ref writer); else WriteUuid64(ref writer, value.Value);
759760
}
760761

762+
public static void WriteVersionStamp(ref TupleWriter writer, VersionStamp value)
763+
{
764+
if (value.HasUserVersion)
765+
{ // 96-bits Versionstamp
766+
writer.Output.EnsureBytes(13);
767+
writer.Output.UnsafeWriteByte(TupleTypes.VersionStamp96);
768+
value.WriteTo(writer.Output.Allocate(12));
769+
}
770+
else
771+
{ // 80-bits Versionstamp
772+
writer.Output.EnsureBytes(11);
773+
writer.Output.UnsafeWriteByte(TupleTypes.VersionStamp80);
774+
value.WriteTo(writer.Output.Allocate(10));
775+
}
776+
}
777+
778+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
779+
public static void WriteVersionStamp(ref TupleWriter writer, VersionStamp? value)
780+
{
781+
if (!value.HasValue) WriteNil(ref writer); else WriteVersionStamp(ref writer, value.Value);
782+
}
783+
761784
/// <summary>Mark the start of a new embedded tuple</summary>
762785
public static void BeginTuple(ref TupleWriter writer)
763786
{
@@ -1096,6 +1119,16 @@ public static Slice ParseNext(ref TupleReader reader)
10961119
return reader.Input.ReadBytes(9);
10971120
}
10981121

1122+
case TupleTypes.VersionStamp80:
1123+
{ // <32>(10 bytes)
1124+
return reader.Input.ReadBytes(11);
1125+
}
1126+
1127+
case TupleTypes.VersionStamp96:
1128+
{ // <33>(12 bytes)
1129+
return reader.Input.ReadBytes(13);
1130+
}
1131+
10991132
case TupleTypes.AliasDirectory:
11001133
case TupleTypes.AliasSystem:
11011134
{ // <FE> or <FF>

FoundationDB.Client/Layers/Tuples/Encoding/TupleTypes.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ internal static class TupleTypes
8686
/// <summary>UUID (64 bits) [DRAFT]</summary>
8787
internal const byte Uuid64 = 49; //TODO: this is not official yet! may change!
8888

89+
//TODO: xmldoc
90+
internal const byte VersionStamp80 = 0x32;
91+
//TODO: xmldoc
92+
internal const byte VersionStamp96 = 0x33;
93+
8994
/// <summary>Standard prefix of the Directory Layer</summary>
9095
/// <remarks>This is not a part of the tuple encoding itself, but helps the tuple decoder pretty-print tuples that would otherwise be unparsable.</remarks>
9196
internal const byte AliasDirectory = 254;
@@ -112,6 +117,8 @@ public static TupleSegmentType DecodeSegmentType(Slice segment)
112117
case Decimal: return TupleSegmentType.Decimal;
113118
case Uuid128: return TupleSegmentType.Uuid128;
114119
case Uuid64: return TupleSegmentType.Uuid64;
120+
case VersionStamp80: return TupleSegmentType.VersionStamp80;
121+
case VersionStamp96: return TupleSegmentType.VersionStamp96;
115122
}
116123

117124
if (type <= IntPos8 && type >= IntNeg8)
@@ -138,6 +145,8 @@ public enum TupleSegmentType
138145
Decimal = 35,
139146
Uuid128 = 48,
140147
Uuid64 = 49,
148+
VersionStamp80 = 0x32,
149+
VersionStamp96 = 0x33,
141150
}
142151

143152
}

0 commit comments

Comments
 (0)