incubator-crunch-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jwi...@apache.org
Subject [3/3] git commit: Add support for working with protoc 2.4.x into Crunch's protobuf utility methods
Date Thu, 04 Oct 2012 19:26:19 GMT
Add support for working with protoc 2.4.x into Crunch's protobuf utility methods


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

Branch: refs/heads/master
Commit: 910b1450de30db0748ff06cfc9626ea4ea367416
Parents: a79ce8a
Author: Josh Wills <jwills@apache.org>
Authored: Mon Oct 1 18:38:41 2012 -0700
Committer: Josh Wills <jwills@apache.org>
Committed: Mon Oct 1 18:38:41 2012 -0700

----------------------------------------------------------------------
 .../main/java/org/apache/crunch/util/PTypes.java   |    3 +-
 .../main/java/org/apache/crunch/util/Protos.java   |   24 ++++++++++++++-
 2 files changed, 25 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-crunch/blob/910b1450/crunch/src/main/java/org/apache/crunch/util/PTypes.java
----------------------------------------------------------------------
diff --git a/crunch/src/main/java/org/apache/crunch/util/PTypes.java b/crunch/src/main/java/org/apache/crunch/util/PTypes.java
index 64fb5f6..a38ba70 100644
--- a/crunch/src/main/java/org/apache/crunch/util/PTypes.java
+++ b/crunch/src/main/java/org/apache/crunch/util/PTypes.java
@@ -17,6 +17,7 @@
  */
 package org.apache.crunch.util;
 
+import java.lang.reflect.Method;
 import java.math.BigInteger;
 import java.nio.ByteBuffer;
 
@@ -129,7 +130,7 @@ public class PTypes {
 
     @Override
     public void initialize() {
-      this.instance = ReflectionUtils.newInstance(clazz, getConfiguration());
+      this.instance = Protos.getDefaultInstance(clazz);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-crunch/blob/910b1450/crunch/src/main/java/org/apache/crunch/util/Protos.java
----------------------------------------------------------------------
diff --git a/crunch/src/main/java/org/apache/crunch/util/Protos.java b/crunch/src/main/java/org/apache/crunch/util/Protos.java
index 9d661c6..fc61ffa 100644
--- a/crunch/src/main/java/org/apache/crunch/util/Protos.java
+++ b/crunch/src/main/java/org/apache/crunch/util/Protos.java
@@ -23,6 +23,7 @@ import java.util.List;
 import org.apache.crunch.DoFn;
 import org.apache.crunch.Emitter;
 import org.apache.crunch.MapFn;
+import org.apache.crunch.impl.mr.run.CrunchRuntimeException;
 import org.apache.hadoop.util.ReflectionUtils;
 
 import com.google.common.base.Splitter;
@@ -35,6 +36,27 @@ import com.google.protobuf.Message.Builder;
  */
 public class Protos {
 
+  /**
+   * Utility function for creating a default PB Messgae from a Class object that
+   * works with both protoc 2.3.0 and 2.4.x.
+   * @param clazz The class of the protocol buffer to create
+   * @return An instance of a protocol buffer
+   */
+  public static <M extends Message> M getDefaultInstance(Class<M> clazz) {
+    if (clazz.getConstructors().length > 0) {
+      // Protobuf 2.3.0
+      return ReflectionUtils.newInstance(clazz, null);
+    } else {
+      // Protobuf 2.4.x
+      try {
+        Message.Builder mb = (Message.Builder) clazz.getDeclaredMethod("newBuilder").invoke(null);
+        return (M) mb.getDefaultInstanceForType();
+      } catch (Exception e) {
+        throw new CrunchRuntimeException(e);
+      }  
+    }
+  }
+  
   public static <M extends Message, K> MapFn<M, K> extractKey(String fieldName)
{
     return new ExtractKeyFn<M, K>(fieldName);
   }
@@ -89,7 +111,7 @@ public class Protos {
 
     @Override
     public void initialize() {
-      this.msgInstance = ReflectionUtils.newInstance(msgClass, getConfiguration());
+      this.msgInstance = getDefaultInstance(msgClass);
       this.fields = msgInstance.getDescriptorForType().getFields();
       this.splitter = Splitter.on(sep);
     }


Mime
View raw message