geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adon...@apache.org
Subject geode git commit: GEODE-2428: Adding support of Serialization/deserialization of LinkedHashMap in DataSerializer Also added a test for the same.
Date Mon, 27 Feb 2017 11:53:32 GMT
Repository: geode
Updated Branches:
  refs/heads/develop 64cd6fcd4 -> a8757f6a1


GEODE-2428: Adding support of Serialization/deserialization of LinkedHashMap in DataSerializer
Also added a test for the same.

GEODE-2428: Addressing review comment, Fixing the wild-card import
issue.


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

Branch: refs/heads/develop
Commit: a8757f6a16920488d15f78174f1638c87565eacf
Parents: 64cd6fc
Author: adongre <adongre@apache.org>
Authored: Sat Feb 4 19:47:51 2017 +0530
Committer: adongre <adongre@apache.org>
Committed: Mon Feb 27 17:20:33 2017 +0530

----------------------------------------------------------------------
 .../java/org/apache/geode/DataSerializer.java   | 69 ++++++++++++++++++++
 .../internal/DataSerializableJUnitTest.java     | 64 +++++++++++++-----
 2 files changed, 117 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/geode/blob/a8757f6a/geode-core/src/main/java/org/apache/geode/DataSerializer.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/DataSerializer.java b/geode-core/src/main/java/org/apache/geode/DataSerializer.java
index 375ff3b..627661a 100644
--- a/geode-core/src/main/java/org/apache/geode/DataSerializer.java
+++ b/geode-core/src/main/java/org/apache/geode/DataSerializer.java
@@ -33,6 +33,7 @@ import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.IdentityHashMap;
 import java.util.LinkedHashSet;
+import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.Map;
 import java.util.Properties;
@@ -2667,6 +2668,74 @@ public abstract class DataSerializer {
     }
   }
 
