org.datanucleus.cache
Class WeakLevel2Cache

java.lang.Object
  extended by org.datanucleus.cache.WeakLevel2Cache
All Implemented Interfaces:
java.io.Serializable, Level2Cache
Direct Known Subclasses:
SoftLevel2Cache

public class WeakLevel2Cache
extends java.lang.Object
implements Level2Cache

Weak referenced implementation of a Level 2 cache.

Operates with 2 maps internally. One stores all pinned objects that have been selected to be retained by user's application. The other stores all other objects. This second map is the default location where objects are placed when being added here. The second (unpinned) map stores weak references meaning that they can get garbage collected as necessary by the JVM.

Maintains collections of the classes and the identities that are to be pinned if they ever are put into the cache. These are defined by the pinAll(), pin() methods.

All mutating methods, and the get method have been synchronized to prevent conflicts.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.datanucleus.cache.Level2Cache
Level2Cache.PinnedClass
 
Field Summary
protected  ApiAdapter apiAdapter
           
protected  java.util.Map pinnedCache
          Pinned objects cache.
protected  java.util.Collection pinnedClasses
          Collection of pinned classes whose objects should be pinned if they ever reach the cache.
protected  java.util.Collection pinnedIds
          Collection of ids whose objects should be pinned if they ever reach the cache.
protected  java.util.Map unpinnedCache
          Unpinned objects cache.
 
Constructor Summary
protected WeakLevel2Cache()
           
  WeakLevel2Cache(OMFContext omfCtx)
          Constructor.
 
Method Summary
 void clear()
          Method to clear the cache.
 void close()
          Method to close the cache when no longer needed.
 boolean containsOid(java.lang.Object oid)
          Method to check if an object with the specified id is in the cache
 void evict(java.lang.Object oid)
          Method to evict an object from the cache.
 void evictAll()
          Method to evict all objects from the L2 cache.
 void evictAll(java.lang.Class pcClass, boolean subclasses)
          Method to evict all objects of the given types from the cache.
 void evictAll(java.util.Collection oids)
          Method to evict the objects with the specified ids.
 void evictAll(java.lang.Object[] oids)
          Method to evict the objects with the specified ids.
 CachedPC get(java.lang.Object oid)
          Accessor for an object from the cache.
 int getNumberOfPinnedObjects()
          Accessor for the number of pinned objects in the cache.
 int getNumberOfUnpinnedObjects()
          Accessor for the number of unpinned objects in the cache.
 int getSize()
          Accessor for the total number of objects in the L2 cache.
 boolean isEmpty()
          Accessor for whether the cache is empty.
 void pin(java.lang.Object oid)
          Method to pin an object to the cache.
 void pinAll(java.lang.Class cls, boolean subs)
          Method to pin all objects of the given types.
 void pinAll(java.util.Collection oids)
          Method to pin all of the supplied objects
 void pinAll(java.lang.Object[] oids)
          Method to pin all of the supplied objects
 CachedPC put(java.lang.Object oid, CachedPC pc)
          Method to put an object in the cache.
 void unpin(java.lang.Object oid)
          Method to unpin an object
 void unpinAll(java.lang.Class cls, boolean subs)
          Method to unpin all objects of the specified types.
 void unpinAll(java.util.Collection oids)
          Method to unpin all of the supplied objects
 void unpinAll(java.lang.Object[] oids)
          Method to unpin all of the specified objects
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

pinnedClasses

protected java.util.Collection pinnedClasses
Collection of pinned classes whose objects should be pinned if they ever reach the cache.


pinnedIds

protected java.util.Collection pinnedIds
Collection of ids whose objects should be pinned if they ever reach the cache.


pinnedCache

protected java.util.Map pinnedCache
Pinned objects cache.


unpinnedCache

protected transient java.util.Map unpinnedCache
Unpinned objects cache.


apiAdapter

protected ApiAdapter apiAdapter
Constructor Detail

WeakLevel2Cache

protected WeakLevel2Cache()

WeakLevel2Cache

public WeakLevel2Cache(OMFContext omfCtx)
Constructor.

Parameters:
omfCtx - OMF Context
Method Detail

close

public void close()
Method to close the cache when no longer needed. Provides a hook to release resources etc.

Specified by:
close in interface Level2Cache

evict

public void evict(java.lang.Object oid)
Method to evict an object from the cache.

Specified by:
evict in interface Level2Cache
Parameters:
oid - The id of the object to evict

evictAll

public void evictAll()
Method to evict all objects from the L2 cache.

Specified by:
evictAll in interface Level2Cache

evictAll

public void evictAll(java.lang.Class pcClass,
                     boolean subclasses)
Method to evict all objects of the given types from the cache.

Specified by:
evictAll in interface Level2Cache
Parameters:
pcClass - The class to evict
subclasses - Whether to also evict subclasses

evictAll

public void evictAll(java.util.Collection oids)
Method to evict the objects with the specified ids.

Specified by:
evictAll in interface Level2Cache
Parameters:
oids - The ids of the objects to evict

evictAll

public void evictAll(java.lang.Object[] oids)
Method to evict the objects with the specified ids.

Specified by:
evictAll in interface Level2Cache
Parameters:
oids - The ids of the objects to evict

pin

public void pin(java.lang.Object oid)
Method to pin an object to the cache.

Specified by:
pin in interface Level2Cache
Parameters:
oid - The id of the object to pin

pinAll

public void pinAll(java.lang.Class cls,
                   boolean subs)
Method to pin all objects of the given types.

Specified by:
pinAll in interface Level2Cache
Parameters:
cls - The class
subs - Whether to include subclasses

pinAll

public void pinAll(java.util.Collection oids)
Method to pin all of the supplied objects

Specified by:
pinAll in interface Level2Cache
Parameters:
oids - The Object ids to pin

pinAll

public void pinAll(java.lang.Object[] oids)
Method to pin all of the supplied objects

Specified by:
pinAll in interface Level2Cache
Parameters:
oids - The object ids to pin

unpin

public void unpin(java.lang.Object oid)
Method to unpin an object

Specified by:
unpin in interface Level2Cache
Parameters:
oid - The object id

unpinAll

public void unpinAll(java.lang.Class cls,
                     boolean subs)
Method to unpin all objects of the specified types.

Specified by:
unpinAll in interface Level2Cache
Parameters:
cls - Base class
subs - Whether to include subclasses

unpinAll

public void unpinAll(java.util.Collection oids)
Method to unpin all of the supplied objects

Specified by:
unpinAll in interface Level2Cache
Parameters:
oids - The object ids to unpin

unpinAll

public void unpinAll(java.lang.Object[] oids)
Method to unpin all of the specified objects

Specified by:
unpinAll in interface Level2Cache
Parameters:
oids - The object ids to unpin

clear

public void clear()
Method to clear the cache.

Specified by:
clear in interface Level2Cache

get

public CachedPC get(java.lang.Object oid)
Accessor for an object from the cache. The returned object will not have a StateManager connected. This is because data stored in the Level 2 cache is StateManager and PersistenceManager independent.

Specified by:
get in interface Level2Cache
Parameters:
oid - The Object ID
Returns:
The L2 cacheable object

getNumberOfPinnedObjects

public int getNumberOfPinnedObjects()
Accessor for the number of pinned objects in the cache.

Specified by:
getNumberOfPinnedObjects in interface Level2Cache
Returns:
Number of pinned objects

getNumberOfUnpinnedObjects

public int getNumberOfUnpinnedObjects()
Accessor for the number of unpinned objects in the cache.

Specified by:
getNumberOfUnpinnedObjects in interface Level2Cache
Returns:
Number of unpinned objects

getSize

public int getSize()
Accessor for the total number of objects in the L2 cache.

Specified by:
getSize in interface Level2Cache
Returns:
Number of objects

put

public CachedPC put(java.lang.Object oid,
                    CachedPC pc)
Method to put an object in the cache. Note that the pc object being passed in must NOT have a StateManager connected. Data stored in the Level 2 cache has to be independent of PersistenceManager and StateManager.

Specified by:
put in interface Level2Cache
Parameters:
oid - The Object id for this object
pc - The cacheable object
Returns:
The value previously associated with this oid

containsOid

public boolean containsOid(java.lang.Object oid)
Method to check if an object with the specified id is in the cache

Specified by:
containsOid in interface Level2Cache
Parameters:
oid - The object ID
Returns:
Whether it is present

isEmpty

public boolean isEmpty()
Accessor for whether the cache is empty.

Specified by:
isEmpty in interface Level2Cache
Returns:
Whether it is empty.


Copyright © 2011. All Rights Reserved.