org.apache.cassandra.utils
Class FastHash

java.lang.Object
  extended by org.apache.cassandra.utils.FastHash
All Implemented Interfaces:
java.lang.Cloneable
Direct Known Subclasses:
FastObjectHash

public abstract class FastHash
extends java.lang.Object
implements java.lang.Cloneable

Base class for hashtables that use open addressing to resolve collisions.

Author:
Avinash Lakshman

Field Summary
protected  float autoCompactionFactor_
          The auto-compaction factor for the table.
protected  int autoCompactRemovesRemaining_
          The number of removes that should be performed before an auto-compaction occurs.
protected static int DEFAULT_INITIAL_CAPACITY
          the default initial capacity for the hash table.
protected static float DEFAULT_LOAD_FACTOR
          the load above which rehashing occurs.
protected  int free_
          the current number of free slots in the hash.
protected  float loadFactor_
          Determines how full the internal table can become before rehashing is required.
protected  int maxSize_
          The maximum number of elements allowed without allocating more space.
protected  int size_
          the current number of occupied slots in the hash.
 
Constructor Summary
FastHash()
          Creates a new THash instance with the default capacity and load factor.
FastHash(int initialCapacity)
          Creates a new THash instance with a prime capacity at or near the specified capacity and with the default load factor.
FastHash(int initialCapacity, float loadFactor)
          Creates a new THash instance with a prime capacity at or near the minimum needed to hold initialCapacity elements with load factor loadFactor without triggering a rehash.
 
Method Summary
protected  int calculateGrownCapacity()
           
protected abstract  int capacity()
           
 void clear()
          Empties the collection.
 java.lang.Object clone()
           
 void compact()
          Compresses the hashtable to the minimum prime size (as defined by PrimeFinder) that will hold all of the elements currently in the table.
 void ensureCapacity(int desiredCapacity)
          Ensure that this hashtable has sufficient capacity to hold desiredCapacity additional elements without requiring a rehash.
 float getAutoCompactionFactor()
           
 boolean isEmpty()
          Tells whether this set is currently holding any elements.
protected  void postInsertHook(boolean usedFreeSlot)
          After an insert, this hook is called to adjust the size/free values of the set and to perform rehashing if necessary.
protected  void reenableAutoCompaction(boolean check_for_compaction)
          Re-enable auto-compaction after it was disabled via tempDisableAutoCompaction().
protected abstract  void rehash(int newCapacity)
          Rehashes the set.
protected  void removeAt(int index)
          Delete the record at index.
 void setAutoCompactionFactor(float factor)
          The auto-compaction factor controls whether and when a table performs a compact() automatically after a certain number of remove operations.
protected  int setUp(int initialCapacity)
          initializes the hashtable to a prime capacity which is at least initialCapacity + 1.
 int size()
          Returns the number of distinct elements in this collection.
protected  void tempDisableAutoCompaction()
          Temporarily disables auto-compaction.
 void trimToSize()
          This simply calls compact.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

size_

protected transient int size_
the current number of occupied slots in the hash.


free_

protected transient int free_
the current number of free slots in the hash.


DEFAULT_LOAD_FACTOR

protected static final float DEFAULT_LOAD_FACTOR
the load above which rehashing occurs.

See Also:
Constant Field Values

DEFAULT_INITIAL_CAPACITY

protected static final int DEFAULT_INITIAL_CAPACITY
the default initial capacity for the hash table. This is one less than a prime value because one is added to it when searching for a prime capacity to account for the free slot required by open addressing. Thus, the real default capacity is 11.

See Also:
Constant Field Values

loadFactor_

protected float loadFactor_
Determines how full the internal table can become before rehashing is required. This must be a value in the range: 0.0 < loadFactor < 1.0. The default value is 0.5, which is about as large as you can get in open addressing without hurting performance. Cf. Knuth, Volume 3., Chapter 6.


maxSize_

