Merge pull request #146 from philwebb/SPR-9796
* SPR-9796: Update cache to support concurrent reads Cache and late resolve annotations for performance Polish Property class Develop ConcurrentReferenceHashMap
This commit is contained in:
commit
409b533281
|
|
@ -16,8 +16,6 @@
|
|||
|
||||
package org.springframework.core;
|
||||
|
||||
import java.lang.ref.Reference;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.GenericArrayType;
|
||||
import java.lang.reflect.Method;
|
||||
|
|
@ -25,16 +23,15 @@ import java.lang.reflect.ParameterizedType;
|
|||
import java.lang.reflect.Type;
|
||||
import java.lang.reflect.TypeVariable;
|
||||
import java.lang.reflect.WildcardType;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.ConcurrentReferenceHashMap;
|
||||
|
||||
/**
|
||||
* Helper class for resolving generic types against type variables.
|
||||
|
|
@ -53,9 +50,8 @@ public abstract class GenericTypeResolver {
|
|||
private static final Log logger = LogFactory.getLog(GenericTypeResolver.class);
|
||||
|
||||
/** Cache from Class to TypeVariable Map */
|
||||
private static final Map<Class, Reference<Map<TypeVariable, Type>>> typeVariableCache =
|
||||
Collections.synchronizedMap(new WeakHashMap<Class, Reference<Map<TypeVariable, Type>>>());
|
||||
|
||||
private static final Map<Class, Map<TypeVariable, Type>> typeVariableCache =
|
||||
new ConcurrentReferenceHashMap<Class, Map<TypeVariable,Type>>();
|
||||
|
||||
/**
|
||||
* Determine the target type for the given parameter specification.
|
||||
|
|
@ -408,8 +404,8 @@ public abstract class GenericTypeResolver {
|
|||
* all super types, enclosing types and interfaces.
|
||||
*/
|
||||
public static Map<TypeVariable, Type> getTypeVariableMap(Class clazz) {
|
||||
Reference<Map<TypeVariable, Type>> ref = typeVariableCache.get(clazz);
|
||||
Map<TypeVariable, Type> typeVariableMap = (ref != null ? ref.get() : null);
|
||||
Map<TypeVariable, Type> ref = typeVariableCache.get(clazz);
|
||||
Map<TypeVariable, Type> typeVariableMap = (ref != null ? ref : null);
|
||||
|
||||
if (typeVariableMap == null) {
|
||||
typeVariableMap = new HashMap<TypeVariable, Type>();
|
||||
|
|
@ -441,7 +437,7 @@ public abstract class GenericTypeResolver {
|
|||
type = type.getEnclosingClass();
|
||||
}
|
||||
|
||||
typeVariableCache.put(clazz, new WeakReference<Map<TypeVariable, Type>>(typeVariableMap));
|
||||
typeVariableCache.put(clazz, typeVariableMap);
|
||||
}
|
||||
|
||||
return typeVariableMap;
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@
|
|||
package org.springframework.core.convert;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.LinkedHashMap;
|
||||
|
|
@ -24,6 +25,8 @@ import java.util.Map;
|
|||
|
||||
import org.springframework.core.GenericTypeResolver;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.util.ConcurrentReferenceHashMap;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
|
|
@ -37,12 +40,16 @@ import org.springframework.util.StringUtils;
|
|||
* The built TypeDescriptor can then be used to convert from/to the property type.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @author Phillip Webb
|
||||
* @since 3.1
|
||||
* @see TypeDescriptor#TypeDescriptor(Property)
|
||||
* @see TypeDescriptor#nested(Property, int)
|
||||
*/
|
||||
public final class Property {
|
||||
|
||||
private static Map<Property, Annotation[]> annotationCache =
|
||||
new ConcurrentReferenceHashMap<Property, Annotation[]>();
|
||||
|
||||
private final Class<?> objectType;
|
||||
|
||||
private final Method readMethod;
|
||||
|
|
@ -53,8 +60,7 @@ public final class Property {
|
|||
|
||||
private final MethodParameter methodParameter;
|
||||
|
||||
private final Annotation[] annotations;
|
||||
|
||||
private Annotation[] annotations;
|
||||
|
||||
public Property(Class<?> objectType, Method readMethod, Method writeMethod) {
|
||||
this(objectType, readMethod, writeMethod, null);
|
||||
|
|
@ -65,13 +71,7 @@ public final class Property {
|
|||
this.readMethod = readMethod;
|
||||
this.writeMethod = writeMethod;
|
||||
this.methodParameter = resolveMethodParameter();
|
||||
if (name != null) {
|
||||
this.name = name;
|
||||
}
|
||||
else {
|
||||
this.name = resolveName();
|
||||
}
|
||||
this.annotations = resolveAnnotations();
|
||||
this.name = (name == null ? resolveName() : name);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -118,6 +118,9 @@ public final class Property {
|
|||
}
|
||||
|
||||
Annotation[] getAnnotations() {
|
||||
if(this.annotations == null) {
|
||||
this.annotations = resolveAnnotations();
|
||||
}
|
||||
return this.annotations;
|
||||
}
|
||||
|
||||
|
|
@ -188,26 +191,26 @@ public final class Property {
|
|||
}
|
||||
|
||||
private Annotation[] resolveAnnotations() {
|
||||
Map<Class<?>, Annotation> annMap = new LinkedHashMap<Class<?>, Annotation>();
|
||||
Method readMethod = getReadMethod();
|
||||
if (readMethod != null) {
|
||||
for (Annotation ann : readMethod.getAnnotations()) {
|
||||
annMap.put(ann.annotationType(), ann);
|
||||
Annotation[] annotations = annotationCache.get(this);
|
||||
if(annotations == null) {
|
||||
Map<Class<? extends Annotation>, Annotation> annotationMap = new LinkedHashMap<Class<? extends Annotation>, Annotation>();
|
||||
addAnnotationsToMap(annotationMap, getReadMethod());
|
||||
addAnnotationsToMap(annotationMap, getWriteMethod());
|
||||
addAnnotationsToMap(annotationMap, getField());
|
||||
annotations = annotationMap.values().toArray(new Annotation[annotationMap.size()]);
|
||||
annotationCache.put(this, annotations);
|
||||
}
|
||||
return annotations;
|
||||
}
|
||||
|
||||
private void addAnnotationsToMap(
|
||||
Map<Class<? extends Annotation>, Annotation> annotationMap,
|
||||
AnnotatedElement object) {
|
||||
if (object != null) {
|
||||
for (Annotation annotation : object.getAnnotations()) {
|
||||
annotationMap.put(annotation.annotationType(), annotation);
|
||||
}
|
||||
}
|
||||
Method writeMethod = getWriteMethod();
|
||||
if (writeMethod != null) {
|
||||
for (Annotation ann : writeMethod.getAnnotations()) {
|
||||
annMap.put(ann.annotationType(), ann);
|
||||
}
|
||||
}
|
||||
Field field = getField();
|
||||
if (field != null) {
|
||||
for (Annotation ann : field.getAnnotations()) {
|
||||
annMap.put(ann.annotationType(), ann);
|
||||
}
|
||||
}
|
||||
return annMap.values().toArray(new Annotation[annMap.size()]);
|
||||
}
|
||||
|
||||
private Field getField() {
|
||||
|
|
@ -238,4 +241,34 @@ public final class Property {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int hashCode = 1;
|
||||
hashCode = prime * hashCode + ObjectUtils.nullSafeHashCode(objectType);
|
||||
hashCode = prime * hashCode + ObjectUtils.nullSafeHashCode(readMethod);
|
||||
hashCode = prime * hashCode + ObjectUtils.nullSafeHashCode(writeMethod);
|
||||
hashCode = prime * hashCode + ObjectUtils.nullSafeHashCode(name);
|
||||
return hashCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Property other = (Property) obj;
|
||||
boolean equals = true;
|
||||
equals &= ObjectUtils.nullSafeEquals(objectType, other.objectType);
|
||||
equals &= ObjectUtils.nullSafeEquals(readMethod, other.readMethod);
|
||||
equals &= ObjectUtils.nullSafeEquals(writeMethod, other.writeMethod);
|
||||
equals &= ObjectUtils.nullSafeEquals(name, other.name);
|
||||
return equals;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,674 @@
|
|||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed 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 org.springframework.util;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.util.ConcurrentReferenceHashMap.Entry;
|
||||
import org.springframework.util.ConcurrentReferenceHashMap.Reference;
|
||||
import org.springframework.util.ConcurrentReferenceHashMap.Restructure;
|
||||
import org.springframework.util.comparator.ComparableComparator;
|
||||
import org.springframework.util.comparator.NullSafeComparator;
|
||||
|
||||
/**
|
||||
* Tests for {@link ConcurrentReferenceHashMap}.
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class ConcurrentReferenceHashMapTests {
|
||||
|
||||
private static final Comparator<? super String> NULL_SAFE_STRING_SORT = new NullSafeComparator<String>(
|
||||
new ComparableComparator<String>(), true);
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private TestWeakConcurrentCache<Integer, String> map = new TestWeakConcurrentCache<Integer, String>();
|
||||
|
||||
@Test
|
||||
public void shouldCreateWithDefaults() throws Exception {
|
||||
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>();
|
||||
assertThat(map.getSegmentsSize(), is(16));
|
||||
assertThat(map.getSegment(0).getSize(), is(1));
|
||||
assertThat(map.getLoadFactor(), is(0.75f));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateWithInitialCapacity() throws Exception {
|
||||
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(
|
||||
32);
|
||||
assertThat(map.getSegmentsSize(), is(16));
|
||||
assertThat(map.getSegment(0).getSize(), is(2));
|
||||
assertThat(map.getLoadFactor(), is(0.75f));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateWithInitialCapacityAndLoadFactor() throws Exception {
|
||||
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(
|
||||
32, 0.5f);
|
||||
assertThat(map.getSegmentsSize(), is(16));
|
||||
assertThat(map.getSegment(0).getSize(), is(2));
|
||||
assertThat(map.getLoadFactor(), is(0.5f));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateWithInitialCapacityAndConcurrenyLevel() throws Exception {
|
||||
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(
|
||||
16, 2);
|
||||
assertThat(map.getSegmentsSize(), is(2));
|
||||
assertThat(map.getSegment(0).getSize(), is(8));
|
||||
assertThat(map.getLoadFactor(), is(0.75f));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateFullyCustom() throws Exception {
|
||||
ConcurrentReferenceHashMap<Integer, String> map = new ConcurrentReferenceHashMap<Integer, String>(
|
||||
5, 0.5f, 3);
|
||||
// concurrencyLevel of 3 ends up as 4 (nearest power of 2)
|
||||
assertThat(map.getSegmentsSize(), is(4));
|
||||
// initialCapacity is 5/4 (rounded up, to nearest power of 2)
|
||||
assertThat(map.getSegment(0).getSize(), is(2));
|
||||
assertThat(map.getLoadFactor(), is(0.5f));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNeedPositiveConcurrenyLevel() throws Exception {
|
||||
new ConcurrentReferenceHashMap<Integer, String>(1, 1);
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("ConcurrencyLevel must be positive");
|
||||
new TestWeakConcurrentCache<Integer, String>(1, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNeedNonNegativeInitialCapacity() throws Exception {
|
||||
new ConcurrentReferenceHashMap<Integer, String>(0, 1);
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("InitialCapactity must not be negative");
|
||||
new TestWeakConcurrentCache<Integer, String>(-1, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNeedPositiveLoadFactor() throws Exception {
|
||||
new ConcurrentReferenceHashMap<Integer, String>(0, 0.1f, 1);
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("LoadFactor must be positive");
|
||||
new TestWeakConcurrentCache<Integer, String>(0, 0.0f, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPutAndGet() throws Exception {
|
||||
// NOTE we are using mock references so we don't need to worry about GC
|
||||
assertThat(this.map.size(), is(0));
|
||||
this.map.put(123, "123");
|
||||
assertThat(this.map.get(123), is("123"));
|
||||
assertThat(this.map.size(), is(1));
|
||||
this.map.put(123, "123b");
|
||||
assertThat(this.map.size(), is(1));
|
||||
this.map.put(123, null);
|
||||
assertThat(this.map.size(), is(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReplaceOnDoublePut() throws Exception {
|
||||
this.map.put(123, "321");
|
||||
this.map.put(123, "123");
|
||||
assertThat(this.map.get(123), is("123"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPutNullKey() throws Exception {
|
||||
this.map.put(null, "123");
|
||||
assertThat(this.map.get(null), is("123"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPutNullValue() throws Exception {
|
||||
this.map.put(123, "321");
|
||||
this.map.put(123, null);
|
||||
assertThat(this.map.get(123), is(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetWithNoItems() throws Exception {
|
||||
assertThat(this.map.get(123), is(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldApplySupplimentalHash() throws Exception {
|
||||
Integer key = 123;
|
||||
this.map.put(key, "123");
|
||||
assertThat(this.map.getSupplimentalHash(), is(not(key.hashCode())));
|
||||
assertThat(this.map.getSupplimentalHash() >> 30 & 0xFF, is(not(0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetFollowingNexts() throws Exception {
|
||||
// Use loadFactor to disable resize
|
||||
this.map = new TestWeakConcurrentCache<Integer, String>(1, 10.0f, 1);
|
||||
this.map.put(1, "1");
|
||||
this.map.put(2, "2");
|
||||
this.map.put(3, "3");
|
||||
assertThat(this.map.getSegment(0).getSize(), is(1));
|
||||
assertThat(this.map.get(1), is("1"));
|
||||
assertThat(this.map.get(2), is("2"));
|
||||
assertThat(this.map.get(3), is("3"));
|
||||
assertThat(this.map.get(4), is(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldResize() throws Exception {
|
||||
this.map = new TestWeakConcurrentCache<Integer, String>(1, 0.75f, 1);
|
||||
this.map.put(1, "1");
|
||||
assertThat(this.map.getSegment(0).getSize(), is(1));
|
||||
assertThat(this.map.get(1), is("1"));
|
||||
|
||||
this.map.put(2, "2");
|
||||
assertThat(this.map.getSegment(0).getSize(), is(2));
|
||||
assertThat(this.map.get(1), is("1"));
|
||||
assertThat(this.map.get(2), is("2"));
|
||||
|
||||
this.map.put(3, "3");
|
||||
assertThat(this.map.getSegment(0).getSize(), is(4));
|
||||
assertThat(this.map.get(1), is("1"));
|
||||
assertThat(this.map.get(2), is("2"));
|
||||
assertThat(this.map.get(3), is("3"));
|
||||
|
||||
this.map.put(4, "4");
|
||||
assertThat(this.map.getSegment(0).getSize(), is(8));
|
||||
assertThat(this.map.get(4), is("4"));
|
||||
|
||||
// Putting again should not increase the count
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
this.map.put(i, String.valueOf(i));
|
||||
}
|
||||
assertThat(this.map.getSegment(0).getSize(), is(8));
|
||||
assertThat(this.map.get(5), is("5"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPurgeOnGet() throws Exception {
|
||||
this.map = new TestWeakConcurrentCache<Integer, String>(1, 0.75f, 1);
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
this.map.put(i, String.valueOf(i));
|
||||
}
|
||||
this.map.getMockReference(1, Restructure.NEVER).queueForPurge();
|
||||
this.map.getMockReference(3, Restructure.NEVER).queueForPurge();
|
||||
assertThat(this.map.getReference(1, Restructure.WHEN_NECESSARY), is(nullValue()));
|
||||
assertThat(this.map.get(2), is("2"));
|
||||
assertThat(this.map.getReference(3, Restructure.WHEN_NECESSARY), is(nullValue()));
|
||||
assertThat(this.map.get(4), is("4"));
|
||||
assertThat(this.map.get(5), is("5"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPergeOnPut() throws Exception {
|
||||
this.map = new TestWeakConcurrentCache<Integer, String>(1, 0.75f, 1);
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
this.map.put(i, String.valueOf(i));
|
||||
}
|
||||
this.map.getMockReference(1, Restructure.NEVER).queueForPurge();
|
||||
this.map.getMockReference(3, Restructure.NEVER).queueForPurge();
|
||||
this.map.put(1, "1");
|
||||
assertThat(this.map.get(1), is("1"));
|
||||
assertThat(this.map.get(2), is("2"));
|
||||
assertThat(this.map.getReference(3, Restructure.WHEN_NECESSARY), is(nullValue()));
|
||||
assertThat(this.map.get(4), is("4"));
|
||||
assertThat(this.map.get(5), is("5"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPutIfAbsent() throws Exception {
|
||||
assertThat(this.map.putIfAbsent(123, "123"), is(nullValue()));
|
||||
assertThat(this.map.putIfAbsent(123, "123b"), is("123"));
|
||||
assertThat(this.map.get(123), is("123"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPutIfAbsentWithNullValue() throws Exception {
|
||||
assertThat(this.map.putIfAbsent(123, null), is(nullValue()));
|
||||
assertThat(this.map.putIfAbsent(123, "123"), is(nullValue()));
|
||||
assertThat(this.map.get(123), is(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPutIfAbsentWithNullKey() throws Exception {
|
||||
assertThat(this.map.putIfAbsent(null, "123"), is(nullValue()));
|
||||
assertThat(this.map.putIfAbsent(null, "123b"), is("123"));
|
||||
assertThat(this.map.get(null), is("123"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRemoveKeyAndValue() throws Exception {
|
||||
this.map.put(123, "123");
|
||||
assertThat(this.map.remove(123, "456"), is(false));
|
||||
assertThat(this.map.get(123), is("123"));
|
||||
assertThat(this.map.remove(123, "123"), is(true));
|
||||
assertFalse(this.map.containsKey(123));
|
||||
assertThat(this.map.isEmpty(), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRemoveKeyAndValueWithExistingNull() throws Exception {
|
||||
this.map.put(123, null);
|
||||
assertThat(this.map.remove(123, "456"), is(false));
|
||||
assertThat(this.map.get(123), is(nullValue()));
|
||||
assertThat(this.map.remove(123, null), is(true));
|
||||
assertFalse(this.map.containsKey(123));
|
||||
assertThat(this.map.isEmpty(), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReplaceOldValueWithNewValue() throws Exception {
|
||||
this.map.put(123, "123");
|
||||
assertThat(this.map.replace(123, "456", "789"), is(false));
|
||||
assertThat(this.map.get(123), is("123"));
|
||||
assertThat(this.map.replace(123, "123", "789"), is(true));
|
||||
assertThat(this.map.get(123), is("789"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReplaceOldNullValueWithNewValue() throws Exception {
|
||||
this.map.put(123, null);
|
||||
assertThat(this.map.replace(123, "456", "789"), is(false));
|
||||
assertThat(this.map.get(123), is(nullValue()));
|
||||
assertThat(this.map.replace(123, null, "789"), is(true));
|
||||
assertThat(this.map.get(123), is("789"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReplaceValue() throws Exception {
|
||||
this.map.put(123, "123");
|
||||
assertThat(this.map.replace(123, "456"), is("123"));
|
||||
assertThat(this.map.get(123), is("456"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReplaceNullValue() throws Exception {
|
||||
this.map.put(123, null);
|
||||
assertThat(this.map.replace(123, "456"), is(nullValue()));
|
||||
assertThat(this.map.get(123), is("456"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetSize() throws Exception {
|
||||
assertThat(this.map.size(), is(0));
|
||||
this.map.put(123, "123");
|
||||
this.map.put(123, null);
|
||||
this.map.put(456, "456");
|
||||
assertThat(this.map.size(), is(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSupportIsEmpty() throws Exception {
|
||||
assertThat(this.map.isEmpty(), is(true));
|
||||
this.map.put(123, "123");
|
||||
this.map.put(123, null);
|
||||
this.map.put(456, "456");
|
||||
assertThat(this.map.isEmpty(), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldContainKey() throws Exception {
|
||||
assertThat(this.map.containsKey(123), is(false));
|
||||
assertThat(this.map.containsKey(456), is(false));
|
||||
this.map.put(123, "123");
|
||||
this.map.put(456, null);
|
||||
assertThat(this.map.containsKey(123), is(true));
|
||||
assertThat(this.map.containsKey(456), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldContainValue() throws Exception {
|
||||
assertThat(this.map.containsValue("123"), is(false));
|
||||
assertThat(this.map.containsValue(null), is(false));
|
||||
this.map.put(123, "123");
|
||||
this.map.put(456, null);
|
||||
assertThat(this.map.containsValue("123"), is(true));
|
||||
assertThat(this.map.containsValue(null), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRemoveWhenKeyIsInMap() throws Exception {
|
||||
this.map.put(123, null);
|
||||
this.map.put(456, "456");
|
||||
this.map.put(null, "789");
|
||||
assertThat(this.map.remove(123), is(nullValue()));
|
||||
assertThat(this.map.remove(456), is("456"));
|
||||
assertThat(this.map.remove(null), is("789"));
|
||||
assertThat(this.map.isEmpty(), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRemoveWhenKeyIsNotInMap() throws Exception {
|
||||
assertThat(this.map.remove(123), is(nullValue()));
|
||||
assertThat(this.map.remove(null), is(nullValue()));
|
||||
assertThat(this.map.isEmpty(), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPutAll() throws Exception {
|
||||
Map<Integer, String> m = new HashMap<Integer, String>();
|
||||
m.put(123, "123");
|
||||
m.put(456, null);
|
||||
m.put(null, "789");
|
||||
this.map.putAll(m);
|
||||
assertThat(this.map.size(), is(3));
|
||||
assertThat(this.map.get(123), is("123"));
|
||||
assertThat(this.map.get(456), is(nullValue()));
|
||||
assertThat(this.map.get(null), is("789"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldClear() throws Exception {
|
||||
this.map.put(123, "123");
|
||||
this.map.put(456, null);
|
||||
this.map.put(null, "789");
|
||||
this.map.clear();
|
||||
assertThat(this.map.size(), is(0));
|
||||
assertThat(this.map.containsKey(123), is(false));
|
||||
assertThat(this.map.containsKey(456), is(false));
|
||||
assertThat(this.map.containsKey(null), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetKeySet() throws Exception {
|
||||
this.map.put(123, "123");
|
||||
this.map.put(456, null);
|
||||
this.map.put(null, "789");
|
||||
Set<Integer> expected = new HashSet<Integer>();
|
||||
expected.add(123);
|
||||
expected.add(456);
|
||||
expected.add(null);
|
||||
assertThat(this.map.keySet(), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetValues() throws Exception {
|
||||
this.map.put(123, "123");
|
||||
this.map.put(456, null);
|
||||
this.map.put(null, "789");
|
||||
List<String> actual = new ArrayList<String>(this.map.values());
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("123");
|
||||
expected.add(null);
|
||||
expected.add("789");
|
||||
Collections.sort(actual, NULL_SAFE_STRING_SORT);
|
||||
Collections.sort(expected, NULL_SAFE_STRING_SORT);
|
||||
assertThat(actual, is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetEntrySet() throws Exception {
|
||||
this.map.put(123, "123");
|
||||
this.map.put(456, null);
|
||||
this.map.put(null, "789");
|
||||
HashMap<Integer, String> expected = new HashMap<Integer, String>();
|
||||
expected.put(123, "123");
|
||||
expected.put(456, null);
|
||||
expected.put(null, "789");
|
||||
assertThat(this.map.entrySet(), is(expected.entrySet()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetEntrySetFollowingNext() throws Exception {
|
||||
// Use loadFactor to disable resize
|
||||
this.map = new TestWeakConcurrentCache<Integer, String>(1, 10.0f, 1);
|
||||
this.map.put(1, "1");
|
||||
this.map.put(2, "2");
|
||||
this.map.put(3, "3");
|
||||
HashMap<Integer, String> expected = new HashMap<Integer, String>();
|
||||
expected.put(1, "1");
|
||||
expected.put(2, "2");
|
||||
expected.put(3, "3");
|
||||
assertThat(this.map.entrySet(), is(expected.entrySet()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRemoveViaEntrySet() throws Exception {
|
||||
this.map.put(1, "1");
|
||||
this.map.put(2, "2");
|
||||
this.map.put(3, "3");
|
||||
Iterator<Map.Entry<Integer, String>> iterator = this.map.entrySet().iterator();
|
||||
iterator.next();
|
||||
iterator.next();
|
||||
iterator.remove();
|
||||
iterator.next();
|
||||
assertThat(iterator.hasNext(), is(false));
|
||||
assertThat(this.map.size(), is(2));
|
||||
assertThat(this.map.containsKey(2), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSetViaEntrySet() throws Exception {
|
||||
this.map.put(1, "1");
|
||||
this.map.put(2, "2");
|
||||
this.map.put(3, "3");
|
||||
Iterator<Map.Entry<Integer, String>> iterator = this.map.entrySet().iterator();
|
||||
iterator.next();
|
||||
iterator.next().setValue("2b");
|
||||
iterator.next();
|
||||
assertThat(iterator.hasNext(), is(false));
|
||||
assertThat(this.map.size(), is(3));
|
||||
assertThat(this.map.get(2), is("2b"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore("Intended for use during development only")
|
||||
public void shouldBeFasterThanSynchronizedMap() throws Exception {
|
||||
Map<Integer, WeakReference<String>> synchronizedMap = Collections.synchronizedMap(new WeakHashMap<Integer, WeakReference<String>>());
|
||||
StopWatch mapTime = timeMultiThreaded("SynchronizedMap", synchronizedMap,
|
||||
new ValueFactory<WeakReference<String>>() {
|
||||
|
||||
public WeakReference<String> newValue(int v) {
|
||||
return new WeakReference<String>(String.valueOf(v));
|
||||
}
|
||||
});
|
||||
System.out.println(mapTime.prettyPrint());
|
||||
|
||||
this.map.setDisableTestHooks(true);
|
||||
StopWatch cacheTime = timeMultiThreaded("WeakConcurrentCache", this.map,
|
||||
new ValueFactory<String>() {
|
||||
|
||||
public String newValue(int v) {
|
||||
return String.valueOf(v);
|
||||
}
|
||||
});
|
||||
System.out.println(cacheTime.prettyPrint());
|
||||
|
||||
// We should be at least 4 time faster
|
||||
assertThat(cacheTime.getTotalTimeSeconds(),
|
||||
is(lessThan(mapTime.getTotalTimeSeconds() / 4.0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSupportNullReference() throws Exception {
|
||||
// GC could happen during restructure so we must be able to create a reference for a null entry
|
||||
map.createReferenceManager().createReference(null, 1234, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Time a multi-threaded access to a cache.
|
||||
*
|
||||
* @param cache the cache to test
|
||||
* @return the timing stopwatch
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
private <V> StopWatch timeMultiThreaded(String id, final Map<Integer, V> map,
|
||||
ValueFactory<V> factory) throws InterruptedException {
|
||||
StopWatch stopWatch = new StopWatch(id);
|
||||
for (int i = 0; i < 500; i++) {
|
||||
map.put(i, factory.newValue(i));
|
||||
}
|
||||
Thread[] threads = new Thread[30];
|
||||
stopWatch.start("Running threads");
|
||||
for (int threadIndex = 0; threadIndex < threads.length; threadIndex++) {
|
||||
threads[threadIndex] = new Thread("Cache access thread " + threadIndex) {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
for (int j = 0; j < 1000; j++) {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
map.get(i);
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
for (int i = 0; i < threads.length; i++) {
|
||||
threads[i].start();
|
||||
}
|
||||
|
||||
for (int i = 0; i < threads.length; i++) {
|
||||
if (threads[i].isAlive()) {
|
||||
threads[i].join(2000);
|
||||
}
|
||||
}
|
||||
stopWatch.stop();
|
||||
return stopWatch;
|
||||
}
|
||||
|
||||
private static interface ValueFactory<V> {
|
||||
|
||||
V newValue(int k);
|
||||
}
|
||||
|
||||
private static class TestWeakConcurrentCache<K, V> extends
|
||||
ConcurrentReferenceHashMap<K, V> {
|
||||
|
||||
private int supplimentalHash;
|
||||
|
||||
private final LinkedList<MockReference<K, V>> queue = new LinkedList<MockReference<K, V>>();
|
||||
|
||||
private boolean disableTestHooks;
|
||||
|
||||
public TestWeakConcurrentCache() {
|
||||
super();
|
||||
}
|
||||
|
||||
public void setDisableTestHooks(boolean disableTestHooks) {
|
||||
this.disableTestHooks = disableTestHooks;
|
||||
}
|
||||
|
||||
public TestWeakConcurrentCache(int initialCapacity, float loadFactor,
|
||||
int concurrencyLevel) {
|
||||
super(initialCapacity, loadFactor, concurrencyLevel);
|
||||
}
|
||||
|
||||
public TestWeakConcurrentCache(int initialCapacity, int concurrencyLevel) {
|
||||
super(initialCapacity, concurrencyLevel);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getHash(Object o) {
|
||||
if (this.disableTestHooks) {
|
||||
return super.getHash(o);
|
||||
}
|
||||
// For testing we want more control of the hash
|
||||
this.supplimentalHash = super.getHash(o);
|
||||
return o == null ? 0 : o.hashCode();
|
||||
}
|
||||
|
||||
public int getSupplimentalHash() {
|
||||
return this.supplimentalHash;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ReferenceManager createReferenceManager() {
|
||||
|
||||
return new ReferenceManager() {
|
||||
|
||||
@Override
|
||||
public Reference<K, V> createReference(Entry<K, V> entry, int hash,
|
||||
Reference<K, V> next) {
|
||||
if (TestWeakConcurrentCache.this.disableTestHooks) {
|
||||
return super.createReference(entry, hash, next);
|
||||
}
|
||||
return new MockReference<K, V>(entry, hash, next, TestWeakConcurrentCache.this.queue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Reference<K, V> pollForPurge() {
|
||||
if (TestWeakConcurrentCache.this.disableTestHooks) {
|
||||
return super.pollForPurge();
|
||||
}
|
||||
return TestWeakConcurrentCache.this.queue.isEmpty() ? null : TestWeakConcurrentCache.this.queue.removeFirst();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public MockReference<K, V> getMockReference(K key, Restructure restructure) {
|
||||
return (MockReference<K, V>) super.getReference(key, restructure);
|
||||
}
|
||||
}
|
||||
|
||||
private static class MockReference<K, V> implements Reference<K, V> {
|
||||
|
||||
private final int hash;
|
||||
|
||||
private Entry<K, V> entry;
|
||||
|
||||
private final Reference<K, V> next;
|
||||
|
||||
private final LinkedList<MockReference<K, V>> queue;
|
||||
|
||||
public MockReference(Entry<K, V> entry, int hash, Reference<K, V> next,
|
||||
LinkedList<MockReference<K, V>> queue) {
|
||||
this.hash = hash;
|
||||
this.entry = entry;
|
||||
this.next = next;
|
||||
this.queue = queue;
|
||||
}
|
||||
|
||||
public Entry<K, V> get() {
|
||||
return this.entry;
|
||||
}
|
||||
|
||||
public int getHash() {
|
||||
return this.hash;
|
||||
}
|
||||
|
||||
public Reference<K, V> getNext() {
|
||||
return this.next;
|
||||
}
|
||||
|
||||
public void release() {
|
||||
this.queue.add(this);
|
||||
this.entry = null;
|
||||
}
|
||||
|
||||
public void queueForPurge() {
|
||||
this.queue.add(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue