Java Code Examples for org.apache.beam.sdk.coders.Coder#Context

The following examples show how to use org.apache.beam.sdk.coders.Coder#Context . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CoderProperties.java    From beam with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
static <T> T decode(Coder<T> coder, Coder.Context context, byte[] bytes)
    throws CoderException, IOException {
  @SuppressWarnings("unchecked")
  Coder<T> deserializedCoder = SerializableUtils.clone(coder);

  byte[] buffer;
  if (Objects.equals(context, Coder.Context.NESTED)) {
    buffer = new byte[bytes.length + 1];
    System.arraycopy(bytes, 0, buffer, 0, bytes.length);
    buffer[bytes.length] = 1;
  } else {
    buffer = bytes;
  }

  CountingInputStream cis = new CountingInputStream(new ByteArrayInputStream(buffer));
  T value = deserializedCoder.decode(new UnownedInputStream(cis), context);
  assertThat(
      "consumed bytes equal to encoded bytes", cis.getCount(), equalTo((long) bytes.length));
  return value;
}
 
Example 2
Source File: DynamicProtoCoderTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testDynamicNestedRepeatedMessage() throws Exception {
  DynamicMessage message =
      DynamicMessage.newBuilder(MessageA.getDescriptor())
          .setField(
              MessageA.getDescriptor().findFieldByNumber(MessageA.FIELD1_FIELD_NUMBER), "foo")
          .addRepeatedField(
              MessageA.getDescriptor().findFieldByNumber(MessageA.FIELD2_FIELD_NUMBER),
              DynamicMessage.newBuilder(MessageB.getDescriptor())
                  .setField(
                      MessageB.getDescriptor().findFieldByNumber(MessageB.FIELD1_FIELD_NUMBER),
                      true)
                  .build())
          .addRepeatedField(
              MessageA.getDescriptor().findFieldByNumber(MessageA.FIELD2_FIELD_NUMBER),
              DynamicMessage.newBuilder(MessageB.getDescriptor())
                  .setField(
                      MessageB.getDescriptor().findFieldByNumber(MessageB.FIELD1_FIELD_NUMBER),
                      false)
                  .build())
          .build();
  Coder<DynamicMessage> coder = DynamicProtoCoder.of(message.getDescriptorForType());

  // Special code to check the DynamicMessage equality (@see IsDynamicMessageEqual)
  for (Coder.Context context : ALL_CONTEXTS) {
    CoderProperties.coderDecodeEncodeInContext(
        coder, context, message, IsDynamicMessageEqual.equalTo(message));
  }
}
 
Example 3
Source File: CoderProperties.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that for the given {@code Coder<T>}, {@code Coder.Context}, and values of type {@code
 * T}, the structural values are equal if and only if the encoded bytes are equal, in any {@code
 * Coder.Context}.
 */
public static <T> void structuralValueConsistentWithEqualsInContext(
    Coder<T> coder, Coder.Context context, T value1, T value2) throws Exception {
  assertEquals(
      coder.structuralValue(value1).equals(coder.structuralValue(value2)),
      Arrays.equals(encode(coder, context, value1), encode(coder, context, value2)));
}
 
Example 4
Source File: CoderProperties.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that for the given {@code Coder<T>} and values of type {@code T}, the structural
 * values are equal if and only if the encoded bytes are equal.
 */
public static <T> void structuralValueConsistentWithEquals(Coder<T> coder, T value1, T value2)
    throws Exception {
  for (Coder.Context context : ALL_CONTEXTS) {
    CoderProperties.structuralValueConsistentWithEqualsInContext(coder, context, value1, value2);
  }
}
 
Example 5
Source File: CoderProperties.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that for the given {@code Coder<T>} and values of type {@code T}, the values are equal
 * if and only if the encoded bytes are equal.
 */
public static <T> void coderConsistentWithEquals(Coder<T> coder, T value1, T value2)
    throws Exception {
  for (Coder.Context context : ALL_CONTEXTS) {
    CoderProperties.coderConsistentWithEqualsInContext(coder, context, value1, value2);
  }
}
 
Example 6
Source File: CoderProperties.java    From beam with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static <T> byte[] encode(Coder<T> coder, Coder.Context context, T value)
    throws CoderException, IOException {
  @SuppressWarnings("unchecked")
  Coder<T> deserializedCoder = SerializableUtils.clone(coder);

  ByteArrayOutputStream os = new ByteArrayOutputStream();
  deserializedCoder.encode(value, new UnownedOutputStream(os), context);
  return os.toByteArray();
}
 
Example 7
Source File: CoderProperties.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that for the given {@code Coder<Collection<T>>}, and value of type {@code
 * Collection<T>}, encoding followed by decoding yields an equal value of type {@code
 * Collection<T>}, in any {@code Coder.Context}.
 */
public static <T, IterableT extends Iterable<T>> void coderDecodeEncodeContentsInSameOrder(
    Coder<IterableT> coder, IterableT value) throws Exception {
  for (Coder.Context context : ALL_CONTEXTS) {
    CoderProperties.coderDecodeEncodeContentsInSameOrderInContext(coder, context, value);
  }
}
 
Example 8
Source File: CoderProperties.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that for the given {@code Coder<Collection<T>>}, and value of type {@code
 * Collection<T>}, encoding followed by decoding yields an equal value of type {@code
 * Collection<T>}, in the given {@code Coder.Context}.
 */
@SuppressWarnings("unchecked")
public static <T, CollectionT extends Collection<T>> void coderDecodeEncodeContentsEqualInContext(
    Coder<CollectionT> coder, Coder.Context context, CollectionT value) throws Exception {
  // Matchers.containsInAnyOrder() requires at least one element
  Collection<T> result = decodeEncode(coder, context, value);
  if (value.isEmpty()) {
    assertThat(result, emptyIterable());
  } else {
    // This is the only Matchers.containInAnyOrder() overload that takes literal values
    assertThat(result, containsInAnyOrder((T[]) value.toArray()));
  }
}
 
Example 9
Source File: CoderProperties.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that for the given {@code Coder<Collection<T>>}, and value of type {@code
 * Collection<T>}, encoding followed by decoding yields an equal value of type {@code
 * Collection<T>}, in any {@code Coder.Context}.
 */
public static <T, CollectionT extends Collection<T>> void coderDecodeEncodeContentsEqual(
    Coder<CollectionT> coder, CollectionT value) throws Exception {
  for (Coder.Context context : ALL_CONTEXTS) {
    coderDecodeEncodeContentsEqualInContext(coder, context, value);
  }
}
 
Example 10
Source File: CoderProperties.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that for the given {@code Coder<T>}, {@code Coder.Context}, and values of type {@code
 * T}, if the values are equal then the encoded bytes are equal.
 */
public static <T> void coderDeterministicInContext(
    Coder<T> coder, Coder.Context context, T value1, T value2) throws Exception {
  try {
    coder.verifyDeterministic();
  } catch (NonDeterministicException e) {
    throw new AssertionError("Expected that the coder is deterministic", e);
  }
  assertThat("Expected that the passed in values are equal()", value1, equalTo(value2));
  assertThat(encode(coder, context, value1), equalTo(encode(coder, context, value2)));
}
 
Example 11
Source File: CoderUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Decodes a value from the given {@code stream}, which should be a stream that never throws
 * {@code IOException}, such as {@code ByteArrayInputStream} or {@link
 * ExposedByteArrayInputStream}.
 */
private static <T> T decodeFromSafeStream(
    Coder<T> coder, InputStream stream, Coder.Context context) throws CoderException {
  try {
    return coder.decode(new UnownedInputStream(stream), context);
  } catch (IOException exn) {
    Throwables.propagateIfPossible(exn, CoderException.class);
    throw new IllegalArgumentException(
        "Forbidden IOException when reading from InputStream", exn);
  }
}
 
Example 12
Source File: CoderUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
public static <T> T decodeFromByteArray(
    Coder<T> coder, byte[] encodedValue, Coder.Context context) throws CoderException {
  try (ExposedByteArrayInputStream stream = new ExposedByteArrayInputStream(encodedValue)) {
    T result = decodeFromSafeStream(coder, stream, context);
    if (stream.available() != 0) {
      throw new CoderException(
          stream.available() + " unexpected extra bytes after decoding " + result);
    }
    return result;
  }
}
 
