R
- cache row typepublic abstract class AbstractCache<R> extends java.lang.Object implements Cache<R>
Constructor and Description |
---|
AbstractCache(Table<R> table,
Cached cachedAnnotation)
Constructs for a table and annotation.
|
Modifier and Type | Method and Description |
---|---|
void |
begin(Transaction transaction)
Indicates start of database transaction.
|
protected void |
check()
Verifies that cache is ready.
|
void |
commit(Transaction transaction)
Indicates database transaction commit.
|
boolean |
contains(java.lang.Object[] primaryKeys)
Tests if row is in cache.
|
void |
evict(R row)
Removes row from the cache.
|
void |
evictAll()
Removes all rows from the cache.
|
Cached |
getCachedAnnotation()
Gets the annotations used to configure cache.
|
R |
getCommitted(CacheKey primaryKeys)
Gets a row from committed cache.
|
protected java.util.Map<CacheKey,R> |
getCommittedCache()
Gets the map that stores committed rows.
|
int |
getHits()
Gets the number of times that requested row was in cache.
|
int |
getMisses()
Gets the number of times that requested row was not in cache.
|
int |
getPercentHits()
Gets hits as a percentage of total requests.
|
FieldExtractor<R> |
getPrimaryKeyExtractor()
Gets the
FieldExtractor used to get all primary key values from a row. |
java.lang.Object[] |
getPrimaryKeyValues(R row)
Gets the primary key values from a row as Object array.
|
Table<R> |
getTable()
Gets table that is cached.
|
UncommittedRow<R> |
getUncommitted(CacheKey primaryKeys)
Gets a row from uncommitted cache.
|
protected java.util.Map<CacheKey,UncommittedRow<R>> |
getUncommittedCache()
Gets the map that stores uncommitted rows.
|
void |
hit()
Increments hit count.
|
protected void |
initCommittedCache()
Initializes committed map with initial capacity of
Cached.size() . |
protected void |
initUncommittedCache()
Initializes uncommitted map with initial capacity of 1/2 of
Cached.size() . |
void |
log()
Writes information to the log about the cache.
|
void |
miss()
Increments miss count.
|
R |
putCommitted(CacheKey primaryKeys,
R row)
Puts a row into committed cache.
|
UncommittedRow<R> |
putUncommitted(UncommittedRow<R> uncommittedRow)
Puts a row into uncommitted cache.
|
R |
removeCommited(CacheKey primaryKeys)
Removes a row from committed cache.
|
void |
rollback(Transaction transaction)
Indicates database transaction rollback.
|
protected void |
setCommittedCache(java.util.Map<CacheKey,R> committedCache)
Sets new committed map.
|
protected void |
setUncommittedCache(java.util.Map<CacheKey,UncommittedRow<R>> uncommittedCache)
Sets new uncommitted map.
|
UncommittedRow<R> |
transition(UncommittedRow<R> currentUncommittedRow,
UncommittedRow<R> newUncommittedRow)
Transition uncommitted cache state.
|
public AbstractCache(Table<R> table, Cached cachedAnnotation) throws CacheException
table
- cache rows for operations on this tablecachedAnnotation
- annotation that defines cache configurationCacheException
- if errorpublic void begin(Transaction transaction) throws CacheException
Table.begin(Transaction)
which
is a listener method of TransactionListener
. Initializes uncommitted map with initCommittedCache()
.begin
in interface Cache<R>
transaction
- database transaction
throws CacheException if errorCacheException
- if errorpublic void commit(Transaction transaction) throws CacheException
Table.commit(Transaction)
which
is a listener method of TransactionListener
. If Cached.evictOnTransactionEnd()
is
false, then updates committed map with UncommittedRow.updateCommitted(Cache)
for all rows
in uncommitted cache. If Cached.evictOnTransactionEnd()
is true, then removes all
rows from cache with evictAll()
.commit
in interface Cache<R>
transaction
- database transaction
throws CacheException if errorCacheException
- if errorpublic void rollback(Transaction transaction) throws CacheException
Table.rollback(Transaction)
which
is a listener method of TransactionListener
. Removes all uncommitted rows. If
Cached.evictOnTransactionEnd()
is true, then removes all committed rows from cache
with evictAll()
.rollback
in interface Cache<R>
transaction
- database transaction
throws CacheException if errorCacheException
- if errorpublic boolean contains(java.lang.Object[] primaryKeys) throws CacheException
Cache.select(Object[])
will return null but contains(Object[])
will true for the
same primary key(s) if row is an uncommitted delete. Cache.select(Object[])
indicates
committed visibility while contains(Object[])
simply indicates if row is known to
the cache. This method is used by ScalarSelectOperation.execute()
to know if
cache is authority for row or if database must be queried to get row.contains
in interface Cache<R>
primaryKeys
- primary key(s) for rowCacheException
- if errorpublic void evict(R row) throws CacheException
evict
in interface Cache<R>
row
- row to removeCacheException
- neverpublic void evictAll() throws CacheException
evictAll
in interface Cache<R>
CacheException
- neverprotected void initCommittedCache() throws CacheException
Cached.size()
. Subclasses
may override to initialize a custom Map
.CacheException
- if errorprotected void initUncommittedCache() throws CacheException
Cached.size()
. Subclasses
may override to initialize a custom Map
.CacheException
- if errorpublic Table<R> getTable()
public Cached getCachedAnnotation()
public FieldExtractor<R> getPrimaryKeyExtractor()
FieldExtractor
used to get all primary key values from a row.getPrimaryKeyValues(Object)
public void log()
protected java.util.Map<CacheKey,R> getCommittedCache()
protected void setCommittedCache(java.util.Map<CacheKey,R> committedCache)
committedCache
- map for storing committed rowsprotected java.util.Map<CacheKey,UncommittedRow<R>> getUncommittedCache()
protected void setUncommittedCache(java.util.Map<CacheKey,UncommittedRow<R>> uncommittedCache)
uncommittedCache
- map for storing uncommitted rowspublic R getCommitted(CacheKey primaryKeys)
primaryKeys
- primary key(s) of a rowpublic R putCommitted(CacheKey primaryKeys, R row)
primaryKeys
- primary key(s) of a rowrow
- row to put in committed cachepublic R removeCommited(CacheKey primaryKeys)
primaryKeys
- primary key(s) of a rowprotected void check() throws CacheException
CacheException
- if cache is not ready for usepublic UncommittedRow<R> getUncommitted(CacheKey primaryKeys)
primaryKeys
- primary key(s) of a rowpublic UncommittedRow<R> putUncommitted(UncommittedRow<R> uncommittedRow)
uncommittedRow
- row to put in uncommitted cachepublic UncommittedRow<R> transition(UncommittedRow<R> currentUncommittedRow, UncommittedRow<R> newUncommittedRow)
currentUncommittedRow
- current uncommitted row in uncommitted cachenewUncommittedRow
- null to remove current uncommitted row; otherwise replace current
uncommitted row with new uncommitted row (if they are not the same object)public java.lang.Object[] getPrimaryKeyValues(R row) throws CacheException
row
- get value from this rowCacheException
- if error extracting valuespublic int getHits()
public void hit()
public int getMisses()
public void miss()
public int getPercentHits()