com.datastax.oss.driver.internal.core.type.codec.registry.DefaultCodecRegistry Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datastax.oss.driver.internal.core.type.codec.registry;
import com.datastax.oss.driver.api.core.DriverExecutionException;
import com.datastax.oss.driver.api.core.type.DataType;
import com.datastax.oss.driver.api.core.type.codec.TypeCodec;
import com.datastax.oss.driver.api.core.type.reflect.GenericType;
import com.datastax.oss.driver.shaded.guava.common.base.Throwables;
import com.datastax.oss.driver.shaded.guava.common.cache.CacheBuilder;
import com.datastax.oss.driver.shaded.guava.common.cache.CacheLoader;
import com.datastax.oss.driver.shaded.guava.common.cache.LoadingCache;
import com.datastax.oss.driver.shaded.guava.common.cache.RemovalListener;
import com.datastax.oss.driver.shaded.guava.common.util.concurrent.ExecutionError;
import com.datastax.oss.driver.shaded.guava.common.util.concurrent.UncheckedExecutionException;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import net.jcip.annotations.ThreadSafe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The default codec registry implementation.
*
* It is a caching registry based on Guava cache (note that the driver shades Guava).
*/
@ThreadSafe
public class DefaultCodecRegistry extends CachingCodecRegistry {
private static final Logger LOG = LoggerFactory.getLogger(DefaultCodecRegistry.class);
private final LoadingCache> cache;
/**
* Creates a new instance that accepts user codecs, with the default built-in codecs and the
* default cache behavior.
*/
public DefaultCodecRegistry(@NonNull String logPrefix) {
this(logPrefix, CodecRegistryConstants.PRIMITIVE_CODECS);
}
/**
* Creates a new instance that accepts user codecs, with the given built-in codecs and the default
* cache behavior.
*/
public DefaultCodecRegistry(@NonNull String logPrefix, @NonNull TypeCodec... primitiveCodecs) {
this(logPrefix, 0, null, 0, null, primitiveCodecs);
}
/**
* Same as {@link #DefaultCodecRegistry(String, TypeCodec[])}, but with some amount of control
* over cache behavior.
*
* Giving full access to the Guava cache API would be too much work, since it is shaded and we
* have to wrap everything. If you need something that's not available here, it's easy enough to
* write your own CachingCodecRegistry implementation. It's doubtful that stuff like cache
* eviction is that useful anyway.
*/
public DefaultCodecRegistry(
@NonNull String logPrefix,
int initialCacheCapacity,
@Nullable BiFunction, Integer> cacheWeigher,
int maximumCacheWeight,
@Nullable BiConsumer> cacheRemovalListener,
@NonNull TypeCodec... primitiveCodecs) {
super(logPrefix, primitiveCodecs);
CacheBuilder