public void Test_Subspace_Partitioning_With_Binary_Suffix() { // start from a parent subspace var parent = KeySubspace.CreateDynamic(Slice.Empty); Assert.That(parent.GetPrefix().ToString(), Is.EqualTo("<empty>")); // create a child subspace using a tuple var child = parent.Partition[FdbKey.Directory]; Assert.That(child, Is.Not.Null); Assert.That(child.GetPrefix().ToString(), Is.EqualTo("<FE>")); // create a key from this child subspace var key = child[Slice.FromFixed32(0x01020304)]; Assert.That(key.ToString(), Is.EqualTo("<FE><04><03><02><01>")); // create another child var grandChild = child.Partition[Slice.FromStringAscii("hello")]; Assert.That(grandChild, Is.Not.Null); Assert.That(grandChild.GetPrefix().ToString(), Is.EqualTo("<FE>hello")); key = grandChild[Slice.FromFixed32(0x01020304)]; Assert.That(key.ToString(), Is.EqualTo("<FE>hello<04><03><02><01>")); // cornercase Assert.That(child.Partition[Slice.Empty].GetPrefix(), Is.EqualTo(child.GetPrefix())); }
public void Test_Subspace_With_Tuple_Prefix() { var subspace = KeySubspace.CreateDynamic(TuPack.EncodeKey("hello")); Assert.That(subspace.GetPrefix().ToString(), Is.EqualTo("<02>hello<00>")); Assert.That(subspace.Copy(), Is.Not.SameAs(subspace)); Assert.That(subspace.Copy().GetPrefix(), Is.EqualTo(subspace.GetPrefix())); // concat(Slice) should append the slice to the tuple prefix directly Assert.That(subspace[Slice.FromInt32(0x01020304)].ToString(), Is.EqualTo("<02>hello<00><04><03><02><01>")); Assert.That(subspace[Slice.FromStringAscii("world")].ToString(), Is.EqualTo("<02>hello<00>world")); // pack(...) should use tuple serialization Assert.That(subspace.Keys.Encode(123).ToString(), Is.EqualTo("<02>hello<00><15>{")); Assert.That(subspace.Keys.Encode("world").ToString(), Is.EqualTo("<02>hello<00><02>world<00>")); // even though the subspace prefix is a tuple, appending to it will only return the new items var k = subspace.Keys.Pack(("world", 123, false)); Assert.That(k.ToString(), Is.EqualTo("<02>hello<00><02>world<00><15>{<14>")); // if we unpack the key with the binary prefix, we should get a valid tuple var t2 = subspace.Keys.Unpack(k); Assert.That(t2, Is.Not.Null); Assert.That(t2.Count, Is.EqualTo(3)); Assert.That(t2.Get <string>(0), Is.EqualTo("world")); Assert.That(t2.Get <int>(1), Is.EqualTo(123)); Assert.That(t2.Get <bool>(2), Is.False); }
public void Test_DynamicKeySpace_API() { var location = KeySubspace.CreateDynamic(Slice.FromString("PREFIX")); Assert.That(location[Slice.FromString("SUFFIX")].ToString(), Is.EqualTo("PREFIXSUFFIX")); // Encode<T...>(...) Assert.That(location.Keys.Encode("hello").ToString(), Is.EqualTo("PREFIX<02>hello<00>")); Assert.That(location.Keys.Encode("hello", 123).ToString(), Is.EqualTo("PREFIX<02>hello<00><15>{")); Assert.That(location.Keys.Encode("hello", 123, "world").ToString(), Is.EqualTo("PREFIX<02>hello<00><15>{<02>world<00>")); Assert.That(location.Keys.Encode("hello", 123, "world", 456).ToString(), Is.EqualTo("PREFIX<02>hello<00><15>{<02>world<00><16><01><C8>")); Assert.That(location.Keys.Encode("hello", 123, "world", 456, "!").ToString(), Is.EqualTo("PREFIX<02>hello<00><15>{<02>world<00><16><01><C8><02>!<00>")); Assert.That(location.Keys.Encode("hello", 123, "world", 456, "!", 789).ToString(), Is.EqualTo("PREFIX<02>hello<00><15>{<02>world<00><16><01><C8><02>!<00><16><03><15>")); // Pack(ITuple) Assert.That(location.Keys.Pack((IVarTuple)STuple.Create("hello")).ToString(), Is.EqualTo("PREFIX<02>hello<00>")); Assert.That(location.Keys.Pack((IVarTuple)STuple.Create("hello", 123)).ToString(), Is.EqualTo("PREFIX<02>hello<00><15>{")); Assert.That(location.Keys.Pack((IVarTuple)STuple.Create("hello", 123, "world")).ToString(), Is.EqualTo("PREFIX<02>hello<00><15>{<02>world<00>")); Assert.That(location.Keys.Pack((IVarTuple)STuple.Create("hello", 123, "world", 456)).ToString(), Is.EqualTo("PREFIX<02>hello<00><15>{<02>world<00><16><01><C8>")); Assert.That(location.Keys.Pack((IVarTuple)STuple.Create("hello", 123, "world", 456, "!")).ToString(), Is.EqualTo("PREFIX<02>hello<00><15>{<02>world<00><16><01><C8><02>!<00>")); Assert.That(location.Keys.Pack((IVarTuple)STuple.Create("hello", 123, "world", 456, "!", 789)).ToString(), Is.EqualTo("PREFIX<02>hello<00><15>{<02>world<00><16><01><C8><02>!<00><16><03><15>")); // Pack(ValueTuple) Assert.That(location.Keys.Pack(ValueTuple.Create("hello")).ToString(), Is.EqualTo("PREFIX<02>hello<00>")); Assert.That(location.Keys.Pack(("hello", 123)).ToString(), Is.EqualTo("PREFIX<02>hello<00><15>{")); Assert.That(location.Keys.Pack(("hello", 123, "world")).ToString(), Is.EqualTo("PREFIX<02>hello<00><15>{<02>world<00>")); Assert.That(location.Keys.Pack(("hello", 123, "world", 456)).ToString(), Is.EqualTo("PREFIX<02>hello<00><15>{<02>world<00><16><01><C8>")); Assert.That(location.Keys.Pack(("hello", 123, "world", 456, "!")).ToString(), Is.EqualTo("PREFIX<02>hello<00><15>{<02>world<00><16><01><C8><02>!<00>")); Assert.That(location.Keys.Pack(("hello", 123, "world", 456, "!", 789)).ToString(), Is.EqualTo("PREFIX<02>hello<00><15>{<02>world<00><16><01><C8><02>!<00><16><03><15>")); // ITuple Unpack(Slice) Assert.That(location.Keys.Unpack(Slice.Unescape("PREFIX<02>hello<00>")), Is.EqualTo(STuple.Create("hello"))); Assert.That(location.Keys.Unpack(Slice.Unescape("PREFIX<02>hello<00><15>{")), Is.EqualTo(STuple.Create("hello", 123))); Assert.That(location.Keys.Unpack(Slice.Unescape("PREFIX<02>hello<00><15>{<02>world<00>")), Is.EqualTo(STuple.Create("hello", 123, "world"))); Assert.That(location.Keys.Unpack(Slice.Unescape("PREFIX<02>hello<00><15>{<02>world<00><16><01><C8>")), Is.EqualTo(STuple.Create("hello", 123, "world", 456))); Assert.That(location.Keys.Unpack(Slice.Unescape("PREFIX<02>hello<00><15>{<02>world<00><16><01><C8><02>!<00>")), Is.EqualTo(STuple.Create("hello", 123, "world", 456, "!"))); Assert.That(location.Keys.Unpack(Slice.Unescape("PREFIX<02>hello<00><15>{<02>world<00><16><01><C8><02>!<00><16><03><15>")), Is.EqualTo(STuple.Create("hello", 123, "world", 456, "!", 789))); // STuple<T...> Decode(Slice) Assert.That(location.Keys.Decode <string>(Slice.Unescape("PREFIX<02>hello<00>")), Is.EqualTo("hello")); Assert.That(location.Keys.Decode <string, int>(Slice.Unescape("PREFIX<02>hello<00><15>{")), Is.EqualTo(("hello", 123))); Assert.That(location.Keys.Decode <string, int, string>(Slice.Unescape("PREFIX<02>hello<00><15>{<02>world<00>")), Is.EqualTo(("hello", 123, "world"))); Assert.That(location.Keys.Decode <string, int, string, int>(Slice.Unescape("PREFIX<02>hello<00><15>{<02>world<00><16><01><C8>")), Is.EqualTo(("hello", 123, "world", 456))); Assert.That(location.Keys.Decode <string, int, string, int, string>(Slice.Unescape("PREFIX<02>hello<00><15>{<02>world<00><16><01><C8><02>!<00>")), Is.EqualTo(("hello", 123, "world", 456, "!"))); Assert.That(location.Keys.Decode <string, int, string, int, string, int>(Slice.Unescape("PREFIX<02>hello<00><15>{<02>world<00><16><01><C8><02>!<00><16><03><15>")), Is.EqualTo(("hello", 123, "world", 456, "!", 789))); // DecodeFirst/DecodeLast Assert.That(location.Keys.DecodeFirst <string>(Slice.Unescape("PREFIX<02>hello<00><15>{<02>world<00><16><01><C8><02>!<00><16><03><15>")), Is.EqualTo("hello")); Assert.That(location.Keys.DecodeLast <int>(Slice.Unescape("PREFIX<02>hello<00><15>{<02>world<00><16><01><C8><02>!<00><16><03><15>")), Is.EqualTo(789)); }
public void Test_Subspace_With_Binary_Prefix() { var subspace = KeySubspace.CreateDynamic(new byte[] { 42, 255, 0, 127 }.AsSlice()); Assert.That(subspace.GetPrefix().ToString(), Is.EqualTo("*<FF><00><7F>")); Assert.That(subspace.Copy(), Is.Not.SameAs(subspace)); Assert.That(subspace.Copy().GetPrefix(), Is.EqualTo(subspace.GetPrefix())); // concat(Slice) should append the slice to the binary prefix directly Assert.That(subspace[Slice.FromInt32(0x01020304)].ToString(), Is.EqualTo("*<FF><00><7F><04><03><02><01>")); Assert.That(subspace[Slice.FromStringAscii("hello")].ToString(), Is.EqualTo("*<FF><00><7F>hello")); // pack(...) should use tuple serialization Assert.That(subspace.Keys.Encode(123).ToString(), Is.EqualTo("*<FF><00><7F><15>{")); Assert.That(subspace.Keys.Encode("hello").ToString(), Is.EqualTo("*<FF><00><7F><02>hello<00>")); Assert.That(subspace.Keys.Encode(Slice.FromStringAscii("world")).ToString(), Is.EqualTo("*<FF><00><7F><01>world<00>")); Assert.That(subspace.Keys.Pack(STuple.Create("hello", 123)).ToString(), Is.EqualTo("*<FF><00><7F><02>hello<00><15>{")); Assert.That(subspace.Keys.Pack(("hello", 123)).ToString(), Is.EqualTo("*<FF><00><7F><02>hello<00><15>{")); // if we encode a tuple from this subspace, it should keep the binary prefix when converted to a key var k = subspace.Keys.Pack(("world", 123, false)); Assert.That(k.ToString(), Is.EqualTo("*<FF><00><7F><02>world<00><15>{<14>")); // if we unpack the key with the binary prefix, we should get a valid tuple var t2 = subspace.Keys.Unpack(k); Assert.That(t2, Is.Not.Null); Assert.That(t2.Count, Is.EqualTo(3)); Assert.That(t2.Get <string>(0), Is.EqualTo("world")); Assert.That(t2.Get <int>(1), Is.EqualTo(123)); Assert.That(t2.Get <bool>(2), Is.False); // ValueTuple Assert.That(subspace.Keys.Pack(ValueTuple.Create("hello")).ToString(), Is.EqualTo("*<FF><00><7F><02>hello<00>")); Assert.That(subspace.Keys.Pack(("hello", 123)).ToString(), Is.EqualTo("*<FF><00><7F><02>hello<00><15>{")); Assert.That(subspace.Keys.Pack(("hello", 123, "world")).ToString(), Is.EqualTo("*<FF><00><7F><02>hello<00><15>{<02>world<00>")); Assert.That(subspace.Keys.Pack(("hello", 123, "world", 456)).ToString(), Is.EqualTo("*<FF><00><7F><02>hello<00><15>{<02>world<00><16><01><C8>")); }