protected int maxSize_
The maximum number of elements allowed without allocating more space.


autoCompactRemovesRemaining_

protected int autoCompactRemovesRemaining_
The number of removes that should be performed before an auto-compaction occurs.


autoCompactionFactor_

protected float autoCompactionFactor_
The auto-compaction factor for the table.

See Also:
setAutoCompactionFactor(float)
Constructor Detail

FastHash

public FastHash()
Creates a new THash instance with the default capacity and load factor.


FastHash

public FastHash(int initialCapacity)
Creates a new THash instance with a prime capacity at or near the specified capacity and with the default load factor.

Parameters:
initialCapacity - an int value

FastHash

public FastHash(int initialCapacity,
                float loadFactor)
Creates a new THash instance with a prime capacity at or near the minimum needed to hold initialCapacity elements with load factor loadFactor without triggering a rehash.

Parameters:
initialCapacity - an int value
loadFactor - a float value
Method Detail

clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object

isEmpty

public boolean isEmpty()
Tells whether this set is currently holding any elements.

Returns:
a boolean value

size

public int size()
Returns the number of distinct elements in this collection.

Returns:
an int value

capacity

protected abstract int capacity()
Returns:
the current physical capacity of the hash table.

ensureCapacity

public void ensureCapacity(int desiredCapacity)
Ensure that this hashtable has sufficient capacity to hold desiredCapacity additional elements without requiring a rehash. This is a tuning method you can call before doing a large insert.

Parameters:
desiredCapacity - an int value

compact

public void compact()
Compresses the hashtable to the minimum prime size (as defined by PrimeFinder) that will hold all of the elements currently in the table. If you have done a lot of remove operations and plan to do a lot of queries or insertions or iteration, it is a good idea to invoke this method. Doing so will accomplish two things:
  1. You'll free memory allocated to the table but no longer needed because of the remove()s.
  2. You'll get better query/insert/iterator performance because there won't be any REMOVED slots to skip over when probing for indices in the table.


setAutoCompactionFactor

public void setAutoCompactionFactor(float factor)
The auto-compaction factor controls whether and when a table performs a compact() automatically after a certain number of remove operations. If the value is non-zero, the number of removes that need to occur for auto-compaction is the size of table at the time of the previous compaction (or the initial capacity) multiplied by this factor.

Setting this value to zero will disable auto-compaction.


getAutoCompactionFactor

public float getAutoCompactionFactor()
See Also:
setAutoCompactionFactor(float)

trimToSize

public final void trimToSize()
This simply calls compact. It is included for symmetry with other collection classes. Note that the name of this method is somewhat misleading (which is why we prefer compact) as the load factor may require capacity above and beyond the size of this collection.

See Also:
compact()

removeAt

protected void removeAt(int index)
Delete the record at index. Reduces the size of the collection by one.

Parameters:
index - an int value

clear

public void clear()
Empties the collection.


setUp

protected int setUp(int initialCapacity)
initializes the hashtable to a prime capacity which is at least initialCapacity + 1.

Parameters:
initialCapacity - an int value
Returns:
the actual capacity chosen

rehash

protected abstract void rehash(int newCapacity)
Rehashes the set.

Parameters:
newCapacity - an int value

tempDisableAutoCompaction

protected void tempDisableAutoCompaction()
Temporarily disables auto-compaction. MUST be followed by calling reenableAutoCompaction(boolean).


reenableAutoCompaction

protected void reenableAutoCompaction(boolean check_for_compaction)
Re-enable auto-compaction after it was disabled via tempDisableAutoCompaction().

Parameters:
check_for_compaction - True if compaction should be performed if needed before returning. If false, no compaction will be performed.

postInsertHook

protected final void postInsertHook(boolean usedFreeSlot)
After an insert, this hook is called to adjust the size/free values of the set and to perform rehashing if necessary.


calculateGrownCapacity

protected int calculateGrownCapacity()


Copyright © 2009 The Apache Software Foundation