public final class CodecRegistry extends Object
TypeCodec
s. When the driver needs to serialize or deserialize a Java type
to/from CQL, it will lookup in the registry for a suitable codec. The registry is initialized
with default codecs that handle basic conversions (e.g. CQL text
to java.lang.String
), and users can add their own. Complex codecs can also be generated on-the-fly
from simpler ones (more details below).
DEFAULT_INSTANCE
, a shareable, JVM-wide
instance initialized with built-in codecs for all the base CQL types. The only reason to create
your own instances is if you have multiple Cluster
objects that use different sets of
codecs. In that case, use Cluster.Builder.withCodecRegistry(CodecRegistry)
to associate the
registry with the cluster:
CodecRegistry myCodecRegistry = new CodecRegistry();
myCodecRegistry.register(myCodec1, myCodec2, myCodec3);
Cluster cluster = Cluster.builder().withCodecRegistry(myCodecRegistry).build();
// To retrieve the registry later:
CodecRegistry registry = cluster.getConfiguration().getCodecRegistry();
CodecRegistry
instances are thread-safe.
It is possible to turn on log messages by setting the com.datastax.driver.core.CodecRegistry
logger level to TRACE
. Beware that the registry
can be very verbose at this log level.
TypeCodec
, create an instance, and
pass it to one of the register
methods; for example, one could
create a codec that maps CQL timestamps to JDK8's java.time.LocalDate
:
class LocalDateCodec extends TypeCodec<java.time.LocalDate> {
...
}
myCodecRegistry.register(new LocalDateCodec());
The conversion will be available to:
GettableByIndexData
, GettableByNameData
,
SettableByIndexData
and/or SettableByNameData
. Namely: Row
, BoundStatement
, UDTValue
and TupleValue
;
simple statements
;
Query
builder
.
Example:
Row row = session.executeQuery("select date from some_table where pk = 1").one();
java.time.LocalDate date = row.get(0, java.time.LocalDate.class); // uses LocalDateCodec registered above
You can also bypass the codec registry by passing a standalone codec instance to methods such as
GettableByIndexData.get(int, TypeCodec)
.
CodecRegistry
cannot find a suitable codec among existing ones, it will attempt to
create it on-the-fly. It can manage:
java.time.LocalDate
like in the example above, you get List<LocalDate>>
and Set<LocalDate>>
handled for free, as well as all Map
types whose keys and/or values are java.time.LocalDate
. This works recursively for
nested collections;
user types
, mapped to UDTValue
objects. Custom codecs are
available recursively to the UDT's fields, so if one of your fields is a timestamp
you can use your LocalDateCodec
to retrieve it as a java.time.LocalDate
;
tuple types
, mapped to TupleValue
(with the same rules for nested
fields);
custom types
, mapped to ByteBuffer
.
CodecNotFoundException
is thrown; you'll need to register a custom codec for it.
LocalDateCodec
and
ask the registry for a codec to convert a CQL list<timestamp>
to a Java List<LocalDate>
:
TypeCodec<List<LocalDate>>
from LocalDateCodec
, and put it in the cache;
codecFor
variant specifies whether the result
can be cached or not.
accepts
methods to determine if it can handle the mapping, and if so
returns it;
register(TypeCodec)
will log a warning and ignore it.Modifier and Type | Field and Description |
---|---|
static CodecRegistry |
DEFAULT_INSTANCE
The default
CodecRegistry instance. |
Constructor and Description |
---|
CodecRegistry()
Creates a new instance initialized with built-in codecs for all the base CQL types.
|
Modifier and Type | Method and Description |
---|---|
<T> TypeCodec<T> |
codecFor(DataType cqlType)
|
<T> TypeCodec<T> |
codecFor(DataType cqlType,
Class<T> javaType)
|
<T> TypeCodec<T> |
codecFor(DataType cqlType,
T value)
|
<T> TypeCodec<T> |
codecFor(DataType cqlType,
TypeToken<T> javaType)
|
<T> TypeCodec<T> |
codecFor(T value)
Returns a
codec that accepts the given value. |
CodecRegistry |
register(Iterable<? extends TypeCodec<?>> codecs)
Register the given codecs with this registry.
|
CodecRegistry |
register(TypeCodec<?>... codecs)
Register the given codecs with this registry.
|
CodecRegistry |
register(TypeCodec<?> newCodec)
Register the given codec with this registry.
|
public static final CodecRegistry DEFAULT_INSTANCE
CodecRegistry
instance.
It will be shared among all Cluster
instances that were not explicitly built with a
different instance.
public CodecRegistry()
public CodecRegistry register(TypeCodec<?> newCodec)
This method will log a warning and ignore the codec if it collides with a previously registered one. Note that this check is not done in a completely thread-safe manner; codecs should typically be registered at application startup, not in a highly concurrent context (if a race condition occurs, the worst possible outcome is that no warning gets logged, and the codec gets registered but will never actually be used).
newCodec
- The codec to add to the registry.public CodecRegistry register(TypeCodec<?>... codecs)
codecs
- The codecs to add to the registry.register(TypeCodec)
public CodecRegistry register(Iterable<? extends TypeCodec<?>> codecs)
codecs
- The codecs to add to the registry.register(TypeCodec)
public <T> TypeCodec<T> codecFor(T value)
codec
that accepts the given value.
This method takes an arbitrary Java object and tries to locate a suitable codec for it.
Codecs must perform a runtime inspection
of the object to
determine if they can accept it or not, which, depending on the implementations, can be
expensive; besides, the resulting codec cannot be cached. Therefore there might be a
performance penalty when using this method.
Furthermore, this method returns the first matching codec, regardless of its accepted CQL
type. It should be reserved for situations where the target CQL type is not available or
unknown. In the Java driver, this happens mainly when serializing a value in a SimpleStatement
or in the QueryBuilder
, where no CQL type information is
available.
Codecs returned by this method are NOT cached (see the top-level documentation
of this class for more explanations about caching).
value
- The value the codec should accept; must not be null
.CodecNotFoundException
- if a suitable codec cannot be found.public <T> TypeCodec<T> codecFor(DataType cqlType) throws CodecNotFoundException
codec
that accepts the given CQL type
.
This method returns the first matching codec, regardless of its accepted Java type. It
should be reserved for situations where the Java type is not available or unknown. In the Java
driver, this happens mainly when deserializing a value using the getObject
method.
Codecs returned by this method are cached (see the top-level
documentation
of this class for more explanations about caching).
cqlType
- The CQL type
the codec should accept; must not be null
.CodecNotFoundException
- if a suitable codec cannot be found.public <T> TypeCodec<T> codecFor(DataType cqlType, Class<T> javaType) throws CodecNotFoundException
codec
that accepts the given CQL type
and the
given Java class.
This method can only handle raw (non-parameterized) Java types. For parameterized types, use
codecFor(DataType, TypeToken)
instead.
Codecs returned by this method are cached (see the top-level
documentation
of this class for more explanations about caching).
cqlType
- The CQL type
the codec should accept; must not be null
.javaType
- The Java type the codec should accept; can be null
.CodecNotFoundException
- if a suitable codec cannot be found.public <T> TypeCodec<T> codecFor(DataType cqlType, TypeToken<T> javaType) throws CodecNotFoundException
codec
that accepts the given CQL type
and the
given Java type.
This method handles parameterized types thanks to Guava's TypeToken
API.
Codecs returned by this method are cached (see the top-level
documentation
of this class for more explanations about caching).
cqlType
- The CQL type
the codec should accept; must not be null
.javaType
- The Java type
the codec should accept; can be null
.CodecNotFoundException
- if a suitable codec cannot be found.public <T> TypeCodec<T> codecFor(DataType cqlType, T value)
codec
that accepts the given CQL type
and the
given value.
This method takes an arbitrary Java object and tries to locate a suitable codec for it.
Codecs must perform a runtime inspection
of the object to
determine if they can accept it or not, which, depending on the implementations, can be
expensive; besides, the resulting codec cannot be cached. Therefore there might be a
performance penalty when using this method.
Codecs returned by this method are NOT cached (see the top-level documentation
of this class for more explanations about caching).
cqlType
- The CQL type
the codec should accept; can be null
.value
- The value the codec should accept; must not be null
.CodecNotFoundException
- if a suitable codec cannot be found.Copyright © 2012–2023. All rights reserved.