Top Description Inners Fields Constructors Methods
org.apache.groovy.util.concurrent.concurrentlinkedhashmap

public final Class ConcurrentLinkedHashMap<K, V>

extends AbstractMap<K, V>
implements ConcurrentMap<K, V>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.util.concurrent.ConcurrentMap, java.util.Map
Annotations
@ThreadSafe
Type Parameters
<K>
the type of keys maintained by this map
<V>
the type of mapped values
Imports
org.apache.groovy.util.ObjectHolder, javax.annotation.concurrent.GuardedBy, .Immutable, .ThreadSafe, java.io.InvalidObjectException, .ObjectInputStream, .Serializable, java.util.AbstractCollection, .AbstractMap, .AbstractQueue, .AbstractSet, .Collection, .Collections, .HashMap, .Iterator, .LinkedHashMap, .LinkedHashSet, .Map, .Queue, .Set, java.util.concurrent.ConcurrentHashMap, .ConcurrentLinkedQueue, .ConcurrentMap, java.util.concurrent.atomic.AtomicLong, .AtomicReference, java.util.concurrent.locks.Lock, .ReentrantLock, java.util.function.Function

A hash table supporting full concurrency of retrievals, adjustable expected concurrency for updates, and a maximum capacity to bound the map by. This implementation differs from ConcurrentHashMap in that it maintains a page replacement algorithm that is used to evict an entry when the map has exceeded its capacity. Unlike the Java Collections Framework, this map does not have a publicly visible constructor and instances are created through a Builder.

An entry is evicted from the map when the weighted capacity exceeds its maximum weighted capacity threshold. A EntryWeigher determines how many units of capacity that an entry consumes. The default weigher assigns each value a weight of 1 to bound the map by the total number of key-value pairs. A map that holds collections may choose to weigh values by the number of elements in the collection and bound the map by the total number of elements that it contains. A change to a value that modifies its weight requires that an update operation is performed on the map.

An EvictionListener may be supplied for notification when an entry is evicted from the map. This listener is invoked on a caller's thread and will not block other threads from operating on the map. An implementation should be aware that the caller's thread will not expect long execution times or failures as a side effect of the listener being notified. Execution safety and a fast turn around time can be achieved by performing the operation asynchronously, such as by submitting a task to an java.util.concurrent.ExecutorService.

The concurrency level determines the number of threads that can concurrently modify the table. Using a significantly higher or lower value than needed can waste space or lead to thread contention, but an estimate within an order of magnitude of the ideal value does not usually have a noticeable impact. Because placement in hash tables is essentially random, the actual concurrency will vary.

This class and its views and iterators implement all of the optional methods of the Map and Iterator interfaces.

Like java.util.Hashtable but unlike HashMap, this class does not allow null to be used as a key or value. Unlike LinkedHashMap, this class does not provide predictable iteration order. A snapshot of the keys and entries may be obtained in ascending and descending order of retention.

See Also
http://code.google.com/p/concurrentlinkedhashmap/

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv class
ConcurrentLinkedHashMap.AddTask

Adds the node to the page replacement policy.

pack-priv static class
ConcurrentLinkedHashMap.BoundedEntryWeigher<K, V>

A weigher that enforces that the weight falls within a valid range.

public static class
ConcurrentLinkedHashMap.Builder<K, V>

A builder that creates ConcurrentLinkedHashMap instances.

pack-priv static enum
ConcurrentLinkedHashMap.DiscardingListener

A listener that ignores all notifications.

pack-priv static class
ConcurrentLinkedHashMap.DiscardingQueue

A queue that discards all additions and is always empty.

pack-priv static enum
ConcurrentLinkedHashMap.DrainStatus

The draining status of the buffers.

pack-priv class
ConcurrentLinkedHashMap.EntryIterator

An adapter to safely externalize the entry iterator.

pack-priv class
ConcurrentLinkedHashMap.EntrySet

An adapter to safely externalize the entries.

pack-priv class
ConcurrentLinkedHashMap.KeyIterator

An adapter to safely externalize the key iterator.

pack-priv class
ConcurrentLinkedHashMap.KeySet

An adapter to safely externalize the keys.

pack-priv static class
ConcurrentLinkedHashMap.Node<K, V>

