Class TypeCodec.AbstractUDTCodec<T>
- Type Parameters:
T- The Java type that the UDT will be mapped to.
user-defined types (UDTs) to Java objects.
It can serve as a base class for codecs dealing with direct UDT-to-Pojo mappings.-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.cassandra.cql3.functions.types.TypeCodec
TypeCodec.AbstractCollectionCodec<E,C extends Collection<E>>, TypeCodec.AbstractMapCodec<K, V>, TypeCodec.AbstractTupleCodec<T>, TypeCodec.AbstractUDTCodec<T>, TypeCodec.PrimitiveBooleanCodec, TypeCodec.PrimitiveByteCodec, TypeCodec.PrimitiveDoubleCodec, TypeCodec.PrimitiveFloatCodec, TypeCodec.PrimitiveIntCodec, TypeCodec.PrimitiveLongCodec, TypeCodec.PrimitiveShortCodec -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptiondeserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) Deserialize the givenByteBufferinstance according to the CQL type handled by this codec.protected abstract TdeserializeAndSetField(ByteBuffer input, T target, String fieldName, ProtocolVersion protocolVersion) Deserialize an individual field and set it on an object, as part of deserializing the whole object from a CQL UDT (seedeserialize(ByteBuffer, ProtocolVersion)).Format the given value as a valid CQL literal according to the CQL type handled by this codec.protected abstract StringformatField(T source, String fieldName) Format an individual field in an object as a CQL literal, as part of formatting the whole object (seeformat(Object)).protected abstract TReturn a new instance ofT.Parse the given CQL literal into an instance of the Java type handled by this codec.protected abstract TparseAndSetField(String input, T target, String fieldName) Parse an individual field and set it on an object, as part of parsing the whole object (seeparse(String)).serialize(T value, ProtocolVersion protocolVersion) Serialize the given value according to the CQL type handled by this codec.protected abstract ByteBufferserializeField(T source, String fieldName, ProtocolVersion protocolVersion) Serialize an individual field in an object, as part of serializing the whole object to a CQL UDT (seeserialize(Object, ProtocolVersion)).Methods inherited from class org.apache.cassandra.cql3.functions.types.TypeCodec
accepts, accepts, accepts, accepts, ascii, bigint, blob, cboolean, cdouble, cfloat, cint, counter, custom, date, decimal, duration, getCqlType, getJavaType, inet, isSerializedSizeFixed, list, map, serializedSize, set, smallInt, time, timestamp, timeUUID, tinyInt, toString, tuple, userType, uuid, varchar, varint, vector
-
Field Details
-
definition
-
-
Method Details
-
serialize
Description copied from class:TypeCodecSerialize the given value according to the CQL type handled by this codec.Implementation notes:
- Null values should be gracefully handled and no exception should be raised; these should be considered as the equivalent of a NULL CQL value;
- Codecs for CQL collection types should not permit null elements;
- Codecs for CQL collection types should treat a
nullinput as the equivalent of an empty collection.
- Specified by:
serializein classTypeCodec<T>- Parameters:
value- An instance of T; may benull.protocolVersion- the protocol version to use when serializingbytes. In most cases, the proper value to provide for this argument is the value returned byProtocolOptions#getProtocolVersion(which is the protocol version in use by the driver).- Returns:
- A
ByteBufferinstance containing the serialized form of T
-
deserialize
Description copied from class:TypeCodecDeserialize the givenByteBufferinstance according to the CQL type handled by this codec.Implementation notes:
- Null or empty buffers should be gracefully handled and no exception should be raised;
these should be considered as the equivalent of a NULL CQL value and, in most cases,
should map to
nullor a default value for the corresponding Java type, if applicable; - Codecs for CQL collection types should clearly document whether they return immutable collections or not (note that the driver's default collection codecs return mutable collections);
- Codecs for CQL collection types should avoid returning
null; they should return empty collections instead (the driver's default collection codecs all comply with this rule). - The provided
ByteBuffershould never be consumed by read operations that modify its current position; if necessary,ByteBuffer.duplicate()duplicate} it before consuming.
- Specified by:
deserializein classTypeCodec<T>- Parameters:
bytes- AByteBufferinstance containing the serialized form of T; may benullor empty.protocolVersion- the protocol version to use when serializingbytes. In most cases, the proper value to provide for this argument is the value returned byProtocolOptions#getProtocolVersion(which is the protocol version in use by the driver).- Returns:
- An instance of T
- Null or empty buffers should be gracefully handled and no exception should be raised;
these should be considered as the equivalent of a NULL CQL value and, in most cases,
should map to
-
format
Description copied from class:TypeCodecFormat the given value as a valid CQL literal according to the CQL type handled by this codec.Implementors should take care of quoting and escaping the resulting CQL literal where applicable. Null values should be accepted; in most cases, implementations should return the CQL keyword
"NULL"fornullinputs.Implementing this method is not strictly mandatory. It is used:
- in the query builder, when values are inlined in the query string (see
querybuilder.BuiltStatementfor a detailed explanation of when this happens); - in the
QueryLogger, if parameter logging is enabled; - to format the INITCOND in
AggregateMetadata#asCQLQuery(boolean); - in the
toString()implementation of some objects (UDTValue,TupleValue, and the internal representation of aROWSresponse), which may appear in driver logs.
If you choose not to implement this method, you should not throw an exception but instead return a constant string (for example "XxxCodec.format not implemented").
- in the query builder, when values are inlined in the query string (see
-
parse
Description copied from class:TypeCodecParse the given CQL literal into an instance of the Java type handled by this codec.Implementors should take care of unquoting and unescaping the given CQL string where applicable. Null values and empty Strings should be accepted, as well as the string
"NULL"; in most cases, implementations should interpret these inputs has equivalent to anullreference.Implementing this method is not strictly mandatory: internally, the driver only uses it to parse the INITCOND when building the metadata of an aggregate function (and in most cases it will use a built-in codec, unless the INITCOND has a custom type).
-
newInstance
Return a new instance ofT.- Returns:
- A new instance of
T.
-
serializeField
protected abstract ByteBuffer serializeField(T source, String fieldName, ProtocolVersion protocolVersion) Serialize an individual field in an object, as part of serializing the whole object to a CQL UDT (seeserialize(Object, ProtocolVersion)).- Parameters:
source- The object to read the field from.fieldName- The name of the field. Note that if it is case-sensitive or contains special characters, it will be double-quoted (i.e. the string will contain actual quote characters, as in"\"foobar\"").protocolVersion- The protocol version to use.- Returns:
- The serialized field, or
nullif that field should be ignored.
-
deserializeAndSetField
protected abstract T deserializeAndSetField(ByteBuffer input, T target, String fieldName, ProtocolVersion protocolVersion) Deserialize an individual field and set it on an object, as part of deserializing the whole object from a CQL UDT (seedeserialize(ByteBuffer, ProtocolVersion)).- Parameters:
input- The serialized form of the field.target- The object to set the field on.fieldName- The name of the field. Note that if it is case-sensitive or contains special characters, it will be double-quoted (i.e. the string will contain actual quote characters, as in"\"foobar\"").protocolVersion- The protocol version to use.- Returns:
- The target object with the field set. In most cases this should be the same as
target, but if you're dealing with immutable types you'll need to return a different instance.
-
formatField
Format an individual field in an object as a CQL literal, as part of formatting the whole object (seeformat(Object)).- Parameters:
source- The object to read the field from.fieldName- The name of the field. Note that if it is case-sensitive or contains special characters, it will be double-quoted (i.e. the string will contain actual quote characters, as in"\"foobar\"").- Returns:
- The formatted value.
-
parseAndSetField
Parse an individual field and set it on an object, as part of parsing the whole object (seeparse(String)).- Parameters:
input- The String to parse the field from.target- The value to write to.fieldName- The name of the field. Note that if it is case-sensitive or contains special characters, it will be double-quoted (i.e. the string will contain actual quote characters, as in"\"foobar\"").- Returns:
- The target object with the field set. In most cases this should be the same as
target, but if you're dealing with immutable types you'll need to return a different instance.
-