ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sboi...@apache.org
Subject [36/50] [abbrv] ignite git commit: IGNITE-2430: Fixed binary builder serialization logic for non-standard collections.
Date Fri, 19 Feb 2016 16:18:45 GMT
IGNITE-2430: Fixed binary builder serialization logic for non-standard collections.


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

Branch: refs/heads/ignite-961
Commit: 159b6cb4e17da75b77c958f5ad2b7b3fb7596c63
Parents: 35b0e6b
Author: dkarachentsev <dkarachentsev@gridgain.com>
Authored: Fri Feb 12 16:02:32 2016 +0300
Committer: vozerov-gridgain <vozerov@gridgain.com>
Committed: Fri Feb 12 16:02:32 2016 +0300

----------------------------------------------------------------------
 .../ignite/internal/binary/BinaryUtils.java     |  4 +-
 .../binary/builder/BinaryBuilderSerializer.java | 16 +++-
 .../binary/builder/BinaryObjectBuilderImpl.java | 12 ++-
 .../binary/builder/BinaryValueWithType.java     |  3 +-
 .../BinaryObjectBuilderAdditionalSelfTest.java  | 91 +++++++++++++++++++-
 ...naryObjectBuilderDefaultMappersSelfTest.java | 30 +++++--
 6 files changed, 141 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/159b6cb4/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java