A node contains the key, the weighted value, and the linkage pointers on the page-replacement algorithm's data structures.

pack-priv class
ConcurrentLinkedHashMap.RemovalTask

Removes a node from the page replacement policy.

pack-priv static class
ConcurrentLinkedHashMap.SerializationProxy<K, V>

A proxy that is serialized instead of the map.

pack-priv class
ConcurrentLinkedHashMap.UpdateTask

Updates the weighted size and evicts an entry on overflow.

pack-priv class
ConcurrentLinkedHashMap.ValueIterator

An adapter to safely externalize the value iterator.

pack-priv class
ConcurrentLinkedHashMap.Values

An adapter to safely externalize the values.

pack-priv static class
ConcurrentLinkedHashMap.WeightedValue<V>

A value, its weight, and the entry's status.

pack-priv class
ConcurrentLinkedHashMap.WriteThroughEntry

An entry that allows updates to write through to the map.

Field Summary

Modifier and TypeField and Description
pack-priv final AtomicLong
pack-priv final int
pack-priv final ConcurrentMap<K, ConcurrentLinkedHashMap.Node<K, V>>
pack-priv static final Queue<?>
DISCARDING_QUEUE

A queue that discards all entries.

pack-priv final AtomicReference<ConcurrentLinkedHashMap.DrainStatus>
pack-priv transient Set<Map.Entry<K, V>>
pack-priv final LinkedDeque<ConcurrentLinkedHashMap.Node<K, V>>
pack-priv final Lock
pack-priv transient Set<K>
pack-priv final EvictionListener<K, V>
pack-priv static final long
MAXIMUM_CAPACITY

The maximum weighted capacity of the map.

pack-priv static final int
NCPU

The number of CPUs

pack-priv static final int
NUMBER_OF_READ_BUFFERS

The number of read buffers to use.

pack-priv final Queue<ConcurrentLinkedHashMap.Node<K, V>>
pack-priv static final int
READ_BUFFER_DRAIN_THRESHOLD

The maximum number of read operations to perform per amortized drain.

pack-priv static final int
READ_BUFFER_INDEX_MASK

Mask value for indexing into the read buffer.

pack-priv static final int
READ_BUFFER_SIZE

The maximum number of pending reads per buffer.

pack-priv static final int
READ_BUFFER_THRESHOLD

The number of pending read operations before attempting to drain.

pack-priv static final int
READ_BUFFERS_MASK

Mask value for indexing into the read buffers.

pack-priv final AtomicLong[]
pack-priv final long[]
pack-priv final AtomicReference<ConcurrentLinkedHashMap.Node<K, V>>[][]
pack-priv final AtomicLong[]
pack-priv static final long
pack-priv transient Collection<V>
pack-priv final EntryWeigher<? super K, ? super V>
pack-priv final AtomicLong
pack-priv static final int
WRITE_BUFFER_DRAIN_THRESHOLD

The maximum number of write operations to perform per amortized drain.

pack-priv final Queue<Runnable>

Constructor Summary

AccessConstructor and Description
private
ConcurrentLinkedHashMap(ConcurrentLinkedHashMap.Builder<K, V> builder)

Creates an instance based on the builder's configuration.

Method Summary

Modifier and TypeMethod and Description
pack-priv void
afterRead(ConcurrentLinkedHashMap.Node<K, V>
the entry in the page replacement policy
node
)

Performs the post-processing work required after a read.

pack-priv void
afterWrite(Runnable
the pending operation to be applied
task
)

Performs the post-processing work required after a write.

pack-priv void
applyRead(ConcurrentLinkedHashMap.Node<K, V> node)

Updates the node's location in the page replacement policy.

public Set<K>

Returns:

an ascending snapshot view of the keys in this map
ascendingKeySet
()

Returns a unmodifiable snapshot Set view of the keys contained in this map.

public Set<K>

Returns:

a ascending snapshot view of the keys in this map
ascendingKeySetWithLimit
(int
the maximum size of the returned set
limit
)

Returns an unmodifiable snapshot Set view of the keys contained in this map.

public Map<K, V>

Returns:

a ascending snapshot view of this map
ascendingMap
()

Returns an unmodifiable snapshot Map view of the mappings contained in this map.

public Map<K, V>

Returns:

a ascending snapshot view of this map
ascendingMapWithLimit
(int
the maximum size of the returned map
limit
)

Returns an unmodifiable snapshot Map view of the mappings contained in this map.

public long

Returns:

the maximum weighted capacity
capacity
()

Retrieves the maximum weighted capacity of the map.

pack-priv static int
pack-priv static void
checkArgument(boolean expression)

Ensures that the argument expression is true.

pack-priv static void
checkNotNull(Object o)

Ensures that the object is not null.

pack-priv static void
checkState(boolean expression)

Ensures that the state expression is true.

public void
clear()

Overrides java.util.AbstractMap.clear.

Implements java.util.Map.clear.

Removes all of the mappings from this map (optional operation).
pack-priv V
compute(final K key, final Function<? super K, ? extends V> mappingFunction, boolean onlyIfAbsent)

public V

Returns:

the current (existing or computed) value associated with the specified key, or null if the computed value is null
computeIfAbsent
(K
key with which the specified value is to be associated
key
,
Function<? super K, ? extends V>
the function to compute a value
mappingFunction
)

Overrides default java.util.concurrent.ConcurrentMap.computeIfAbsent, java.util.Map.computeIfAbsent.

If the specified key is not already associated with a value, attempts to compute its value using the given mapping function and enters it into this map unless null.
public boolean
containsKey(Object
key whose presence in this map is to be tested
key
)

Overrides java.util.AbstractMap.containsKey.

Implements java.util.Map.containsKey.

Returns true if this map contains a mapping for the specified key.
public boolean
containsValue(Object
value whose presence in this map is to be tested
value
)

Overrides java.util.AbstractMap.containsValue.

Implements java.util.Map.containsValue.

Returns true if this map maps one or more keys to the specified value.
public Set<K>

Returns:

a descending snapshot view of the keys in this map
descendingKeySet
()

Returns an unmodifiable snapshot Set view of the keys contained in this map.

public Set<K>

Returns:

a descending snapshot view of the keys in this map
descendingKeySetWithLimit
(int
the maximum size of the returned set
limit
)

Returns an unmodifiable snapshot Set view of the keys contained in this map.

public Map<K, V>

Returns:

a descending snapshot view of this map
descendingMap
()

Returns an unmodifiable snapshot Map view of the mappings contained in this map.

public Map<K, V>

Returns:

a descending snapshot view of this map
descendingMapWithLimit
(int
the maximum size of the returned map
limit
)

Returns an unmodifiable snapshot Map view of the mappings contained in this map.

pack-priv void
drainBuffers()

Drains the read and write buffers up to an amortized threshold.

pack-priv void
drainOnReadIfNeeded(int
the index to the chosen read buffer
bufferIndex
,
long
the number of writes on the chosen read buffer
writeCount
)

Attempts to drain the buffers if it is determined to be needed when post-processing a read.

pack-priv void
drainReadBuffer(int bufferIndex)

Drains the read buffer up to an amortized threshold.

pack-priv void
drainReadBuffers()

Drains the read buffers, each up to an amortized threshold.

pack-priv void
drainWriteBuffer()

Drains the read buffer up to an amortized threshold.

public Set<Map.Entry<K, V>>
entrySet()

Implements abstract java.util.AbstractMap.entrySet.

Implements java.util.Map.entrySet.

Returns a Set view of the mappings contained in this map.
pack-priv void
evict()

Evicts entries from the map while it exceeds the capacity and appends evicted entries to the notification queue for processing.

public V
get(Object
the key whose associated value is to be returned
key
)

Overrides java.util.AbstractMap.get.

Implements java.util.Map.get.

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
public V

Returns:

the value to which the specified key is mapped, or null if this map contains no mapping for the key
getQuietly
(Object
the key whose associated value is to be returned
key
)

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

pack-priv boolean
hasOverflowed()

Determines whether the map has exceeded its capacity.

public boolean
isEmpty()

Overrides java.util.AbstractMap.isEmpty.

Implements java.util.Map.isEmpty.

Returns true if this map contains no key-value mappings.
public Set<K>
keySet()

