IGNITE-6768 .NET: Thin client: Fix cache id calculation Do not force lower case
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/717c5492 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/717c5492 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/717c5492 Branch: refs/heads/ignite-6748 Commit: 717c549248eb377dd0dc7b28c8707d2e496c5a4e Parents: e228ce3 Author: Pavel Tupitsyn <[email protected]> Authored: Fri Oct 27 12:56:37 2017 +0300 Committer: Pavel Tupitsyn <[email protected]> Committed: Fri Oct 27 12:56:37 2017 +0300 ---------------------------------------------------------------------- .../Binary/BinaryBuilderSelfTest.cs | 10 ++--- .../Binary/EnumsTest.cs | 2 +- .../Client/Cache/CacheTest.cs | 42 ++++++++++++++++++++ .../Client/RawSocketTest.cs | 2 +- .../Impl/Binary/BinaryUtils.cs | 30 +++++++++++--- .../Impl/Binary/Marshaller.cs | 4 +- .../Impl/Binary/SerializableSerializer.cs | 18 ++++----- 7 files changed, 85 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/717c5492/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 61f90a3..5837ab1 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/BinaryBuilderSelfTest.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/BinaryBuilderSelfTest.cs @@ -1600,7 +1600,7 @@ namespace Apache.Ignite.Core.Tests.Binary Assert.AreEqual(IdMapper.TestTypeId, _grid.GetBinary().GetTypeId(IdMapper.TestTypeName)); - Assert.AreEqual(BinaryUtils.GetStringHashCode("someTypeName"), _grid.GetBinary().GetTypeId("someTypeName")); + Assert.AreEqual(BinaryUtils.GetStringHashCodeLowerCase("someTypeName"), _grid.GetBinary().GetTypeId("someTypeName")); } /// <summary> @@ -1615,7 +1615,7 @@ namespace Apache.Ignite.Core.Tests.Binary var binType = bin.GetBinaryType(); - Assert.AreEqual(BinaryUtils.GetStringHashCode(NameMapper.TestTypeName + "_"), binType.TypeId); + Assert.AreEqual(BinaryUtils.GetStringHashCodeLowerCase(NameMapper.TestTypeName + "_"), binType.TypeId); Assert.AreEqual(17, bin.GetField<int>(NameMapper.TestFieldName)); } @@ -1666,7 +1666,7 @@ namespace Apache.Ignite.Core.Tests.Binary var enumVal = TestEnumRegistered.Two; var intVal = (int) enumVal; var typeName = GetTypeName(typeof(TestEnumRegistered)); - var typeId = BinaryUtils.GetStringHashCode(typeName); + var typeId = BinaryUtils.GetStringHashCodeLowerCase(typeName); var binEnums = new[] { @@ -2170,13 +2170,13 @@ namespace Apache.Ignite.Core.Tests.Binary /** <inheritdoc /> */ public int GetTypeId(string typeName) { - return typeName == TestTypeName ? TestTypeId : BinaryUtils.GetStringHashCode(typeName); + return typeName == TestTypeName ? TestTypeId : BinaryUtils.GetStringHashCodeLowerCase(typeName); } /** <inheritdoc /> */ public int GetFieldId(int typeId, string fieldName) { - return BinaryUtils.GetStringHashCode(fieldName); + return BinaryUtils.GetStringHashCodeLowerCase(fieldName); } } http://git-wip-us.apache.org/repos/asf/ignite/blob/717c5492/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/EnumsTest.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/EnumsTest.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/EnumsTest.cs index 18ef29a..8993fb4 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/EnumsTest.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/EnumsTest.cs @@ -88,7 +88,7 @@ namespace Apache.Ignite.Core.Tests.Binary else { Assert.AreEqual(string.Format("BinaryEnum [typeId={0}, enumValue={1}]", - BinaryUtils.GetStringHashCode(typeof(T).FullName), binRes.EnumValue), binRes.ToString()); + BinaryUtils.GetStringHashCodeLowerCase(typeof(T).FullName), binRes.EnumValue), binRes.ToString()); } } else http://git-wip-us.apache.org/repos/asf/ignite/blob/717c5492/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/Cache/CacheTest.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/Cache/CacheTest.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/Cache/CacheTest.cs index f2dd1de..cfdce73 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/Cache/CacheTest.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/Cache/CacheTest.cs @@ -845,6 +845,48 @@ namespace Apache.Ignite.Core.Tests.Client.Cache } } + /// <summary> + /// Tests various cache names. + /// Cache id as calculated as a hash code and passed to the server side; this test verifies correct id + /// calculation for different strings. + /// </summary> + [Test] + public void TestCacheNames() + { + var cacheNames = new[] + { + "foo-bar", + "Foo-Bar", + "FOO-BAR", + "testCache1", + "TestCache2", + "TESTCACHE3", + new string('c', 100), + new string('C', 100), + Guid.NewGuid().ToString(), + "ÑеÑÑ", + "ТеÑÑ", + "ТÐСТ", + "ÑеÑÑ1", + "ТеÑÑ2", + "ТÐСТ3" + }; + + var ignite = Ignition.GetIgnite(); + + for (var i = 0; i < cacheNames.Length; i++) + { + var cacheName = cacheNames[i]; + ignite.CreateCache<int, string>(cacheName).Put(i, cacheName); + + using (var client = GetClient()) + { + var cache = client.GetCache<int, string>(cacheName); + Assert.AreEqual(cacheName, cache[i]); + } + } + } + private class Container { public Container Inner; http://git-wip-us.apache.org/repos/asf/ignite/blob/717c5492/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/RawSocketTest.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/RawSocketTest.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/RawSocketTest.cs index b637e88..0f1358a 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/RawSocketTest.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/RawSocketTest.cs @@ -54,7 +54,7 @@ namespace Apache.Ignite.Core.Tests.Client { stream.WriteShort(1); // OP_GET stream.WriteLong(1); // Request id. - var cacheId = BinaryUtils.GetStringHashCode(cache.Name); + var cacheId = BinaryUtils.GetStringHashCodeLowerCase(cache.Name); stream.WriteInt(cacheId); stream.WriteByte(0); // Flags (withSkipStore, etc) http://git-wip-us.apache.org/repos/asf/ignite/blob/717c5492/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryUtils.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryUtils.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryUtils.cs index 5233db8..20fea02 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryUtils.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryUtils.cs @@ -23,12 +23,10 @@ namespace Apache.Ignite.Core.Impl.Binary using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.IO; - using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using Apache.Ignite.Core.Binary; - using Apache.Ignite.Core.Cache.Affinity; using Apache.Ignite.Core.Impl.Binary.IO; using Apache.Ignite.Core.Impl.Common; @@ -1037,6 +1035,8 @@ namespace Apache.Ignite.Core.Impl.Binary { var elemType = val.GetType().GetElementType(); + Debug.Assert(elemType != null); + var typeId = ObjTypeId; if (elemType != typeof(object)) @@ -1333,9 +1333,9 @@ namespace Apache.Ignite.Core.Impl.Binary } /// <summary> - /// Gets the string hash code using Java algorithm. + /// Gets the string hash code using Java algorithm, converting English letters to lower case. /// </summary> - public static int GetStringHashCode(string val) + public static int GetStringHashCodeLowerCase(string val) { if (val == null) return 0; @@ -1353,6 +1353,26 @@ namespace Apache.Ignite.Core.Impl.Binary } /// <summary> + /// Gets the string hash code using Java algorithm. + /// </summary> + private static int GetStringHashCode(string val) + { + if (val == null) + return 0; + + int hash = 0; + + unchecked + { + // ReSharper disable once LoopCanBeConvertedToQuery (performance) + foreach (var c in val) + hash = 31 * hash + c; + } + + return hash; + } + + /// <summary> /// Gets the cache identifier. /// </summary> public static int GetCacheId(string cacheName) @@ -1447,7 +1467,7 @@ namespace Apache.Ignite.Core.Impl.Binary } if (id == 0) - id = GetStringHashCode(fieldName); + id = GetStringHashCodeLowerCase(fieldName); if (id == 0) throw new BinaryObjectException("Field ID is zero (please provide ID mapper or change field name) " + http://git-wip-us.apache.org/repos/asf/ignite/blob/717c5492/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/Marshaller.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/Marshaller.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/Marshaller.cs index 55b6121..7212cd6 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/Marshaller.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/Marshaller.cs @@ -694,7 +694,7 @@ namespace Apache.Ignite.Core.Impl.Binary if (typeId == 0) { - typeId = BinaryUtils.GetStringHashCode(typeName); + typeId = BinaryUtils.GetStringHashCodeLowerCase(typeName); } AddType(type, typeId, typeName, false, false, null, null, serializer, affKeyFldName, false); @@ -826,7 +826,7 @@ namespace Apache.Ignite.Core.Impl.Binary if (id == 0) { - id = BinaryUtils.GetStringHashCode(typeName); + id = BinaryUtils.GetStringHashCodeLowerCase(typeName); } return id; http://git-wip-us.apache.org/repos/asf/ignite/blob/717c5492/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/SerializableSerializer.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/SerializableSerializer.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/SerializableSerializer.cs index 80f267a..fc91edb 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/SerializableSerializer.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/SerializableSerializer.cs @@ -174,7 +174,7 @@ namespace Apache.Ignite.Core.Impl.Binary foreach (var dotNetField in dotNetFields) { - writer.WriteInt(BinaryUtils.GetStringHashCode(dotNetField)); + writer.WriteInt(BinaryUtils.GetStringHashCodeLowerCase(dotNetField)); } } @@ -617,49 +617,49 @@ namespace Apache.Ignite.Core.Impl.Binary { if (fieldType == typeof(byte)) { - return dotNetFields.Contains(BinaryUtils.GetStringHashCode(fieldName)) + return dotNetFields.Contains(BinaryUtils.GetStringHashCodeLowerCase(fieldName)) ? (sbyte) (byte) fieldVal : fieldVal; } if (fieldType == typeof(short)) { - return dotNetFields.Contains(BinaryUtils.GetStringHashCode(fieldName)) + return dotNetFields.Contains(BinaryUtils.GetStringHashCodeLowerCase(fieldName)) ? (ushort) (short) fieldVal : fieldVal; } if (fieldType == typeof(int)) { - return dotNetFields.Contains(BinaryUtils.GetStringHashCode(fieldName)) + return dotNetFields.Contains(BinaryUtils.GetStringHashCodeLowerCase(fieldName)) ? (uint) (int) fieldVal : fieldVal; } if (fieldType == typeof(long)) { - return dotNetFields.Contains(BinaryUtils.GetStringHashCode(fieldName)) + return dotNetFields.Contains(BinaryUtils.GetStringHashCodeLowerCase(fieldName)) ? (ulong) (long) fieldVal : fieldVal; } if (fieldType == typeof(byte[])) { - return dotNetFields.Contains(BinaryUtils.GetStringHashCode(fieldName)) + return dotNetFields.Contains(BinaryUtils.GetStringHashCodeLowerCase(fieldName)) ? ConvertArray<byte, sbyte>((byte[]) fieldVal) : fieldVal; } if (fieldType == typeof(short[])) { - return dotNetFields.Contains(BinaryUtils.GetStringHashCode(fieldName)) + return dotNetFields.Contains(BinaryUtils.GetStringHashCodeLowerCase(fieldName)) ? ConvertArray<short, ushort>((short[]) fieldVal) : fieldVal; } if (fieldType == typeof(int[])) { - return dotNetFields.Contains(BinaryUtils.GetStringHashCode(fieldName)) + return dotNetFields.Contains(BinaryUtils.GetStringHashCodeLowerCase(fieldName)) ? ConvertArray<int, uint>((int[]) fieldVal) : fieldVal; } if (fieldType == typeof(long[])) { - return dotNetFields.Contains(BinaryUtils.GetStringHashCode(fieldName)) + return dotNetFields.Contains(BinaryUtils.GetStringHashCodeLowerCase(fieldName)) ? ConvertArray<long, ulong>((long[]) fieldVal) : fieldVal; } }