b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java
index c3343d4..8b5d780 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java
@@ -1055,7 +1055,7 @@ public class BinaryUtils {
      * @param cls Class.
      * @return {@code True} if this is a special collection class.
      */
-    private static boolean isSpecialCollection(Class cls) {
+    public static boolean isSpecialCollection(Class cls) {
         return ArrayList.class.equals(cls) || LinkedList.class.equals(cls) ||
             HashSet.class.equals(cls) || LinkedHashSet.class.equals(cls);
     }
@@ -1066,7 +1066,7 @@ public class BinaryUtils {
      * @param cls Class.
      * @return {@code True} if this is a special map class.
      */
-    private static boolean isSpecialMap(Class cls) {
+    public static boolean isSpecialMap(Class cls) {
         return HashMap.class.equals(cls) || LinkedHashMap.class.equals(cls);
     }
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/159b6cb4/modules/core/src/main/java/org/apache/ignite/internal/binary/builder/BinaryBuilderSerializer.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/binary/builder/BinaryBuilderSerializer.java
b/modules/core/src/main/java/org/apache/ignite/internal/binary/builder/BinaryBuilderSerializer.java
index 6e5a621..b296437 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/binary/builder/BinaryBuilderSerializer.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/binary/builder/BinaryBuilderSerializer.java
@@ -50,6 +50,18 @@ class BinaryBuilderSerializer {
      * @param val Value.
      */
     public void writeValue(BinaryWriterExImpl writer, Object val) {
+        writeValue(writer, val, false, false);
+    }
+
+    /**     *
+     * @param writer Writer.
+     * @param val Value.
+     * @param forceCol Whether to force collection type.
+     * @param forceMap Whether to force map type.
+     */
+    public void writeValue(BinaryWriterExImpl writer, Object val, boolean forceCol, boolean
forceMap) {
+        assert !(forceCol && forceMap);
+
         if (val == null) {
             writer.writeByte(GridBinaryMarshaller.NULL);
 
@@ -113,7 +125,7 @@ class BinaryBuilderSerializer {
             return;
         }
 
-        if (val instanceof Collection) {
+        if (forceCol || BinaryUtils.isSpecialCollection(val.getClass())) {
             Collection<?> c = (Collection<?>)val;
 
             writer.writeByte(GridBinaryMarshaller.COL);
@@ -129,7 +141,7 @@ class BinaryBuilderSerializer {
             return;
         }
 
-        if (val instanceof Map) {
+        if (forceMap || BinaryUtils.isSpecialMap(val.getClass())) {
             Map<?, ?> map = (Map<?, ?>)val;
 
             writer.writeByte(GridBinaryMarshaller.MAP);

http://git-wip-us.apache.org/repos/asf/ignite/blob/159b6cb4/modules/core/src/main/java/org/apache/ignite/internal/binary/builder/BinaryObjectBuilderImpl.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/binary/builder/BinaryObjectBuilderImpl.java
b/modules/core/src/main/java/org/apache/ignite/internal/binary/builder/BinaryObjectBuilderImpl.java
index e2290d6..9043a8b 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/binary/builder/BinaryObjectBuilderImpl.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/binary/builder/BinaryObjectBuilderImpl.java
@@ -36,6 +36,7 @@ import org.apache.ignite.internal.util.typedef.internal.U;
 import org.apache.ignite.lang.IgniteBiTuple;
 import org.jetbrains.annotations.Nullable;
 
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
@@ -508,10 +509,19 @@ public class BinaryObjectBuilderImpl implements BinaryObjectBuilder
{
 
     /** {@inheritDoc} */
     @Override public <T> BinaryObjectBuilder setField(String name, @Nullable T val,
Class<? super T> type) {
+        byte typeId;
+
+        if (Collection.class.equals(type))
+            typeId = GridBinaryMarshaller.COL;
+        else if (Map.class.equals(type))
+            typeId = GridBinaryMarshaller.MAP;
+        else
+            typeId = BinaryUtils.typeByClass(type);
+
         if (assignedVals == null)
             assignedVals = new LinkedHashMap<>();
 
-        assignedVals.put(name, new BinaryValueWithType(BinaryUtils.typeByClass(type), val));
+        assignedVals.put(name, new BinaryValueWithType(typeId, val));
 
         return this;
     }

http://git-wip-us.apache.org/repos/asf/ignite/blob/159b6cb4/modules/core/src/main/java/org/apache/ignite/internal/binary/builder/BinaryValueWithType.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/binary/builder/BinaryValueWithType.java
b/modules/core/src/main/java/org/apache/ignite/internal/binary/builder/BinaryValueWithType.java
index 9f43bdb..c5a3e0a 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/binary/builder/BinaryValueWithType.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/binary/builder/BinaryValueWithType.java
@@ -18,6 +18,7 @@
 package org.apache.ignite.internal.binary.builder;
 
 import org.apache.ignite.internal.binary.BinaryWriterExImpl;
+import org.apache.ignite.internal.binary.GridBinaryMarshaller;
 import org.apache.ignite.internal.util.typedef.internal.S;
 
 /**
@@ -44,7 +45,7 @@ class BinaryValueWithType implements BinaryLazyValue {
         if (val instanceof BinaryBuilderSerializationAware)
             ((BinaryBuilderSerializationAware)val).writeTo(writer, ctx);
         else
-            ctx.writeValue(writer, val);
+            ctx.writeValue(writer, val, type == GridBinaryMarshaller.COL, type == GridBinaryMarshaller.MAP);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/ignite/blob/159b6cb4/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryObjectBuilderAdditionalSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryObjectBuilderAdditionalSelfTest.java
b/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryObjectBuilderAdditionalSelfTest.java
index 06e1e93..804c060 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryObjectBuilderAdditionalSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryObjectBuilderAdditionalSelfTest.java
@@ -30,10 +30,10 @@ import org.apache.ignite.binary.BinaryType;
 import org.apache.ignite.configuration.BinaryConfiguration;
 import org.apache.ignite.configuration.CacheConfiguration;
 import org.apache.ignite.configuration.IgniteConfiguration;
-import org.apache.ignite.internal.binary.mutabletest.GridBinaryTestClasses;
-import org.apache.ignite.internal.binary.builder.BinaryObjectBuilderImpl;
 import org.apache.ignite.internal.binary.builder.BinaryBuilderEnum;
+import org.apache.ignite.internal.binary.builder.BinaryObjectBuilderImpl;
 import org.apache.ignite.internal.binary.mutabletest.GridBinaryMarshalerAwareTestClass;
+import org.apache.ignite.internal.binary.mutabletest.GridBinaryTestClasses;
 import org.apache.ignite.internal.processors.cache.binary.CacheObjectBinaryProcessorImpl;
 import org.apache.ignite.internal.processors.cache.binary.IgniteBinaryImpl;
 import org.apache.ignite.internal.util.lang.GridMapEntry;
@@ -43,12 +43,18 @@ import org.junit.Assert;
 
 import java.lang.reflect.Field;
 import java.math.BigDecimal;
+import java.math.BigInteger;
 import java.sql.Timestamp;
+import java.util.AbstractMap;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
@@ -1261,4 +1267,85 @@ public class BinaryObjectBuilderAdditionalSelfTest extends GridCommonAbstractTes
         return new BinaryObjectBuilderImpl(processor.binaryContext(), processor.typeId(aCls.getName()),

             processor.binaryContext().userTypeName(aCls.getName()));
     }
+
+    /**
+     * Check that correct type is stored in binary object.
+     */
+    public void testCollectionsSerialization() {
+        final BinaryObjectBuilder root = newWrapper(BigInteger.class);
+
+        final List<Integer> arrList = new ArrayList<>();
+
+        arrList.add(Integer.MAX_VALUE);
+
+        final List<Integer> linkedList = new LinkedList<>();
+
+        linkedList.add(Integer.MAX_VALUE);
+
+        final Set<Integer> hashSet = new HashSet<>();
+
+        hashSet.add(Integer.MAX_VALUE);
+
+        final Set<Integer> linkedHashSet = new LinkedHashSet<>();
+
+        linkedHashSet.add(Integer.MAX_VALUE);
+
+        final Map<String, String> hashMap = new HashMap<>();
+
+        hashMap.put("key", "val");
+
+        final Map<String, String> linkedHashMap = new LinkedHashMap<>();
+
+        linkedHashMap.put("key", "val");
+
+        // collections
+        root.setField("arrayList", arrList);
+        root.setField("linkedList", linkedList);
+        root.setField("hashSet", hashSet);
+        root.setField("linkedHashSet", linkedHashSet);
+
+        root.setField("singletonList", Collections.singletonList(Integer.MAX_VALUE), Collection.class);
+        root.setField("singletonSet",  Collections.singleton(Integer.MAX_VALUE), Collection.class);
+
+        // maps
+        root.setField("hashMap", hashMap);
+        root.setField("linkedHashMap", linkedHashMap);
+
+        root.setField("singletonMap", Collections.singletonMap("key", "val"), Map.class);
+
+        // objects
+        root.setField("asList", Collections.singletonList(Integer.MAX_VALUE));
+        root.setField("asSet", Collections.singleton(Integer.MAX_VALUE));
+        root.setField("asMap", Collections.singletonMap("key", "val"));
+        root.setField("asListHint", Collections.singletonList(Integer.MAX_VALUE), List.class);
+        root.setField("asSetHint", Collections.singleton(Integer.MAX_VALUE), Set.class);
+        root.setField("asMapHint", (AbstractMap) Collections.singletonMap("key", "val"),
AbstractMap.class);
+
+        BinaryObject binaryObj = root.build();
+
+        final String COL = "Collection";
+        final String MAP = "Map";
+        final String OBJ = "Object";
+
+        assert COL.equals(binaryObj.type().fieldTypeName("arrayList"));
+        assert COL.equals(binaryObj.type().fieldTypeName("linkedList"));
+        assert COL.equals(binaryObj.type().fieldTypeName("hashSet"));
+        assert COL.equals(binaryObj.type().fieldTypeName("linkedHashSet"));
+        assert COL.equals(binaryObj.type().fieldTypeName("linkedHashSet"));
+        assert COL.equals(binaryObj.type().fieldTypeName("linkedHashSet"));
+
+        assert COL.equals(binaryObj.type().fieldTypeName("singletonList"));
+        assert COL.equals(binaryObj.type().fieldTypeName("singletonSet"));
+
+        assert MAP.equals(binaryObj.type().fieldTypeName("singletonMap"));
+
+        assert OBJ.equals(binaryObj.type().fieldTypeName("asList"));
+        assert OBJ.equals(binaryObj.type().fieldTypeName("asSet"));
+        assert OBJ.equals(binaryObj.type().fieldTypeName("asMap"));
+        assert OBJ.equals(binaryObj.type().fieldTypeName("asListHint"));
+        assert OBJ.equals(binaryObj.type().fieldTypeName("asSetHint"));
+        assert OBJ.equals(binaryObj.type().fieldTypeName("asMapHint"));
+
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/ignite/blob/159b6cb4/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryObjectBuilderDefaultMappersSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryObjectBuilderDefaultMappersSelfTest.java
b/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryObjectBuilderDefaultMappersSelfTest.java
index 990c928..9659c39 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryObjectBuilderDefaultMappersSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryObjectBuilderDefaultMappersSelfTest.java
@@ -674,18 +674,24 @@ public class BinaryObjectBuilderDefaultMappersSelfTest extends GridCommonAbstrac
         builder.hashCode(100);
 
         builder.setField("collectionField", Arrays.asList(new Value(1), new Value(2)));
+        builder.setField("collectionField2", Arrays.asList(new Value(1), new Value(2)), Collection.class);
 
         BinaryObject po = builder.build();
 
         assertEquals(expectedHashCode("Class"), po.type().typeId());
         assertEquals(100, po.hashCode());
 
-        List<BinaryObject> list = po.field("collectionField");
+        List<Value> list = po.field("collectionField");
 
         assertEquals(2, list.size());
+        assertEquals(1, list.get(0).i);
+        assertEquals(2, list.get(1).i);
 
-        assertEquals(1, list.get(0).<Value>deserialize().i);
-        assertEquals(2, list.get(1).<Value>deserialize().i);
+        List<BinaryObject> list2 = po.field("collectionField2");
+
+        assertEquals(2, list2.size());
+        assertEquals(1, list2.get(0).<Value>deserialize().i);
+        assertEquals(2, list2.get(1).<Value>deserialize().i);
     }
 
     /**
@@ -697,17 +703,27 @@ public class BinaryObjectBuilderDefaultMappersSelfTest extends GridCommonAbstrac
         builder.hashCode(100);
 
         builder.setField("mapField", F.asMap(new Key(1), new Value(1), new Key(2), new Value(2)));
+        builder.setField("mapField2", F.asMap(new Key(1), new Value(1), new Key(2), new Value(2)),
Map.class);
 
         BinaryObject po = builder.build();
 
         assertEquals(expectedHashCode("Class"), po.type().typeId());
         assertEquals(100, po.hashCode());
 
-        Map<BinaryObject, BinaryObject> map = po.field("mapField");
+        // Test non-standard map.
+        Map<Key, Value> map = po.field("mapField");
 
         assertEquals(2, map.size());
 
-        for (Map.Entry<BinaryObject, BinaryObject> e : map.entrySet())
+        for (Map.Entry<Key, Value> e : map.entrySet())
+            assertEquals(e.getKey().i, e.getValue().i);
+
+        // Test binary map
+        Map<BinaryObject, BinaryObject> map2 = po.field("mapField2");
+
+        assertEquals(2, map2.size());
+
+        for (Map.Entry<BinaryObject, BinaryObject> e : map2.entrySet())
             assertEquals(e.getKey().<Key>deserialize().i, e.getValue().<Value>deserialize().i);
     }
 
@@ -723,7 +739,7 @@ public class BinaryObjectBuilderDefaultMappersSelfTest extends GridCommonAbstrac
         builder.setField("f", 111.111f);
         builder.setField("iArr", new int[] {1, 2, 3});
         builder.setField("obj", new Key(1));
-        builder.setField("col", Arrays.asList(new Value(1), new Value(2)));
+        builder.setField("col", Arrays.asList(new Value(1), new Value(2)), Collection.class);
 
         BinaryObject po = builder.build();
 
@@ -755,7 +771,7 @@ public class BinaryObjectBuilderDefaultMappersSelfTest extends GridCommonAbstrac
         builder.setField("f", 111.111f);
         builder.setField("iArr", new int[] {1, 2, 3});
         builder.setField("obj", new Key(1));
-        builder.setField("col", Arrays.asList(new Value(1), new Value(2)));
+        builder.setField("col", Arrays.asList(new Value(1), new Value(2)), Collection.class);
 
         BinaryObject po = builder.build();
 


Mime
View raw message