|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.helidb.backend.AbstractDatabaseBackendProxy<K,V,P>
org.helidb.backend.cache.lru.LruCacheBackend<K,V,P>
K
- The type of keys in the database.V
- The type of values in the database.P
- The type the proxied backend uses to represent positions.public class LruCacheBackend<K,V,P>
The LRUCacheBackend
is a proxy for another DatabaseBackend
object and caches results from searches and updates. It contains two LRU
caches, one cache for database records and one cache for negative search
information (we did not find the key k when searching for it the last time,
so we would not find it if we searched for it again).
Results from calling DatabaseBackend.getNextPosition(Object)
and
DatabaseBackend.getPreviousPosition(Object)
(used by database
Cursor
:s) are not cached. Those operations is assumed to
have efficient implementations in the backend.
LRU stands for Least Recently Used and means that, if the cache is full, the least recently used element is purged to make room for a new element.
Consider the key and value objects stored in a caching database as immutable. If they are modified, the behavior of the database is unspecified and, almost certainly, unpleasant.
LruCacheBackendFactory
Constructor Summary | |
---|---|
LruCacheBackend(DatabaseBackend<K,V,P> proxied,
boolean readOnly,
int maxCacheSize,
int maxNegativeCacheSize)
|
Method Summary | |
---|---|
void |
clear()
Wipe out the entire database. |
void |
close()
Subclasses overriding this method must call super.close() . |
boolean |
delete(K key)
Delete the record with the supplied key, if it exists. |
P |
find(Comparable<K> key,
SearchMode mode)
The results from this method are not cached. |
P |
find(K key)
Find the position of the record with the supplied key in the database backend. |
P |
find(K key,
SearchMode mode,
Comparator<? super K> cmp)
The results from this method are not cached. |
V |
getValueFor(K key)
Get the value for the record with the supplied key. |
P |
insert(K key,
V value)
Insert a new record in the database. |
P |
insertCheckKeyUnique(K key,
V value)
Insert a new record in the database after verifying that the key is unique within the database. |
boolean |
insertOrUpdate(K key,
V value)
If a record with the supplied key exists, update its value. |
K |
readKeyAt(P pos)
Read the key at the supplied position. |
Record<K,V> |
readRecordAt(P pos)
Read the record (key and value) at the specified position. |
V |
readValueAt(P pos)
Read the value at the specified position. |
V |
remove(K key)
Delete the record with the supplied key and return its value. |
void |
removeAt(P pos,
K key)
Delete the record at the supplied position. |
void |
replaceContentsWith(RandomAccess ra,
long dataSize)
Replace the contents of the database with content read from the supplied RandomAccess . |
Pair<P> |
update(K key,
V value)
Update an existing record with a new value. |
P |
updateAt(P pos,
K key,
V value)
Update the record at the supplied position with the new value. |
Methods inherited from class org.helidb.backend.AbstractDatabaseBackendProxy |
---|
addRecordMoveListener, assertNotClosed, assertNotReadOnly, compact, finalize, forEachKey, getContentsVersion, getFirstPosition, getKeys, getLastPosition, getNextPosition, getPreviousPosition, getProxied, getRecords, getValues, isClosed, isReadOnly, iterator, keyIterator, removeRecordMoveListener, valueIterator, writeContentsTo |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public LruCacheBackend(DatabaseBackend<K,V,P> proxied, boolean readOnly, int maxCacheSize, int maxNegativeCacheSize) throws IllegalArgumentException
IllegalArgumentException
Method Detail |
---|
public P insert(K key, V value)
DatabaseBackend
key
- The key. It is not verified that this key is unique. Inserting
duplicate keys will result in unspecified behavior from the backend.value
- The value.
public P insertCheckKeyUnique(K key, V value)
DatabaseBackend
key
- The key.value
- The value.
public P find(K key)
DatabaseBackend
key
- The key to find.
null
if not found.DatabaseBackend.find(Comparable, SearchMode)
,
DatabaseBackend.find(Object, SearchMode, Comparator)
public P find(Comparable<K> key, SearchMode mode)
key
- The key to find.mode
- The search mode. The backend implementation must support the
SearchMode.EXACT_MATCH
mode (which makes this method behave
exactly like DatabaseBackend.find(Object)
). The backend may also support
SearchMode.CLOSEST_ABOVE
, SearchMode.CLOSEST_BELOW
and
SearchMode.CLOSEST_MATCH
. If not, this method should throw an
UnsupportedOperationException
when called with any of those
search modes. The backend implementation may also invent its own search
modes.
The closest match is defined as the match that gives the lowest positive
(SearchMode.CLOSEST_ABOVE
), negative (
SearchMode.CLOSEST_BELOW
) or absolute (
SearchMode.CLOSEST_MATCH
) value from the supplied key's
Comparable.compareTo(Object)
method when comparing the keys in
the database with the key to search for.
null
if no record matching the search criteria is found.DatabaseBackend.find(Object)
,
DatabaseBackend.find(Object, SearchMode, Comparator)
public P find(K key, SearchMode mode, Comparator<? super K> cmp)
key
- The key to find.mode
- The search mode. The backend implementation must support the
SearchMode.EXACT_MATCH
mode (which makes this method behave
exactly like DatabaseBackend.find(Object)
). The backend may also support
SearchMode.CLOSEST_ABOVE
, SearchMode.CLOSEST_BELOW
and
SearchMode.CLOSEST_MATCH
. If not, this method should throw an
UnsupportedOperationException
when called with any of those
search modes. The backend implementation may also invent its own search
modes.
The closest match is defined as the match that gives the lowest positive
(SearchMode.CLOSEST_ABOVE
), negative (
SearchMode.CLOSEST_BELOW
) or absolute (
SearchMode.CLOSEST_MATCH
) value from the comparator when
comparing the keys in the database with the key to search for.
cmp
- The Comparator
used to compare keys.
null
if no record matching the search criteria is found.DatabaseBackend.find(Object)
,
DatabaseBackend.find(Comparable, SearchMode)
public V getValueFor(K key)
DatabaseBackend
key
- The record's key.
null
if no record in the database
has the supplied key.public P updateAt(P pos, K key, V value)
DatabaseBackend
It is up to the implementation to decide exactly how this is done. It is not required that the record should be located at the same position in the backend after the update. One strategy for implementing this method is to delete the old record and insert a new record with the updated values.
pos
- The position of the record to update.key
- The record's key. The key is not modified by the update.value
- The new value.
public Pair<P> update(K key, V value)
DatabaseBackend
It is up to the implementation to decide exactly how this is done. It is not required that the record should be located at the same position in the backend after the update. One strategy for implementing this method is to delete the old record and insert a new record with the updated values.
key
- The record's key.value
- The record's new value.
public boolean insertOrUpdate(K key, V value)
DatabaseBackend
key
- The key.value
- The value.
true
if an existing record was updated, false
if
a new record was inserted.public V readValueAt(P pos)
DatabaseBackend
pos
- The position of the record containing the value. This must be
a valid position in the backend. If it is not, the behavior of this
method is unspecified.
DatabaseBackend.readKeyAt(Object)
,
DatabaseBackend.readRecordAt(Object)
public K readKeyAt(P pos) throws WrappedIOException, IllegalStateException
DatabaseBackend
pos
- The position of the record containing the key. This must be a
valid position in the backend. It it is not, the behavior of this method
is unspecified.
WrappedIOException
- On I/O errors.
IllegalStateException
- If the database backend has been closed.DatabaseBackend.readValueAt(Object)
,
DatabaseBackend.readRecordAt(Object)
public Record<K,V> readRecordAt(P pos) throws WrappedIOException, IllegalStateException
DatabaseBackend
pos
- The position of the record. This must be a valid position in
the backend. If it is not, the behavior of this method is unspecified.
WrappedIOException
- On I/O errors.
IllegalStateException
- If the database backend has been closed.DatabaseBackend.readKeyAt(Object)
,
DatabaseBackend.readValueAt(Object)
public void clear()
DatabaseBackend
public void removeAt(P pos, K key)
DatabaseBackend
pos
- The position of the record to delete.key
- The key for the record to remove.public boolean delete(K key)
DatabaseBackend
key
- The record's key.
true
if a record was deleted.public V remove(K key)
DatabaseBackend
key
- The record's key.
null
if no record in the database
has the supplied key.public void replaceContentsWith(RandomAccess ra, long dataSize)
DatabaseBackend
RandomAccess
. The database contents start at the RandomAccess
' current position.
ra
- The RandomAccess
to read data from. It should not be
closed after reading.dataSize
- The total size of the database data in bytes.public void close()
AbstractDatabaseBackendProxy
super.close()
.
close
in interface DatabaseBackend<K,V,P>
close
in class AbstractDatabaseBackendProxy<K,V,P>
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |