Class IterableLikeCoder<T,IterableT extends Iterable<T>>
- Type Parameters:
T- the type of the elements of theIterables being transcodedIterableT- the type of the Iterables being transcoded
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
CollectionCoder,DequeCoder,IterableCoder,ListCoder,SetCoder
Coder for a class that
implements Iterable.
To complete a subclass, implement the decodeToIterable(java.util.List<T>) method. This superclass will
decode the elements in the input stream into a List and then pass them to that method to
be converted into the appropriate iterable type. Note that this means the input iterables must
fit into memory.
The format of this coder is as follows:
- If the input
Iterablehas a known and finite size, then the size is written to the output stream in big endian format, followed by all of the encoded elements. - If the input
Iterableis not known to have a finite size, then each element of the input is preceded bytrueencoded as a byte (indicating "more data") followed by the encoded element, and terminated byfalseencoded as a byte.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.beam.sdk.coders.Coder
Coder.Context, Coder.NonDeterministicException -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedIterableLikeCoder(Coder<T> elementCoder, String iterableName) -
Method Summary
Modifier and TypeMethodDescriptiondecode(InputStream inStream) Decodes a value of typeTfrom the given input stream in the given context.protected abstract IterableTdecodeToIterable(List<T> decodedElements) Builds an instance ofIterableT, this coder's associatedIterable-like subtype, from a list of decoded elements.protected IterableTdecodeToIterable(List<T> decodedElements, long terminatorValue, InputStream in) Builds an instance ofIterableT, this coder's associatedIterable-like subtype, from a list of decoded elements with theInputStreamat the position where this coder detected the end of the stream.voidencode(IterableT iterable, OutputStream outStream) Encodes the given value of typeTonto the given output stream.booleanisRegisterByteSizeObserverCheap(IterableT iterable) Returns whetherCoder.registerByteSizeObserver(T, org.apache.beam.sdk.util.common.ElementByteSizeObserver)cheap enough to call for every element, that is, if thisCodercan calculate the byte size of the element to be coded in roughly constant time (or lazily).voidregisterByteSizeObserver(IterableT iterable, org.apache.beam.sdk.util.common.ElementByteSizeObserver observer) Notifies theElementByteSizeObserverabout the byte size of the encoded value using thisCoder.voidThrowCoder.NonDeterministicExceptionif the coding is not deterministic.Methods inherited from class org.apache.beam.sdk.coders.StructuredCoder
equals, getComponents, hashCode, toStringMethods inherited from class org.apache.beam.sdk.coders.Coder
consistentWithEquals, decode, encode, getEncodedElementByteSize, getEncodedElementByteSizeUsingCoder, getEncodedTypeDescriptor, structuralValue, verifyDeterministic, verifyDeterministic
-
Constructor Details
-
IterableLikeCoder
-
-
Method Details
-
getElemCoder
-
decodeToIterable
Builds an instance ofIterableT, this coder's associatedIterable-like subtype, from a list of decoded elements.Override
decodeToIterable(List, long, InputStream)if you need access to the terminator value and theInputStream. -
decodeToIterable
protected IterableT decodeToIterable(List<T> decodedElements, long terminatorValue, InputStream in) throws IOException Builds an instance ofIterableT, this coder's associatedIterable-like subtype, from a list of decoded elements with theInputStreamat the position where this coder detected the end of the stream.- Throws:
IOException
-
encode
Description copied from class:CoderEncodes the given value of typeTonto the given output stream. Multiple elements can be encoded next to each other on the output stream, each coder should encode information to know how many bytes to read when decoding. A common approach is to prefix the encoding with the element's encoded length.- Specified by:
encodein classCoder<IterableT extends Iterable<T>>- Throws:
IOException- if writing to theOutputStreamfails for some reasonCoderException- if the value could not be encoded for some reason
-
decode
Description copied from class:CoderDecodes a value of typeTfrom the given input stream in the given context. Returns the decoded value. Multiple elements can be encoded next to each other on the input stream, each coder should encode information to know how many bytes to read when decoding. A common approach is to prefix the encoding with the element's encoded length.- Specified by:
decodein classCoder<IterableT extends Iterable<T>>- Throws:
IOException- if reading from theInputStreamfails for some reasonCoderException- if the value could not be decoded for some reason
-
getCoderArguments
Description copied from class:CoderIf this is aCoderfor a parameterized type, returns the list ofCoders being used for each of the parameters in the same order they appear within the parameterized type's type signature. If this cannot be done, or thisCoderdoes not encode/decode a parameterized type, returns the empty list.- Specified by:
getCoderArgumentsin classCoder<IterableT extends Iterable<T>>
-
verifyDeterministic
ThrowCoder.NonDeterministicExceptionif the coding is not deterministic.In order for a
Coderto be considered deterministic, the following must be true:- two values that compare as equal (via
Object.equals()orComparable.compareTo(), if supported) have the same encoding. - the
Coderalways produces a canonical encoding, which is the same for an instance of an object even if produced on different computers at different times.
- Specified by:
verifyDeterministicin classCoder<IterableT extends Iterable<T>>- Throws:
Coder.NonDeterministicException- always. Encoding is not deterministic for the generalIterablecase, as it depends upon the type of iterable. This may allow two objects to compare as equal while the encoding differs.
- two values that compare as equal (via
-
isRegisterByteSizeObserverCheap
Returns whetherCoder.registerByteSizeObserver(T, org.apache.beam.sdk.util.common.ElementByteSizeObserver)cheap enough to call for every element, that is, if thisCodercan calculate the byte size of the element to be coded in roughly constant time (or lazily).Not intended to be called by user code, but instead by
PipelineRunnerimplementations.By default, returns false. The default
Coder.registerByteSizeObserver(T, org.apache.beam.sdk.util.common.ElementByteSizeObserver)implementation invokesCoder.getEncodedElementByteSize(T)which requires re-encoding an element unless it is overridden. This is considered expensive.- Overrides:
isRegisterByteSizeObserverCheapin classCoder<IterableT extends Iterable<T>>- Returns:
trueif the iterable is of a known class that supports lazy counting of byte size, since that requires minimal extra computation.
-
registerByteSizeObserver
public void registerByteSizeObserver(IterableT iterable, org.apache.beam.sdk.util.common.ElementByteSizeObserver observer) throws Exception Description copied from class:CoderNotifies theElementByteSizeObserverabout the byte size of the encoded value using thisCoder.Not intended to be called by user code, but instead by
PipelineRunnerimplementations.By default, this notifies
observerabout the byte size of the encoded value using this coder as returned byCoder.getEncodedElementByteSize(T).
-