IGNITE-6768 .NET: Thin client: Fix cache id calculation
authorPavel Tupitsyn <ptupitsyn@apache.org>
Fri, 27 Oct 2017 09:56:37 +0000 (12:56 +0300)
committerPavel Tupitsyn <ptupitsyn@apache.org>
Fri, 27 Oct 2017 09:56:37 +0000 (12:56 +0300)
Do not force lower case

modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/BinaryBuilderSelfTest.cs
modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/EnumsTest.cs
modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/Cache/CacheTest.cs
modules/platforms/dotnet/Apache.Ignite.Core.Tests/Client/RawSocketTest.cs
modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryUtils.cs
modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/Marshaller.cs
modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/SerializableSerializer.cs

index 61f90a3..5837ab1 100644 (file)
@@ -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);
         }
     }
 
index 18ef29a..8993fb4 100644 (file)
@@ -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
index f2dd1de..cfdce73 100644 (file)
@@ -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;
index b637e88..0f1358a 100644 (file)
@@ -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)
 
index 5233db8..20fea02 100644 (file)
@@ -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) " +
index 55b6121..7212cd6 100644 (file)
@@ -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;
index 80f267a..fc91edb 100644 (file)
@@ -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;
                 }
             }