|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.derby.impl.services.cache.Clock
final class Clock
A cache manager that uses a HashMap and an ArrayList. The ArrayList holds CachedItem objects, each with a holder object. The HashMap is keyed by the identity of the holder object (Cacheable.getIdentity()) and the data portion is a pointer to the CachedItem. CachedItems that have holder objects with no identity do not have entries in the hash map.
CachedItems can in various state.
Multithreading considerations:
A clock cache manager must be MT-safe.
All member variables are accessed single threaded (synchronized on this) or
set once or readonly. Assumptions: holders size() and addElement must be
synchronized.
CachedItem is never passed out of the clock cache manager, only the
Cacheable object is. The cachedItem is responsible for the setting and
clearing of its own member fields (RESOLVE: now they are done in cache
manager, need to be moved to the cachedItem). The cache manager will
following the following rules while accessing a cacheditem:
CachedItem
,
Cacheable
Field Summary | |
---|---|
private boolean |
active
|
private java.util.HashMap |
cache_
|
private DaemonService |
cleaner
|
private boolean |
cleanerRunning
|
private int |
clockHand
|
private long |
currentByteCount
|
private CacheableFactory |
holderFactory
|
private java.util.ArrayList |
holders
|
private static int |
ITEM_OVERHEAD
|
private long |
maximumSize
|
private int |
myClientNumber
|
private java.lang.String |
name
|
private boolean |
needService
|
private CacheStat |
stat
|
private int |
trimRequests
|
private boolean |
useByteCount
|
private int |
validItemCount
|
private boolean |
wokenToClean
|
Fields inherited from interface org.apache.derby.iapi.services.daemon.Serviceable |
---|
DONE, REQUEUE |
Constructor Summary | |
---|---|
Clock(CacheableFactory holderFactory,
java.lang.String name,
int initialSize,
long maximumSize,
boolean useByteCount)
Construct a new clock cache manager. |
Method Summary | |
---|---|
private Cacheable |
addEntry(CachedItem item,
java.lang.Object key,
boolean forCreate,
java.lang.Object createParameter)
Add a new CachedItem and a holder object to the cache. |
void |
ageOut()
Age as many objects as possible out of the cache. |
void |
clean(Matchable partialKey)
Clean all objects that match a partial key. |
void |
cleanAll()
Clean all objects in the cache. |
private void |
cleanCache(Matchable partialKey)
Write out all dirty buffers. |
boolean |
containsKey(java.lang.Object k)
Tell if a key exists in the cache. |
Cacheable |
create(java.lang.Object key,
java.lang.Object createParameter)
Create a new object with the said key. |
boolean |
discard(Matchable partialKey)
Discard all objects that match the partial key. |
Cacheable |
find(java.lang.Object key)
Find the object or materialize one in the cache. |
Cacheable |
findCached(java.lang.Object key)
Find an object in the cache. |
private CachedItem |
findFreeItem()
|
long[] |
getCacheStats()
Return statistics about cache that may be implemented. |
private long |
getCurrentSize()
|
private long |
getCurrentSizeNoSync()
|
private int |
getItemSize(CachedItem item)
|
long |
getMaximumSize()
|
int |
getNumberInUse()
|
private CachedItem |
growCache()
Grow the cache and return a unused, kept item. |
private int |
incrClockHand()
Synchronously increment clock hand position |
private int |
performWork(boolean shrinkOnly)
The background cleaner tries to make sure that there are serveral cleaned or invalied buffers ahead of the clock hand so that when they are evicted, they don't need to be cleaned. |
int |
performWork(ContextManager contextMgr)
Do whatever it is that you want the daemon to do for you. |
void |
release(Cacheable entry)
The caller is no longer looking at or updating the entry. |
private void |
release(CachedItem item)
|
void |
remove(Cacheable entry)
Remove an object from the cache. |
private long |
removeIdentity(CachedItem item)
Clear an item's identity. |
void |
resetCacheStats()
Reset the statistics to 0. |
void |
resize(long newSize)
Change the maximum size of the cache. |
private CachedItem |
rotateClock(float percentOfClock)
Go through the list of holder objects and find a free one. |
void |
scan(Matchable filter,
Operator operator)
Perform an operation on (approximately) all entries that matches the filter, or all entries if the filter is null. |
boolean |
serviceASAP()
MT - read only. |
boolean |
serviceImmediately()
If this work should be done immediately on the user thread then return true. |
void |
setUsed(java.lang.Object[] keys)
Mark a set of entries as having been used. |
private long |
shrinkSize(long currentSize)
|
void |
shutdown()
MT - synchronization provided by caller |
private void |
trimToSize()
|
void |
useDaemonService(DaemonService daemon)
MT - synchronization provided by caller can use this Daemomn service if needed |
java.util.Collection |
values()
Return a Collection of the Cacheables currently in the cache. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private final CacheStat stat
private final java.util.HashMap cache_
private DaemonService cleaner
private final java.util.ArrayList holders
private int validItemCount
private long maximumSize
private boolean useByteCount
private long currentByteCount
private static final int ITEM_OVERHEAD
private final CacheableFactory holderFactory
private boolean active
private java.lang.String name
private int clockHand
private int myClientNumber
private boolean wokenToClean
private boolean cleanerRunning
private boolean needService
private int trimRequests
Constructor Detail |
---|
Clock(CacheableFactory holderFactory, java.lang.String name, int initialSize, long maximumSize, boolean useByteCount)
MT - not needed for constructor.
holderFactory
- the cacheable object classname
- the name of the cacheinitialSize
- the initial number of cachable object this cache
holds.maximumSize
- the maximum size of the cache. The cache may grow
from initialSize to maximumSize if the cache policy notices that there
is not enough free buffers availiable. Once the cache hits maximumSize
it will not grow. If the cache is full, an exception will be thrownMethod Detail |
---|
public Cacheable find(java.lang.Object key) throws StandardException
MT - must be MT-safe. The cache is single threaded through finding
the item in cache and finding a free item if it is not in cache, thus
preventing another thread from creating the same item while is is being
faulted in. (RESOLVE - this is really low performance if the cache
cleaner cannot keep a steady supply of free items and we have to do an
I/O while blocking the cache). If it needs to be faulted in, the
inCreate bit is set. The item is kept before it exits the sync block.
If the item is in cache but in the middle of being faulted in or
cleaned, it needs to wait until this is done being before returning.
The keep status prevents other threads from removing this item.
The inCreate status prevents other threads from looking at or writing
out this item while it is being faulted in.
(RESOLVE: need to handle the case where the object is marked for
removal and being waited on)
find
in interface CacheManager
key
- the key to the object
StandardException
- Standard Derby error policyCacheable.setIdentity(java.lang.Object)
public Cacheable findCached(java.lang.Object key) throws StandardException
MT - must be MT-safe. The cache is single threaded through finding the item in cache. If it needs to wait for it to be faulted in or cleaned it is synchronized/waited on the cached item itself.
findCached
in interface CacheManager
key
- the key to the object
StandardException
- Standard Derby error policy.public void setUsed(java.lang.Object[] keys)
keys
- the key of the used entry.public Cacheable create(java.lang.Object key, java.lang.Object createParameter) throws StandardException
MT - must be MT-safe. Single thread thru verifying no such item exist in cache and finding a free item, keep the item and set inCreate state. The actual creating of the object is done outside the sync block and is protected by the isKept and inCreate bits.
create
in interface CacheManager
key
- the key to the object
StandardException
- Standard Derby error policyCacheable.createIdentity(java.lang.Object, java.lang.Object)
public void release(Cacheable entry)
MT - must be MT-safe. Getting and deleting item from the hash map is in the same synchronized block. If the cacheable object is waiting to be removed, that is synchronized thru the cachedItem itself (RESOLVE: need to move this sync block to cachedItem instead)
release
in interface CacheManager
entry
- the cached entryprivate void release(CachedItem item)
public void remove(Cacheable entry) throws StandardException
MT - must be MT-safe. Single thread thru finding and setting the remove state of the item, the actual removal of the cacheable is synchronized on the cachedItem itself.
remove
in interface CacheManager
entry
- the object to remove from the cache
StandardException
- Standard Derby error policy.public void cleanAll() throws StandardException
cleanAll
in interface CacheManager
StandardException
- Standard Derby error policy.Cacheable.clean(boolean)
,
Cacheable.isDirty()
public void clean(Matchable partialKey) throws StandardException
clean
in interface CacheManager
StandardException
- Standard Derby error policy.public void ageOut()
ageOut
in interface CacheManager
CacheManager.ageOut()
public void shutdown() throws StandardException
shutdown
in interface CacheManager
StandardException
- Standard Derby error policy.public void useDaemonService(DaemonService daemon)
useDaemonService
in interface CacheManager
public boolean discard(Matchable partialKey)
discard
in interface CacheManager
private Cacheable addEntry(CachedItem item, java.lang.Object key, boolean forCreate, java.lang.Object createParameter) throws StandardException
MT - need to be MT-safe. The insertion of the key into the hash table is synchronized on this.
StandardException
private CachedItem findFreeItem() throws StandardException
StandardException
private CachedItem rotateClock(float percentOfClock) throws StandardException
MT - must be MT-safe. The moving of the clockHand and finding of an eviction candidate is synchronized. The cleaning of the cachable is handled by the cacheable itself.
StandardException
private int incrClockHand()
public int performWork(ContextManager contextMgr)
Serviceable
performWork
in interface Serviceable
contextMgr
- the contextManager set up by the DaemonService. There
may or may not be the necessary context on it, depending on which other
Serviceable object it has done work for.
public boolean serviceASAP()
MT - read only.
serviceASAP
in interface Serviceable
public boolean serviceImmediately()
Serviceable
serviceImmediately
in interface Serviceable
public int getNumberInUse()
private CachedItem growCache()
StandardException
- Thrown if the cache cannot be grown.private long removeIdentity(CachedItem item)
private void cleanCache(Matchable partialKey) throws StandardException
MT - must be MT safe. Single thread on the part that finds the next dirty buffer to write out, the synchronization of cleaning of the individual cachable is provided by the cacheable itself.
StandardException
private long shrinkSize(long currentSize)
private int performWork(boolean shrinkOnly)
MT - must be MT-safe. It takes a snapshot of the current clock hand position (a synchronous call). Getting and looking at the next serveral cached item is synchronized on this (RESOLVE: probably doesn't need to be). Cleaning of the cacheable is handle by the cacheable itself.
private int getItemSize(CachedItem item)
public long[] getCacheStats()
public void resetCacheStats()
public long getMaximumSize()
public void resize(long newSize) throws StandardException
newSize
- the new maximum cache size
StandardException
- Standard Derby error policyprivate long getCurrentSize()
private long getCurrentSizeNoSync()
public void scan(Matchable filter, Operator operator)
filter
- operator
- private void trimToSize()
public boolean containsKey(java.lang.Object k)
k
- the key to test for
public java.util.Collection values()
values
in interface CacheManager
|
Built on Thu 2010-12-23 20:49:13+0000, from revision ??? | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |