You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by lc...@apache.org on 2017/05/04 15:59:19 UTC

[1/2] beam git commit: Split Coder's encode/decode methods into two methods depending on context.

Repository: beam
Updated Branches:
  refs/heads/master 588f57a1e -> d9293007d


Split Coder's encode/decode methods into two methods depending on context.

This allows the outer context to be marked deprecated.  A follow-up PR will
remove the old method once all consumers have been updated.


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

Branch: refs/heads/master
Commit: fba3d87ffec08f84c8be08ee16942b13364da2d9
Parents: 588f57a
Author: Robert Bradshaw <ro...@google.com>
Authored: Wed May 3 14:56:37 2017 -0700
Committer: Luke Cwik <lc...@google.com>
Committed: Thu May 4 08:57:26 2017 -0700

----------------------------------------------------------------------
 .../java/org/apache/beam/sdk/coders/Coder.java  | 72 ++++++++++++++++++++
 .../apache/beam/sdk/coders/StructuredCoder.java | 61 +++++++++++++++++
 2 files changed, 133 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/beam/blob/fba3d87f/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/Coder.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/Coder.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/Coder.java
index 8ba8ad3..c923719 100644
--- a/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/Coder.java
+++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/Coder.java
@@ -59,6 +59,7 @@ import org.apache.beam.sdk.values.TypeDescriptor;
  */
 public interface Coder<T> extends Serializable {
   /** The context in which encoding or decoding is being done. */
+  @Deprecated
   class Context {
     /**
      * The outer context: the value being encoded or decoded takes
@@ -111,6 +112,28 @@ public interface Coder<T> extends Serializable {
   }
 
   /**
+   * Encodes the given value of type {@code T} onto the given output stream.
+   *
+   * @throws IOException if writing to the {@code OutputStream} fails
+   * for some reason
+   * @throws CoderException if the value could not be encoded for some reason
+   */
+  void encode(T value, OutputStream outStream)
+      throws CoderException, IOException;
+
+  /**
+   * Encodes the given value of type {@code T} onto the given output stream
+   * in the outer context.
+   *
+   * @throws IOException if writing to the {@code OutputStream} fails
+   * for some reason
+   * @throws CoderException if the value could not be encoded for some reason
+   */
+  @Deprecated
+  void encodeOuter(T value, OutputStream outStream)
+      throws CoderException, IOException;
+
+  /**
    * Encodes the given value of type {@code T} onto the given output stream
    * in the given context.
    *
@@ -118,6 +141,7 @@ public interface Coder<T> extends Serializable {
    * for some reason
    * @throws CoderException if the value could not be encoded for some reason
    */
+  @Deprecated
   void encode(T value, OutputStream outStream, Context context)
       throws CoderException, IOException;
 
@@ -129,6 +153,28 @@ public interface Coder<T> extends Serializable {
    * for some reason
    * @throws CoderException if the value could not be decoded for some reason
    */
+  T decode(InputStream inStream) throws CoderException, IOException;
+
+  /**
+   * Decodes a value of type {@code T} from the given input stream in
+   * the outer context.  Returns the decoded value.
+   *
+   * @throws IOException if reading from the {@code InputStream} fails
+   * for some reason
+   * @throws CoderException if the value could not be decoded for some reason
+   */
+  @Deprecated
+  T decodeOuter(InputStream inStream) throws CoderException, IOException;
+
+  /**
+   * Decodes a value of type {@code T} from the given input stream in
+   * the given context.  Returns the decoded value.
+   *
+   * @throws IOException if reading from the {@code InputStream} fails
+   * for some reason
+   * @throws CoderException if the value could not be decoded for some reason
+   */
+  @Deprecated
   T decode(InputStream inStream, Context context)
       throws CoderException, IOException;
 
@@ -200,6 +246,19 @@ public interface Coder<T> extends Serializable {
    * {@link org.apache.beam.sdk.runners.PipelineRunner}
    * implementations.
    */
+  boolean isRegisterByteSizeObserverCheap(T value);
+
+  /**
+   * Returns whether {@link #registerByteSizeObserver} cheap enough to
+   * call for every element, that is, if this {@code Coder} can
+   * calculate the byte size of the element to be coded in roughly
+   * constant time (or lazily).
+   *
+   * <p>Not intended to be called by user code, but instead by
+   * {@link org.apache.beam.sdk.runners.PipelineRunner}
+   * implementations.
+   */
+  @Deprecated
   boolean isRegisterByteSizeObserverCheap(T value, Context context);
 
   /**
@@ -211,6 +270,19 @@ public interface Coder<T> extends Serializable {
    * implementations.
    */
   void registerByteSizeObserver(
+      T value, ElementByteSizeObserver observer)
+      throws Exception;
+
+  /**
+   * Notifies the {@code ElementByteSizeObserver} about the byte size
+   * of the encoded value using this {@code Coder}.
+   *
+   * <p>Not intended to be called by user code, but instead by
+   * {@link org.apache.beam.sdk.runners.PipelineRunner}
+   * implementations.
+   */
+  @Deprecated
+  void registerByteSizeObserver(
       T value, ElementByteSizeObserver observer, Context context)
       throws Exception;
 

http://git-wip-us.apache.org/repos/asf/beam/blob/fba3d87f/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/StructuredCoder.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/StructuredCoder.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/StructuredCoder.java
index 0cd53b0..cc39429 100644
--- a/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/StructuredCoder.java
+++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/StructuredCoder.java
@@ -20,6 +20,9 @@ package org.apache.beam.sdk.coders;
 import com.google.common.io.ByteStreams;
 import com.google.common.io.CountingOutputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
@@ -98,6 +101,58 @@ public abstract class StructuredCoder<T> implements Coder<T> {
     return builder.toString();
   }
 
+  public void encode(T value, OutputStream outStream)
+      throws CoderException, IOException {
+    encode(value, outStream, Coder.Context.NESTED);
+  }
+
+  @Deprecated
+  public void encodeOuter(T value, OutputStream outStream)
+      throws CoderException, IOException {
+    encode(value, outStream, Coder.Context.OUTER);
+  }
+
+  @Deprecated
+  public void encode(T value, OutputStream outStream, Coder.Context context)
+      throws CoderException, IOException {
+    if (context == Coder.Context.NESTED) {
+      encode(value, outStream);
+    } else {
+      encodeOuter(value, outStream);
+    }
+  }
+
+  public T decode(InputStream inStream) throws CoderException, IOException {
+    return decode(inStream, Coder.Context.NESTED);
+  }
+
+  @Deprecated
+  public T decodeOuter(InputStream inStream) throws CoderException, IOException {
+    return decode(inStream, Coder.Context.OUTER);
+  }
+
+  @Deprecated
+  public T decode(InputStream inStream, Coder.Context context)
+      throws CoderException, IOException {
+    if (context == Coder.Context.NESTED) {
+      return decode(inStream);
+    } else {
+      return decodeOuter(inStream);
+    }
+  }
+
+  /**
+   * {@inheritDoc}
+   *
+   * @return {@code false} unless it is overridden. {@link StructuredCoder#registerByteSizeObserver}
+   *         invokes {@link #getEncodedElementByteSize} which requires re-encoding an element
+   *         unless it is overridden. This is considered expensive.
+   */
+  @Override
+  public boolean isRegisterByteSizeObserverCheap(T value) {
+    return isRegisterByteSizeObserverCheap(value, Context.NESTED);
+  }
+
   /**
    * {@inheritDoc}
    *
@@ -124,6 +179,12 @@ public abstract class StructuredCoder<T> implements Coder<T> {
     }
   }
 
+  @Override
+  public void registerByteSizeObserver(T value, ElementByteSizeObserver observer)
+      throws Exception {
+    registerByteSizeObserver(value, observer, Context.NESTED);
+  }
+
   /**
    * {@inheritDoc}
    *


[2/2] beam git commit: [BEAM-2166] Split Coder's encode/decode methods into two methods depending on context.

Posted by lc...@apache.org.
[BEAM-2166] Split Coder's encode/decode methods into two methods depending on context.

This closes #2871


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

Branch: refs/heads/master
Commit: d9293007d065c82111bf449502b5466042dc6335
Parents: 588f57a fba3d87
Author: Luke Cwik <lc...@google.com>
Authored: Thu May 4 08:59:05 2017 -0700
Committer: Luke Cwik <lc...@google.com>
Committed: Thu May 4 08:59:05 2017 -0700

----------------------------------------------------------------------
 .../java/org/apache/beam/sdk/coders/Coder.java  | 72 ++++++++++++++++++++
 .../apache/beam/sdk/coders/StructuredCoder.java | 61 +++++++++++++++++
 2 files changed, 133 insertions(+)
----------------------------------------------------------------------