Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new LookupCache interface #2455

Merged
merged 6 commits into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.fasterxml.jackson.databind.type.*;
import com.fasterxml.jackson.databind.util.ClassUtil;
import com.fasterxml.jackson.databind.util.Converter;
import com.fasterxml.jackson.databind.util.LookupCache;
import com.fasterxml.jackson.databind.util.SimpleLookupCache;

/**
Expand Down Expand Up @@ -41,7 +42,7 @@ public final class DeserializerCache
* This currently (3.0) means POJO, Enum and Container (collection,
* map) deserializers.
*/
private final SimpleLookupCache<JavaType, JsonDeserializer<Object>> _cachedDeserializers;
private final LookupCache<JavaType, JsonDeserializer<Object>> _cachedDeserializers;

/**
* During deserializer construction process we may need to keep track of partially
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.type.SimpleType;
import com.fasterxml.jackson.databind.util.ClassUtil;
import com.fasterxml.jackson.databind.util.LookupCache;
import com.fasterxml.jackson.databind.util.SimpleLookupCache;

public class BasicClassIntrospector
Expand Down Expand Up @@ -60,13 +61,13 @@ public class BasicClassIntrospector
* because {@link #forMapper(Object)} initializes it properly, when mapper get
* constructed.
*/
protected final transient SimpleLookupCache<JavaType,BasicBeanDescription> _cachedFCA;
protected final transient LookupCache<JavaType,BasicBeanDescription> _cachedFCA;

public BasicClassIntrospector() {
this(null);
}

