com.unister.semweb.drums.bucket.hashfunction
Class RangeHashFunction

java.lang.Object
  extended by com.unister.semweb.drums.bucket.hashfunction.AbstractHashFunction
      extended by com.unister.semweb.drums.bucket.hashfunction.RangeHashFunction
All Implemented Interfaces:
java.io.Serializable

public class RangeHashFunction
extends AbstractHashFunction

This hashFunction maps an element to a specific range. The ranges are not overlapping. It is not needed, that the ranges are consecutive.

Author:
Martin Nettling
See Also:
Serialized Form

Constructor Summary
RangeHashFunction(byte[][] rangeValues, java.lang.String[] filenames, java.lang.String hashFunctionFilename)
          This method instantiates a new RangeHashFunction by the given rangeValues.
RangeHashFunction(byte[] minKey, byte[] maxKey, int ranges, java.lang.String hashFunctionFilename)
          This constructor instantiates a new RangeHashFunction with the given number of ranges.
RangeHashFunction(java.io.File file)
          This method instantiates a new RangeHashFunction by the given File.
RangeHashFunction(int ranges, int keySize, java.lang.String hashFunctionFilename)
          This constructor instantiates a new RangeHashFunction with the given number of ranges.
 
Method Summary
 RangeHashFunction copy()
          Makes a copy of the current RangeHashFunction.
 int getBucketId(AbstractKVStorable key)
          Gets the bucket id from the given date.
 int getBucketId(byte[] key)
          Determines the bucket id to the given key.
 int getBucketId(java.lang.String dbFilename)
           
 java.lang.String getFilename(int bucketId)
          Get the file name of the given bucket.
 java.lang.String getHashFunctionFile()
          Returns the File, where the HashFunction is stored human-readable
 byte[][] getRanges()
           
 byte[] getUpperBound(int bucketId)
           
 void load(java.io.InputStream in)
          Loads the hashfunction from the given InputStream
 void replace(int bucketId, byte[][] keysToInsert)
          Replaces one bucket line within the RangeHashFunction with the lines given.
 void store(java.io.OutputStream os)
          Writes this hashfunction to the given OutputStream
static int stringToByteCount(java.lang.String code)
          The header of could contain characters which are not numbers.
 java.lang.String toString()
           
 void writeToFile()
          Writes the hash function, represented as tuples (range, filename) into the file that is linked with the HashFunction.
 
Methods inherited from class com.unister.semweb.drums.bucket.hashfunction.AbstractHashFunction
getNumberOfBuckets
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

RangeHashFunction

public RangeHashFunction(byte[] minKey,
                         byte[] maxKey,
                         int ranges,
                         java.lang.String hashFunctionFilename)
This constructor instantiates a new RangeHashFunction with the given number of ranges. It tries to size all ranges equally between the smallest and the largest key.

Parameters:
minKey - the smallest expected key
maxKey - the largest expected key
ranges - the number of ranges
hashFunctionFilename - the filename of the file, where to store the hash-function

RangeHashFunction

public RangeHashFunction(int ranges,
                         int keySize,
                         java.lang.String hashFunctionFilename)
This constructor instantiates a new RangeHashFunction with the given number of ranges. It tries to size all ranges equally within the complete available space of numbers.

Parameters:
ranges - the number of ranges
keySize - the size in bytes of the key
hashFunctionFilename - the filename of the file, where to store the hash-function

RangeHashFunction

public RangeHashFunction(byte[][] rangeValues,
                         java.lang.String[] filenames,
                         java.lang.String hashFunctionFilename)
This method instantiates a new RangeHashFunction by the given rangeValues. The given array should contain only the maximal allowed value per bucket. The minimal value will be the direct successor of the previous maximal value. Remember: the array will be handled circular.

Parameters:
rangeValues - the maximum keys for all buckets
filenames - the filenames for all buckets
hashFunctionFilename - the file name of the range hash function

RangeHashFunction

public RangeHashFunction(java.io.File file)
                  throws java.io.IOException
This method instantiates a new RangeHashFunction by the given File. The File contains some long values, which describe the maximal allowed values for the buckets. The minimal value will be the direct successor of the previous maximal value. Remember: the array will be handled circular.

Parameters:
file - the file, which contains the maximal keys
Throws:
java.io.IOException
Method Detail

getHashFunctionFile

public java.lang.String getHashFunctionFile()
Returns the File, where the HashFunction is stored human-readable

Returns:
File

getUpperBound

public byte[] getUpperBound(int bucketId)
Parameters:
bucketId -
Returns:
the maximal key in the bucket with the given bucketId.

getBucketId

public int getBucketId(byte[] key)
Determines the bucket id to the given key.

Specified by:
getBucketId in class AbstractHashFunction
Parameters:
key - the key to map to a bucket-id
Returns:
the bucket-id belonging to the given key

getBucketId

public int getBucketId(AbstractKVStorable key)
Gets the bucket id from the given date.

Specified by:
getBucketId in class AbstractHashFunction
Parameters:
key - the element containing the key to map a bucket-id
Returns:
the bucket-id belonging to the given AbstractKVStorable

getFilename

public java.lang.String getFilename(int bucketId)
Get the file name of the given bucket.

Specified by:
getFilename in class AbstractHashFunction
Returns:
the filename of the bucket for the given bucket-id.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getBucketId

public int getBucketId(java.lang.String dbFilename)
Specified by:
getBucketId in class AbstractHashFunction
Returns:
the bucket-id of the bucket belonging to the given filename.

replace

public void replace(int bucketId,
                    byte[][] keysToInsert)
Replaces one bucket line within the RangeHashFunction with the lines given. All added buckets are set to the specified bucket size. If the bucketId that is to replaced is invalid a IllegalArgumentException is thrown.

Parameters:
keysToInsert -
bucketId -

getRanges

public byte[][] getRanges()
Returns:
the ranges of this hash function.

copy

public RangeHashFunction copy()
Makes a copy of the current RangeHashFunction. Note: the file name is also copied. Make sure that you don't overwrite the file if you change one of the functions.

Returns:
a copy of this RangeHashFunction

stringToByteCount

public static int stringToByteCount(java.lang.String code)
The header of could contain characters which are not numbers. Some of them can be translated into bytes. E.g. char would be two byte.

Parameters:
code - the code to look for
Returns:
the size of the given code

writeToFile

public void writeToFile()
                 throws java.io.IOException
Writes the hash function, represented as tuples (range, filename) into the file that is linked with the HashFunction. The content of the file is overwritten.

Throws:
java.io.IOException

store

public void store(java.io.OutputStream os)
           throws java.io.IOException
Description copied from class: AbstractHashFunction
Writes this hashfunction to the given OutputStream

Specified by:
store in class AbstractHashFunction
Throws:
java.io.IOException

load

public void load(java.io.InputStream in)
          throws java.io.IOException
Description copied from class: AbstractHashFunction
Loads the hashfunction from the given InputStream

Specified by:
load in class AbstractHashFunction
Throws:
java.io.IOException