|
|||||||||
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.index.bplus.BPlusTreeIndexBackend<K,V,H,P>
K
- The type of keys in the database.V
- The type of values in the database.H
- The type of the hashed keys.P
- The type of positions in the indexed backend.public class BPlusTreeIndexBackend<K,V,H extends Comparable<H>,P>
This DatabaseBackend
provides a B+ Tree index for
the keys of another, proxied DatabaseBackend
. If searching that
backend is slow, the B+ Tree index can be used to speed it up.
The keys of the index B+ Tree are hashes of the database keys, created by
running the keys through a Hasher
object. The key hash has the
following properties
Hasher
such as the
IntegerToIntegerHasher
or the
ConstantSizeValueHasher
can be used.
If the database should support null
keys, the Hasher
must
support hashing null
values.
If the key Hasher
is value-preserving, this index modifies the
behavior of the database's key iterator and its Cursor
to
return the keys/records in the natural order of the keys (the order
determined by the keys' implementation of
Comparable.compareTo(Object)
). The behaviors of the other iterators
are not modified.
The values in the index B+ Tree are the positions of the indexed records in
the proxied backend. This backend registers a
RecordMoveListener
with the proxied backend, so it
is able to cope with records moving around in the proxied backend.
BPlusTreeIndexBackendFactory
,
BPlusTreeIndexBackendBuilder
Constructor Summary | |
---|---|
BPlusTreeIndexBackend(DatabaseBackend<K,V,P> backend,
boolean readOnly,
BPlusTree<H,P> tree,
Hasher<K,H> keyHasher,
LogAdapterHolder lah)
Constructor. |
Method Summary | |
---|---|
void |
clear()
Wipe out the entire database. |
void |
close()
Subclasses overriding this method must call super.close() . |
boolean |
compact()
Call compact on the proxied backend. |
boolean |
delete(K key)
Delete the record with the supplied key, if it exists. |
protected void |
finalize()
|
P |
find(Comparable<K> key,
SearchMode mode)
If the B+ Tree uses a value-preserving Hasher and the natural
ordering of its (Comparable ) keys (i.e., not a custom key
Comparator ), use the tree's fast find method. |
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)
If the B+ Tree uses a value-preserving Hasher and a key
Comparator that is equal to the supplied Comparator , use
the tree's efficient find method. |
void |
forEachKey(ForEachKeyCallback<K,P> callback)
Call the proxied backend's forEachKey method. |
P |
getFirstPosition()
Call getFirstPosition on the proxied backend. |
P |
getLastPosition()
Call getLastPosition on the proxied backend. |
P |
getNextPosition(P pos)
Call getNextPosition on the proxied backend. |
P |
getPreviousPosition(P pos)
Call getPreviousPosition on the proxied backend. |
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. |
Iterator<K> |
keyIterator()
Get a key iterator from the proxied backend. |
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. |
long |
writeContentsTo(RandomAccess ra)
Call writeContentsTo on the proxied backend. |
Methods inherited from class org.helidb.backend.AbstractDatabaseBackendProxy |
---|
addRecordMoveListener, assertNotClosed, assertNotReadOnly, getContentsVersion, getKeys, getProxied, getRecords, getValues, isClosed, isReadOnly, iterator, removeRecordMoveListener, valueIterator |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public BPlusTreeIndexBackend(DatabaseBackend<K,V,P> backend, boolean readOnly, BPlusTree<H,P> tree, Hasher<K,H> keyHasher, LogAdapterHolder lah)
The supplied B+ Tree must contain a valid index for the database backend. This is trivially true if both are empty.
The BPlusTreeIndexBackendBuilder
may be more convenient to use
than this constructor.
backend
- The database backend that should be indexed.readOnly
- Should the database backend be read only?tree
- A B+ Tree containing the index for the database backend. The
keys of the B+ Tree are the hashes of the database keys, and the values
are the positions of each record in the proxied backend.keyHasher
- Hasher
for hashing keys.lah
- A log adapter holder.Method Detail |
---|
public void forEachKey(ForEachKeyCallback<K,P> callback)
AbstractDatabaseBackendProxy
forEachKey
method.
forEachKey
in interface DatabaseBackend<K,V,P>
forEachKey
in class AbstractDatabaseBackendProxy<K,V,P>
callback
- The callback object to call for each key.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 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 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)
Hasher
and the natural
ordering of its (Comparable
) keys (i.e., not a custom key
Comparator
), use the tree's fast find method. Otherwise, use the
find method of the proxied backend.
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)
Hasher
and a key
Comparator
that is equal to the supplied Comparator
, use
the tree's efficient find method. Otherwise, use the find method of the
proxied backend.
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 void removeAt(P pos, K key)
DatabaseBackend
pos
- The position of the record to delete.key
- The key for the record to remove.public V remove(K key)
DatabaseBackend
key
- The record's key.
null
if no record in the database
has the supplied key.public boolean delete(K key)
DatabaseBackend
key
- The record's key.
true
if a record was deleted.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 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 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 Iterator<K> keyIterator()
AbstractDatabaseBackendProxy
keyIterator
in interface DatabaseBackend<K,V,P>
keyIterator
in class AbstractDatabaseBackendProxy<K,V,P>
Iterator
over the database's keys.public P getFirstPosition() throws WrappedIOException, IllegalStateException
AbstractDatabaseBackendProxy
getFirstPosition
on the proxied backend.
getFirstPosition
in interface DatabaseBackend<K,V,P>
getFirstPosition
in class AbstractDatabaseBackendProxy<K,V,P>
null
if the backend is empty.
WrappedIOException
- On I/O errors.
IllegalStateException
- If the database backend has been closed.DatabaseBackend.getLastPosition()
,
DatabaseBackend.getNextPosition(Object)
,
DatabaseBackend.getPreviousPosition(Object)
public P getLastPosition() throws WrappedIOException, IllegalStateException
AbstractDatabaseBackendProxy
getLastPosition
on the proxied backend.
getLastPosition
in interface DatabaseBackend<K,V,P>
getLastPosition
in class AbstractDatabaseBackendProxy<K,V,P>
null
if the backend is empty.
WrappedIOException
- On I/O errors.
IllegalStateException
- If the database backend has been closed.DatabaseBackend.getFirstPosition()
,
DatabaseBackend.getNextPosition(Object)
,
DatabaseBackend.getPreviousPosition(Object)
public P getNextPosition(P pos) throws WrappedIOException, IllegalStateException
AbstractDatabaseBackendProxy
getNextPosition
on the proxied backend.
getNextPosition
in interface DatabaseBackend<K,V,P>
getNextPosition
in class AbstractDatabaseBackendProxy<K,V,P>
pos
- The position to search from. This must be the position of a
record in the backend. If it is not, the behavior of this method is
unspecified.
null
if the supplied position references the last record in the backend
(the position returned from DatabaseBackend.getLastPosition()
).
WrappedIOException
- On I/O errors.
IllegalStateException
- If the database backend has been closed.DatabaseBackend.getFirstPosition()
,
DatabaseBackend.getLastPosition()
,
DatabaseBackend.getPreviousPosition(Object)
public P getPreviousPosition(P pos) throws WrappedIOException, IllegalStateException, UnsupportedOperationException
AbstractDatabaseBackendProxy
getPreviousPosition
on the proxied backend.
getPreviousPosition
in interface DatabaseBackend<K,V,P>
getPreviousPosition
in class AbstractDatabaseBackendProxy<K,V,P>
pos
- The position to search from. This must be the position of a
record in the backend. If it is not, the behavior of this method is
unspecified.
null
if the supplied position references the first record in the
backend (the position returned from DatabaseBackend.getFirstPosition()
).
WrappedIOException
- On I/O errors.
IllegalStateException
- If the database backend has been closed.
UnsupportedOperationException
- If this method is not supported by
the backend implementation.DatabaseBackend.getFirstPosition()
,
DatabaseBackend.getLastPosition()
,
DatabaseBackend.getNextPosition(Object)
public void clear()
DatabaseBackend
public boolean compact()
AbstractDatabaseBackendProxy
compact
on the proxied backend.
compact
in interface DatabaseBackend<K,V,P>
compact
in class AbstractDatabaseBackendProxy<K,V,P>
true
if the backend was modified, false
if not.public long writeContentsTo(RandomAccess ra)
AbstractDatabaseBackendProxy
writeContentsTo
on the proxied backend.
writeContentsTo
in interface DatabaseBackend<K,V,P>
writeContentsTo
in class AbstractDatabaseBackendProxy<K,V,P>
ra
- The RandomAccess
to write to. It is not closed after
writing.
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>
protected void finalize() throws Throwable
finalize
in class AbstractDatabaseBackendProxy<K,V,P>
Throwable
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |