org.helidb.impl
Class AbstractDatabase<K,V,P>

java.lang.Object
  extended by org.helidb.impl.AbstractDatabase<K,V,P>
Type Parameters:
K - The type of the keys in the database.
V - The type of the values in the database.
P - The type of positions used by the DatabaseBackend to locate records.
All Implemented Interfaces:
Iterable<Record<K,V>>, Map<K,V>, Database<K,V>
Direct Known Subclasses:
AbstractTransactionalDatabase, SimpleDatabase

public abstract class AbstractDatabase<K,V,P>
extends Object
implements Database<K,V>

Abstract implementation of the Database interface that can be used as a starting point for database implementation. This class implements all methods in the Database interface. Subclasses only have to implement the abstract methods defined in this class.

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

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Constructor Summary
protected AbstractDatabase(LogAdapterHolder lah)
           
 
Method Summary
protected  void assertNotClosed()
          Throw an exception if this database instance has been closed.
 void clear()
           
 void close()
          If overriding this method, make sure super.close() is called.
protected abstract  void closeBackend()
          Subclasses implement this to call close on the backend.
 boolean compact()
          Defragment the database backend.
 boolean containsKey(Object key)
           
 boolean containsValue(Object val)
           
 boolean delete(K key)
          Delete the record with the supplied key from the database.
 Set<Map.Entry<K,V>> entrySet()
           
 boolean equals(Object o)
          Implemented according as specified by the contract in Map.
 void fasterInsert(K key, V value)
          This is a faster version of Database.insert(Object, Object) that does not check if the key already exists in the database.
 Cursor<K,V> find(K key)
          Search for the supplied key in the database and return a Cursor at the found record.
 Cursor<K,V> find(K key, SearchMode mode)
          Search for the key in the database using the supplied SearchMode.
 Cursor<K,V> find(K key, SearchMode mode, Comparator<? super K> cmp)
          Search for the key in the database using the supplied SearchMode.
 Cursor<K,V> firstRecord()
          Get a Cursor at the first record in the database.
 V get(Object key)
           
protected abstract  DatabaseBackend<K,V,P> getBackendForReading()
          Subclasses implement this to return a backend object that can be used for reading data.
protected abstract  DatabaseBackend<K,V,P> getBackendForWriting()
          Subclasses implement this to return a backend object that can be used for writing data to.
protected  LogAdapterHolder getLogAdapterHolder()
          Subclasses call this to get something to log to.
 int hashCode()
          Implemented according as specified by the contract in Map.
 void insert(K key, V value)
          Insert a new record in the database.
 boolean insertOrUpdate(K key, V value)
          If the record's key already exists in the database, update the existing record.
 boolean isClosed()
          Is this database closed?
 boolean isEmpty()
           
 Iterator<Record<K,V>> iterator()
           
 Iterator<K> keyIterator()
          This method returns an iterator over the keys in the database.
 Set<K> keySet()
           
 Cursor<K,V> lastRecord()
          Get a Cursor at the last record in the database.
 V put(K key, V value)
           
 void putAll(Map<? extends K,? extends V> m)
           
 V remove(Object key)
           
 int size()
           
 void update(K key, V value)
          Update an existing record in the database with a new value.
 Iterator<V> valueIterator()
          Get an iterator over the values in the database.
 Collection<V> values()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractDatabase

protected AbstractDatabase(LogAdapterHolder lah)
Method Detail

getBackendForReading

protected abstract DatabaseBackend<K,V,P> getBackendForReading()
Subclasses implement this to return a backend object that can be used for reading data. Transactional database implementations make sure to join the current transaction, if the database has not already joined.

Returns:
A backend object for reading data from.

getBackendForWriting

protected abstract DatabaseBackend<K,V,P> getBackendForWriting()
Subclasses implement this to return a backend object that can be used for writing data to. Transactional database implementations make sure to join the current transaction, if the database has not already joined.

Returns:
A backend object for writing data to.

closeBackend

protected abstract void closeBackend()
Subclasses implement this to call close on the backend. This is called by close().


getLogAdapterHolder

protected final LogAdapterHolder getLogAdapterHolder()
Subclasses call this to get something to log to.

Returns:
The database's log adapter.

assertNotClosed

protected void assertNotClosed()
                        throws IllegalStateException
Throw an exception if this database instance has been closed.

Throws:
IllegalStateException - If the database instance has been closed.

keySet

public Set<K> keySet()
Specified by:
keySet in interface Map<K,V>

values

public Collection<V> values()
Specified by:
values in interface Map<K,V>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map<K,V>

entrySet

public Set<Map.Entry<K,V>> entrySet()
Specified by:
entrySet in interface Map<K,V>

containsValue

public boolean containsValue(Object val)
Specified by:
containsValue in interface Map<K,V>

insert

public void insert(K key,
                   V value)
Description copied from interface: Database
Insert a new record in the database. The record's key must not exist already in the database.

This method makes sure that the supplied key is unique before the record is inserted. The Database.fasterInsert(Object, Object) can be used instead when the client is absolute sure that the key is unique. It should be significantly faster for large databases.

Specified by:
insert in interface Database<K,V>
Parameters:
key - The record's key
value - The record's value
See Also:
Database.insertOrUpdate(Object, Object), Database.update(Object, Object), Map.put(Object, Object), Database.fasterInsert(Object, Object)

fasterInsert

public void fasterInsert(K key,
                         V value)
Description copied from interface: Database
This is a faster version of Database.insert(Object, Object) that does not check if the key already exists in the database. It is the client's responsibility to avoid key collisions.

The database's behavior if duplicate keys are inserted is unspecified, and it probably varies between different DatabaseBackend implementations.

Specified by:
fasterInsert in interface Database<K,V>
Parameters:
key - The record's key.
value - The record's value.
See Also:
Database.insert(Object, Object), Database.insertOrUpdate(Object, Object), Map.put(Object, Object)

insertOrUpdate

public boolean insertOrUpdate(K key,
                              V value)
Description copied from interface: Database
If the record's key already exists in the database, update the existing record. If not, insert a new record.

This method is quite similar to Map.put(Object, Object), but its implementation should be slightly more efficient since it does not return the record's old value.

Specified by:
insertOrUpdate in interface Database<K,V>
Parameters:
key - The record's key.
value - The record's value.
Returns:
true if an existing record was updated, false if a new record was inserted.
See Also:
Database.insert(Object, Object), Database.update(Object, Object), Map.put(Object, Object), Database.fasterInsert(Object, Object)

update

public void update(K key,
                   V value)
Description copied from interface: Database
Update an existing record in the database with a new value.

Specified by:
update in interface Database<K,V>
Parameters:
key - The record's key.
value - The record's new value.
See Also:
Database.insert(Object, Object), Database.fasterInsert(Object, Object), Database.insertOrUpdate(Object, Object), Map.put(Object, Object)

put

public V put(K key,
             V value)
Specified by:
put in interface Map<K,V>

putAll

public void putAll(Map<? extends K,? extends V> m)
Specified by:
putAll in interface Map<K,V>

clear

public void clear()
Specified by:
clear in interface Map<K,V>

compact

public boolean compact()
Description copied from interface: Database
Defragment the database backend. How this is done is up to the backend implementation that is used.

Since defragmenting a database potentially involves modifying the entire database, this method may take a long time to complete depending on the size of the database.

Specified by:
compact in interface Database<K,V>
Returns:
true if the database was compacted, or false if there was nothing to compact.
See Also:
DatabaseBackend.compact()

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map<K,V>

size

public int size()
Specified by:
size in interface Map<K,V>

get

public V get(Object key)
      throws ClassCastException
Specified by:
get in interface Map<K,V>
Throws:
ClassCastException

remove

public V remove(Object key)
         throws ClassCastException,
                NullPointerException
Specified by:
remove in interface Map<K,V>
Throws:
ClassCastException
NullPointerException

delete

public boolean delete(K key)
Description copied from interface: Database
Delete the record with the supplied key from the database. If the key does not exist in the database, this method returns false.

This method should be slightly more efficient than Map.remove(Object) since it does not return the record's previous value.

Specified by:
delete in interface Database<K,V>
Parameters:
key - The key of the record to delete.
Returns:
true if a record was deleted, false if there was no record with the supplied key to delete.
See Also:
Map.remove(Object)

iterator

public Iterator<Record<K,V>> iterator()
Specified by:
iterator in interface Iterable<Record<K,V>>

keyIterator

public Iterator<K> keyIterator()
Description copied from interface: Database
This method returns an iterator over the keys in the database. The order that the keys are returned in is dependent on the DatabaseBackend implementation used by the database and whether the database uses an index or not. (See the class documentation above.)

If the database is modified after calling this method, the methods of the Iterator returned will throw ConcurrentModificationException on subsequent invocations.

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

valueIterator

public Iterator<V> valueIterator()
Description copied from interface: Database
Get an iterator over the values in the database. The order that the values are returned in is dependent on the DatabaseBackend implementation used by the database and whether the database uses an index or not. (See the class documentation above.)

If the database is modified after calling this method, the methods of the Iterator returned will throw ConcurrentModificationException on subsequent invocations.

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

firstRecord

public Cursor<K,V> firstRecord()
Description copied from interface: Database
Get a Cursor at the first record in the database. Which record that is and how the Cursor will navigate through the database is determined by the database backend's search order.

The returned Cursor is valid until the database is updated.

Specified by:
firstRecord in interface Database<K,V>
Returns:
A Cursor at the database's first record, or null if the database is empty.
See Also:
Database.lastRecord()

lastRecord

public Cursor<K,V> lastRecord()
Description copied from interface: Database
Get a Cursor at the last record in the database. Which record that is and how the Cursor will navigate through the database is determined by the database backend's search order.

The returned Cursor is valid until the database is updated.

Specified by:
lastRecord in interface Database<K,V>
Returns:
A Cursor at the database's last record, or null if the database is empty.
See Also:
Database.lastRecord()

find

public Cursor<K,V> find(K key)
Description copied from interface: Database
Search for the supplied key in the database and return a Cursor at the found record.

The returned Cursor is valid until the database is updated.

Specified by:
find in interface Database<K,V>
Parameters:
key - The key to search for.
Returns:
A Cursor at the found record, or null if the key was not in the database.
See Also:
Database.find(Object, SearchMode), Database.find(Object, SearchMode, Comparator)

find

public Cursor<K,V> find(K key,
                        SearchMode mode)
Description copied from interface: Database
Search for the key in the database using the supplied SearchMode. The natural ordering of the database keys, which must be Comparable, is used to find closest matches.

The returned Cursor is valid until the database is updated.

Specified by:
find in interface Database<K,V>
Parameters:
key - The key to search for.
mode - The search mode. This should be a mode that is supported by the database and its backend.
Returns:
A Cursor at the found record, or null if no matching key was found.
See Also:
Database.find(Object), Database.find(Object, SearchMode, Comparator)

find

public Cursor<K,V> find(K key,
                        SearchMode mode,
                        Comparator<? super K> cmp)
Description copied from interface: Database
Search for the key in the database using the supplied SearchMode. The supplied Comparator is used to find closest matches.

The returned Cursor is valid until the database is updated.

Specified by:
find in interface Database<K,V>
Parameters:
key - The key to search for.
mode - The search mode. This should be a mode that is supported by the database and its backend.
cmp - A Comparator used for finding closest matches between the search key and the keys in the database.
Returns:
A Cursor at the found record, or null if no matching key was found.
See Also:
Database.find(Object), Database.find(Object, SearchMode)

close

public void close()
If overriding this method, make sure super.close() is called.

Specified by:
close in interface Database<K,V>

isClosed

public boolean isClosed()
Description copied from interface: Database
Is this database closed?

Specified by:
isClosed in interface Database<K,V>
Returns:
true if this database is closed.

equals

public boolean equals(Object o)
Implemented according as specified by the contract in Map.

Specified by:
equals in interface Map<K,V>
Overrides:
equals in class Object

hashCode

public int hashCode()
Implemented according as specified by the contract in Map.

Specified by:
hashCode in interface Map<K,V>
Overrides:
hashCode in class Object