You are here: Usage Pitfalls > Persistent Hashtables

Persistent Hashtables

Hashtable or Hashmap is a data structure that associates keys to values. Hashtable uses a hash function to quickly navigate to a specific value. Hash function returns an integer value based on a specific algorithm whichis based on the contents of the object. Different hash algorithms can be used to produce hash codes for different objects. The general requirements for hash code are the following:

As you can see from the last point there can be more than one distinct key object in a hashtable that have the same hash code. Special methods called collision resolution are used to find the correct value for the specific key. Usually a separate storage - a bucket - is used for all keys with the same hash code. In this case a bucket is located by the hash code and then the right key is searched within the bucket, which allows to get a good enough performance. This works good enough for an in-memory hashtable as the hash values are not changed during application lifetime. However, it gets more difficult with a persistent hashtable.

When a hashtable is stored to a database - the hash values are not stored. As we know from the definition, the hash value of an object is only required to stay the same during the application lifetime, which means that if the hashtable will be loaded into memory from the database in another application or in another session, the hash values of the keys can differ from their initial value. We will still be able to retrieve values by their key objects if equals and hashCode functions are based on the object contents. However the consistency of the hashtable can potentially be broken. This can happen if the key objects from different buckets will obtain the same hash value as the result of re-instantiation from the database.

The simplest way to avoid the inconsistency of the persisted hash table use object content-based hash code functions for your key objects.