Example 13
Source File: CoderUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Encodes {@code value} to the given {@code stream}, which should be a stream that never throws
 * {@code IOException}, such as {@code ByteArrayOutputStream} or {@link
 * ExposedByteArrayOutputStream}.
 */
private static <T> void encodeToSafeStream(
    Coder<T> coder, T value, OutputStream stream, Coder.Context context) throws CoderException {
  try {
    coder.encode(value, new UnownedOutputStream(stream), context);
  } catch (IOException exn) {
    Throwables.propagateIfPossible(exn, CoderException.class);
    throw new IllegalArgumentException("Forbidden IOException when writing to OutputStream", exn);
  }
}
 
Example 14
Source File: Combine.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public InputOrAccum<InputT, AccumT> decode(InputStream inStream, Coder.Context context)
    throws CoderException, IOException {
  if (inStream.read() == 0) {
    return InputOrAccum.input(inputCoder.decode(inStream, context));
  } else {
    return InputOrAccum.accum(accumCoder.decode(inStream, context));
  }
}
 
Example 15
Source File: CoderProperties.java    From beam with Apache License 2.0 4 votes vote down vote up
/**
 * Verifies that for the given {@code Coder<T>}, and value of type {@code T}, encoding followed by
 * decoding yields an equal value of type {@code T}, in any {@code Coder.Context}.
 */
public static <T> void coderDecodeEncodeEqual(Coder<T> coder, T value) throws Exception {
  for (Coder.Context context : ALL_CONTEXTS) {
    coderDecodeEncodeEqualInContext(coder, context, value);
  }
}
 
Example 16
Source File: ViewTest.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public String decode(InputStream inStream, Coder.Context context)
    throws CoderException, IOException {
  return StringUtf8Coder.of().decode(inStream, context);
}
 
Example 17
Source File: CoderProperties.java    From beam with Apache License 2.0 4 votes vote down vote up
/**
 * Verifies that for the given {@code Coder<T>}, {@code Coder.Context}, and value of type {@code
 * T}, encoding followed by decoding yields a value of type {@code T} and tests that the matcher
 * succeeds on the values.
 */
public static <T> void coderDecodeEncodeInContext(
    Coder<T> coder, Coder.Context context, T value, org.hamcrest.Matcher<T> matcher)
    throws Exception {
  assertThat(decodeEncode(coder, context, value), matcher);
}
 
Example 18
Source File: CoderProperties.java    From beam with Apache License 2.0 4 votes vote down vote up
/**
 * Verifies that for the given {@code Coder<T>}, {@code Coder.Context}, and value of type {@code
 * T}, the structural value is equal to the structural value yield by encoding and decoding the
 * original value, in any {@code Coder.Context}.
 */
public static <T> void structuralValueDecodeEncodeEqualInContext(
    Coder<T> coder, Coder.Context context, T value) throws Exception {
  assertEquals(
      coder.structuralValue(value), coder.structuralValue(decodeEncode(coder, context, value)));
}
 
Example 19
Source File: ViewTest.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public void encode(String value, OutputStream outStream, Coder.Context context)
    throws CoderException, IOException {
  StringUtf8Coder.of().encode(value, outStream, context);
}
 
Example 20
Source File: CoderProperties.java    From beam with Apache License 2.0 3 votes vote down vote up
/**
 * Verifies that for the given {@code Coder<T>} and value of type {@code T}, the structural value
 * is equal to the structural value yield by encoding and decoding the original value.
 *
 * <p>This is useful to test the correct implementation of a Coder structural equality with values
 * that don't implement the equals contract.
 */
public static <T> void structuralValueDecodeEncodeEqual(Coder<T> coder, T value)
    throws Exception {
  for (Coder.Context context : ALL_CONTEXTS) {
    CoderProperties.structuralValueDecodeEncodeEqualInContext(coder, context, value);
  }
}