org.helidb.impl.txn.log
Class LoggingTransactionalDatabase<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>
          extended by org.helidb.impl.txn.log.LoggingTransactionalDatabase<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>

public class LoggingTransactionalDatabase<K,V,P>
extends AbstractTransactionalDatabase<K,V,P>

This Database implementation keeps a rollback log for read/write transactions. If the transaction is rolled back the rollback log is replayed to restore the database state. The rollback log is also used for restoring the database state when a new database object is created after the old one has crashed.

Read/write transactions hold an exclusive write lock on the entire database. Read only transactions hold a shared read lock that can be shared with other read only transactions.

Since the database uses its own locking, the database and log files should not be in a locking file system.

ACID:ity is guaranteed in the following way:

Atomicity
When a read/write transaction is rolled back, its rollback log is played back to restore the database to the state it had when the transaction started. When a read/write transaction is committed, the rollback log is discarded.
Consistency
If a transaction is committed, all its changes are already persisted in the database. If it is rolled back, its rollback log is played back to restore the database state. If the database crashes during the transaction, the rollback log can be used to restore its state.
Isolation
Isolation is achieved by locking the database pessimistically when writing to it.
Durability
When the transaction is committed, the contents of the database file is updated.

This database is thread safe.

The equals and hashCode methods are not implemented according to the contract in Map. See AbstractTransactionalDatabase.

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

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Constructor Summary
LoggingTransactionalDatabase(DatabaseBackend<K,V,P> backend, ReadWritableFile logFile, Serializer<K> keySerializer, Serializer<V> valueSerializer, LogAdapterHolder lah)
          Constructor.
 
Method Summary
protected  void closeBackend()
          Subclasses implement this to call close on the backend.
protected  TransactionCollaborator<K,V,P> createReadOnlyCollaborator(Lock readLock)
          Create a TransactionCollaborator for a read only transaction.
protected  TransactionCollaborator<K,V,P> createReadWriteCollaborator(Lock writeLock)
          Create a TransactionCollaborator for a read/write transaction.
 
Methods inherited from class org.helidb.impl.txn.AbstractTransactionalDatabase
cleanupBeforeReleasingReadLock, close, closeWithWriteLockOnDb, equals, getBackendForReading, getBackendForWriting, getCloseObservable, getLock, hashCode, isClosed, joinTransaction
 
Methods inherited from class org.helidb.impl.AbstractDatabase
assertNotClosed, clear, 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

LoggingTransactionalDatabase

public LoggingTransactionalDatabase(DatabaseBackend<K,V,P> backend,
                                    ReadWritableFile logFile,
                                    Serializer<K> keySerializer,
                                    Serializer<V> valueSerializer,
                                    LogAdapterHolder lah)
Constructor.

The LoggingTransactionalDatabaseBuilder may be more convenient to use than this constructor.

Parameters:
backend - The database backend.
logFile - The rollback log file. If it is not empty, it is replayed to restore the database state.
keySerializer - Serializer for database keys.
valueSerializer - Serializer for database values.
lah - A log adapter holder.
Method Detail

createReadOnlyCollaborator

protected TransactionCollaborator<K,V,P> createReadOnlyCollaborator(Lock readLock)
Description copied from class: AbstractTransactionalDatabase
Create a TransactionCollaborator for a read only transaction.

Specified by:
createReadOnlyCollaborator in class AbstractTransactionalDatabase<K,V,P>
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 TransactionCollaborator<K,V,P> createReadWriteCollaborator(Lock writeLock)
Description copied from class: AbstractTransactionalDatabase
Create a TransactionCollaborator for a read/write transaction.

Specified by:
createReadWriteCollaborator in class AbstractTransactionalDatabase<K,V,P>
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.

closeBackend

protected void closeBackend()
Description copied from class: AbstractDatabase
Subclasses implement this to call close on the backend. This is called by AbstractDatabase.close().

Specified by:
closeBackend in class AbstractDatabase<K,V,P>