ehcache

net.sf.ehcache.store.chm
Class SelectableConcurrentHashMap

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by net.sf.ehcache.store.chm.SelectableConcurrentHashMap
All Implemented Interfaces:
Serializable, ConcurrentMap<Object,Element>, Map<Object,Element>

public class SelectableConcurrentHashMap
extends AbstractMap<K,V>

SelectableConcurrentHashMap subclasses a repackaged version of ConcurrentHashMap ito allow efficient random sampling of the map values.

The random sampling technique involves randomly selecting a map Segment, and then selecting a number of random entry chains from that segment.

Author:
Chris Dennis
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleImmutableEntry<K,V>
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Constructor Summary
SelectableConcurrentHashMap(PoolAccessor poolAccessor, boolean elementPinningEnabled, int initialCapacity, float loadFactor, int concurrency, long maximumSize, RegisteredEventListeners cacheEventNotificationService)
           
 
Method Summary
 void clear()
          Removes all of the mappings from this map.
 boolean contains(Object value)
          Legacy method testing if some key maps into the specified value in this table.
 boolean containsKey(Object key)
          Tests if the specified object is a key in this table.
 boolean containsValue(Object value)
          Returns true if this map maps one or more keys to the specified value.
protected  net.sf.ehcache.store.chm.ConcurrentHashMap.Segment<Object,Element> createSegment(int initialCapacity, float lf)
           
 Enumeration<V> elements()
          Returns an enumeration of the values in this table.
 Set<Map.Entry<Object,Element>> entrySet()
          Returns a Set view of the mappings contained in this map.
 boolean evict()
           
 V get(Object key)
          Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
 Element[] getRandomValues(int size, Object keyHint)
           
protected static int hash(int h)
          Applies a supplemental hash function to a given hashCode, which defends against poor quality hash functions.
 boolean isEmpty()
          Returns true if this map contains no key-value mappings.
 boolean isPinned(Object key)
           
 Enumeration<K> keys()
          Returns an enumeration of the keys in this table.
 Set<Object> keySet()
          Returns a Set view of the keys contained in this map.
 ReentrantReadWriteLock lockFor(Object key)
           
 ReentrantReadWriteLock[] locks()
           
 Set pinnedKeySet()
           
 int pinnedSize()
           
 Element put(Object key, Element value)
          Maps the specified key to the specified value in this table.
 Element put(Object key, Element element, long sizeOf)
           
 void putAll(Map<? extends K,? extends V> m)
          Copies all of the mappings from the specified map to this one.
 Element putIfAbsent(Object key, Element value)
          
 Element putIfAbsent(Object key, Element element, long sizeOf)
           
 int quickSize()
          Returns the number of key-value mappings in this map without locking anything.
 void recalculateSize(Object key)
           
 V remove(Object key)
          Removes the key (and its corresponding value) from this map.
 boolean remove(Object key, Object value)
          
 V replace(K key, V value)
          
 boolean replace(K key, V oldValue, V newValue)
          
 void setMaxSize(long maxSize)
           
 void setPinned(Object key, boolean pinned)
           
 int size()
          Returns the number of key-value mappings in this map.
 Object storedObject(Element e)
          Return an object of the kind which will be stored when the element is going to be inserted
 void unpinAll()
           
 Collection<Element> values()
          Returns a Collection view of the values contained in this map.
 
Methods inherited from class java.util.AbstractMap
clone, equals, hashCode, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

SelectableConcurrentHashMap

public SelectableConcurrentHashMap(PoolAccessor poolAccessor,
                                   boolean elementPinningEnabled,
                                   int initialCapacity,
                                   float loadFactor,
                                   int concurrency,
                                   long maximumSize,
                                   RegisteredEventListeners cacheEventNotificationService)
Method Detail

setMaxSize

public void setMaxSize(long maxSize)

getRandomValues

public Element[] getRandomValues(int size,
                                 Object keyHint)

storedObject

public Object storedObject(Element e)
Return an object of the kind which will be stored when the element is going to be inserted

Parameters:
e - the element
Returns:
an object looking-alike the stored one

quickSize

public int quickSize()
Returns the number of key-value mappings in this map without locking anything. This may not give the exact element count as locking is avoided. If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Returns:
the number of key-value mappings in this map

size

public int size()
Returns the number of key-value mappings in this map. If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Specified by:
size in interface Map<Object,Element>
Returns:
the number of key-value mappings in this map

pinnedSize

public int pinnedSize()

lockFor

public ReentrantReadWriteLock lockFor(Object key)

locks

public ReentrantReadWriteLock[] locks()

put

public Element put(Object key,
                   Element value)
Maps the specified key to the specified value in this table. Neither the key nor the value can be null.

The value can be retrieved by calling the get method with a key that is equal to the original key.

Specified by:
put in interface Map<Object,Element>
Parameters:
key - key with which the specified value is to be associated
value - value to be associated with the specified key
Returns:
the previous value associated with key, or null if there was no mapping for key

putIfAbsent

public Element putIfAbsent(Object key,
                           Element value)

Specified by:
putIfAbsent in interface ConcurrentMap<Object,Element>
Returns:
the previous value associated with the specified key, or null if there was no mapping for the key

put

public Element put(Object key,
                   Element element,
                   long sizeOf)

