org.terracotta.cache.impl
Class DistributedCacheImpl<K,V>

java.lang.Object
  extended by org.terracotta.cache.impl.DistributedCacheImpl<K,V>
All Implemented Interfaces:
com.tc.object.bytecode.NotClearable, ConcurrentMap<K,V>, Map<K,V>, DistributedCache<K,V>, Evictable<K>, LockableMap<K>
Direct Known Subclasses:
ClusteredStoreBackend

public class DistributedCacheImpl<K,V>
extends Object
implements DistributedCache<K,V>, Evictable<K>, com.tc.object.bytecode.NotClearable


Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
protected  ConcurrentDistributedMap<K,TimestampedValue<V>> data
           
 
Constructor Summary
  DistributedCacheImpl(CacheConfig config)
           
protected DistributedCacheImpl(CacheConfig config, ConcurrentDistributedMap<K,TimestampedValue<V>> concurrentDistributedMap)
           
 
Method Summary
 void clear()
           
 boolean containsKey(Object key)
           
 boolean containsValue(Object value)
           
 FinegrainedLock createFinegrainedLock(K key)
          Obtain a fine-grained lock instance for a particular key.
 Set<Map.Entry<K,V>> entrySet()
           
 void evictExpiredLocalElements()
          Trigger to walk through the local elements, look for expired elements, and evict them.
 void evictOrphanElements(com.tc.cluster.DsoCluster clusterInfo)
          Trigger to walk through the specified set of orphaned elements, check whether they have expired, and evict them from the store.
 V get(Object key)
           
 CacheConfig getConfig()
          Get the current config for the cache (which may or may not be the config passed to the cache originally.
 String getLockIdForKey(K key)
          Retrieves a lock ID that's specific for this cache and key.
 EvictionStatistics getStatistics()
          Retrieve the statistics for the current JVM.
 TimeSource getTimeSource()
           
 TimestampedValue<V> getTimestampedValue(K key)
          Get the value wrapped in a timestamped object.
 TimestampedValue<V> getTimestampedValueQuiet(K key)
          Get the value wrapped in a timestamped object without updating the last usage statistics.
 void initializeOnLoad()
          Terracotta method
protected  void initializeOnLoad(boolean startEviction)
           
protected  void invalidateCacheEntries(Iterator<K> keys)
           
protected  boolean isCapacityEvictionEnabled()
           
 boolean isEmpty()
           
protected  boolean isEvictionEnabled()
           
 boolean isStatisticsEnabled()
          Returns whether statistics are enabled.
 Set<K> keySet()
           
 int localSize()
          Get the number of elements local in the current node - this number should always be less than size(), and is likely to be different.
 void lockEntry(K key)
          Lock the entry corresponding to this key.
protected  void onEvict(K key, V value)
          Used by notify listeners of eviction By the time this function is called, the item has already been evicted
 V put(K key, V value)
           
 void putAll(Map<? extends K,? extends V> t)
           
 V putIfAbsent(K key, V value)
           
 void putNoReturn(K key, V value)
          Put the key/value into the map, replacing the existing value if present.
 V remove(Object key)
           
 boolean remove(Object key, Object value)
           
 void removeNoReturn(Object key)
          Remove the key from the map, if it exists.
 TimestampedValue<V> removeTimestampedValue(K key)
          Remove the timestamped value for the given key.
 V replace(K key, V value)
           
 boolean replace(K key, V oldValue, V newValue)
           
 void setStatisticsEnabled(boolean enabled)
          Toggle statistics being enabled or not.
 void setTimeSource(TimeSource timeSource)
          This is provided for testing purposes - it lets you override the source of System.currentTimeMillis() so that you can control it yourself in the test.
 void shutdown()
          Shut down the background eviction thread, if any.
 int size()
           
 boolean unlockedContainsKey(Object key)
           
 V unlockedGet(K key)
           
 TimestampedValue<V> unlockedGetTimestampedValue(K key)
           
 TimestampedValue unlockedGetTimestampedValueQuiet(K key)
           
 void unlockedPutNoReturn(K key, V value)
           
 void unlockedRemoveNoReturn(Object key)
           
 void unlockEntry(K key)
          Unlock the entry corresponding to this key
 V unsafeGet(K key, boolean quiet)
           
 TimestampedValue<V> unsafeGetTimestampedValue(K key, boolean quiet)
           
 Collection<V> values()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Field Detail

data

protected final ConcurrentDistributedMap<K,TimestampedValue<V>> data
Constructor Detail

DistributedCacheImpl

public DistributedCacheImpl(CacheConfig config)

DistributedCacheImpl

protected DistributedCacheImpl(CacheConfig config,
                               ConcurrentDistributedMap<K,TimestampedValue<V>> concurrentDistributedMap)
Method Detail

initializeOnLoad

public void initializeOnLoad()
Terracotta method


initializeOnLoad

protected void initializeOnLoad(boolean startEviction)

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map<K,V>

get

public V get(Object key)
Specified by:
get in interface Map<K,V>

getTimestampedValue

public TimestampedValue<V> getTimestampedValue(K key)
Description copied from interface: DistributedCache
Get the value wrapped in a timestamped object. This value is used entirely for maintaining the creation and last accessed time for TTI and TTL eviction.

Specified by:
getTimestampedValue in interface DistributedCache<K,V>
Parameters:
key - The key to search for
Returns:
The wrapped value or null if not found

getTimestampedValueQuiet

public TimestampedValue<V> getTimestampedValueQuiet(K key)
Description copied from interface: DistributedCache
Get the value wrapped in a timestamped object without updating the last usage statistics.

Specified by:
getTimestampedValueQuiet in interface DistributedCache<K,V>
Parameters:
key - The key to search for
Returns:
The wrapped value or null if not found
See Also:
DistributedCache.getTimestampedValue(Object)

removeTimestampedValue

public TimestampedValue<V> removeTimestampedValue(K key)
Description copied from interface: DistributedCache
Remove the timestamped value for the given key. Previous values will be returned as null if they're expired.

Specified by:
removeTimestampedValue in interface DistributedCache<K,V>
Parameters:
key - The key to search for
Returns:
The wrapped value or null if not found or expired

getLockIdForKey

public String getLockIdForKey(K key)
Description copied from interface: DistributedCache
Retrieves a lock ID that's specific for this cache and key.

Specified by:
getLockIdForKey in interface DistributedCache<K,V>

keySet

public Set<K> keySet()
Specified by:
keySet in interface Map<K,V>

put

public V put(K key,
             V value)
Specified by:
put in interface Map<K,V>

putIfAbsent

public V putIfAbsent(K key,
                     V value)
Specified by:
putIfAbsent in interface ConcurrentMap<K,V>

remove

public V remove(Object key)
Specified by:
remove in interface Map<K,V>

replace

public V replace(K key,
                 V value)
Specified by:
replace in interface ConcurrentMap<K,V>

clear

public void clear()
Specified by:
clear in interface Map<K,V>

size

public int size()
Specified by:
size in interface Map<K,V>

localSize

public int localSize()
Description copied from interface: DistributedCache
Get the number of elements local in the current node - this number should always be less than size(), and is likely to be different.

Specified by:
localSize in interface DistributedCache<K,V>
Returns:
The local size, in range, such that 0 < localSize < size

shutdown

public void shutdown()
Description copied from interface: DistributedCache
Shut down the background eviction thread, if any.

Specified by:
shutdown in interface DistributedCache<K,V>

evictExpiredLocalElements

public void evictExpiredLocalElements()
Description copied from interface: Evictable
Trigger to walk through the local elements, look for expired elements, and evict them.

Specified by:
evictExpiredLocalElements in interface Evictable<K>

evictOrphanElements

public void evictOrphanElements(com.tc.cluster.DsoCluster clusterInfo)
Description copied from interface: Evictable
Trigger to walk through the specified set of orphaned elements, check whether they have expired, and evict them from the store.

Specified by:
evictOrphanElements in interface Evictable<K>
Parameters:
clusterInfo - The dso cluster info which can be used to obtain the orphaned element list

invalidateCacheEntries

protected void invalidateCacheEntries(Iterator<K> keys)

onEvict

protected void onEvict(K key,
                       V value)
Used by notify listeners of eviction By the time this function is called, the item has already been evicted


setTimeSource

public void setTimeSource(TimeSource timeSource)
This is provided for testing purposes - it lets you override the source of System.currentTimeMillis() so that you can control it yourself in the test. If it's not called, SystemTimeSource is used which just calls System.currentTimeMillis(). Method public for tests in other projects

Parameters:
timeSource - The alternate TimeSource implementation

getTimeSource

public TimeSource getTimeSource()

entrySet

public Set<Map.Entry<K,V>> entrySet()
Specified by:
entrySet in interface Map<K,V>

putNoReturn

public void putNoReturn(K key,
                        V value)
Description copied from interface: DistributedCache
Put the key/value into the map, replacing the existing value if present. This method differs from the normal put() method only in that it returns void instead of the prior value. This is important in the cluster as it avoids faulting that value to return when it is rarely used.

Specified by:
putNoReturn in interface DistributedCache<K,V>
Parameters:
key - Key
value - Value

unlockedPutNoReturn

public void unlockedPutNoReturn(K key,
                                V value)

unsafeGet

public V unsafeGet(K key,
                   boolean quiet)

unsafeGetTimestampedValue

public TimestampedValue<V> unsafeGetTimestampedValue(K key,
                                                     boolean quiet)

unlockedGet

public V unlockedGet(K key)

unlockedGetTimestampedValue

public TimestampedValue<V> unlockedGetTimestampedValue(K key)

unlockedGetTimestampedValueQuiet

public TimestampedValue unlockedGetTimestampedValueQuiet(K key)

unlockedContainsKey

public boolean unlockedContainsKey(Object key)

removeNoReturn

public void removeNoReturn(Object key)
Description copied from interface: DistributedCache
Remove the key from the map, if it exists. This method differs from the normal remove() method only in that it returns void instead of the prior value. This is important in the cluster as it avoids faulting that value to return when it is rarely used.

Specified by:
removeNoReturn in interface DistributedCache<K,V>
Parameters:
key - Key

unlockedRemoveNoReturn

public void unlockedRemoveNoReturn(Object key)

isStatisticsEnabled

public boolean isStatisticsEnabled()
Description copied from interface: Evictable
Returns whether statistics are enabled.

Specified by:
isStatisticsEnabled in interface Evictable<K>
Returns:
true�when statistics are enabled; and false otherwise

setStatisticsEnabled

public void setStatisticsEnabled(boolean enabled)
Description copied from interface: Evictable
Toggle statistics being enabled or not. When statistics are disabled, the already accumulated data will be reset.

Specified by:
setStatisticsEnabled in interface Evictable<K>
Parameters:
enabled - true to enable statistics; and {@code false) to disable them

getStatistics

public EvictionStatistics getStatistics()
Description copied from interface: Evictable
Retrieve the statistics for the current JVM.

Specified by:
getStatistics in interface Evictable<K>
Returns:
the statistics instance for the current JVM, statistics aren't shared across the cluster

getConfig

public CacheConfig getConfig()
Description copied from interface: DistributedCache
Get the current config for the cache (which may or may not be the config passed to the cache originally.

Specified by:
getConfig in interface DistributedCache<K,V>
Returns:
The configuration for this cache

isEvictionEnabled

protected boolean isEvictionEnabled()

isCapacityEvictionEnabled

protected boolean isCapacityEvictionEnabled()

remove

public boolean remove(Object key,
                      Object value)
Specified by:
remove in interface ConcurrentMap<K,V>

replace

public boolean replace(K key,
                       V oldValue,
                       V newValue)
Specified by:
replace in interface ConcurrentMap<K,V>

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map<K,V>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map<K,V>

putAll

public void putAll(Map<? extends K,? extends V> t)
Specified by:
putAll in interface Map<K,V>

values

public Collection<V> values()
Specified by:
values in interface Map<K,V>

createFinegrainedLock

public FinegrainedLock createFinegrainedLock(K key)
Description copied from interface: LockableMap
Obtain a fine-grained lock instance for a particular key.

Specified by:
createFinegrainedLock in interface LockableMap<K>
Parameters:
key - the key for which a fine-grained lock has to be constructed
Returns:
an instance of fine-grained lock that can be used to perform primitive locking operations on a key

lockEntry

public void lockEntry(K key)
Description copied from interface: LockableMap
Lock the entry corresponding to this key.

Specified by:
lockEntry in interface LockableMap<K>
Parameters:
key - the key for which all entry accesses have to be locked

unlockEntry

public void unlockEntry(K key)
Description copied from interface: LockableMap
Unlock the entry corresponding to this key

Specified by:
unlockEntry in interface LockableMap<K>
Parameters:
key - the key for which all entry accesses have to be unlocked


Copyright © 2010 Terracotta, Inc.. All Rights Reserved.