org.gillius.realdb.model.pool
Class DefaultBlockPool

java.lang.Object
  extended by org.gillius.realdb.model.pool.DefaultBlockPool
All Implemented Interfaces:
java.io.Flushable, Formattable, BlockPool

public class DefaultBlockPool
extends java.lang.Object
implements BlockPool

This implementation always chooses reclaimable blocks before unused blocks, and always chooses the lowest number block in getFreeBlock().

The pool must be opened before calling any other methods besides format().

Author:
Jason Winnebeck

Constructor Summary
DefaultBlockPool(TransactionLog log, int numDataBlocks)
          Creates a DefaultBlockPool.
 
Method Summary
 void addBlock(BlockTransaction tx, int lastBlock, int destination, int seqNum)
          Adds a block obtained from BlockPool.getFreeBlock() or BlockPool.removeBlock(int, int, int, long).
 void flush()
           
 void format()
          Formats the underlying data storage for this object to represent an "initial" (usually means "empty" state).
 int getDataBlocksUsed()
          Returns the number of data blocks in use.
 BlockTransaction getFreeBlock()
          If BlockPool.hasFreeBlocks(), then returns a new transaction with the free block.
 TransactionLog getLog()
           
 int getNextUnusedBlock()
           
 int getNumDataBlocks()
          Returns the total number of data blocks managed by this pool.
 java.util.Set<java.lang.Integer> getReclaimableBlocks()
          Returns a set of blocks that were used at one point but no longer and will be reclaimed first when getting free blocks.
 boolean hasFreeBlocks()
          Returns true if there are any free blocks in the pool.
 boolean isOpened()
           
 void open()
          Opens the TransactionLog given when this object was constructed.
 BlockTransaction removeBlock(int block, int sourceIndex, int seqNum, long secondBlockStartTime)
          Signals an intention to remove the indicated block from the indicated index immediately after calling this method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultBlockPool

public DefaultBlockPool(TransactionLog log,
                        int numDataBlocks)
Creates a DefaultBlockPool. The given TransactionLog will have its header writer set.

The pool must be opened before calling any other methods besides format().

Parameters:
log - non-null TransactionLog
numDataBlocks - the number of data blocks in the pool
Method Detail

open

public void open()
          throws java.io.IOException,
                 BadFileException
Description copied from interface: BlockPool
Opens the TransactionLog given when this object was constructed. Any blocks in open transactions become reclaimable blocks.

The pool must be opened before calling any other methods besides Formattable.format().

Specified by:
open in interface BlockPool
Throws:
java.io.IOException
BadFileException

getLog

public TransactionLog getLog()
Specified by:
getLog in interface BlockPool

getNumDataBlocks

public int getNumDataBlocks()
Description copied from interface: BlockPool
Returns the total number of data blocks managed by this pool.

Specified by:
getNumDataBlocks in interface BlockPool

getDataBlocksUsed

public int getDataBlocksUsed()
Description copied from interface: BlockPool
Returns the number of data blocks in use.

Specified by:
getDataBlocksUsed in interface BlockPool

getNextUnusedBlock

public int getNextUnusedBlock()

getReclaimableBlocks

public java.util.Set<java.lang.Integer> getReclaimableBlocks()
Returns a set of blocks that were used at one point but no longer and will be reclaimed first when getting free blocks.


isOpened

public boolean isOpened()

hasFreeBlocks

public boolean hasFreeBlocks()
Description copied from interface: BlockPool
Returns true if there are any free blocks in the pool.

Specified by:
hasFreeBlocks in interface BlockPool

getFreeBlock

public BlockTransaction getFreeBlock()
                              throws java.io.IOException
Description copied from interface: BlockPool
If BlockPool.hasFreeBlocks(), then returns a new transaction with the free block.

Specified by:
getFreeBlock in interface BlockPool
Returns:
a BlockTransaction with the free block and a single action to flush the transaction log.
Throws:
java.io.IOException - if there is an implicit log flush and it fails.

removeBlock

public BlockTransaction removeBlock(int block,
                                    int sourceIndex,
                                    int seqNum,
                                    long secondBlockStartTime)
                             throws java.io.IOException
Description copied from interface: BlockPool
Signals an intention to remove the indicated block from the indicated index immediately after calling this method.

Specified by:
removeBlock in interface BlockPool
Parameters:
block - block number to remove
sourceIndex - source index ID
seqNum - index's head sequence number (used in recovery to verify action)
secondBlockStartTime - start time of the second block in the index that will be passed to DataIndex.removeFirstBlock(long, org.gillius.realdb.model.Transaction); needed for replay.
Returns:
a BlockTransaction with the removed block and a single action to flush the transaction log.
Throws:
java.io.IOException - if there is an implicit log flush and it fails.

addBlock

public void addBlock(BlockTransaction tx,
                     int lastBlock,
                     int destination,
                     int seqNum)
              throws java.io.IOException
Description copied from interface: BlockPool
Adds a block obtained from BlockPool.getFreeBlock() or BlockPool.removeBlock(int, int, int, long). This method must be called after writing the data block to the new state and before adding the block to the index. This method finalizes the transaction; the only operation remaining is to commit it.

Specified by:
addBlock in interface BlockPool
Parameters:
tx - transaction returned from BlockPool.getFreeBlock() or BlockPool.removeBlock(int, int, int, long)
lastBlock - current last block in the index, used only for extra verification in recovery, or -1 if the index is empty.
destination - index destination ID
seqNum - index tail sequence number on disk (needed for recovery)
Throws:
java.io.IOException

flush

public void flush()
           throws java.io.IOException
Specified by:
flush in interface java.io.Flushable
Throws:
java.io.IOException

format

public void format()
            throws java.io.IOException
Description copied from interface: Formattable
Formats the underlying data storage for this object to represent an "initial" (usually means "empty" state). This operation is not interruptable and if it fails the object and the underlying data storage is an undefined state, which only can be recovered through another format operation.

Specified by:
format in interface Formattable
Throws:
java.io.IOException - if an error occurs writing to the data storage


Copyright © 2008-2010 Jason Winnebeck. All Rights Reserved.