org.helidb.backend
Class AbstractDatabaseBackend<K,V,P>

java.lang.Object
  extended by org.helidb.backend.AbstractDatabaseBackend<K,V,P>
Type Parameters:
K - The type of keys in the database.
V - The type of values in the database.
P - The type of positions in the database.
All Implemented Interfaces:
Iterable<Record<K,V>>, DatabaseBackend<K,V,P>
Direct Known Subclasses:
ConstantRecordSizeBPlusTreeBackend, ConstantRecordSizeHeapBackend, HeapBackend, MapBackend

public abstract class AbstractDatabaseBackend<K,V,P>
extends Object
implements DatabaseBackend<K,V,P>

This abstract class can be used as a starting point for a DatabaseBackend implementation. It contains methods for handling record move listeners.

Since:
1.0
Author:
Karl Gustafsson
See Also:
RecordMoveListener
In_jar:
helidb-core

Constructor Summary
AbstractDatabaseBackend()
           
 
Method Summary
 void addRecordMoveListener(RecordMoveListener<? super K,? super V,? super P> l)
          Add a listener that will receive notifications whenever a database record is moved to a new position.
protected abstract  void assertNotClosed()
          Subclasses implement this to throw an IllegalStateException if the database backend is closed.
 P find(Comparable<K> key, SearchMode mode)
          This implementation iterates through all keys in the database to find the best match.
 P find(K key, SearchMode mode, Comparator<? super K> cmp)
          This implementation iterates through all keys in the database to find the best match.
 int getContentsVersion()
          Get a version number for the contents of the database backend.
protected  boolean hasRecordMoveListeners()
          Does this database backend have any registered record move listeners?
protected  void notifyRecordMoveListeners(K key, P oldPos, P newPos)
          Notify all record move listeners that a record has been relocated.
 void removeRecordMoveListener(RecordMoveListener<? super K,? super V,? super P> l)
          Remove a record move listener.
protected  void updateContentsVersion()
          This is used by subclasses to update the contents version whenever the database is modified.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.helidb.backend.DatabaseBackend
clear, close, compact, delete, find, forEachKey, getFirstPosition, getKeys, getLastPosition, getNextPosition, getPreviousPosition, getRecords, getValueFor, getValues, insert, insertCheckKeyUnique, insertOrUpdate, keyIterator, readKeyAt, readRecordAt, readValueAt, remove, removeAt, replaceContentsWith, update, updateAt, valueIterator, writeContentsTo
 
Methods inherited from interface java.lang.Iterable
iterator
 

Constructor Detail

AbstractDatabaseBackend

public AbstractDatabaseBackend()
Method Detail

assertNotClosed

protected abstract void assertNotClosed()
                                 throws IllegalStateException
Subclasses implement this to throw an IllegalStateException if the database backend is closed.

Throws:
IllegalStateException - Thrown if the database backend is closed.
Since:
1.1

addRecordMoveListener

public void addRecordMoveListener(RecordMoveListener<? super K,? super V,? super P> l)
Description copied from interface: DatabaseBackend
Add a listener that will receive notifications whenever a database record is moved to a new position.

Specified by:
addRecordMoveListener in interface DatabaseBackend<K,V,P>
Parameters:
l - The listener.
See Also:
DatabaseBackend.removeRecordMoveListener(RecordMoveListener)

removeRecordMoveListener

public void removeRecordMoveListener(RecordMoveListener<? super K,? super V,? super P> l)
Description copied from interface: DatabaseBackend
Remove a record move listener.

Specified by:
removeRecordMoveListener in interface DatabaseBackend<K,V,P>
Parameters:
l - The listener to remove.
See Also:
DatabaseBackend.addRecordMoveListener(RecordMoveListener)

notifyRecordMoveListeners

protected void notifyRecordMoveListeners(K key,
                                         P oldPos,
                                         P newPos)
Notify all record move listeners that a record has been relocated.

Parameters:
key - The record's key.
oldPos - The old position.
newPos - The new position.

hasRecordMoveListeners

protected boolean hasRecordMoveListeners()
Does this database backend have any registered record move listeners?

Returns:
true if there are registered record move listeners.

getContentsVersion

public int getContentsVersion()
                       throws IllegalStateException
Description copied from interface: DatabaseBackend
Get a version number for the contents of the database backend. This version number should be updated every time that the backend contents are modified in any way.

The version number is used by database cursors to detect changes in the backend (upon which they throw ConcurrentModificationException:s).

The version number does not have to be persisted. It does not have to be consistent between different backend objects created on the same data.

Specified by:
getContentsVersion in interface DatabaseBackend<K,V,P>
Returns:
A version number.
Throws:
IllegalStateException - If the database backend has been closed.

updateContentsVersion

protected void updateContentsVersion()
This is used by subclasses to update the contents version whenever the database is modified.

Since:
1.1

find

public P find(Comparable<K> key,
              SearchMode mode)
This implementation iterates through all keys in the database to find the best match. Subclasses may override this to implement a more efficient solution, if possible.

Specified by:
find in interface DatabaseBackend<K,V,P>
Parameters:
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.

Returns:
The position of the record that matches the search criteria, or null if no record matching the search criteria is found.
See Also:
DatabaseBackend.find(Object), DatabaseBackend.find(Object, SearchMode, Comparator)

find

public P find(K key,
              SearchMode mode,
              Comparator<? super K> cmp)
This implementation iterates through all keys in the database to find the best match. Subclasses may override this to implement a more efficient solution, if possible.

Specified by:
find in interface DatabaseBackend<K,V,P>
Parameters:
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.
Returns:
The position of the record that matches the search criteria, or null if no record matching the search criteria is found.
See Also:
DatabaseBackend.find(Object), DatabaseBackend.find(Comparable, SearchMode)