putIfAbsent

public Element putIfAbsent(Object key,
                           Element element,
                           long sizeOf)

unpinAll

public void unpinAll()

setPinned

public void setPinned(Object key,
                      boolean pinned)

isPinned

public boolean isPinned(Object key)

keySet

public Set<Object> keySet()
Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. The set supports element removal, which removes the corresponding mapping from this map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

The view's iterator is a "weakly consistent" iterator that will never throw ConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.

Specified by:
keySet in interface Map<Object,Element>

values

public Collection<Element> values()
Returns a Collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. The collection supports element removal, which removes the corresponding mapping from this map, via the Iterator.remove, Collection.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

The view's iterator is a "weakly consistent" iterator that will never throw ConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.

Specified by:
values in interface Map<Object,Element>

entrySet

public Set<Map.Entry<Object,Element>> entrySet()
Returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

The view's iterator is a "weakly consistent" iterator that will never throw ConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.

Specified by:
entrySet in interface Map<Object,Element>

createSegment

protected net.sf.ehcache.store.chm.ConcurrentHashMap.Segment<Object,Element> createSegment(int initialCapacity,
                                                                                           float lf)

evict

public boolean evict()

recalculateSize

public void recalculateSize(Object key)

pinnedKeySet

public Set pinnedKeySet()

hash

protected static int hash(int h)
Applies a supplemental hash function to a given hashCode, which defends against poor quality hash functions. This is critical because ConcurrentHashMap uses power-of-two length hash tables, that otherwise encounter collisions for hashCodes that do not differ in lower or upper bits.


isEmpty

public boolean isEmpty()
Returns true if this map contains no key-value mappings.

Specified by:
isEmpty in interface Map<K,V>
Overrides:
isEmpty in class AbstractMap<K,V>
Returns:
true if this map contains no key-value mappings

get

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

More formally, if this map contains a mapping from a key k to a value v such that key.equals(k), then this method returns v; otherwise it returns null. (There can be at most one such mapping.)

Specified by:
get in interface Map<K,V>
Overrides:
get in class AbstractMap<K,V>
Throws:
NullPointerException - if the specified key is null

containsKey

public boolean containsKey(Object key)
Tests if the specified object is a key in this table.

Specified by:
containsKey in interface Map<K,V>
Overrides:
containsKey in class AbstractMap<K,V>
Parameters:
key - possible key
Returns:
true if and only if the specified object is a key in this table, as determined by the equals method; false otherwise.
Throws:
NullPointerException - if the specified key is null

containsValue

public boolean containsValue(Object value)
Returns true if this map maps one or more keys to the specified value. Note: This method requires a full internal traversal of the hash table, and so is much slower than method containsKey.

Specified by:
containsValue in interface Map<K,V>
Overrides:
containsValue in class AbstractMap<K,V>
Parameters:
value - value whose presence in this map is to be tested
Returns:
true if this map maps one or more keys to the specified value
Throws:
NullPointerException - if the specified value is null

contains

public boolean contains(Object value)
Legacy method testing if some key maps into the specified value in this table. This method is identical in functionality to containsValue(java.lang.Object), and exists solely to ensure full compatibility with class Hashtable, which supported this method prior to introduction of the Java Collections framework.

Parameters:
value - a value to search for
Returns:
true if and only if some key maps to the value argument in this table as determined by the equals method; false otherwise
Throws:
NullPointerException - if the specified value is null

putAll

public void putAll(Map<? extends K,? extends V> m)
Copies all of the mappings from the specified map to this one. These mappings replace any mappings that this map had for any of the keys currently in the specified map.

Specified by:
putAll in interface Map<K,V>
Overrides:
putAll in class AbstractMap<K,V>
Parameters:
m - mappings to be stored in this map

remove

public V remove(Object key)
Removes the key (and its corresponding value) from this map. This method does nothing if the key is not in the map.

Specified by:
remove in interface Map<K,V>
Overrides:
remove in class AbstractMap<K,V>
Parameters:
key - the key that needs to be removed
Returns:
the previous value associated with key, or null if there was no mapping for key
Throws:
NullPointerException - if the specified key is null

remove

public boolean remove(Object key,
                      Object value)

Specified by:
remove in interface ConcurrentMap<K,V>
Throws:
NullPointerException - if the specified key is null

replace

public boolean replace(K key,
                       V oldValue,
                       V newValue)

Specified by:
replace in interface ConcurrentMap<K,V>
Throws:
NullPointerException - if any of the arguments are null

replace

public V replace(K key,
                 V value)

Specified by:
replace in interface ConcurrentMap<K,V>
Returns:
the previous value associated with the specified key, or null if there was no mapping for the key
Throws:
NullPointerException - if the specified key or value is null

clear

public void clear()
Removes all of the mappings from this map.

Specified by:
clear in interface Map<K,V>
Overrides:
clear in class AbstractMap<K,V>

keys

public Enumeration<K> keys()
Returns an enumeration of the keys in this table.

Returns:
an enumeration of the keys in this table
See Also:
keySet()

elements

public Enumeration<V> elements()
Returns an enumeration of the values in this table.

Returns:
an enumeration of the values in this table
See Also:
values()

ehcache

Copyright © 2003-2012 Terracotta, Inc.. All Rights Reserved.