HHH-15138 Further simplify BoundedConcurrentHashMap by removing support for NONE eviction
This commit is contained in:
parent
04bfdd49ab
commit
bee5bae7b6
|
@ -106,24 +106,12 @@ public class BoundedConcurrentHashMap<K, V> extends AbstractMap<K, V>
|
||||||
|
|
||||||
/* ---------------- Constants -------------- */
|
/* ---------------- Constants -------------- */
|
||||||
|
|
||||||
/**
|
|
||||||
* The default initial capacity for this table,
|
|
||||||
* used when not otherwise specified in a constructor.
|
|
||||||
*/
|
|
||||||
static final int DEFAULT_MAXIMUM_CAPACITY = 512;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The default load factor for this table, used when not
|
* The default load factor for this table, used when not
|
||||||
* otherwise specified in a constructor.
|
* otherwise specified in a constructor.
|
||||||
*/
|
*/
|
||||||
static final float DEFAULT_LOAD_FACTOR = 0.75f;
|
static final float DEFAULT_LOAD_FACTOR = 0.75f;
|
||||||
|
|
||||||
/**
|
|
||||||
* The default concurrency level for this table, used when not
|
|
||||||
* otherwise specified in a constructor.
|
|
||||||
*/
|
|
||||||
static final int DEFAULT_CONCURRENCY_LEVEL = 16;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The maximum capacity, used if a higher value is implicitly
|
* The maximum capacity, used if a higher value is implicitly
|
||||||
* specified by either of the constructors with arguments. MUST
|
* specified by either of the constructors with arguments. MUST
|
||||||
|
@ -258,12 +246,6 @@ public class BoundedConcurrentHashMap<K, V> extends AbstractMap<K, V>
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum Eviction {
|
public enum Eviction {
|
||||||
NONE {
|
|
||||||
@Override
|
|
||||||
public <K, V> EvictionPolicy<K, V> make(Segment<K, V> s, int capacity, float lf) {
|
|
||||||
return new NullEvictionPolicy<>();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
LRU {
|
LRU {
|
||||||
@Override
|
@Override
|
||||||
public <K, V> EvictionPolicy<K, V> make(Segment<K, V> s, int capacity, float lf) {
|
public <K, V> EvictionPolicy<K, V> make(Segment<K, V> s, int capacity, float lf) {
|
||||||
|
@ -345,47 +327,6 @@ public class BoundedConcurrentHashMap<K, V> extends AbstractMap<K, V>
|
||||||
boolean thresholdExpired();
|
boolean thresholdExpired();
|
||||||
}
|
}
|
||||||
|
|
||||||
static class NullEvictionPolicy<K, V> implements EvictionPolicy<K, V> {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void clear() {
|
|
||||||
// Do nothing.
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void execute() {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onEntryHit(HashEntry<K, V> e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onEntryMiss(HashEntry<K, V> e) {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onEntryRemove(HashEntry<K, V> e) {
|
|
||||||
// Do nothing.
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean thresholdExpired() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Eviction strategy() {
|
|
||||||
return Eviction.NONE;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public HashEntry<K, V> createNewEntry(K key, int hash, HashEntry<K, V> next, V value) {
|
|
||||||
return new HashEntry<>( key, hash, next, value );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static final class LRU<K, V> extends LinkedHashMap<HashEntry<K, V>, V> implements EvictionPolicy<K, V> {
|
static final class LRU<K, V> extends LinkedHashMap<HashEntry<K, V>, V> implements EvictionPolicy<K, V> {
|
||||||
|
|
||||||
private final ConcurrentLinkedQueue<HashEntry<K, V>> accessQueue;
|
private final ConcurrentLinkedQueue<HashEntry<K, V>> accessQueue;
|
||||||
|
@ -1333,9 +1274,7 @@ public class BoundedConcurrentHashMap<K, V> extends AbstractMap<K, V>
|
||||||
lock();
|
lock();
|
||||||
try {
|
try {
|
||||||
int c = count;
|
int c = count;
|
||||||
if ( c++ > threshold && eviction.strategy() == Eviction.NONE ) {
|
c++;
|
||||||
rehash();
|
|
||||||
}
|
|
||||||
HashEntry<K, V>[] tab = table;
|
HashEntry<K, V>[] tab = table;
|
||||||
int index = hash & tab.length - 1;
|
int index = hash & tab.length - 1;
|
||||||
HashEntry<K, V> first = tab[index];
|
HashEntry<K, V> first = tab[index];
|
||||||
|
@ -1356,21 +1295,16 @@ public class BoundedConcurrentHashMap<K, V> extends AbstractMap<K, V>
|
||||||
oldValue = null;
|
oldValue = null;
|
||||||
++modCount;
|
++modCount;
|
||||||
count = c; // write-volatile
|
count = c; // write-volatile
|
||||||
if ( eviction.strategy() != Eviction.NONE ) {
|
if ( c > evictCap ) {
|
||||||
if ( c > evictCap ) {
|
// remove entries;lower count
|
||||||
// remove entries;lower count
|
eviction.execute();
|
||||||
eviction.execute();
|
// re-read first
|
||||||
// re-read first
|
first = tab[index];
|
||||||
first = tab[index];
|
|
||||||
}
|
|
||||||
// add a new entry
|
|
||||||
tab[index] = eviction.createNewEntry( key, hash, first, value );
|
|
||||||
// notify a miss
|
|
||||||
eviction.onEntryMiss( tab[index] );
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
tab[index] = eviction.createNewEntry( key, hash, first, value );
|
|
||||||
}
|
}
|
||||||
|
// add a new entry
|
||||||
|
tab[index] = eviction.createNewEntry( key, hash, first, value );
|
||||||
|
// notify a miss
|
||||||
|
eviction.onEntryMiss( tab[index] );
|
||||||
}
|
}
|
||||||
return oldValue;
|
return oldValue;
|
||||||
}
|
}
|
||||||
|
@ -1608,29 +1542,6 @@ public class BoundedConcurrentHashMap<K, V> extends AbstractMap<K, V>
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new, empty map with the specified maximum capacity, default concurrency
|
|
||||||
* level and LRU eviction policy.
|
|
||||||
*
|
|
||||||
* @param capacity is the upper bound capacity for the number of elements in this map
|
|
||||||
*
|
|
||||||
* @throws IllegalArgumentException if the initial capacity of
|
|
||||||
* elements is negative or the load factor is nonpositive
|
|
||||||
* @since 1.6
|
|
||||||
*/
|
|
||||||
public BoundedConcurrentHashMap(int capacity) {
|
|
||||||
this( capacity, DEFAULT_CONCURRENCY_LEVEL );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new, empty map with the default maximum capacity
|
|
||||||
*/
|
|
||||||
public BoundedConcurrentHashMap() {
|
|
||||||
this( DEFAULT_MAXIMUM_CAPACITY, DEFAULT_CONCURRENCY_LEVEL );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns {@code true} if this map contains no key-value mappings.
|
|
||||||
*
|
|
||||||
* @return {@code true} if this map contains no key-value mappings
|
* @return {@code true} if this map contains no key-value mappings
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue