Package org.datanucleus.cache
Class AbstractReferencedLevel2Cache
- java.lang.Object
-
- org.datanucleus.cache.AbstractReferencedLevel2Cache
-
- All Implemented Interfaces:
Serializable
,Level2Cache
- Direct Known Subclasses:
SoftLevel2Cache
,WeakLevel2Cache
public abstract class AbstractReferencedLevel2Cache extends Object implements Level2Cache
Abstract referenced implementation of a Level 2 cache, to be used as the base for weak/soft variants.Operates with 3 maps internally. One stores all pinned objects that have been selected to be retained by user's application. The second stores all other objects, and is the default location where objects are placed when being added here, using weak references meaning that they can get garbage collected as necessary by the JVM. The third stores objects keyed by the unique key that they relate to.
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
-
-
Field Summary
Fields Modifier and Type Field Description protected ApiAdapter
apiAdapter
protected Map<Object,CachedPC>
pinnedCache
Pinned objects cache.protected Collection<org.datanucleus.cache.AbstractReferencedLevel2Cache.PinnedClass>
pinnedClasses
Collection of pinned classes whose objects should be pinned if they ever reach the cache.protected Collection
pinnedIds
Collection of ids whose objects should be pinned if they ever reach the cache.protected Map<CacheUniqueKey,CachedPC>
uniqueKeyCache
Unique Key cache.protected Map<Object,CachedPC>
unpinnedCache
Unpinned objects cache.
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractReferencedLevel2Cache()
AbstractReferencedLevel2Cache(NucleusContext nucleusCtx)
Constructor.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
close()
Method to close the cache when no longer needed.boolean
containsOid(Object oid)
Method to check if an object with the specified id is in the cachevoid
evict(Object oid)
Method to evict an object from the cache.void
evictAll()
Method to evict all objects from the L2 cache.void
evictAll(Class pcClass, boolean subclasses)
Method to evict all objects of the given types from the cache.void
evictAll(Object[] oids)
Method to evict the objects with the specified ids.void
evictAll(Collection oids)
Method to evict the objects with the specified ids.CachedPC
get(Object oid)
Accessor for an object from the cache.Map<Object,CachedPC>
getAll(Collection oids)
Accessor for a collection of objects 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.CachedPC
getUnique(CacheUniqueKey key)
Method to retrieve the id represented by the specified unique key.protected abstract void
initialiseCaches()
boolean
isEmpty()
Accessor for whether the cache is empty.void
pin(Object oid)
Method to pin an object to the cache.void
pinAll(Class cls, boolean subs)
Method to pin all objects of the given types.void
pinAll(Object[] oids)
Method to pin all of the supplied objectsvoid
pinAll(Collection oids)
Method to pin all of the supplied objectsCachedPC
put(Object oid, CachedPC pc)
Method to put an object in the cache.void
putAll(Map<Object,CachedPC> objs)
Method to put several objects into the cache.CachedPC
putUnique(CacheUniqueKey key, CachedPC pc)
Method to store a persistable object for this unique key.void
putUniqueAll(Map<CacheUniqueKey,CachedPC> objs)
Method to put several objects into the cache.void
removeUnique(CacheUniqueKey key)
Method to remove any object cached against the provided unique key.void
unpin(Object oid)
Method to unpin an objectvoid
unpinAll(Class cls, boolean subs)
Method to unpin all objects of the specified types.void
unpinAll(Object[] oids)
Method to unpin all of the specified objectsvoid
unpinAll(Collection oids)
Method to unpin all of the supplied objects
-
-
-
Field Detail
-
pinnedClasses
protected Collection<org.datanucleus.cache.AbstractReferencedLevel2Cache.PinnedClass> pinnedClasses
Collection of pinned classes whose objects should be pinned if they ever reach the cache.
-
pinnedIds
protected Collection pinnedIds
Collection of ids whose objects should be pinned if they ever reach the cache.
-
uniqueKeyCache
protected transient Map<CacheUniqueKey,CachedPC> uniqueKeyCache
Unique Key cache.
-
apiAdapter
protected ApiAdapter apiAdapter
-
-
Constructor Detail
-
AbstractReferencedLevel2Cache
protected AbstractReferencedLevel2Cache()
-
AbstractReferencedLevel2Cache
public AbstractReferencedLevel2Cache(NucleusContext nucleusCtx)
Constructor.- Parameters:
nucleusCtx
- Context
-
-
Method Detail
-
initialiseCaches
protected abstract void initialiseCaches()
-
close
public void close()
Method to close the cache when no longer needed. Provides a hook to release resources etc.- Specified by:
close
in interfaceLevel2Cache
-
evict
public void evict(Object oid)
Method to evict an object from the cache.- Specified by:
evict
in interfaceLevel2Cache
- 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 interfaceLevel2Cache
-
evictAll
public void evictAll(Class pcClass, boolean subclasses)
Method to evict all objects of the given types from the cache.- Specified by:
evictAll
in interfaceLevel2Cache
- Parameters:
pcClass
- The class to evictsubclasses
- Whether to also evict subclasses
-
evictAll
public void evictAll(Collection oids)
Method to evict the objects with the specified ids.- Specified by:
evictAll
in interfaceLevel2Cache
- Parameters:
oids
- The ids of the objects to evict
-
evictAll
public void evictAll(Object[] oids)
Method to evict the objects with the specified ids.- Specified by:
evictAll
in interfaceLevel2Cache
- Parameters:
oids
- The ids of the objects to evict
-
get
public CachedPC get(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 interfaceLevel2Cache
- Parameters:
oid
- The Object ID- Returns:
- The L2 cacheable object
-
getAll
public Map<Object,CachedPC> getAll(Collection oids)
Description copied from interface:Level2Cache
Accessor for a collection of objects from the cache.- Specified by:
getAll
in interfaceLevel2Cache
- Parameters:
oids
- The Object IDs- Returns:
- Map of the objects, keyed by the oids that are found
-
getNumberOfPinnedObjects
public int getNumberOfPinnedObjects()
Accessor for the number of pinned objects in the cache.- Specified by:
getNumberOfPinnedObjects
in interfaceLevel2Cache
- Returns:
- Number of pinned objects
-
getNumberOfUnpinnedObjects
public int getNumberOfUnpinnedObjects()
Accessor for the number of unpinned objects in the cache.- Specified by:
getNumberOfUnpinnedObjects
in interfaceLevel2Cache
- Returns:
- Number of unpinned objects
-
getSize
public int getSize()
Accessor for the total number of objects in the L2 cache.- Specified by:
getSize
in interfaceLevel2Cache
- Returns:
- Number of objects
-
putAll
public void putAll(Map<Object,CachedPC> objs)
Description copied from interface:Level2Cache
Method to put several objects into the cache.- Specified by:
putAll
in interfaceLevel2Cache
- Parameters:
objs
- Map of cacheable object keyed by its oid.
-
put
public CachedPC put(Object oid, CachedPC pc)
Method to put an object in the cache.- Specified by:
put
in interfaceLevel2Cache
- Parameters:
oid
- The Object id for this objectpc
- The cacheable object- Returns:
- The value previously associated with this oid
-
containsOid
public boolean containsOid(Object oid)
Method to check if an object with the specified id is in the cache- Specified by:
containsOid
in interfaceLevel2Cache
- 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 interfaceLevel2Cache
- Returns:
- Whether it is empty.
-
getUnique
public CachedPC getUnique(CacheUniqueKey key)
Description copied from interface:Level2Cache
Method to retrieve the id represented by the specified unique key.- Specified by:
getUnique
in interfaceLevel2Cache
- Parameters:
key
- Unique key- Returns:
- The "identity" of the object that this unique key represents
-
putUnique
public CachedPC putUnique(CacheUniqueKey key, CachedPC pc)
Description copied from interface:Level2Cache
Method to store a persistable object for this unique key.- Specified by:
putUnique
in interfaceLevel2Cache
- Parameters:
key
- The unique keypc
- The representation of the persistable object to cache- Returns:
- The previous object for this unique key if one was present, otherwise null
-
putUniqueAll
public void putUniqueAll(Map<CacheUniqueKey,CachedPC> objs)
Description copied from interface:Level2Cache
Method to put several objects into the cache.- Specified by:
putUniqueAll
in interfaceLevel2Cache
- Parameters:
objs
- Map of cacheable object keyed by the unique keys.
-
removeUnique
public void removeUnique(CacheUniqueKey key)
Description copied from interface:Level2Cache
Method to remove any object cached against the provided unique key.- Specified by:
removeUnique
in interfaceLevel2Cache
- Parameters:
key
- Unique key
-
pin
public void pin(Object oid)
Method to pin an object to the cache.- Specified by:
pin
in interfaceLevel2Cache
- Parameters:
oid
- The id of the object to pin
-
pinAll
public void pinAll(Class cls, boolean subs)
Method to pin all objects of the given types.- Specified by:
pinAll
in interfaceLevel2Cache
- Parameters:
cls
- The classsubs
- Whether to include subclasses
-
pinAll
public void pinAll(Collection oids)
Method to pin all of the supplied objects- Specified by:
pinAll
in interfaceLevel2Cache
- Parameters:
oids
- The Object ids to pin
-
pinAll
public void pinAll(Object[] oids)
Method to pin all of the supplied objects- Specified by:
pinAll
in interfaceLevel2Cache
- Parameters:
oids
- The object ids to pin
-
unpin
public void unpin(Object oid)
Method to unpin an object- Specified by:
unpin
in interfaceLevel2Cache
- Parameters:
oid
- The object id
-
unpinAll
public void unpinAll(Class cls, boolean subs)
Method to unpin all objects of the specified types.- Specified by:
unpinAll
in interfaceLevel2Cache
- Parameters:
cls
- Base classsubs
- Whether to include subclasses
-
unpinAll
public void unpinAll(Collection oids)
Method to unpin all of the supplied objects- Specified by:
unpinAll
in interfaceLevel2Cache
- Parameters:
oids
- The object ids to unpin
-
unpinAll
public void unpinAll(Object[] oids)
Method to unpin all of the specified objects- Specified by:
unpinAll
in interfaceLevel2Cache
- Parameters:
oids
- The object ids to unpin
-
-