IGNITE-4669 .NET: Sort binary object fields This closes #1686
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/dff77389 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/dff77389 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/dff77389 Branch: refs/heads/ignite-1561-1 Commit: dff77389d8864ce66e176d828d72d209ef401820 Parents: 2a2e719 Author: Pavel Tupitsyn <[email protected]> Authored: Mon Apr 17 17:34:36 2017 +0300 Committer: Pavel Tupitsyn <[email protected]> Committed: Mon Apr 17 17:34:36 2017 +0300 ---------------------------------------------------------------------- .../Binary/BinaryBuilderSelfTest.cs | 32 ++++++++++++++++++-- .../Binary/BinarySelfTest.cs | 20 ++++++------ .../Cache/Query/CacheDmlQueriesTest.cs | 4 +++ .../Impl/Binary/BinaryObjectBuilder.cs | 4 +-- 4 files changed, 45 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/dff77389/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/BinaryBuilderSelfTest.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/BinaryBuilderSelfTest.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/BinaryBuilderSelfTest.cs index 63faaec..c104e15 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/BinaryBuilderSelfTest.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/BinaryBuilderSelfTest.cs @@ -601,10 +601,10 @@ namespace Apache.Ignite.Core.Tests.Binary Assert.AreEqual(obj1, obj2); - Assert.AreEqual(1823354401, obj1.GetHashCode()); - Assert.AreEqual(1823354401, obj2.GetHashCode()); + Assert.AreEqual(-88648479, obj1.GetHashCode()); + Assert.AreEqual(obj1.GetHashCode(), obj2.GetHashCode()); - Assert.IsTrue(Regex.IsMatch(obj1.ToString(), @"myType \[idHash=[0-9]+, str=foo, int=1\]")); + Assert.AreEqual("myType [, int=1, str=foo]", Regex.Replace(obj1.ToString(), "idHash=\\d+", "")); } /// <summary> @@ -1711,6 +1711,32 @@ namespace Apache.Ignite.Core.Tests.Binary Assert.AreEqual(3, cache2[6].GetField<int>("foo2")); } } + + /// <summary> + /// Tests that fields are sorted by name in serialized form. + /// </summary> + [Test] + public void TestFieldSorting() + { + var obj1 = (BinaryObject)_grid.GetBinary().GetBuilder("sortTest") + .SetByteField("c", 3).SetByteField("b", 1).SetByteField("a", 2).Build(); + + var obj2 = (BinaryObject)_grid.GetBinary().GetBuilder("sortTest") + .SetByteField("b", 1).SetByteField("a", 2).SetByteField("c", 3).Build(); + + Assert.AreEqual(obj1, obj2); + Assert.AreEqual(obj1.GetHashCode(), obj2.GetHashCode()); + + Assert.AreEqual("sortTest [, a=2, b=1, c=3]", Regex.Replace(obj1.ToString(), "idHash=\\d+", "")); + + // Skip header, take 3 fields (type code + value). + var bytes1 = obj1.Data.Skip(24).Take(6).ToArray(); + var bytes2 = obj2.Data.Skip(24).Take(6).ToArray(); + + Assert.AreEqual(bytes1, bytes2); + + Assert.AreEqual(new[] {1, 2, 1, 1, 1, 3}, bytes1); + } } /// <summary> http://git-wip-us.apache.org/repos/asf/ignite/blob/dff77389/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/BinarySelfTest.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/BinarySelfTest.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/BinarySelfTest.cs index 06fbe48..bde8166 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/BinarySelfTest.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/BinarySelfTest.cs @@ -2392,21 +2392,21 @@ namespace Apache.Ignite.Core.Tests.Binary public void WriteBinary(IBinaryWriter writer) { - writer.WriteCollection("col", Collection); - writer.WriteDictionary("dict", Dictionary); - writer.WriteObject("dictEntry", DictionaryEntry); - writer.WriteObject("obj", Object); - writer.WriteArray("arr", Array); + writer.WriteCollection("1col", Collection); + writer.WriteDictionary("2dict", Dictionary); + writer.WriteObject("3dictEntry", DictionaryEntry); + writer.WriteObject("4obj", Object); + writer.WriteArray("5arr", Array); writer.GetRawWriter().WriteCollection(CollectionRaw); } public void ReadBinary(IBinaryReader reader) { - Collection = reader.ReadCollection("col"); - Dictionary = reader.ReadDictionary("dict"); - DictionaryEntry = reader.ReadObject<DictionaryEntry>("dictEntry"); - Object = reader.ReadObject<object>("obj"); - Array = reader.ReadArray<object>("arr"); + Collection = reader.ReadCollection("1col"); + Dictionary = reader.ReadDictionary("2dict"); + DictionaryEntry = reader.ReadObject<DictionaryEntry>("3dictEntry"); + Object = reader.ReadObject<object>("4obj"); + Array = reader.ReadArray<object>("5arr"); CollectionRaw = reader.GetRawReader().ReadCollection(); } } http://git-wip-us.apache.org/repos/asf/ignite/blob/dff77389/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Cache/Query/CacheDmlQueriesTest.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Cache/Query/CacheDmlQueriesTest.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Cache/Query/CacheDmlQueriesTest.cs index d687894..6afeff4 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Cache/Query/CacheDmlQueriesTest.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Cache/Query/CacheDmlQueriesTest.cs @@ -127,11 +127,15 @@ namespace Apache.Ignite.Core.Tests.Cache.Query Assert.IsTrue(cache.ContainsKey(foos[0].Key)); Assert.IsTrue(binCache.ContainsKey( binary.GetBuilder(typeof(Key)).SetField("hi", 1).SetField("lo", 2).Build())); + Assert.IsTrue(binCache.ContainsKey( // Fields are sorted. + binary.GetBuilder(typeof(Key)).SetField("lo", 2).SetField("hi", 1).Build())); Assert.IsTrue(cache.ContainsKey(new Key(5, 4))); Assert.IsTrue(cache.ContainsKey(foos[1].Key)); Assert.IsTrue(binCache.ContainsKey( binary.GetBuilder(typeof(Key)).SetField("hi", 4).SetField("lo", 5).Build())); + Assert.IsTrue(binCache.ContainsKey( // Fields are sorted. + binary.GetBuilder(typeof(Key)).SetField("lo", 5).SetField("hi", 4).Build())); } /// <summary> http://git-wip-us.apache.org/repos/asf/ignite/blob/dff77389/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryObjectBuilder.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryObjectBuilder.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryObjectBuilder.cs index 41c7305..91fe12a 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryObjectBuilder.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryObjectBuilder.cs @@ -49,7 +49,7 @@ namespace Apache.Ignite.Core.Impl.Binary private readonly IBinaryTypeDescriptor _desc; /** Values. */ - private IDictionary<string, BinaryBuilderField> _vals; + private SortedDictionary<string, BinaryBuilderField> _vals; /** Contextual fields. */ private IDictionary<int, BinaryBuilderField> _cache; @@ -476,7 +476,7 @@ namespace Apache.Ignite.Core.Impl.Binary private IBinaryObjectBuilder SetField0(string fieldName, BinaryBuilderField val) { if (_vals == null) - _vals = new Dictionary<string, BinaryBuilderField>(); + _vals = new SortedDictionary<string, BinaryBuilderField>(); _vals[fieldName] = val;
