incubator-crunch-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gr...@apache.org
Subject git commit: CRUNCH-152 Handle nulls when making deep copies
Date Wed, 30 Jan 2013 08:38:01 GMT
Updated Branches:
  refs/heads/master d743ce7c8 -> 03f3ce800


CRUNCH-152 Handle nulls when making deep copies


Project: http://git-wip-us.apache.org/repos/asf/incubator-crunch/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-crunch/commit/03f3ce80
Tree: http://git-wip-us.apache.org/repos/asf/incubator-crunch/tree/03f3ce80
Diff: http://git-wip-us.apache.org/repos/asf/incubator-crunch/diff/03f3ce80

Branch: refs/heads/master
Commit: 03f3ce8008da6546193678e1a60f032dda028483
Parents: d743ce7
Author: Gabriel Reid <greid@apache.org>
Authored: Wed Jan 30 09:30:55 2013 +0100
Committer: Gabriel Reid <greid@apache.org>
Committed: Wed Jan 30 09:30:55 2013 +0100

----------------------------------------------------------------------
 .../org/apache/crunch/types/MapDeepCopier.java     |    4 ++
 .../org/apache/crunch/types/TupleDeepCopier.java   |    5 +++
 .../apache/crunch/types/avro/AvroDeepCopier.java   |    5 +++
 .../crunch/types/writable/WritableDeepCopier.java  |    5 +++
 .../org/apache/crunch/types/MapDeepCopierTest.java |   13 +++++++
 .../apache/crunch/types/TupleDeepCopierTest.java   |   26 +++++++++++++++
 .../crunch/types/avro/AvroDeepCopierTest.java      |   13 +++++++-
 .../types/writable/WritableDeepCopierTest.java     |    9 +++++
 8 files changed, 79 insertions(+), 1 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-crunch/blob/03f3ce80/crunch/src/main/java/org/apache/crunch/types/MapDeepCopier.java