+
+  /**
+   * Writes a <code>LinkedHashMap</code> to a <code>DataOutput</code>.
Note that even though
+   * <code>map</code> may be an instance of a subclass of <code>LinkedHashMap</code>,
+   * <code>readLinkedHashMap</code> will always return an instance of <code>LinkedHashMap</code>,
+   * <B>not</B> an instance of the subclass. To preserve the class type of <code>map</code>,
+   * {@link #writeObject(Object, DataOutput)} should be used for data serialization. This
method
+   * will serialize a <code>null</code> map and not throw a <code>NullPointerException</code>.
+   *
+   * @throws IOException A problem occurs while writing to <code>out</code>
+   * @see #readLinkedHashMap
+   */
+  public static void writeLinkedHashMap(Map<?, ?> map, DataOutput out) throws IOException
{
+
+    InternalDataSerializer.checkOut(out);
+
+    int size;
+    if (map == null) {
+      size = -1;
+    } else {
+      size = map.size();
+    }
+    InternalDataSerializer.writeArrayLength(size, out);
+    if (logger.isTraceEnabled(LogMarker.SERIALIZER)) {
+      logger.trace(LogMarker.SERIALIZER, "Writing LinkedHashMap with {} elements: {}", size,
map);
+    }
+    if (size > 0) {
+      for (Map.Entry<?, ?> entry : map.entrySet()) {
+        writeObject(entry.getKey(), out);
+        writeObject(entry.getValue(), out);
+      }
+    }
+  }
+
+  /**
+   * Reads a <code>LinkedHashMap</code> from a <code>DataInput</code>.
+   *
+   * @throws IOException A problem occurs while reading from <code>in</code>
+   * @throws ClassNotFoundException The class of one of the <Code>HashMap</code>'s
elements cannot
+   *         be found.
+   * @see #writeLinkedHashMap
+   */
+  public static <K, V> LinkedHashMap<K, V> readLinkedHashMap(DataInput in)
+      throws IOException, ClassNotFoundException {
+
+    InternalDataSerializer.checkIn(in);
+
+    int size = InternalDataSerializer.readArrayLength(in);
+    if (size == -1) {
+      return null;
+    } else {
+      LinkedHashMap<K, V> map = new LinkedHashMap<>(size);
+      for (int i = 0; i < size; i++) {
+        K key = DataSerializer.<K>readObject(in);
+        V value = DataSerializer.<V>readObject(in);
+        map.put(key, value);
+      }
+
+      if (logger.isTraceEnabled(LogMarker.SERIALIZER)) {
+        logger.trace(LogMarker.SERIALIZER, "Read LinkedHashMap with {} elements: {}", size,
map);
+      }
+
+      return map;
+    }
+  }
+
+
+
   /**
    * Writes a <code>TreeSet</code> to a <code>DataOutput</code>.
Note that even though
    * <code>set</code> may be an instance of a subclass of <code>TreeSet</code>,

http://git-wip-us.apache.org/repos/asf/geode/blob/a8757f6a/geode-core/src/test/java/org/apache/geode/internal/DataSerializableJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/internal/DataSerializableJUnitTest.java
b/geode-core/src/test/java/org/apache/geode/internal/DataSerializableJUnitTest.java
index eec5fc2..0c5d561 100755
--- a/geode-core/src/test/java/org/apache/geode/internal/DataSerializableJUnitTest.java
+++ b/geode-core/src/test/java/org/apache/geode/internal/DataSerializableJUnitTest.java
@@ -32,22 +32,7 @@ import java.math.BigInteger;
 import java.net.InetAddress;
 import java.nio.ByteBuffer;
 import java.sql.Timestamp;
-import java.util.ArrayList;
-import java.util.Comparator;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Hashtable;
-import java.util.IdentityHashMap;
-import java.util.LinkedHashSet;
-import java.util.LinkedList;
-import java.util.Properties;
-import java.util.Random;
-import java.util.Stack;
-import java.util.TreeMap;
-import java.util.TreeSet;
-import java.util.UUID;
-import java.util.Vector;
+import java.util.*;
 import java.util.concurrent.TimeUnit;
 
 import org.junit.Before;
@@ -1580,6 +1565,53 @@ public class DataSerializableJUnitTest implements Serializable {
   }
 
   /**
+   * Tests data serializing an {@link java.util.LinkedHashMap}
+   */
+  @Test
+  public void testLinkedHashMap() throws Exception {
+    Random random = getRandom();
+    LinkedHashMap map = new LinkedHashMap();
+    int size = random.nextInt(50);
+    for (int i = 0; i < size; i++) {
+      Object key = new Long(random.nextLong());
+      Object value = String.valueOf(random.nextLong());
+      map.put(key, value);
+    }
+
+    DataOutputStream out = getDataOutput();
+    DataSerializer.writeLinkedHashMap(map, out);
+    out.flush();
+
+    DataInput in = getDataInput();
+    LinkedHashMap map2 = DataSerializer.readLinkedHashMap(in);
+    assertEquals(map, map2);
+  }
+
+  /**
+   * Tests data serializing an {@link LinkedHashMap} using {@link DataSerializer#writeObject}.
+   */
+  @Test
+  public void testLinkedHashMapObject() throws Exception {
+    Random random = getRandom();
+    LinkedHashMap map = new LinkedHashMap();
+    int size = random.nextInt(50);
+    for (int i = 0; i < size; i++) {
+      Object key = new Long(random.nextLong());
+      Object value = String.valueOf(random.nextLong());
+      map.put(key, value);
+    }
+
+    DataOutputStream out = getDataOutput();
+    DataSerializer.writeObject(map, out);
+    out.flush();
+
+    DataInput in = getDataInput();
+    LinkedHashMap map2 = (LinkedHashMap) DataSerializer.readObject(in);
+    assertEquals(map, map2);
+  }
+
+
+  /**
    * Tests data serializing an {@link IdentityHashMap}
    */
   @Test


Mime
View raw message