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

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

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

This is an abstract base class that can be used as a starting point for implementing proxy objects for DatabaseBackend:s.

It implements methods for registering and deregistering RecordMoveListener:s. The listeners are registered on the proxied backend.

Since:
1.0
Author:
Karl Gustafsson
In_jar:
helidb-core

Constructor Summary
protected AbstractDatabaseBackendProxy(DatabaseBackend<K,V,P> proxied, boolean readOnly)
           
 
Method Summary
 void addRecordMoveListener(RecordMoveListener<? super K,? super V,? super P> l)
          Register the RecordMoveListener on the proxied backend.
protected  void assertNotClosed()
           
protected  void assertNotReadOnly()
           
 void close()
          Subclasses overriding this method must call super.close().
 boolean compact()
          Call compact on the proxied backend.
protected  void finalize()
           
 void forEachKey(ForEachKeyCallback<K,P> callback)
          Call the proxied backend's forEachKey method.
 int getContentsVersion()
          Call getContentsVersion on the proxied backend.
 P getFirstPosition()
          Call getFirstPosition on the proxied backend.
 Set<K> getKeys()
          Get the keys from 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.
protected  DatabaseBackend<K,V,P> getProxied()
          Get the proxied backend.
 Set<Record<K,V>> getRecords()
          Get the records from the proxied backend.
 Collection<V> getValues()
          Get the values from the proxied backend.
protected  boolean isClosed()
           
protected  boolean isReadOnly()
           
 Iterator<Record<K,V>> iterator()
          Get an iterator from the proxied backend.
 Iterator<K> keyIterator()
          Get a key iterator from the proxied backend.
 void removeRecordMoveListener(RecordMoveListener<? super K,? super V,? super P> l)
          Remove the RecordMoveListener from the proxied backend.
 Iterator<V> valueIterator()
          Get a value iterator from the proxied backend.
 long writeContentsTo(RandomAccess ra)
          Call writeContentsTo on the proxied backend.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.helidb.backend.DatabaseBackend
clear, delete, find, find, find, getValueFor, insert, insertCheckKeyUnique, insertOrUpdate, readKeyAt, readRecordAt, readValueAt, remove, removeAt, replaceContentsWith, update, updateAt
 

Constructor Detail

AbstractDatabaseBackendProxy

protected AbstractDatabaseBackendProxy(DatabaseBackend<K,V,P> proxied,
                                       boolean readOnly)
Method Detail

getProxied

protected final DatabaseBackend<K,V,P> getProxied()
Get the proxied backend.

Returns:
The proxied backend.

isClosed

protected final boolean isClosed()
Returns:
true if the backend is closed.

isReadOnly

protected final boolean isReadOnly()
Returns:
true if the backend is read only.

assertNotClosed

protected void assertNotClosed()
                        throws IllegalStateException
Throws:
IllegalStateException - If the backend is closed.

assertNotReadOnly

protected void assertNotReadOnly()
                          throws ReadOnlyException
Throws:
ReadOnlyException - If the backend is read only.

addRecordMoveListener

public void addRecordMoveListener(RecordMoveListener<? super K,? super V,? super P> l)
Register the RecordMoveListener on the proxied backend.

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)
Remove the RecordMoveListener from the proxied backend.

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

keyIterator

public Iterator<K> keyIterator()
                        throws IllegalStateException
Get a key iterator from the proxied backend.

Specified by:
keyIterator in interface DatabaseBackend<K,V,P>
Returns:
An Iterator over the database's keys.
Throws:
IllegalStateException

valueIterator

public Iterator<V> valueIterator()
                          throws IllegalStateException
Get a value iterator from the proxied backend.

Specified by:
valueIterator in interface DatabaseBackend<K,V,P>
Returns:
An Iterator over the database's values.
Throws:
IllegalStateException

iterator

public Iterator<Record<K,V>> iterator()
                               throws IllegalStateException
Get an iterator from the proxied backend.

Specified by:
iterator in interface Iterable<Record<K,V>>
Throws:
IllegalStateException

getKeys

public Set<K> getKeys()
               throws IllegalStateException
Get the keys from the proxied backend.

Specified by:
getKeys in interface DatabaseBackend<K,V,P>
Returns:
An immutable Set containing all keys in the database. If the database is empty, this method returns an empty Set.
Throws:
IllegalStateException - If the database backend has been closed.

getValues

public Collection<V> getValues()
                        throws IllegalStateException
Get the values from the proxied backend.

Specified by:
getValues in interface DatabaseBackend<K,V,P>
Returns:
An immutable Collection containing all values in the database. If the database is empty, this method returns an empty Collection.
Throws:
IllegalStateException - If the database backend has been closed.

getRecords

public Set<Record<K,V>> getRecords()
                            throws IllegalStateException
Get the records from the proxied backend.

Specified by:
getRecords in interface DatabaseBackend<K,V,P>
Returns:
An immutable Set containing all database records. If the database is empty, this method returns an empty Set.
Throws:
IllegalStateException - If the database backend has been closed.

forEachKey

public void forEachKey(ForEachKeyCallback<K,P> callback)
                throws IllegalStateException
Call the proxied backend's forEachKey method.

Specified by:
forEachKey in interface DatabaseBackend<K,V,P>
Parameters:
callback - The callback object to call for each key.
Throws:
IllegalStateException - If the database backend has been closed.

writeContentsTo

public long writeContentsTo(RandomAccess ra)
                     throws IllegalStateException
Call writeContentsTo on the proxied backend.

Specified by:
writeContentsTo in interface DatabaseBackend<K,V,P>
Parameters:
ra - The RandomAccess to write to. It is not closed after writing.
Returns:
The size of the contents in bytes.
Throws:
IllegalStateException - If the database backend has been closed.

getContentsVersion

public int getContentsVersion()
                       throws IllegalStateException
Call getContentsVersion on the proxied backend.

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

getFirstPosition

public P getFirstPosition()
                   throws WrappedIOException,
                          IllegalStateException
Call getFirstPosition on the proxied backend.

Specified by:
getFirstPosition in interface DatabaseBackend<K,V,P>
Returns:
The position of the first record in the backend, or null if the backend is empty.
Throws:
WrappedIOException - On I/O errors.
IllegalStateException - If the database backend has been closed.
See Also:
DatabaseBackend.getLastPosition(), DatabaseBackend.getNextPosition(Object), DatabaseBackend.getPreviousPosition(Object)

getLastPosition

public P getLastPosition()
                  throws WrappedIOException,
                         IllegalStateException
Call getLastPosition on the proxied backend.

Specified by:
getLastPosition in interface DatabaseBackend<K,V,P>
Returns:
The position of the last record in the backend, or null if the backend is empty.
Throws:
WrappedIOException - On I/O errors.
IllegalStateException - If the database backend has been closed.
See Also:
DatabaseBackend.getFirstPosition(), DatabaseBackend.getNextPosition(Object), DatabaseBackend.getPreviousPosition(Object)

getNextPosition

public P getNextPosition(P pos)
                  throws WrappedIOException,
                         IllegalStateException
Call getNextPosition on the proxied backend.

Specified by:
getNextPosition in interface DatabaseBackend<K,V,P>
Parameters:
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.
Returns:
The position of the next key in the database backend, or null if the supplied position references the last record in the backend (the position returned from DatabaseBackend.getLastPosition()).
Throws:
WrappedIOException - On I/O errors.
IllegalStateException - If the database backend has been closed.
See Also:
DatabaseBackend.getFirstPosition(), DatabaseBackend.getLastPosition(), DatabaseBackend.getPreviousPosition(Object)

getPreviousPosition

public P getPreviousPosition(P pos)
                      throws WrappedIOException,
                             IllegalStateException,
                             UnsupportedOperationException
Call getPreviousPosition on the proxied backend.

Specified by:
getPreviousPosition in interface DatabaseBackend<K,V,P>
Parameters:
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.
Returns:
The position of the previous key in the database backend, or null if the supplied position references the first record in the backend (the position returned from DatabaseBackend.getFirstPosition()).
Throws:
WrappedIOException - On I/O errors.
IllegalStateException - If the database backend has been closed.
UnsupportedOperationException - If this method is not supported by the backend implementation.
See Also:
DatabaseBackend.getFirstPosition(), DatabaseBackend.getLastPosition(), DatabaseBackend.getNextPosition(Object)

compact

public boolean compact()
                throws IllegalStateException
Call compact on the proxied backend.

Specified by:
compact in interface DatabaseBackend<K,V,P>
Returns:
true if the backend was modified, false if not.
Throws:
IllegalStateException - If the database backend has been closed.

close

public void close()
Subclasses overriding this method must call super.close().

Specified by:
close in interface DatabaseBackend<K,V,P>

finalize

protected void finalize()
                 throws Throwable
Overrides:
finalize in class Object
Throws:
Throwable