org.apache.jcs.auxiliary.disk.block
Class BlockDisk

java.lang.Object
  extended by org.apache.jcs.auxiliary.disk.block.BlockDisk

public class BlockDisk
extends java.lang.Object

This class manages reading an writing data to disk. When asked to write a value, it returns a block array. It can read an object from the block numbers in a byte array.

Author:
Aaron Smuts

Field Summary
protected  IElementSerializer elementSerializer
          The serializer.
static byte HEADER_SIZE_BYTES
          The size of the header that indicates the amount of data stored in an occupied block.
 
Constructor Summary
BlockDisk(java.io.File file, IElementSerializer elementSerializer)
          Constructor for the Disk object
BlockDisk(java.io.File file, int blockSizeBytes)
          Creates the file and set the block size in bytes.
 
Method Summary
protected  int calculateByteOffsetForBlock(int block)
          Calcuates the file offset for a particular block.
protected  int calculateTheNumberOfBlocksNeeded(byte[] data)
          The number of blocks needed.
protected  void close()
          Closes the raf.
protected  void freeBlocks(int[] blocksToFree)
          Add these blocks to the emptyBlock list.
protected  long getAveragePutSizeBytes()
           
protected  byte[][] getBlockChunks(byte[] complete, int numBlocksNeeded)
          Return the amount to put in each block.
protected  int getBlockSizeBytes()
           
protected  int getEmptyBlocks()
           
protected  java.lang.String getFilePath()
          This is used for debugging.
protected  int getNumberOfBlocks()
           
protected  long length()
          Returns the raf length.
protected  java.io.Serializable read(int[] blockNumbers)
          Reads an object that is located in the specified blocks.
 java.lang.String toString()
          For debugging only.
protected  int[] write(java.io.Serializable object)
          This writes an object to disk and returns the blocks it was stored in.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

HEADER_SIZE_BYTES

public static final byte HEADER_SIZE_BYTES
The size of the header that indicates the amount of data stored in an occupied block.

See Also:
Constant Field Values

elementSerializer

protected IElementSerializer elementSerializer
The serializer. Uses a standard serializer by default.

Constructor Detail

BlockDisk

public BlockDisk(java.io.File file,
                 IElementSerializer elementSerializer)
          throws java.io.FileNotFoundException
Constructor for the Disk object

Parameters:
file -
elementSerializer -
Throws:
java.io.FileNotFoundException

BlockDisk

public BlockDisk(java.io.File file,
                 int blockSizeBytes)
          throws java.io.FileNotFoundException
Creates the file and set the block size in bytes.

Parameters:
file -
blockSizeBytes -
Throws:
java.io.FileNotFoundException
Method Detail

write

protected int[] write(java.io.Serializable object)
               throws java.io.IOException
This writes an object to disk and returns the blocks it was stored in.

The program flow is as follows:

  1. Serialize the object.
  2. Detemine the number of blocks needed.
  3. Look for free blocks in the emptyBlock list.
  4. If there were not enough in the empty list. Take the nextBlock and increment it.
  5. If the data will not fit in one block, create sub arrays.
  6. Write the subarrays to disk.
  7. If the process fails we should decrement the block count if we took from it.

Parameters:
object -
Returns:
the blocks we used.
Throws:
java.io.IOException

getBlockChunks

protected byte[][] getBlockChunks(byte[] complete,
                                  int numBlocksNeeded)
Return the amount to put in each block. Fill them all the way, minus the header.

Parameters:
complete -
numBlocksNeeded -
Returns:
byte[][]

read

protected java.io.Serializable read(int[] blockNumbers)
                             throws java.io.IOException,
                                    java.lang.ClassNotFoundException
Reads an object that is located in the specified blocks.

Parameters:
blockNumbers -
Returns:
Serializable
Throws:
java.io.IOException
java.lang.ClassNotFoundException

freeBlocks

protected void freeBlocks(int[] blocksToFree)
Add these blocks to the emptyBlock list.

Parameters:
blocksToFree -

calculateByteOffsetForBlock

protected int calculateByteOffsetForBlock(int block)
Calcuates the file offset for a particular block.

Parameters:
block -
Returns:
the offset for this block

calculateTheNumberOfBlocksNeeded

protected int calculateTheNumberOfBlocksNeeded(byte[] data)
The number of blocks needed.

Parameters:
data -
Returns:
the number of blocks needed to store the byte array

length

protected long length()
               throws java.io.IOException
Returns the raf length.

Returns:
the size of the file.
Throws:
java.io.IOException

close

protected void close()
              throws java.io.IOException
Closes the raf.

Throws:
java.io.IOException

getNumberOfBlocks

protected int getNumberOfBlocks()
Returns:
Returns the numberOfBlocks.

getBlockSizeBytes

protected int getBlockSizeBytes()
Returns:
Returns the blockSizeBytes.

getAveragePutSizeBytes

protected long getAveragePutSizeBytes()
Returns:
Returns the average size of the an element inserted.

getEmptyBlocks

protected int getEmptyBlocks()
Returns:
Returns the number of empty blocks.

toString

public java.lang.String toString()
For debugging only.

Overrides:
toString in class java.lang.Object
Returns:
String with details.

getFilePath

protected java.lang.String getFilePath()
This is used for debugging.

Returns:
the file path.


Copyright © 2002-2011 Apache Software Foundation. All Rights Reserved.