----------------------------------------------------------------------
diff --git a/crunch/src/main/java/org/apache/crunch/types/MapDeepCopier.java b/crunch/src/main/java/org/apache/crunch/types/MapDeepCopier.java
index 55daaee..de8903b 100644
--- a/crunch/src/main/java/org/apache/crunch/types/MapDeepCopier.java
+++ b/crunch/src/main/java/org/apache/crunch/types/MapDeepCopier.java
@@ -39,6 +39,10 @@ public class MapDeepCopier<T> implements DeepCopier<Map<String,
T>> {
 
   @Override
   public Map<String, T> deepCopy(Map<String, T> source) {
+    if (source == null) {
+      return null;
+    }
+    
     Map<String, T> deepCopyMap = Maps.newHashMap();
     for (Entry<String, T> entry : source.entrySet()) {
       deepCopyMap.put(entry.getKey(), ptype.getDetachedValue(entry.getValue()));

http://git-wip-us.apache.org/repos/asf/incubator-crunch/blob/03f3ce80/crunch/src/main/java/org/apache/crunch/types/TupleDeepCopier.java
----------------------------------------------------------------------
diff --git a/crunch/src/main/java/org/apache/crunch/types/TupleDeepCopier.java b/crunch/src/main/java/org/apache/crunch/types/TupleDeepCopier.java
index 45e38c7..a2ffae3 100644
--- a/crunch/src/main/java/org/apache/crunch/types/TupleDeepCopier.java
+++ b/crunch/src/main/java/org/apache/crunch/types/TupleDeepCopier.java
@@ -48,6 +48,11 @@ public class TupleDeepCopier<T extends Tuple> implements DeepCopier<T>
{
 
   @Override
   public T deepCopy(T source) {
+    
+    if (source == null) {
+      return null;
+    }
+    
     Object[] deepCopyValues = new Object[source.size()];
 
     for (int valueIndex = 0; valueIndex < elementTypes.size(); valueIndex++) {

http://git-wip-us.apache.org/repos/asf/incubator-crunch/blob/03f3ce80/crunch/src/main/java/org/apache/crunch/types/avro/AvroDeepCopier.java
----------------------------------------------------------------------
diff --git a/crunch/src/main/java/org/apache/crunch/types/avro/AvroDeepCopier.java b/crunch/src/main/java/org/apache/crunch/types/avro/AvroDeepCopier.java
index 2deeddb..0fe9288 100644
--- a/crunch/src/main/java/org/apache/crunch/types/avro/AvroDeepCopier.java
+++ b/crunch/src/main/java/org/apache/crunch/types/avro/AvroDeepCopier.java
@@ -169,6 +169,11 @@ abstract class AvroDeepCopier<T> implements DeepCopier<T>,
Serializable {
    */
   @Override
   public T deepCopy(T source) {
+    
+    if (source == null) {
+      return null;
+    }
+    
     if (datumReader == null) {
       datumReader = createDatumReader(conf);
     }

http://git-wip-us.apache.org/repos/asf/incubator-crunch/blob/03f3ce80/crunch/src/main/java/org/apache/crunch/types/writable/WritableDeepCopier.java
----------------------------------------------------------------------
diff --git a/crunch/src/main/java/org/apache/crunch/types/writable/WritableDeepCopier.java
b/crunch/src/main/java/org/apache/crunch/types/writable/WritableDeepCopier.java
index 84e22e3..7b6e11b 100644
--- a/crunch/src/main/java/org/apache/crunch/types/writable/WritableDeepCopier.java
+++ b/crunch/src/main/java/org/apache/crunch/types/writable/WritableDeepCopier.java
@@ -47,6 +47,11 @@ public class WritableDeepCopier<T extends Writable> implements DeepCopier<T>
{
 
   @Override
   public T deepCopy(T source) {
+    
+    if (source == null) {
+      return null;
+    }
+    
     ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
     DataOutputStream dataOut = new DataOutputStream(byteOutStream);
     T copiedValue = null;

http://git-wip-us.apache.org/repos/asf/incubator-crunch/blob/03f3ce80/crunch/src/test/java/org/apache/crunch/types/MapDeepCopierTest.java
----------------------------------------------------------------------
diff --git a/crunch/src/test/java/org/apache/crunch/types/MapDeepCopierTest.java b/crunch/src/test/java/org/apache/crunch/types/MapDeepCopierTest.java
index b61a33f..c13e4a2 100644
--- a/crunch/src/test/java/org/apache/crunch/types/MapDeepCopierTest.java
+++ b/crunch/src/test/java/org/apache/crunch/types/MapDeepCopierTest.java
@@ -19,6 +19,7 @@ package org.apache.crunch.types;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertNull;
 
 import java.util.Map;
 
@@ -46,5 +47,17 @@ public class MapDeepCopierTest {
     assertEquals(map, deepCopy);
     assertNotSame(map.get(key), deepCopy.get(key));
   }
+  
+  @Test
+  public void testDeepCopy_Null() {
+    Map<String, StringWrapper> map = null;
+
+    MapDeepCopier<StringWrapper> deepCopier = new MapDeepCopier<StringWrapper>(
+        Avros.reflects(StringWrapper.class));
+    deepCopier.initialize(new Configuration());
+    Map<String, StringWrapper> deepCopy = deepCopier.deepCopy(map);
+
+    assertNull(deepCopy);
+  }
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-crunch/blob/03f3ce80/crunch/src/test/java/org/apache/crunch/types/TupleDeepCopierTest.java
----------------------------------------------------------------------
diff --git a/crunch/src/test/java/org/apache/crunch/types/TupleDeepCopierTest.java b/crunch/src/test/java/org/apache/crunch/types/TupleDeepCopierTest.java
index 0760c7e..e46a680 100644
--- a/crunch/src/test/java/org/apache/crunch/types/TupleDeepCopierTest.java
+++ b/crunch/src/test/java/org/apache/crunch/types/TupleDeepCopierTest.java
@@ -19,6 +19,7 @@ package org.apache.crunch.types;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertNull;
 
 import org.apache.crunch.Pair;
 import org.apache.crunch.test.Person;
@@ -47,5 +48,30 @@ public class TupleDeepCopierTest {
     assertEquals(inputPair, deepCopyPair);
     assertNotSame(inputPair.second(), deepCopyPair.second());
   }
+  
+  @Test
+  public void testDeepCopy_PairContainingNull() {
+
+    Pair<Integer, Person> inputPair = Pair.of(1, null);
+    DeepCopier<Pair> deepCopier = new TupleDeepCopier<Pair>(Pair.class, Avros.ints(),
+        Avros.records(Person.class));
+
+    deepCopier.initialize(new Configuration());
+    Pair<Integer, Person> deepCopyPair = deepCopier.deepCopy(inputPair);
+
+    assertEquals(inputPair, deepCopyPair);
+  }
+  
+  @Test
+  public void testDeepCopy_NullPair() {
+    Pair<Integer, Person> inputPair = null;
+    DeepCopier<Pair> deepCopier = new TupleDeepCopier<Pair>(Pair.class, Avros.ints(),
+        Avros.records(Person.class));
+
+    deepCopier.initialize(new Configuration());
+    Pair<Integer, Person> deepCopyPair = deepCopier.deepCopy(inputPair);
+
+    assertNull(deepCopyPair);
+  }
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-crunch/blob/03f3ce80/crunch/src/test/java/org/apache/crunch/types/avro/AvroDeepCopierTest.java
----------------------------------------------------------------------
diff --git a/crunch/src/test/java/org/apache/crunch/types/avro/AvroDeepCopierTest.java b/crunch/src/test/java/org/apache/crunch/types/avro/AvroDeepCopierTest.java
index bb59136..37c13c0 100644
--- a/crunch/src/test/java/org/apache/crunch/types/avro/AvroDeepCopierTest.java
+++ b/crunch/src/test/java/org/apache/crunch/types/avro/AvroDeepCopierTest.java
@@ -19,6 +19,7 @@ package org.apache.crunch.types.avro;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertNull;
 
 import java.util.List;
 
@@ -31,7 +32,7 @@ import org.junit.Test;
 import com.google.common.collect.Lists;
 
 public class AvroDeepCopierTest {
-
+  
   @Test
   public void testDeepCopySpecific() {
     Person person = new Person();
@@ -92,5 +93,15 @@ public class AvroDeepCopierTest {
     assertNotSame(person, deepCopyPerson);
 
   }
+  
+  @Test
+  public void testDeepCopy_Null() {
+    Person person = null;
+
+    Person deepCopyPerson = new AvroSpecificDeepCopier<Person>(Person.class, Person.SCHEMA$)
+        .deepCopy(person);
+
+    assertNull(deepCopyPerson);
+  }
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-crunch/blob/03f3ce80/crunch/src/test/java/org/apache/crunch/types/writable/WritableDeepCopierTest.java
----------------------------------------------------------------------
diff --git a/crunch/src/test/java/org/apache/crunch/types/writable/WritableDeepCopierTest.java
b/crunch/src/test/java/org/apache/crunch/types/writable/WritableDeepCopierTest.java
index eae8218..c49491b 100644
--- a/crunch/src/test/java/org/apache/crunch/types/writable/WritableDeepCopierTest.java
+++ b/crunch/src/test/java/org/apache/crunch/types/writable/WritableDeepCopierTest.java
@@ -19,6 +19,7 @@ package org.apache.crunch.types.writable;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertNull;
 
 import org.apache.hadoop.io.Text;
 import org.junit.Before;
@@ -41,5 +42,13 @@ public class WritableDeepCopierTest {
     assertEquals(text, deepCopy);
     assertNotSame(text, deepCopy);
   }
+  
+  @Test
+  public void testDeepCopy_Null() {
+    Text text = null;
+    Text deepCopy = deepCopier.deepCopy(text);
+    
+    assertNull(deepCopy);
+  }
 
 }


Mime
View raw message