public abstract class StandardCoder<T> extends Object implements Coder<T>
Coder that defines equality, hashing, and printing
via the class name and recursively using getComponents().
To extend StandardCoder, override the following methods as appropriate:
getComponents(): the default implementation returns Coder.getCoderArguments().getEncodedElementByteSize(T, org.apache.beam.sdk.coders.Coder.Context) and
isRegisterByteSizeObserverCheap(T, org.apache.beam.sdk.coders.Coder.Context): the
default implementation encodes values to bytes and counts the bytes, which is considered
expensive.getEncodingId() and getAllowedEncodings(): by default, the encoding id
is the empty string, so only the canonical name of the subclass will be used for
compatibility checks, and no other encoding ids are allowed.Coder.Context, Coder.NonDeterministicException| Modifier | Constructor and Description |
|---|---|
protected |
StandardCoder() |
| Modifier and Type | Method and Description |
|---|---|
org.apache.beam.sdk.util.CloudObject |
asCloudObject()
Returns the
CloudObject that represents this Coder. |
boolean |
consistentWithEquals()
|
boolean |
equals(Object o) |
Collection<String> |
getAllowedEncodings()
A collection of encodings supported by
Coder.decode(java.io.InputStream, org.apache.beam.sdk.coders.Coder.Context) in addition to the encoding
from Coder.getEncodingId() (which is assumed supported). |
List<? extends Coder<?>> |
getComponents()
|
protected long |
getEncodedElementByteSize(T value,
Coder.Context context)
Returns the size in bytes of the encoded value using this coder.
|
String |
getEncodingId()
An identifier for the binary format written by
Coder.encode(T, java.io.OutputStream, org.apache.beam.sdk.coders.Coder.Context). |
int |
hashCode() |
boolean |
isRegisterByteSizeObserverCheap(T value,
Coder.Context context)
Returns whether
Coder.registerByteSizeObserver(T, org.apache.beam.sdk.util.common.ElementByteSizeObserver, org.apache.beam.sdk.coders.Coder.Context) cheap enough to
call for every element, that is, if this Coder can
calculate the byte size of the element to be coded in roughly
constant time (or lazily). |
void |
registerByteSizeObserver(T value,
org.apache.beam.sdk.util.common.ElementByteSizeObserver observer,
Coder.Context context)
Notifies the
ElementByteSizeObserver about the byte size
of the encoded value using this Coder. |
Object |
structuralValue(T value)
Returns an object with an
Object.equals() method that represents structural equality
on the argument. |
String |
toString() |
protected void |
verifyDeterministic(String message,
Coder<?>... coders) |
protected void |
verifyDeterministic(String message,
Iterable<Coder<?>> coders) |
clone, finalize, getClass, notify, notifyAll, wait, wait, waitdecode, encode, getCoderArguments, verifyDeterministicpublic String getEncodingId()
CoderCoder.encode(T, java.io.OutputStream, org.apache.beam.sdk.coders.Coder.Context).
This value, along with the fully qualified class name, forms an identifier for the binary format of this coder. Whenever this value changes, the new encoding is considered incompatible with the prior format: It is presumed that the prior version of the coder will be unable to correctly read the new format and the new version of the coder will be unable to correctly read the old format.
If the format is changed in a backwards-compatible way (the Coder can still accept data from
the prior format), such as by adding optional fields to a Protocol Buffer or Avro definition,
and you want Dataflow to understand that the new coder is compatible with the prior coder,
this value must remain unchanged. It is then the responsibility of Coder.decode(java.io.InputStream, org.apache.beam.sdk.coders.Coder.Context) to correctly
read data from the prior format.
getEncodingId in interface Coder<T>public Collection<String> getAllowedEncodings()
CoderCoder.decode(java.io.InputStream, org.apache.beam.sdk.coders.Coder.Context) in addition to the encoding
from Coder.getEncodingId() (which is assumed supported).
This information is not currently used for any purpose. It is descriptive only, and this method is subject to change.
getAllowedEncodings in interface Coder<T>Coder.getEncodingId()public boolean equals(Object o)
equals in class Objecttrue if the two StandardCoder instances have the
same class and equal components.public org.apache.beam.sdk.util.CloudObject asCloudObject()
CoderCloudObject that represents this Coder.asCloudObject in interface Coder<T>public boolean isRegisterByteSizeObserverCheap(T value, Coder.Context context)
Coder.registerByteSizeObserver(T, org.apache.beam.sdk.util.common.ElementByteSizeObserver, org.apache.beam.sdk.coders.Coder.Context) cheap enough to
call for every element, that is, if this Coder can
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
PipelineRunner
implementations.
isRegisterByteSizeObserverCheap in interface Coder<T>false unless it is overridden. registerByteSizeObserver(T, org.apache.beam.sdk.util.common.ElementByteSizeObserver, org.apache.beam.sdk.coders.Coder.Context)
invokes getEncodedElementByteSize(T, org.apache.beam.sdk.coders.Coder.Context) which requires re-encoding an element
unless it is overridden. This is considered expensive.protected long getEncodedElementByteSize(T value, Coder.Context context) throws Exception
Exceptionpublic void registerByteSizeObserver(T value, org.apache.beam.sdk.util.common.ElementByteSizeObserver observer, Coder.Context context) throws Exception
ElementByteSizeObserver about the byte size
of the encoded value using this Coder.
Not intended to be called by user code, but instead by
PipelineRunner
implementations.
For StandardCoder subclasses, this notifies observer about the byte size
of the encoded value using this coder as returned by getEncodedElementByteSize(T, org.apache.beam.sdk.coders.Coder.Context).
registerByteSizeObserver in interface Coder<T>Exceptionprotected void verifyDeterministic(String message, Iterable<Coder<?>> coders) throws Coder.NonDeterministicException
Coder.NonDeterministicExceptionprotected void verifyDeterministic(String message, Coder<?>... coders) throws Coder.NonDeterministicException
Coder.NonDeterministicExceptionpublic boolean consistentWithEquals()
true if this Coder is injective with respect to Object.equals(java.lang.Object).
Whenever the encoded bytes of two values are equal, then the original values are equal
according to Objects.equals(). Note that this is well-defined for null.
This condition is most notably false for arrays. More generally, this condition is false
whenever equals() compares object identity, rather than performing a
semantic/structural comparison.
consistentWithEquals in interface Coder<T>false for StandardCoder unless overridden.public Object structuralValue(T value) throws Exception
CoderObject.equals() method that represents structural equality
on the argument.
For any two values x and y of type T, if their encoded bytes are the
same, then it must be the case that structuralValue(x).equals(@code structuralValue(y).
Most notably:
null should be a proper object with
an equals() method, even if the input value is null.
See also Coder.consistentWithEquals().
structuralValue in interface Coder<T>Exception