@Override public void registerByteSizeObserver( BoundedHeap<T, ComparatorT> value, ElementByteSizeObserver observer) throws Exception { listCoder.registerByteSizeObserver(value.asList(), observer); }
/** * Notifies ElementByteSizeObserver about the byte size of the encoded value using this coder. */ @Override public void registerByteSizeObserver( QuantileState<T, ComparatorT> state, ElementByteSizeObserver observer) throws Exception { elementCoder.registerByteSizeObserver(state.min, observer); elementCoder.registerByteSizeObserver(state.max, observer); elementListCoder.registerByteSizeObserver(state.unbufferedElements, observer); BigEndianIntegerCoder.of().registerByteSizeObserver(state.buffers.size(), observer); for (QuantileBuffer<T> buffer : state.buffers) { observer.update(4L + 8); elementListCoder.registerByteSizeObserver(buffer.elements, observer); } }
@Override public void registerByteSizeObserver(WindowedValue<T> value, ElementByteSizeObserver observer) throws Exception { valueCoder.registerByteSizeObserver(value.getValue(), observer); }
@Override public void registerByteSizeObserver(CountSum value, ElementByteSizeObserver observer) throws Exception { LONG_CODER.registerByteSizeObserver(value.count, observer); DOUBLE_CODER.registerByteSizeObserver(value.sum, observer); } }
@Override public void registerByteSizeObserver(Map<K, V> map, ElementByteSizeObserver observer) throws Exception { observer.update(4L); if (map.isEmpty()) { return; } Iterator<Entry<K, V>> entries = map.entrySet().iterator(); Entry<K, V> entry = entries.next(); while (entries.hasNext()) { keyCoder.registerByteSizeObserver(entry.getKey(), observer); valueCoder.registerByteSizeObserver(entry.getValue(), observer); entry = entries.next(); } keyCoder.registerByteSizeObserver(entry.getKey(), observer); valueCoder.registerByteSizeObserver(entry.getValue(), observer); }
/** * Overridden to short-circuit the default {@code StructuredCoder} behavior of encoding and * counting the bytes. The size is known (1 byte) when {@code value} is {@code null}, otherwise * the size is 1 byte plus the size of nested {@code Coder}'s encoding of {@code value}. * * <p>{@inheritDoc} */ @Override public void registerByteSizeObserver(@Nullable T value, ElementByteSizeObserver observer) throws Exception { observer.update(1); if (value != null) { valueCoder.registerByteSizeObserver(value, observer); } }
/** Notifies ElementByteSizeObserver about the byte size of the encoded value using this coder. */ @Override public void registerByteSizeObserver(KV<K, V> kv, ElementByteSizeObserver observer) throws Exception { if (kv == null) { throw new CoderException("cannot encode a null KV"); } keyCoder.registerByteSizeObserver(kv.getKey(), observer); valueCoder.registerByteSizeObserver(kv.getValue(), observer); }
/** 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); }
@Override public void registerByteSizeObserver(WindowedValue<T> value, ElementByteSizeObserver observer) throws Exception { InstantCoder.of().registerByteSizeObserver(value.getTimestamp(), observer); windowsCoder.registerByteSizeObserver(value.getWindows(), observer); PaneInfoCoder.INSTANCE.registerByteSizeObserver(value.getPane(), observer); valueCoder.registerByteSizeObserver(value.getValue(), observer); }
@Override public void registerByteSizeObserver(Timer<T> value, ElementByteSizeObserver observer) throws Exception { InstantCoder.of().registerByteSizeObserver(value.getTimestamp(), observer); payloadCoder.registerByteSizeObserver(value.getPayload(), observer); } }
observer.update(4L); for (T elem : collection) { elementCoder.registerByteSizeObserver(elem, observer); for (T elem : iterable) { count += 1; elementCoder.registerByteSizeObserver(elem, observer);
@Test public void testGetEncodedElementByteSize() throws Exception { TestElementByteSizeObserver observer = new TestElementByteSizeObserver(); for (BigInteger value : TEST_VALUES) { TEST_CODER.registerByteSizeObserver(value, observer); observer.advance(); assertThat( observer.getSumAndReset(), equalTo( (long) CoderUtils.encodeToByteArray(TEST_CODER, value, Coder.Context.NESTED).length)); } }
@Test public void testGetEncodedElementByteSize() throws Exception { TestElementByteSizeObserver observer = new TestElementByteSizeObserver(); for (BigDecimal value : TEST_VALUES) { TEST_CODER.registerByteSizeObserver(value, observer); observer.advance(); assertThat( observer.getSumAndReset(), equalTo( (long) CoderUtils.encodeToByteArray(TEST_CODER, value, Coder.Context.NESTED).length)); } }
/** * 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()); } }