Overrides java.util.AbstractMap.keySet.

Implements java.util.Map.keySet.

Returns a Set view of the keys contained in this map.
pack-priv void
makeDead(ConcurrentLinkedHashMap.Node<K, V>
the entry in the page replacement policy
node
)

Atomically transitions the node to the dead state and decrements the weightedSize.

pack-priv void
makeRetired(ConcurrentLinkedHashMap.Node<K, V>
the entry in the page replacement policy
node
)

Atomically transitions the node from the alive state to the retired state, if a valid transition.

pack-priv void
notifyListener()

Notifies the listener of entries that were evicted.

pack-priv Set<K>
orderedKeySet(boolean ascending, int limit)

pack-priv Map<K, V>
orderedMap(boolean ascending, int limit)

public V
put(K
key with which the specified value is to be associated
key
,
V
value to be associated with the specified key
value
)

Overrides java.util.AbstractMap.put.

Implements java.util.Map.put.

Associates the specified value with the specified key in this map (optional operation).
pack-priv V

Returns:

the prior value in the data store or null if no mapping was found
put
(K
key with which the specified value is to be associated
key
,
V
value to be associated with the specified key
value
,
boolean
a write is performed only if the key is not already associated with a value
onlyIfAbsent
)

Adds a node to the list and the data store.

public V
putIfAbsent(K
key with which the specified value is to be associated
key
,
V
value to be associated with the specified key
value
)

Overrides default java.util.Map.putIfAbsent.

Implements java.util.concurrent.ConcurrentMap.putIfAbsent.

If the specified key is not already associated with a value, associates it with the given value.
pack-priv static int
readBufferIndex()

Returns the index to the read buffer to record into.

private void
pack-priv long

Returns:

the number of writes on the chosen read buffer
recordRead
(int
the index to the chosen read buffer
bufferIndex
,
ConcurrentLinkedHashMap.Node<K, V>
the entry in the page replacement policy
node
)

Records a read in the buffer and return its write count.

public V
remove(Object
key whose mapping is to be removed from the map
key
)

Overrides java.util.AbstractMap.remove.

Implements java.util.Map.remove.

Removes the mapping for a key from this map if it is present (optional operation).
public boolean
remove(Object
key with which the specified value is associated
key
,
Object
value expected to be associated with the specified key
value
)

Overrides default java.util.Map.remove.

Implements java.util.concurrent.ConcurrentMap.remove.

Removes the entry for a key only if currently mapped to a given value.
public V
replace(K
key with which the specified value is associated
key
,
V
value to be associated with the specified key
value
)

Overrides default java.util.Map.replace.

Implements java.util.concurrent.ConcurrentMap.replace.

Replaces the entry for a key only if currently mapped to some value.
public boolean
replace(K
key with which the specified value is associated
key
,
V
value expected to be associated with the specified key
oldValue
,
V
value to be associated with the specified key
newValue
)

Overrides default java.util.Map.replace.

Implements java.util.concurrent.ConcurrentMap.replace.

Replaces the entry for a key only if currently mapped to a given value.
public void
setCapacity(long
the maximum weighted capacity of the map
capacity
)

Sets the maximum weighted capacity of the map and eagerly evicts entries until it shrinks to the appropriate size.

public int
size()

Overrides java.util.AbstractMap.size.

Implements java.util.Map.size.

Returns the number of key-value mappings in this map.
pack-priv void
tryToDrainBuffers()

Attempts to acquire the eviction lock and apply the pending operations, up to the amortized threshold, to the page replacement policy.

pack-priv boolean

Returns:

if successful
tryToRetire
(ConcurrentLinkedHashMap.Node<K, V>
the entry in the page replacement policy
node
,
ConcurrentLinkedHashMap.WeightedValue<V>
the expected weighted value
expect
)

Attempts to transition the node from the alive state to the retired state.

public Collection<V>
values()

Overrides java.util.AbstractMap.values.

Implements java.util.Map.values.

Returns a Collection view of the values contained in this map.
public long

Returns:

the combined weight of the values in this map
weightedSize
()

Returns the weighted size of this map.

pack-priv Object
Inherited from java.util.AbstractMap:
cloneequalshashCodeputAlltoString