org.helidb.impl.txn
Class AbstractTransactionalDatabase<K,V,P>

java.lang.Object
  extended by org.helidb.impl.AbstractDatabase<K,V,P>
      extended by org.helidb.impl.txn.AbstractTransactionalDatabase<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>, TransactionalDatabase<K,V>
Direct Known Subclasses:
LoggingTransactionalDatabase, ShadowCopyTransactionalDatabase

public abstract class AbstractTransactionalDatabase<K,V,P>
extends AbstractDatabase<K,V,P>
implements TransactionalDatabase<K,V>

This abstract class can be extended by subclasses that implement a transactional database. It implements all methods of the TransactionalDatabase. Subclasses only have to implement the abstract methods of this class and of AbstractDatabase.

When participating in a read/write transaction, this database is locked by a database-global, exclusive write lock. When participating in a read only transaction, it is locked by a database-global, shared read lock that can be shared by other read only transactions.

The inherited equals(Object) and hashCode() methods are overridden by this class and they no longer follow the contract from Map since that would require a transaction every time that they were called.

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 AbstractTransactionalDatabase(LogAdapterHolder lah)
           
 
Method Summary
protected  void cleanupBeforeReleasingReadLock()
          This method is called to clean up the database state before releasing the read lock for a read only transaction.
 void close()
          This method overrides the inherited close method to acquire a write lock on the database before calling super.close().
protected  void closeWithWriteLockOnDb()
          This method is called by close() when it has acquired an exclusive write lock on the database.
protected abstract  TransactionCollaborator<K,V,P> createReadOnlyCollaborator(Lock readLock)
          Create a TransactionCollaborator for a read only transaction.
protected abstract  TransactionCollaborator<K,V,P> createReadWriteCollaborator(Lock writeLock)
          Create a TransactionCollaborator for a read/write transaction.
 boolean equals(Object o)
          Not implemented according to the contract in Map since that would require a transaction.
protected  DatabaseBackend<K,V,P> getBackendForReading()
          Get a readable database backend.
protected  DatabaseBackend<K,V,P> getBackendForWriting()
          Get a writable database backend.
protected  Observable getCloseObservable()
          Get an observable that notifies its observers when the database is closed.
protected  ReadWriteLock getLock()
          Get the database's ReadWriteLock.
 int hashCode()
          Not implemented according to the contract in Map since that would require a transaction.
 boolean isClosed()
          This method overrides the inherited isClosed method to acquire a read lock on the database before calling super.isClosed().
 void joinTransaction(boolean readOnly)
          Manually join the calling thread's transaction.
 
Methods inherited from class org.helidb.impl.AbstractDatabase
assertNotClosed, clear, closeBackend, compact, containsKey, containsValue, delete, entrySet, fasterInsert, find, find, find, firstRecord, get, getLogAdapterHolder, insert, insertOrUpdate, isEmpty, iterator, keyIterator, keySet, lastRecord, put, putAll, remove, size, update, valueIterator, values
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.helidb.Database
compact, delete, fasterInsert, find, find, find, firstRecord, insert, insertOrUpdate, keyIterator, lastRecord, update, valueIterator
 
Methods inherited from interface java.util.Map
clear, containsKey, containsValue, entrySet, get, isEmpty, keySet, put, putAll, remove, size, values
 
Methods inherited from interface java.lang.Iterable
iterator
 

Constructor Detail

AbstractTransactionalDatabase

protected AbstractTransactionalDatabase(LogAdapterHolder lah)
Method Detail

cleanupBeforeReleasingReadLock

protected void cleanupBeforeReleasingReadLock()
This method is called to clean up the database state before releasing the read lock for a read only transaction.

This method does nothing. Subclasses may override it.


createReadOnlyCollaborator

protected abstract TransactionCollaborator<K,V,P> createReadOnlyCollaborator(Lock readLock)
Create a TransactionCollaborator for a read only transaction.

Parameters:
readLock - A locked, shared read lock for the database. The collaborator is responsible for releasing this lock when it is committed or rolled back.
Returns:
A collaborator for a read only transaction.

createReadWriteCollaborator

protected abstract TransactionCollaborator<K,V,P> createReadWriteCollaborator(Lock writeLock)
Create a TransactionCollaborator for a read/write transaction.

Parameters:
writeLock - A locked, exclusive write lock for the database. The collaborator is responsible for releasing this lock when it is committed or rolled back.
Returns:
A collaborator for a read/write transaction.

getCloseObservable

protected Observable getCloseObservable()
Get an observable that notifies its observers when the database is closed. The observable can be handed on to collaborators that need it.

Returns:
An observable that notifies its observers when the database is closed.

getLock

protected ReadWriteLock getLock()
Get the database's ReadWriteLock.

Returns:
The database's ReadWriteLock.

joinTransaction

public void joinTransaction(boolean readOnly)
Description copied from interface: TransactionalDatabase
Manually join the calling thread's transaction. If this is not called, the calling thread's transaction is joined automatically when the thread reads from or writes to the database.

Specified by:
joinTransaction in interface TransactionalDatabase<K,V>
Parameters:
readOnly - Should this database join the transaction as read only?

getBackendForWriting

protected DatabaseBackend<K,V,P> getBackendForWriting()
                                               throws NoTransactionException,
                                                      ReadOnlyTransactionException,
                                                      WrappedIOException
Get a writable database backend.

If this database does not already participate in a read/write transaction, this method joins the current transaction and copies the database backend file to a temporary location for writing.

Specified by:
getBackendForWriting in class AbstractDatabase<K,V,P>
Returns:
A writable database backend.
Throws:
NoTransactionException - If there is no current transaction.
ReadOnlyTransactionException - If the current transaction is read only.
WrappedIOException - If there is an error copying the database file when joining a read/write transaction.

getBackendForReading

protected DatabaseBackend<K,V,P> getBackendForReading()
                                               throws NoTransactionException,
                                                      WrappedIOException
Get a readable database backend.

If this database does not already participate in a read only or read/write transaction, this method joins the current transaction. If the current transaction is read/write, the database backend file is copied to a temporary location when joining.

Specified by:
getBackendForReading in class AbstractDatabase<K,V,P>
Returns:
A backend object for reading data from.
Throws:
NoTransactionException - If there is no current transaction.
WrappedIOException - If there is an error copying the database file when joining a read/write transaction.

closeWithWriteLockOnDb

protected void closeWithWriteLockOnDb()
This method is called by close() when it has acquired an exclusive write lock on the database. Subclasses may override this method, but they should remember to call super.closeWithWriteLockOnDb().


close

public void close()
This method overrides the inherited close method to acquire a write lock on the database before calling super.close().

Specified by:
close in interface Database<K,V>
Overrides:
close in class AbstractDatabase<K,V,P>

isClosed

public boolean isClosed()
This method overrides the inherited isClosed method to acquire a read lock on the database before calling super.isClosed().

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

equals

public boolean equals(Object o)
Not implemented according to the contract in Map since that would require a transaction.

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

hashCode

public int hashCode()
Not implemented according to the contract in Map since that would require a transaction.

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