Repository: ignite
Updated Branches:
  refs/heads/ignite-2.0 82857d0cb -> b0f848729


IGNITE-4592 .NET: Fix binary enum serialization

This closes #1456


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/b0f84872
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/b0f84872
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/b0f84872

Branch: refs/heads/ignite-2.0
Commit: b0f84872945f36f240218636c298ecc9f765c152
Parents: 82857d0
Author: Pavel Tupitsyn <ptupit...@apache.org>
Authored: Tue Jan 24 14:58:52 2017 +0300
Committer: Pavel Tupitsyn <ptupit...@apache.org>
Committed: Tue Jan 24 14:58:52 2017 +0300

----------------------------------------------------------------------
 .gitignore                                      |  2 +-
 .../platform/PlatformComputeEchoTask.java       | 12 +++++
 .../Binary/BinaryBuilderSelfTest.cs             | 20 ++++----
 .../Compute/ComputeApiTest.cs                   | 48 ++++++++++++++++++++
 .../Impl/Binary/BinaryObjectBuilder.cs          |  2 +-
 .../Impl/Binary/BinarySystemHandlers.cs         | 14 ++++--
 .../Impl/Binary/BinaryWriter.cs                 |  6 ++-
 7 files changed, 87 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/b0f84872/.gitignore
----------------------------------------------------------------------
diff --git a/.gitignore b/.gitignore
index db6128e..e118746 100644
--- a/.gitignore
+++ b/.gitignore
@@ -27,6 +27,7 @@ git-patch-prop-local.sh
 .project
 /modules/platforms/**/*.VC.db
 **/dotnet/libs/
+*.classname*
 
 #Visual Studio files
 *.[Oo]bj
@@ -58,5 +59,4 @@ ipch/
 [Dd]ebug*/
 [Rr]elease*/
 packages
-*.classname
 *.nupkg
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/b0f84872/modules/core/src/test/java/org/apache/ignite/platform/PlatformComputeEchoTask.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/platform/PlatformComputeEchoTask.java
 
b/modules/core/src/test/java/org/apache/ignite/platform/PlatformComputeEchoTask.java
index c01c6f4..036491f 100644
--- 
a/modules/core/src/test/java/org/apache/ignite/platform/PlatformComputeEchoTask.java
+++ 
b/modules/core/src/test/java/org/apache/ignite/platform/PlatformComputeEchoTask.java
@@ -101,6 +101,12 @@ public class PlatformComputeEchoTask extends 
ComputeTaskAdapter<Integer, Object>
     /** Type: enum array. */
     private static final int TYPE_AFFINITY_KEY = 19;
 
+    /** Type: enum from cache. */
+    private static final int TYPE_ENUM_FROM_CACHE = 20;
+
+    /** Type: enum array from cache. */
+    private static final int TYPE_ENUM_ARRAY_FROM_CACHE = 21;
+
     /** {@inheritDoc} */
     @Nullable @Override public Map<? extends ComputeJob, ClusterNode> 
map(List<ClusterNode> subgrid,
         @Nullable Integer arg) {
@@ -190,6 +196,9 @@ public class PlatformComputeEchoTask extends 
ComputeTaskAdapter<Integer, Object>
                 case TYPE_ENUM:
                     return PlatformComputeEnum.BAR;
 
+                case TYPE_ENUM_FROM_CACHE:
+                    return ignite.cache(null).get(TYPE_ENUM_FROM_CACHE);
+
                 case TYPE_ENUM_ARRAY:
                     return new PlatformComputeEnum[] {
                         PlatformComputeEnum.BAR,
@@ -197,6 +206,9 @@ public class PlatformComputeEchoTask extends 
ComputeTaskAdapter<Integer, Object>
                         PlatformComputeEnum.FOO
                     };
 
+                case TYPE_ENUM_ARRAY_FROM_CACHE:
+                    return ignite.cache(null).get(TYPE_ENUM_ARRAY_FROM_CACHE);
+
                 case TYPE_ENUM_FIELD:
                     IgniteCache<Integer, BinaryObject> cache = 
ignite.cache(null).withKeepBinary();
                     BinaryObject obj = cache.get(TYPE_ENUM_FIELD);

http://git-wip-us.apache.org/repos/asf/ignite/blob/b0f84872/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 d6551b5..7475f5a 100644
--- 
a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/BinaryBuilderSelfTest.cs
+++ 
b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/BinaryBuilderSelfTest.cs
@@ -1059,12 +1059,13 @@ namespace Apache.Ignite.Core.Tests.Binary
             Assert.AreEqual(nDate, binObj.GetField<DateTime?>("fNDate"));
             Assert.AreEqual(nDate, binObj.GetField<DateTime?>("fNTimestamp"));
             Assert.AreEqual(nGuid, binObj.GetField<Guid?>("fNGuid"));
-            Assert.AreEqual(TestEnum.One, binObj.GetField<TestEnum>("fEnum"));
+            Assert.AreEqual(TestEnum.One, 
binObj.GetField<IBinaryObject>("fEnum").Deserialize<TestEnum>());
             Assert.AreEqual(new[] {"str"}, 
binObj.GetField<string[]>("fStrArr"));
             Assert.AreEqual(new[] {nDate}, 
binObj.GetField<DateTime?[]>("fDateArr"));
             Assert.AreEqual(new[] {nDate}, 
binObj.GetField<DateTime?[]>("fTimestampArr"));
             Assert.AreEqual(new[] {nGuid}, 
binObj.GetField<Guid?[]>("fGuidArr"));
-            Assert.AreEqual(new[] {TestEnum.One}, 
binObj.GetField<TestEnum[]>("fEnumArr"));
+            Assert.AreEqual(new[] {TestEnum.One},
+                binObj.GetField<IBinaryObject[]>("fEnumArr").Select(x => 
x.Deserialize<TestEnum>()));
 
             StringDateGuidEnum obj = binObj.Deserialize<StringDateGuidEnum>();
 
@@ -1085,12 +1086,13 @@ namespace Apache.Ignite.Core.Tests.Binary
             Assert.AreEqual(nDate, builder.GetField<DateTime?>("fNDate"));
             Assert.AreEqual(nDate, builder.GetField<DateTime?>("fNTimestamp"));
             Assert.AreEqual(nGuid, builder.GetField<Guid?>("fNGuid"));
-            Assert.AreEqual(TestEnum.One, builder.GetField<TestEnum>("fEnum"));
+            Assert.AreEqual(TestEnum.One, 
builder.GetField<IBinaryObject>("fEnum").Deserialize<TestEnum>());
             Assert.AreEqual(new[] {"str"}, 
builder.GetField<string[]>("fStrArr"));
             Assert.AreEqual(new[] {nDate}, 
builder.GetField<DateTime?[]>("fDateArr"));
             Assert.AreEqual(new[] {nDate}, 
builder.GetField<DateTime?[]>("fTimestampArr"));
             Assert.AreEqual(new[] {nGuid}, 
builder.GetField<Guid?[]>("fGuidArr"));
-            Assert.AreEqual(new[] {TestEnum.One}, 
builder.GetField<TestEnum[]>("fEnumArr"));
+            Assert.AreEqual(new[] {TestEnum.One},
+                builder.GetField<IBinaryObject[]>("fEnumArr").Select(x => 
x.Deserialize<TestEnum>()));
 
             // Check reassemble.
             binObj = builder.Build();
@@ -1099,12 +1101,13 @@ namespace Apache.Ignite.Core.Tests.Binary
             Assert.AreEqual(nDate, binObj.GetField<DateTime?>("fNDate"));
             Assert.AreEqual(nDate, binObj.GetField<DateTime?>("fNTimestamp"));
             Assert.AreEqual(nGuid, binObj.GetField<Guid?>("fNGuid"));
-            Assert.AreEqual(TestEnum.One, binObj.GetField<TestEnum>("fEnum"));
+            Assert.AreEqual(TestEnum.One, 
binObj.GetField<IBinaryObject>("fEnum").Deserialize<TestEnum>());
             Assert.AreEqual(new[] {"str"}, 
binObj.GetField<string[]>("fStrArr"));
             Assert.AreEqual(new[] {nDate}, 
binObj.GetField<DateTime?[]>("fDateArr"));
             Assert.AreEqual(new[] {nDate}, 
binObj.GetField<DateTime?[]>("fTimestampArr"));
             Assert.AreEqual(new[] {nGuid}, 
binObj.GetField<Guid?[]>("fGuidArr"));
-            Assert.AreEqual(new[] {TestEnum.One}, 
binObj.GetField<TestEnum[]>("fEnumArr"));
+            Assert.AreEqual(new[] { TestEnum.One },
+                binObj.GetField<IBinaryObject[]>("fEnumArr").Select(x => 
x.Deserialize<TestEnum>()));
 
             obj = binObj.Deserialize<StringDateGuidEnum>();
 
@@ -1128,12 +1131,13 @@ namespace Apache.Ignite.Core.Tests.Binary
             Assert.AreEqual(nDate, binObj.GetField<DateTime?>("fNDate"));
             Assert.AreEqual(nDate, binObj.GetField<DateTime?>("fNTimestamp"));
             Assert.AreEqual(nGuid, binObj.GetField<Guid?>("fNGuid"));
-            Assert.AreEqual(TestEnum.Two, binObj.GetField<TestEnum>("fEnum"));
+            Assert.AreEqual(TestEnum.Two, 
binObj.GetField<IBinaryObject>("fEnum").Deserialize<TestEnum>());
             Assert.AreEqual(new[] { "str2" }, 
binObj.GetField<string[]>("fStrArr"));
             Assert.AreEqual(new[] { nDate }, 
binObj.GetField<DateTime?[]>("fDateArr"));
             Assert.AreEqual(new[] { nDate }, 
binObj.GetField<DateTime?[]>("fTimestampArr"));
             Assert.AreEqual(new[] { nGuid }, 
binObj.GetField<Guid?[]>("fGuidArr"));
-            Assert.AreEqual(new[] { TestEnum.Two }, 
binObj.GetField<TestEnum[]>("fEnumArr"));
+            Assert.AreEqual(new[] {TestEnum.Two},
+                binObj.GetField<IBinaryObject[]>("fEnumArr").Select(x => 
x.Deserialize<TestEnum>()));
 
             var obj = binObj.Deserialize<StringDateGuidEnum>();
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/b0f84872/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Compute/ComputeApiTest.cs
----------------------------------------------------------------------
diff --git 
a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Compute/ComputeApiTest.cs 
b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Compute/ComputeApiTest.cs
index 71a4718..e2d3f61 100644
--- 
a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Compute/ComputeApiTest.cs
+++ 
b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Compute/ComputeApiTest.cs
@@ -113,6 +113,12 @@ namespace Apache.Ignite.Core.Tests.Compute
         /** Type: affinity key. */
         public const int EchoTypeAffinityKey = 19;
 
+        /** Type: enum from cache. */
+        private const int EchoTypeEnumFromCache = 20;
+
+        /** Type: enum array from cache. */
+        private const int EchoTypeEnumArrayFromCache = 21;
+
         /** First node. */
         private IIgnite _grid1;
 
@@ -948,6 +954,24 @@ namespace Apache.Ignite.Core.Tests.Compute
         /// Tests the echo task returning enum.
         /// </summary>
         [Test]
+        public void TestEchoTaskEnumFromCache()
+        {
+            var cache = _grid1.GetCache<int, PlatformComputeEnum>(null);
+
+            foreach (PlatformComputeEnum val in 
Enum.GetValues(typeof(PlatformComputeEnum)))
+            {
+                cache[EchoTypeEnumFromCache] = val;
+
+                var res = 
_grid1.GetCompute().ExecuteJavaTask<PlatformComputeEnum>(EchoTask, 
EchoTypeEnumFromCache);
+
+                Assert.AreEqual(val, res);
+            }
+        }
+
+        /// <summary>
+        /// Tests the echo task returning enum.
+        /// </summary>
+        [Test]
         public void TestEchoTaskEnumArray()
         {
             var res = 
_grid1.GetCompute().ExecuteJavaTask<PlatformComputeEnum[]>(EchoTask, 
EchoTypeEnumArray);
@@ -961,6 +985,30 @@ namespace Apache.Ignite.Core.Tests.Compute
         }
 
         /// <summary>
+        /// Tests the echo task returning enum.
+        /// </summary>
+        [Test]
+        public void TestEchoTaskEnumArrayFromCache()
+        {
+            var cache = _grid1.GetCache<int, PlatformComputeEnum[]>(null);
+
+            foreach (var val in new[]
+            {
+                new[] {PlatformComputeEnum.Bar, PlatformComputeEnum.Baz, 
PlatformComputeEnum.Foo },
+                new[] {PlatformComputeEnum.Foo, PlatformComputeEnum.Baz},
+                new[] {PlatformComputeEnum.Bar}
+            })
+            {
+                cache[EchoTypeEnumArrayFromCache] = val;
+
+                var res = 
_grid1.GetCompute().ExecuteJavaTask<PlatformComputeEnum[]>(
+                    EchoTask, EchoTypeEnumArrayFromCache);
+
+                Assert.AreEqual(val, res);
+            }
+        }
+
+        /// <summary>
         /// Tests the echo task reading enum from a binary object field.
         /// Ensures that Java can understand enums written by .NET.
         /// </summary>

http://git-wip-us.apache.org/repos/asf/ignite/blob/b0f84872/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 db18638..40f1862 100644
--- 
a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryObjectBuilder.cs
+++ 
b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryObjectBuilder.cs
@@ -1010,7 +1010,7 @@ namespace Apache.Ignite.Core.Impl.Binary
                     break;
 
                 case BinaryUtils.TypeEnum:
-                    TransferBytes(inStream, outStream, 4); // Integer ordinal.
+                    TransferBytes(inStream, outStream, 8); // int typeId, int 
value.
 
                     break;
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/b0f84872/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinarySystemHandlers.cs
----------------------------------------------------------------------
diff --git 
a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinarySystemHandlers.cs
 
b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinarySystemHandlers.cs
index bdd7137..f601a34 100644
--- 
a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinarySystemHandlers.cs
+++ 
b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinarySystemHandlers.cs
@@ -527,15 +527,19 @@ namespace Apache.Ignite.Core.Impl.Binary
 
             BinaryUtils.WriteGuidArray((Guid?[])obj, ctx.Stream);
         }
-        
-        /**
-         * <summary>Write enum array.</summary>
-         */
+
+        /// <summary>
+        /// Writes the enum array.
+        /// </summary>
         private static void WriteEnumArray(BinaryWriter ctx, object obj)
         {
             ctx.Stream.WriteByte(BinaryUtils.TypeArrayEnum);
 
-            BinaryUtils.WriteArray((Array)obj, ctx);
+            var desc = ctx.Marshaller.GetDescriptor(obj.GetType());
+
+            int typeId = desc == null ? BinaryUtils.ObjTypeId : desc.TypeId;
+
+            BinaryUtils.WriteArray((Array)obj, ctx, typeId);
         }
 
         /**

http://git-wip-us.apache.org/repos/asf/ignite/blob/b0f84872/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryWriter.cs
----------------------------------------------------------------------
diff --git 
a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryWriter.cs 
b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryWriter.cs
index 47b0663..a943937 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryWriter.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryWriter.cs
@@ -887,8 +887,10 @@ namespace Apache.Ignite.Core.Impl.Binary
                 }
                 else
                 {
-                    // Unregistered enum, write as serializable
-                    Write(new SerializableObjectHolder(val));
+                    // Unregistered enum, write with object type id.
+                    _stream.WriteByte(BinaryUtils.TypeEnum);
+                    _stream.WriteInt(BinaryUtils.ObjTypeId);
+                    _stream.WriteInt(TypeCaster<int>.Cast(val));
                 }
             }
         }

Reply via email to