wt.cache
Class DirtyMap

java.lang.Object
  extended byjava.util.AbstractMap
      extended bywt.cache.DirtyMap
All Implemented Interfaces:
Map

public class DirtyMap
extends AbstractMap

Manages thread-local modifications to a backing "global" map.

The following methods make thread-local modifications that are not committed to the global map until the current transaction commits:

The modification operations inherited from Map modify the backing map directly.

The query operations in this Map all check for thread-local modifications before going to the backing map. The key, entry, and values views of this map also check for thread-local modifications.

addListener and removeListener allow DirtyMapListeners to receive notifications of puts and removes.

Supported API: false

Extendable: false


Nested Class Summary
(package private)  class DirtyMap.DirtyEntryIterator
          Iterates over the thread-local and global mappinsg
static interface DirtyMap.DirtyMapListener
          Notified by putLater, putAllLater, removeLater, and removeAllLater when changes to mappings are commited to the shared cache.
static class DirtyMap.DirtyMapListenerAdapter
          Convenience base class for listener implementations.
(package private)  class DirtyMap.EntrySet
           
(package private)  class DirtyMap.KeySet
           
(package private)  class DirtyMap.Values
           
 
Nested classes inherited from class java.util.AbstractMap
 
Nested classes inherited from class java.util.Map
Map.Entry
 
Field Summary
protected  Object contextKey
          Key used to look up the thread-local cache in the method context
protected  Map data
          Backing store of mappings
private static DirtyMap.DirtyMapListener[] EMPTY_LISTENERS
           
private  Set entrySet
           
private  Set keySet
           
private  DirtyMap.DirtyMapListener[] listeners
           
protected static Object REMOVED
          Used as thev value in the dirty map to indicate a thread-local remove.
private  Collection values
           
 
Constructor Summary
DirtyMap(Map data)
          Constructs a cache with a private context key
DirtyMap(Map data, Object context_key)
          Constructs a cache with the given context key
 
Method Summary
 void addListener(DirtyMap.DirtyMapListener listener)
           
 void clear()
          Clears all mappings from the global cache.
protected  Object clearDirty(Object key)
          Removes the given mapping from the thread-local cache
 void clearLater()
          Removes all the current mappings from the global cache.
 boolean containsKey(Object key)
          Determine if the given key exists in the thread-local cache; if there is no thread-local mapping, check the global cache.
 boolean containsValue(Object value)
          Determine if the given value exists in the thread-local cache; if there is no thread-local mapping, check the global cache.
 Set entrySet()
          Returns a Set view of the thread-local and global Entry objects in this cache.
 boolean equals(Object o)
           
 Object get(Object key)
          Get the mapping for the given key from the thread-local cache; if there is no thread-local mapping, check the global cache.
protected  Map getDirtyMap()
          Gets the current thread's dirty map.
 int hashCode()
           
protected  boolean isTransactionActive()
          Determines if we are in a transaction
 Set keySet()
          Returns a Set view of the thread-local and global keys in this cache.
private  void notifyPut(Object key, Object value, Object placeholder)
           
private  void notifyPutAll(Map mappings, Object placeholder)
           
private  void notifyRemove(Object key)
           
private  void notifyRemoveAll(Collection keys)
           
 Object put(Object key, Object value)
          Puts the given mapping in the global cache.
 void putAllLater(Map mappings, Object placeholder)
          Puts all the mappings in the given map into the cache, following the same semantics as putLater(Object,Object,Object).
protected  Object putDirty(Object key, Object dirty)
          Puts the given mapping in the thread-local cache
 void putLater(Object key, Object value, Object placeholder)
          Keeps change in thread-local cache until the current transaction commits.
 Object remove(Object key)
          Remove the mapping for the given key from the global cache.
 void removeAll(Collection keys)
          Convenience method that removes all the mappings for the keys in the given collection
 void removeAllLater(Collection keys)
          Removes all the keys in the given set, following the semantics of removeLater(Object)
protected  Object removeDirty(Object key)
          Records the removal of the given mapping in the thread-local cache.
 void removeLater(Object key)
          Overridden to keep change in thread-local cache until the current transaction commits
 void removeListener(DirtyMap.DirtyMapListener listener)
           
 int size()
          Returns the size of the global cache, minus any thread-local removals.
 String toString()
           
 Collection values()
          Returns a Collection view of the thread-local and global values in this cache.
 
Methods inherited from class java.util.AbstractMap
clone, isEmpty, putAll
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

EMPTY_LISTENERS

private static final DirtyMap.DirtyMapListener[] EMPTY_LISTENERS

contextKey

protected final Object contextKey
Key used to look up the thread-local cache in the method context


REMOVED

protected static final Object REMOVED
Used as thev value in the dirty map to indicate a thread-local remove. Also used as the key in the dirty map to look up the Set of all current thread-local removes


data

protected final Map data
Backing store of mappings


keySet

private volatile Set keySet

entrySet

private volatile Set entrySet

values

private volatile Collection values

listeners

private volatile DirtyMap.DirtyMapListener[] listeners
Constructor Detail

DirtyMap

public DirtyMap(Map data)
Constructs a cache with a private context key

Parameters:
data -

DirtyMap

public DirtyMap(Map data,
                Object context_key)
Constructs a cache with the given context key

Parameters:
data -
context_key -
Method Detail

size

public int size()
Returns the size of the global cache, minus any thread-local removals. The result of this method may be incorrect if mappings that have been removed thread-locally are directly re-added to the backing gloabl cache

Returns:
int

containsKey

public boolean containsKey(Object key)
Determine if the given key exists in the thread-local cache; if there is no thread-local mapping, check the global cache.

Parameters:
key -
Returns:
boolean

containsValue

public boolean containsValue(Object value)
Determine if the given value exists in the thread-local cache; if there is no thread-local mapping, check the global cache.

Parameters:
value -
Returns:
boolean

get

public Object get(Object key)
Get the mapping for the given key from the thread-local cache; if there is no thread-local mapping, check the global cache.

Parameters:
key -
Returns:
boolean

put

public Object put(Object key,
                  Object value)
Puts the given mapping in the global cache. To keep the modification thread-local until the current transaction commits, use putLater

Parameters:
key -
value -
Returns:
The previous global mapping for this key

remove

public Object remove(Object key)
Remove the mapping for the given key from the global cache. To keep the modification thread-local until the current transaction commits, use removeLater

Parameters:
key -
Returns:
The previous global mapping for this key

clear

public void clear()
Clears all mappings from the global cache. To clear the thread-local cache, use clearLater()


keySet

public Set keySet()
Returns a Set view of the thread-local and global keys in this cache. For implementation reasons the iterator over this set may not throw ConcurrentModificationExceptions. Removals from this set's iterator are not thread-local.

Returns:
Set

entrySet

public Set entrySet()
Returns a Set view of the thread-local and global Entry objects in this cache. For implementation reasons the iterator over this set may not throw ConcurrentModificationExceptions. Removals from this set's iterator are not thread-local.

Returns:
Set

values

public Collection values()
Returns a Collection view of the thread-local and global values in this cache. For implementation reasons the iterator over this Collection may not throw ConcurrentModificationExceptions. Removals from this collection's iterator are not thread-local.

Returns:
Collection

equals

public boolean equals(Object o)

hashCode

public int hashCode()

toString

public String toString()

getDirtyMap

protected Map getDirtyMap()
Gets the current thread's dirty map.

Returns:
The map, or null if there are no current thread-local modifications

removeAll

public void removeAll(Collection keys)
Convenience method that removes all the mappings for the keys in the given collection

Parameters:
keys - The keys to remove mappings for

putDirty

protected Object putDirty(Object key,
                          Object dirty)
Puts the given mapping in the thread-local cache

Parameters:
key -
Returns:
The previous thread-local mapping for the key, or null if there was no previous mapping

removeDirty

protected Object removeDirty(Object key)
Records the removal of the given mapping in the thread-local cache. The mapping stays in the global cache.

Parameters:
key -
Returns:
The previous thread-local mapping for the key, or null if there was no previous mapping

clearDirty

protected Object clearDirty(Object key)
Removes the given mapping from the thread-local cache

Parameters:
key -
Returns:
The previous thread-local mapping for the key, or null if there was no previous mapping

putLater

public void putLater(Object key,
                     Object value,
                     Object placeholder)
              throws WTException
Keeps change in thread-local cache until the current transaction commits. If placeholder is non-null, then only modifies the cache if the key still maps to the local placeholder. This allows for the detection of concurrent modification of the key by another thread.

Warning: This method must be called from within an active transaction

Parameters:
key -
value -
placeholder -
Throws:
WTException

putAllLater

public void putAllLater(Map mappings,
                        Object placeholder)
                 throws WTException
Puts all the mappings in the given map into the cache, following the same semantics as putLater(Object,Object,Object). The implementation assumes that each key in the given map is cuurrently assigned to the placeholder parameter.

Parameters:
mappings - The mappings that should be added to the cache
placeholder - The placeholder to use
Throws:
WTException

removeLater

public void removeLater(Object key)
                 throws WTException
Overridden to keep change in thread-local cache until the current transaction commits

Warning: This method must be called from within an active transaction

Parameters:
key -
Throws:
WTException

removeAllLater

public void removeAllLater(Collection keys)
                    throws WTException
Removes all the keys in the given set, following the semantics of removeLater(Object)

Parameters:
keys -
Throws:
WTException

clearLater

public void clearLater()
                throws WTException
Removes all the current mappings from the global cache. Keeps the removals thread-local until the current transaction commits.

Throws:
WTException

isTransactionActive

protected boolean isTransactionActive()
Determines if we are in a transaction

Returns:
boolean

addListener

public void addListener(DirtyMap.DirtyMapListener listener)

removeListener

public void removeListener(DirtyMap.DirtyMapListener listener)

notifyPut

private void notifyPut(Object key,
                       Object value,
                       Object placeholder)

notifyRemove

private void notifyRemove(Object key)

notifyPutAll

private void notifyPutAll(Map mappings,
                          Object placeholder)

notifyRemoveAll

private void notifyRemoveAll(Collection keys)