protected BasicClassIntrospector(SimpleLookupCache<JavaType,BasicBeanDescription> cache) {
protected BasicClassIntrospector(LookupCache<JavaType,BasicBeanDescription> cache) {
_cachedFCA = cache;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class JacksonAnnotationIntrospector
*<p>
* Non-final only because it needs to be re-created after deserialization.
*/
protected transient SimpleLookupCache<Class<?>,Boolean> _annotationsInside = new SimpleLookupCache<Class<?>,Boolean>(48, 96);
protected transient LookupCache<Class<?>,Boolean> _annotationsInside = new SimpleLookupCache<Class<?>,Boolean>(48, 96);

/*
/**********************************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.core.util.Snapshottable;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.ser.impl.ReadOnlyClassToSerializerMap;
import com.fasterxml.jackson.databind.util.LookupCache;
import com.fasterxml.jackson.databind.util.SimpleLookupCache;
import com.fasterxml.jackson.databind.util.TypeKey;

Expand Down Expand Up @@ -40,7 +41,7 @@ public final class SerializerCache
* NOTE: keys are of various types (see below for key types), in addition to
* basic {@link JavaType} used for "untyped" serializers.
*/
private final SimpleLookupCache<TypeKey, JsonSerializer<Object>> _sharedMap;
private final LookupCache<TypeKey, JsonSerializer<Object>> _sharedMap;

/**
* Most recent read-only instance, created from _sharedMap, if any.
Expand All @@ -60,7 +61,7 @@ public SerializerCache(int maxCached) {
_readOnlyMap = new AtomicReference<ReadOnlyClassToSerializerMap>();
}

private SerializerCache(SimpleLookupCache<TypeKey, JsonSerializer<Object>> shared) {
protected SerializerCache(LookupCache<TypeKey, JsonSerializer<Object>> shared) {
_sharedMap = shared;
_readOnlyMap = new AtomicReference<ReadOnlyClassToSerializerMap>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ser.SerializerCache;
import com.fasterxml.jackson.databind.util.SimpleLookupCache;
import com.fasterxml.jackson.databind.util.LookupCache;
import com.fasterxml.jackson.databind.util.TypeKey;

/**
Expand All @@ -27,7 +27,7 @@ public final class ReadOnlyClassToSerializerMap
private final int _mask;

protected ReadOnlyClassToSerializerMap(SerializerCache shared,
SimpleLookupCache<TypeKey, JsonSerializer<Object>> src)
LookupCache<TypeKey, JsonSerializer<Object>> src)
{
_sharedCache = shared;
_size = findSize(src.size());
Expand Down Expand Up @@ -55,7 +55,7 @@ private final static int findSize(int size)
* Factory method for constructing an instance.
*/
public static ReadOnlyClassToSerializerMap from(SerializerCache shared,
SimpleLookupCache<TypeKey, JsonSerializer<Object>> src) {
LookupCache<TypeKey, JsonSerializer<Object>> src) {
return new ReadOnlyClassToSerializerMap(shared, src);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.util.ArrayBuilders;
import com.fasterxml.jackson.databind.util.ClassUtil;
import com.fasterxml.jackson.databind.util.LookupCache;
import com.fasterxml.jackson.databind.util.SimpleLookupCache;

/**
Expand Down Expand Up @@ -121,7 +122,7 @@ public final class TypeFactory
* actual generic types), we will use small cache to avoid repetitive
* resolution of core types
*/
protected final SimpleLookupCache<Object,JavaType> _typeCache;
protected final LookupCache<Object,JavaType> _typeCache;

/*
/**********************************************************************
Expand Down Expand Up @@ -150,7 +151,7 @@ private TypeFactory() {
this(null);
}

protected TypeFactory(SimpleLookupCache<Object,JavaType> typeCache) {
protected TypeFactory(LookupCache<Object,JavaType> typeCache) {
if (typeCache == null) {
typeCache = new SimpleLookupCache<Object,JavaType>(16, 200);
}
Expand All @@ -159,7 +160,7 @@ protected TypeFactory(SimpleLookupCache<Object,JavaType> typeCache) {
_classLoader = null;
}

protected TypeFactory(SimpleLookupCache<Object,JavaType> typeCache,
protected TypeFactory(LookupCache<Object,JavaType> typeCache,
TypeModifier[] mods, ClassLoader classLoader)
{
if (typeCache == null) {
Expand Down Expand Up @@ -190,7 +191,7 @@ public TypeFactory snapshot() {
*/
public TypeFactory withModifier(TypeModifier mod)
{
SimpleLookupCache<Object,JavaType> typeCache = _typeCache;
LookupCache<Object,JavaType> typeCache = _typeCache;
TypeModifier[] mods;
if (mod == null) { // mostly for unit tests
mods = null;
Expand Down Expand Up @@ -222,7 +223,7 @@ public TypeFactory withClassLoader(ClassLoader classLoader) {
* identical settings except for different cache; most likely one with
* bigger maximum size.
*/
public TypeFactory withCache(SimpleLookupCache<Object,JavaType> cache) {
public TypeFactory withCache(LookupCache<Object,JavaType> cache) {
return new TypeFactory(cache, _modifiers, _classLoader);
}

Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/fasterxml/jackson/databind/util/LookupCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.fasterxml.jackson.databind.util;

import com.fasterxml.jackson.core.util.Snapshottable;

import java.util.function.BiConsumer;

/**
* An interface describing the required API for the Jackson-Databind Type cache.
*
* @see com.fasterxml.jackson.databind.type.TypeFactory#withCache(LookupCache<java.lang.Object,com.fasterxml.jackson.databind.JavaType>)
* @see SimpleLookupCache
*/
public interface LookupCache <K,V>
extends Snapshottable<LookupCache<K,V>>,
java.io.Serializable {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure java.io.Serializable is actually needed for interface itself.
Serializability in 3.0 is by serializing Builders. Although it may be true that for some types, implementations would need to be serializable. Snapshottable does make sense, I think.

void contents(BiConsumer<K,V> consumer);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to add Javadocs in general. But especially here, as to if and why iteration over entries is desired for all implementations.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. Hmmh. Yes, that makes sense then... if that specific case is to be made pluggable.

Actually: let's drop that from interface, and not make this particular cache pluggable.
So contents() is perfectly fine for SimpleLookupCache for now. We can consider adding it into interface later on if other use is needed, but for now I'd rather keep API minimalistic.

int size();

/**
* NOTE: key is of type Object only to retain binary backwards-compatibility
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept this from existing SimpleLookupCache documentation

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. We wouldn't have to retain that (binary compatibility not preserved across 2.x -> 3.0 boundary) but... I don't feel strongly either way. I know that casting to type variable here won't be of much use as it's truly generic type. But it might help catch some type mismatches.

So I am fine either way at this point.

* @param key
* @return value associated with key (can return null)
*/
V get(Object key);

V put(K key, V value);
V putIfAbsent(K key, V value);
void clear();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class RootNameLookup implements java.io.Serializable
* For efficient operation, let's try to minimize number of times we
* need to introspect root element name to use.
*/
protected transient SimpleLookupCache<ClassKey,PropertyName> _rootNames;
protected transient LookupCache<ClassKey,PropertyName> _rootNames;

public RootNameLookup() {
_rootNames = new SimpleLookupCache<ClassKey,PropertyName>(20, 200);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;

import com.fasterxml.jackson.core.util.Snapshottable;

/**
* Synchronized cache with bounded size: used for reusing lookup values
* and lazily instantiated reusable items.
Expand All @@ -26,8 +24,7 @@
* a shaded variant may be used one day.
*/
public class SimpleLookupCache<K,V>
implements Snapshottable<SimpleLookupCache<K,V>>,
java.io.Serializable
implements LookupCache<K,V>
{
private static final long serialVersionUID = 3L;

Expand Down Expand Up @@ -59,6 +56,7 @@ public SimpleLookupCache<K, V> snapshot() {
return new SimpleLookupCache<K,V>(_initialEntries, _maxEntries);
}

@Override
public void contents(BiConsumer<K,V> consumer) {
for (Map.Entry<K,V> entry : _map.entrySet()) {
consumer.accept(entry.getKey(), entry.getValue());
Expand All @@ -70,7 +68,7 @@ public void contents(BiConsumer<K,V> consumer) {
/* Public API
/**********************************************************************
*/

@Override
public V put(K key, V value) {
if (_map.size() >= _maxEntries) {
// double-locking, yes, but safe here; trying to avoid "clear storms"
Expand All @@ -83,6 +81,7 @@ public V put(K key, V value) {
return _map.put(key, value);
}

@Override
public V putIfAbsent(K key, V value) {
// not 100% optimal semantically, but better from correctness (never exceeds
// defined maximum) and close enough all in all:
Expand All @@ -96,9 +95,12 @@ public V putIfAbsent(K key, V value) {
return _map.putIfAbsent(key, value);
}

// NOTE: key is of type Object only to retain binary backwards-compatibility
@Override
public V get(Object key) { return _map.get(key); }

@Override
public void clear() { _map.clear(); }

@Override
public int size() { return _map.size(); }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.fasterxml.jackson.databind.util;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;

/**
* A LookupCache implementation that has no synchronization (like SimpleLookupCache does)
* but that has the downside of not limiting the size of the cache.
*/
public class UnlimitedLookupCache<K,V> implements LookupCache<K,V> {

private final int _initialEntries, _maxEntries;
private final transient ConcurrentHashMap<K,V> _map;

public UnlimitedLookupCache(int initialEntries, int maxEntries)
{
_initialEntries = initialEntries;
_maxEntries = maxEntries;
// We'll use concurrency level of 4, seems reasonable
_map = new ConcurrentHashMap<K,V>(initialEntries, 0.8f, 4);
}

@Override
public void contents(BiConsumer<K, V> consumer) {
for (Map.Entry<K,V> entry : _map.entrySet()) {
consumer.accept(entry.getKey(), entry.getValue());
}
}

@Override
public int size() {
return _map.size();
}

@Override
public V get(Object key) {
return _map.get(key);
}

@Override
public V put(K key, V value) {
return _map.put(key, value);
}

@Override
public V putIfAbsent(K key, V value) {
return _map.putIfAbsent(key, value);
}

@Override
public void clear() {
_map.clear();
}

@Override
public LookupCache<K, V> snapshot() {
return new UnlimitedLookupCache<K,V>(_initialEntries, _maxEntries);
}

protected Object readResolve() {
return snapshot();
}
}