|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.util.Dictionary
java.util.Hashtable
org.apache.derby.impl.services.cache.Clock
A cache manager that uses a Hashtable and a ArrayList. The ArrayList holds CachedItem objects, each with a holder object. The Hashtable 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 hashtable.
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
Nested Class Summary |
Nested classes inherited from class java.util.Hashtable |
|
Field Summary | |
private boolean |
active
|
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
|
CacheStat |
stat
|
private int |
trimRequests
|
private boolean |
useByteCount
|
private int |
validItemCount
|
private boolean |
wokenToClean
|
Fields inherited from class java.util.Hashtable |
|
Fields inherited from interface org.apache.derby.iapi.services.daemon.Serviceable |
DONE, REQUEUE |
Constructor Summary | |
(package private) |
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. |
protected void |
cleanCache(Matchable partialKey)
Write out all dirty buffers. |
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. |
protected CachedItem |
findFreeItem()
|
long[] |
getCacheStats()
Return statistics about cache that may be implemented. |
private long |
getCurrentSize()
|
private int |
getItemSize(CachedItem item)
|
long |
getMaximumSize()
|
int |
getNumberInUse()
Report the number of items in use (with Identity) in this cache. |
private CachedItem |
growCache()
Grow the cache and return a unused, kept item. |
private int |
incrClockHand()
Synchronously increment clock hand position |
protected 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. |
protected void |
release(CachedItem item)
|
void |
remove(Cacheable entry)
Remove an object from the cache. |
protected 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. |
protected 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. |
protected 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 |
Methods inherited from class java.util.Hashtable |
clear, clone, contains, containsKey, containsValue, elements, entrySet, equals, get, hashCode, isEmpty, keys, keySet, put, putAll, rehash, remove, size, toString, values |
Methods inherited from class java.lang.Object |
finalize, getClass, notify, notifyAll, wait, wait, wait |
Methods inherited from interface org.apache.derby.iapi.services.cache.CacheManager |
containsKey |
Field Detail |
public final CacheStat stat
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.
name
- 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
- Cloudscape Standard 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 Cloudscape error policy.public void setUsed(java.lang.Object[] keys)
setUsed
in interface CacheManager
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
- Cloudscape Standard error policyCacheable.createIdentity(java.lang.Object, java.lang.Object)
public void release(Cacheable entry)
MT - must be MT-safe. Getting and deleteing item from the hashtable 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 entryprotected 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
StandardException
- Standard Cloudscape error policy.public void cleanAll() throws StandardException
cleanAll
in interface CacheManager
StandardException
- Standard Cloudscape error policy.Cacheable.clean(boolean)
,
Cacheable.isDirty()
public void clean(Matchable partialKey) throws StandardException
clean
in interface CacheManager
StandardException
- Standard Cloudscape error policy.public void ageOut()
ageOut
in interface CacheManager
CacheManager.ageOut()
public void shutdown() throws StandardException
shutdown
in interface CacheManager
StandardException
- Standard Cloudscape 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
protected CachedItem findFreeItem() throws StandardException
StandardException
protected 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()
CacheManager
getNumberInUse
in interface CacheManager
private CachedItem growCache()
StandardException
- Thrown if the cache cannot be grown.protected long removeIdentity(CachedItem item)
protected 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
protected long shrinkSize(long currentSize)
protected 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()
getCacheStats
in interface CacheManager
public void resetCacheStats()
resetCacheStats
in interface CacheManager
public long getMaximumSize()
getMaximumSize
in interface CacheManager
public void resize(long newSize) throws StandardException
resize
in interface CacheManager
newSize
- the new maximum cache size
StandardException
- Cloudscape Standard error policyprivate long getCurrentSize()
public void scan(Matchable filter, Operator operator)
scan
in interface CacheManager
filter
- operator
- private void trimToSize()
|
Built on Mon 2007-06-04 09:58:47+0400, from revision ??? | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |