Java Code Examples for org.apache.beam.sdk.coders.Coder#registerByteSizeObserver()

The following examples show how to use org.apache.beam.sdk.coders.Coder#registerByteSizeObserver() . 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
/**
 * A utility method that passes the given (unencoded) elements through coder's
 * registerByteSizeObserver() and encode() methods, and confirms they are mutually consistent.
 * This is useful for testing coder implementations.
 */
public static <T> void testByteCount(Coder<T> coder, Coder.Context context, T[] elements)
    throws Exception {
  TestElementByteSizeObserver observer = new TestElementByteSizeObserver();

  try (CountingOutputStream os = new CountingOutputStream(ByteStreams.nullOutputStream())) {
    for (T elem : elements) {
      coder.registerByteSizeObserver(elem, observer);
      coder.encode(elem, os, context);
      observer.advance();
    }
    long expectedLength = os.getCount();

    if (!context.isWholeStream) {
      assertEquals(expectedLength, observer.getSum());
    }
    assertEquals(elements.length, observer.getCount());
  }
}
 
Example 2
Source File: UnionCoder.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Notifies ElementByteSizeObserver about the byte size of the encoded value using this coder. */
@Override
public void registerByteSizeObserver(RawUnionValue union, ElementByteSizeObserver observer)
    throws Exception {
  int index = getIndexForEncoding(union);
  // Write out the union tag.
  observer.update(VarInt.getLength(index));
  // Write out the actual value.
  @SuppressWarnings("unchecked")
  Coder<Object> coder = (Coder<Object>) elementCoders.get(index);
  coder.registerByteSizeObserver(union.getValue(), observer);
}