|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.helidb.backend.AbstractDatabaseBackend<K,V,KeyAndValue<K,V>>
org.helidb.backend.bpluscrs.ConstantRecordSizeBPlusTreeBackend<K,V>
K
- The types of keys in the backend. The keys must be of a fixed size
when serialized.V
- The types of values in the backend. The values must be of a fixed
size when serialized.public class ConstantRecordSizeBPlusTreeBackend<K,V>
This DatabaseBackend
uses a B+ Tree for storing
its records. Its keys and values must be of a fixed size when serialized.
Keys must either be Comparable
with each other, or a separate key
Comparator
must be supplied when creating the backend.
This implementation provides fast retrieval, updating and deleting of its data.
Cursor
:s, key, value and record iterators created by this
backend return records in the order defined by the key
Comparator
used, or in the keys' natural order if no
Comparator
is used. (See Comparable
.) This behavior
may be modified if an index is used with the database.
Comparable
,
Comparator
,
ConstantRecordSizeBPlusTreeBackendFactory
Constructor Summary | |
---|---|
ConstantRecordSizeBPlusTreeBackend(NodeRepository<K> nr,
boolean readOnly,
Comparator<? super K> cmp,
LogAdapterHolder lah)
Constructor. |
|
ConstantRecordSizeBPlusTreeBackend(NodeRepository<K> nr,
boolean readOnly,
LogAdapterHolder lah)
Constructor. |
Method Summary | |
---|---|
protected void |
assertNotClosed()
Subclasses implement this to throw an IllegalStateException if
the database backend is closed. |
void |
clear()
Wipe out the entire database. |
void |
close()
Close this database backend and release all resources associated with it (close files and release locks, for instance). |
boolean |
compact()
Compact the backing database file if it has become fragmented. |
boolean |
delete(K key)
Delete the record with the supplied key, if it exists. |
protected void |
finalize()
Close the backend before discarding the object. |
KeyAndValue<K,V> |
find(Comparable<K> key,
SearchMode mode)
If the B+ Tree uses the natural ordering of its ( Comparable ) keys
(i.e. |
KeyAndValue<K,V> |
find(K key)
Find the position of the record with the supplied key in the database backend. |
KeyAndValue<K,V> |
find(K key,
SearchMode mode,
Comparator<? super K> cmp)
If the supplied Comparator is equal to the Comparator
used by the B+ Tree, the tree's fast find method is used. |
void |
forEachKey(ForEachKeyCallback<K,KeyAndValue<K,V>> callback)
For each key in the database, call the callback object. |
KeyAndValue<K,V> |
getFirstPosition()
Get the position of the "first" record in the backend. |
Set<K> |
getKeys()
Return an immutable Set containing all keys in the database. |
KeyAndValue<K,V> |
getLastPosition()
Get the position of the "last" record in the backend. |
KeyAndValue<K,V> |
getNextPosition(KeyAndValue<K,V> pos)
Get the position of the "next" record in the backend, relative to the record at the supplied position. |
KeyAndValue<K,V> |
getPreviousPosition(KeyAndValue<K,V> pos)
Get the position of the "previous" record in the backend, relative to the record at the supplied position. |
Set<Record<K,V>> |
getRecords()
Return an immutable Set containing all database records (keys and
values). |
V |
getValueFor(K key)
Get the value for the record with the supplied key. |
Collection<V> |
getValues()
Return an immutable Collection containing all values in the
database. |
KeyAndValue<K,V> |
insert(K key,
V value)
Insert a new record in the database. |
KeyAndValue<K,V> |
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<Record<K,V>> |
iterator()
|
Iterator<K> |
keyIterator()
Get an iterator for all keys in the database. |
K |
readKeyAt(KeyAndValue<K,V> pos)
Read the key at the supplied position. |
Record<K,V> |
readRecordAt(KeyAndValue<K,V> pos)
Read the record (key and value) at the specified position. |
V |
readValueAt(KeyAndValue<K,V> 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(KeyAndValue<K,V> 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<KeyAndValue<K,V>> |
update(K key,
V value)
Update an existing record with a new value. |
KeyAndValue<K,V> |
updateAt(KeyAndValue<K,V> pos,
K key,
V value)
Update the record at the supplied position with the new value. |
Iterator<V> |
valueIterator()
Get an iterator for all the values in the database. |
long |
writeContentsTo(RandomAccess ra)
Write the contents of the entire database to the current position in the supplied RandomAccess . |
Methods inherited from class org.helidb.backend.AbstractDatabaseBackend |
---|
addRecordMoveListener, getContentsVersion, hasRecordMoveListeners, notifyRecordMoveListeners, removeRecordMoveListener, updateContentsVersion |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public ConstantRecordSizeBPlusTreeBackend(NodeRepository<K> nr, boolean readOnly, LogAdapterHolder lah) throws IllegalArgumentException
nr
- A NodeRepository
for the B+ Tree.readOnly
- Should the backend be used read only?lah
- A log adapter holder.
IllegalArgumentException
- If the NodeRepository
does not
support iteration.public ConstantRecordSizeBPlusTreeBackend(NodeRepository<K> nr, boolean readOnly, Comparator<? super K> cmp, LogAdapterHolder lah) throws IllegalArgumentException
nr
- A NodeRepository
for the B+ Tree.readOnly
- Should the backend be used read only?cmp
- A Comparator
for establishing the key ordering.lah
- A log adapter holder.
IllegalArgumentException
- If the NodeRepository
does not
support iteration.Method Detail |
---|
protected void assertNotClosed() throws IllegalStateException
AbstractDatabaseBackend
IllegalStateException
if
the database backend is closed.
assertNotClosed
in class AbstractDatabaseBackend<K,V,KeyAndValue<K,V>>
IllegalStateException
- Thrown if the database backend is closed.public Iterator<Record<K,V>> iterator()
public Iterator<K> keyIterator()
DatabaseBackend
If the database is modified after calling this method, the methods of the
Iterator
returned will throw
ConcurrentModificationException
Iterator
over the database's keys.public Iterator<V> valueIterator()
DatabaseBackend
If the database is modified after calling this method, the methods of the
Iterator
returned will throw
ConcurrentModificationException
Iterator
over the database's values.public void forEachKey(ForEachKeyCallback<K,KeyAndValue<K,V>> callback)
DatabaseBackend
This can for instance be used for building indices for the database.
callback
- The callback object to call for each key.public KeyAndValue<K,V> find(K key)
DatabaseBackend
key
- The key to find.
null
if not found.DatabaseBackend.find(Comparable, SearchMode)
,
DatabaseBackend.find(Object, SearchMode, Comparator)
public KeyAndValue<K,V> find(Comparable<K> key, SearchMode mode)
Comparable
) keys
(i.e. it does not use a custom Comparator
), this method uses the
fast find methods of the B+ Tree. Otherwise it uses the slower inherited
method.
find
in interface DatabaseBackend<K,V,KeyAndValue<K,V>>
find
in class AbstractDatabaseBackend<K,V,KeyAndValue<K,V>>
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 KeyAndValue<K,V> find(K key, SearchMode mode, Comparator<? super K> cmp)
Comparator
is equal to the Comparator
used by the B+ Tree, the tree's fast find method is used. Otherwise the
slower inherited method is used.
find
in interface DatabaseBackend<K,V,KeyAndValue<K,V>>
find
in class AbstractDatabaseBackend<K,V,KeyAndValue<K,V>>
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 V readValueAt(KeyAndValue<K,V> 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 KeyAndValue<K,V> 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 KeyAndValue<K,V> insertCheckKeyUnique(K key, V value)
DatabaseBackend
key
- The key.value
- The 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 Pair<KeyAndValue<K,V>> 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 void removeAt(KeyAndValue<K,V> 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 KeyAndValue<K,V> updateAt(KeyAndValue<K,V> 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 Set<Record<K,V>> getRecords()
DatabaseBackend
Set
containing all database records (keys and
values).
Set
containing all database records. If the
database is empty, this method returns an empty Set
.public Set<K> getKeys()
DatabaseBackend
Set
containing all keys in the database.
Set
containing all keys in the database. If
the database is empty, this method returns an empty Set
.public Collection<V> getValues()
DatabaseBackend
Collection
containing all values in the
database.
Collection
containing all values in the
database. If the database is empty, this method returns an empty Collection
.public void clear()
DatabaseBackend
public boolean compact()
DatabaseBackend
true
if the backend was modified, false
if not.public long writeContentsTo(RandomAccess ra)
DatabaseBackend
RandomAccess
.
After writing, the current position of the RandomAccess
should be
at the end of the written data.
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 KeyAndValue<K,V> getFirstPosition() throws WrappedIOException, IllegalStateException
DatabaseBackend
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 KeyAndValue<K,V> getLastPosition() throws WrappedIOException, IllegalStateException
DatabaseBackend
Some backend implementations do not support this method. They let this
method throw an UnsupportedOperationException
.
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 KeyAndValue<K,V> getNextPosition(KeyAndValue<K,V> pos) throws WrappedIOException, IllegalStateException
DatabaseBackend
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 KeyAndValue<K,V> getPreviousPosition(KeyAndValue<K,V> pos) throws WrappedIOException, IllegalStateException, UnsupportedOperationException
DatabaseBackend
Some backend implementations do not support this method. They let this
method throw an UnsupportedOperationException
.
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 K readKeyAt(KeyAndValue<K,V> 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(KeyAndValue<K,V> 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 close()
DatabaseBackend
protected void finalize() throws Throwable
finalize
in class Object
Throwable
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |