org.helidb.impl.txn.sc
Class ShadowCopyTransactionalDatabase<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.sc.ShadowCopyTransactionalDatabase<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 ShadowCopyTransactionalDatabase<K,V,P>
extends AbstractTransactionalDatabase<K,V,P>

This transactional database frontend creates shadow copies of its database files for read/write transactions, and uses them to replace the original files when the transaction is committed. If the transaction is rolled back, the shadow copies are just deleted. In other words: joining a read/write transaction forces a copy of all database files.

The database implementation uses pessimistic read/write locking for preventing changes made in one transaction to disturb other transactions. This means that there can be several ongoing read only transactions against the same database at any time, but if a transaction wants to write to the database, it must have exclusive access to it and hold the database's write lock for the duration of the transaction.

ACID:ity is guaranteed in the following way:

Atomicity
When the transaction is committed, the contents of the original database file is replaced in its entirety or not at all.
Consistency
When the transaction is committed, the contents of the original database file is replaced in its entirety or not at all.
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.

In order to be able to create new DatabaseBackend objects for the shadow copy files, this class uses a DatabaseBackendFactory instead of just one DatabaseBackend.

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-shadow_copy_txn

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Constructor Summary
ShadowCopyTransactionalDatabase(ShadowCopyTxnDatabaseFileManager fileManager, DatabaseBackendFactory<K,V,P> dbf, boolean autoCompact, LogAdapterHolder lah)
          Create a new database.
 
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.
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
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

ShadowCopyTransactionalDatabase

public ShadowCopyTransactionalDatabase(ShadowCopyTxnDatabaseFileManager fileManager,
                                       DatabaseBackendFactory<K,V,P> dbf,
                                       boolean autoCompact,
                                       LogAdapterHolder lah)
Create a new database.

Parameters:
fileManager - A file manager for managing the database files. The file manager must be one that works for the supplied DatabaseBackendFactory, for instance a BPlusTreeIndexFileManager for a BPlusTreeIndexBackendFactory.
dbf - A factory for creating DatabaseBackend objects.
autoCompact - Should the backend be compacted before every time that a transaction is committed?
lah - A log adapter holder.
Method Detail

cleanupBeforeReleasingReadLock

protected void cleanupBeforeReleasingReadLock()
Description copied from class: AbstractTransactionalDatabase
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.

Overrides:
cleanupBeforeReleasingReadLock in class AbstractTransactionalDatabase<K,V,P>

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>