org.tranche.flatfile
Class DataBlock

java.lang.Object
  extended by org.tranche.commons.Debuggable
      extended by org.tranche.flatfile.DataBlock
All Implemented Interfaces:
java.lang.Comparable

public class DataBlock
extends org.tranche.commons.Debuggable
implements java.lang.Comparable

Represents a block of data. Might have more than one file in it.

Note that the following critical activities are synchronized on the DataBlock object:

Author:
Jayson Falkner - jfalkner@umich.edu, Bryan E. Smith - bryanesmith@gmail.com

Field Summary
static BigHash HASH_LENGTH_ZERO
           
static int HEADERS_PER_FILE
          The amount of headers per file.
static int MAX_BLOCK_SIZE
          The maximum DataBlock size, in bytes.
 
Constructor Summary
DataBlock(java.lang.String filename, DataDirectoryConfiguration ddc, DataBlockUtil dbu)
           
 
Method Summary
 void addBytes(BigHash hash, boolean isMetaData, byte[] bytes)
          Add a chunk to this DataBlock.
 void addBytes(BigHash hash, boolean isMetaData, byte[] bytes, int recursionCount)
          Add a chunk to this DataBlock.
 int compareTo(java.lang.Object o)
          Compare underlying files for DataBlock instances.
 void deleteBytes(BigHash hash, boolean isMetaData)
          Delete the bytes (chunk) from this DataBlock based on hash.
 java.lang.String getAbsolutePath()
          Get absolute path to underlying file for DataBlock.
 byte[] getBytes(BigHash hash, boolean isMetaData)
          Returns the bytes (chunk) representing by the hash, or throws a FileNotFoundException if not found.
 byte[] getBytes(int o, int s)
          Uses cached information to get chunk.
 java.util.List<BigHash> getHashes(boolean isMetaData)
          Returns the list of either data or meta-data hashes stored in this block.
protected  DataBlock[] getSubBlocks()
          Return the subblocks for this DataBlock.
 boolean hasBytes(BigHash hash, boolean isMetaData)
          Check whether the bytes (chunk) represented by the hash exist in this DataBlock instance.
 boolean isDirectory()
          Returns true if this DataBlock is a directory.
 boolean isMerging()
          Returns true if this file is half-way through a merge.
 long length()
          Return the size in bytes of the underlying file for the DataBlock instance.
protected  boolean moveToDataDirectoryConfiguration(DataDirectoryConfiguration newDDC)
          The data block moves itself.
 
Methods inherited from class org.tranche.commons.Debuggable
debugErr, debugErr, debugOut, isDebug, setDebug
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HASH_LENGTH_ZERO

public static BigHash HASH_LENGTH_ZERO

MAX_BLOCK_SIZE

public static final int MAX_BLOCK_SIZE

The maximum DataBlock size, in bytes.

See Also:
Constant Field Values

HEADERS_PER_FILE

public static final int HEADERS_PER_FILE

The amount of headers per file.

Each header's size in bytes is equal to BigHash.HASH_LENGTH + 1 + 1 + 4 + 4. Every new block will start with this many headers. To get the blocks size, you must also include all the data in the block.

See Also:
Constant Field Values
Constructor Detail

DataBlock

public DataBlock(java.lang.String filename,
                 DataDirectoryConfiguration ddc,
                 DataBlockUtil dbu)
Parameters:
filename -
ddc -
dbu -
Method Detail

isDirectory

public final boolean isDirectory()

Returns true if this DataBlock is a directory. (I.e., it has children DataBlock instances.)

Returns:

getAbsolutePath

public final java.lang.String getAbsolutePath()

Get absolute path to underlying file for DataBlock. No guarentee if regular file or directory.

Returns:
The absolute path to the file

isMerging

public final boolean isMerging()

Returns true if this file is half-way through a merge.

Returns:

length

public final long length()

Return the size in bytes of the underlying file for the DataBlock instance.

Returns:

getHashes

public final java.util.List<BigHash> getHashes(boolean isMetaData)
                                        throws java.lang.Exception

Returns the list of either data or meta-data hashes stored in this block. If true is passed as a parameter, only meta-data is returned. Otherwise only data hashes are returned.

Parameters:
isMetaData -
Returns:
Throws:
java.lang.Exception

getBytes

public final byte[] getBytes(int o,
                             int s)
                      throws java.lang.Exception

Uses cached information to get chunk.

Parameters:
o - Offset of chunk
s - Size of chunk
Returns:
Bytes for chunk
Throws:
java.lang.Exception

getBytes

public final byte[] getBytes(BigHash hash,
                             boolean isMetaData)
                      throws java.lang.Exception

Returns the bytes (chunk) representing by the hash, or throws a FileNotFoundException if not found.

Parameters:
hash -
isMetaData -
Returns:
Throws:
java.lang.Exception

hasBytes

public final boolean hasBytes(BigHash hash,
                              boolean isMetaData)
                       throws java.lang.Exception

Check whether the bytes (chunk) represented by the hash exist in this DataBlock instance.

Parameters:
hash -
isMetaData -
Returns:
Throws:
java.lang.Exception

addBytes

public final void addBytes(BigHash hash,
                           boolean isMetaData,
                           byte[] bytes)
                    throws java.lang.Exception

Add a chunk to this DataBlock.

Parameters:
hash -
isMetaData -
bytes -
Throws:
java.lang.Exception

addBytes

public final void addBytes(BigHash hash,
                           boolean isMetaData,
                           byte[] bytes,
                           int recursionCount)
                    throws java.lang.Exception

Add a chunk to this DataBlock.

Parameters:
hash -
isMetaData -
bytes -
recursionCount - The number of times this has been recursively called while waiting for DataBlock to merge. (After a certain number of times, this will fail and throw an exception.)
Throws:
java.lang.Exception

deleteBytes

public final void deleteBytes(BigHash hash,
                              boolean isMetaData)
                       throws java.lang.Exception

Delete the bytes (chunk) from this DataBlock based on hash.

Parameters:
hash -
isMetaData -
Throws:
java.lang.Exception

compareTo

public final int compareTo(java.lang.Object o)

Compare underlying files for DataBlock instances.

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
o -
Returns:

getSubBlocks

protected final DataBlock[] getSubBlocks()

Return the subblocks for this DataBlock.

Returns:

moveToDataDirectoryConfiguration

protected final boolean moveToDataDirectoryConfiguration(DataDirectoryConfiguration newDDC)
                                                  throws java.lang.Exception

The data block moves itself. This synchronized method places the data block in a new DataDirectoryConfiguration.

The intended use for this method is to balance a server's data across data directories.

Parameters:
newDDC -
Returns:
Throws:
java.lang.Exception


This code is free for use both commercially and non-commercially as stated in the project's license