com.equitysoft.hashstore
Class DiskHashtable

java.lang.Object
  |
  +--com.equitysoft.hashstore.DiskHashtable
Direct Known Subclasses:
DuplicateDiskHashtable, SyncDiskHashtable

public class DiskHashtable
extends java.lang.Object

This class implements a hashtable on disk. It mimics the behaviour of java.util.Hashtable with identical methods to put, get and remove instances from the table.

The underlying file system is used to allocate and deallocate space for storage so no initial capacity or load factor need be specified. A capacity of one billion keys is hardcoded into the class. Disk space is only requested from the file system piece by piece and when required so the programmer need not be concerned if a few hundred or a few million entries are being stored. Performance should always be the same no matter how many keys are stored.

None of the methods are synchronized. The class SyncDiskHashtable provides a synchronized version.

Author:
Colin Mummery - equitysoft@iname.com - http://www.kagi.com/equitysoft

Constructor Summary
DiskHashtable(java.io.File root)
           
 
Method Summary
 void clear()
          Removes all the keys and values in the table.
 boolean containsKey(java.lang.Object key)
          Returns true true if the hashtable contains the given key otherwise false.
 void deleteTable()
          Deletes all the files and directories associated with the table and leaves nothing behind.
 java.util.Enumeration elements()
          Returns an Enumeration of all the values in the hashtable.
 java.lang.Object get(java.lang.Object key)
          Gets the object associated with the key.
 boolean isEmpty()
          Returns true if the table contains at least one entry.
 java.util.Enumeration keys()
          Returns an Enumeration of all the keys in the hashtable.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Puts the specified key and value pair into the table and returns the original value for the key if the key already exists.
 java.lang.Object remove(java.lang.Object key)
          Removes the specified key from the hashtable
 int size()
          Returns the number of entries in the hashtable.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DiskHashtable

public DiskHashtable(java.io.File root)
Method Detail

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
                     throws java.io.IOException
Puts the specified key and value pair into the table and returns the original value for the key if the key already exists.
Returns:
The original value for the key if it already exists

get

public java.lang.Object get(java.lang.Object key)
                     throws java.io.IOException
Gets the object associated with the key. If the object causes a ClassNotFoundException when deserializing then the exception is transformed into an IOException for convenience.
Returns:
null if the key isn't found otherwise the associated object

remove

public java.lang.Object remove(java.lang.Object key)
                        throws java.io.IOException
Removes the specified key from the hashtable
Returns:
The Object associated with the key removed

containsKey

public boolean containsKey(java.lang.Object key)
                    throws java.io.IOException
Returns true true if the hashtable contains the given key otherwise false. This method is implemented by seeing if invoking get returns null.
Returns:
true if the key exists

isEmpty

public boolean isEmpty()
Returns true if the table contains at least one entry. This method functions by getting an enumeration and checking if it has any elements.
Returns:
true is there are no entries in the hashtable.

size

public int size()
Returns the number of entries in the hashtable. Care should be exercised with this method as it may take some time to return as it functions by counting all the key entries.
Returns:
The number of keys in the hashtable.

keys

public java.util.Enumeration keys()
Returns an Enumeration of all the keys in the hashtable. Repeated getNextElement calls will then return the hashtable keys. It is more efficient to make repeated called of getNextElement until a null value is returned instead of using hasMoreElements.
Returns:
An Enumeration of the hashtable keys
See Also:
elements()

elements

public java.util.Enumeration elements()
Returns an Enumeration of all the values in the hashtable. Repeated getNextElement calls will then return the hashtable values. It is more efficient to make repeated called of getNextElement until a null value is returned instead of using hasMoreElements. The enumeration order will be the same as keys().
Returns:
An Enumeration of the hashtable values
See Also:
keys()

clear

public void clear()
Removes all the keys and values in the table. All the hashtable files are deleted but the directories are left intact. This should be used if the the table is to be refilled with entries and is the quickest way to remove all the entries. The method won't return until the oeration is complete. It is always synchronized as only one thread should do this.
See Also:
deleteTable()

deleteTable

public void deleteTable()
Deletes all the files and directories associated with the table and leaves nothing behind. This is useful if all traces of the table should disappear. It is not necessary to call clear() before invoking this method. The method won't return until the operation is complete and may take some time. It is always synchronized as only one thread should do this.
See Also:
clear()