/* File: CRHashtable.java Written by Doug Lea. Adapted from JDK1.2 HashMap.java which carries the following copyright: * Copyright 1997 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. History: Date Who What 28oct1999 dl Created 14dec1999 dl jmm snapshot */ //package EDU.oswego.cs.dl.util.concurrent; import java.util.Map; import java.util.AbstractMap; import java.util.AbstractSet; import java.util.AbstractCollection; import java.util.AbstractMap; import java.util.Collection; import java.util.Set; import java.util.Iterator; import java.util.Enumeration; import java.util.ConcurrentModificationException; import java.util.NoSuchElementException; import java.io.Serializable; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; // TODO: // Ideally, selectively use barriers instead of volatile for // value field invalidation. /** * A version of Hashtable that supports mostly-concurrent reading, but * exclusive writing. Because reads * are not limited to periods without writes, a concurrent reader * policy is weaker than a classic reader/writer policy, but is generally * faster and allows more concurrency. This class is a * good choice especially for tables that are mainly created * by one thread during the start-up phase of a program, and from then on, * are mainly read (with perhaps occasional additions or removals) in * many threads. *

* Successful retrievals using get(key) and * containsKey(key) usually run without locking. Unsuccessful ones * (i.e., when the key is not present) do involve brief synchronization * (locking). Also, the size and isEmpty methods are always * synchronized. *

* Because retrieval operations can ordinarily overlap * with writing operations (i.e., put, remove, and their derivatives), * retrievals can only be guaranteed to return the results of the most * recently completed operations holding * upon their onset. Retrieval operations may or may not return * results reflecting in-progress writing operations. * However, the retrieval operations do always return consistent * results -- either those holding before any single modification * or after it, but never a nonsense result. * (For aggregate operations such as putAll and clear, concurrent * reads may reflect insertion or removal of only some entries.) * Also, this class * supports optional guaranteed exclusive reads, simply by surrounding * a call within a synchronized block, as in
* CRHashtable t; ... Object v; synchronized(t) { v = t.get(k); } * This is practically never necessary in practice however. For example, * it is generally poor practice to write: *

 *   CRHashtable t; ...                // Inefficient version
 *   Object key; ...
 *   Object value; ...
 *   synchronized(t) { 
 *     if (!t.containsKey(key))
 *       t.put(key, value);
 *       // other code if not previously present
 *     }
 *     else {
 *       // other code if it was previously present
 *     }
 *   }
 *
* Instead, just take advantage of the fact that put returns * null if the key was not previously present: *
 *   CRHashtable t; ...                // Use this instead
 *   Object key; ...
 *   Object value; ...
 *   Object oldValue = t.put(key, value);
 *   if (oldValue == null) {
 *     // other code if not previously present
 *   }
 *   else {
 *     // other code if it was previously present
 *   }
 *
*

* Iterators and Enumerations (i.e., those returned by * keySet().iterator(), entrySet().iterator(), values().iterator(), * keys(), and elements()) are also mostly-unsynchronized. They return * elements reflecting the state of the hash table at some point at or * since the creation of the iterator/enumeration. They will return at most * one instance of each element (via next()/nextElement()), but might * or might not reflect puts and removes that have been processed * since they were created. Also, these * iterators are designed to be used by only one thread at a time -- * use across multiple threads may lead to unpredictable results if * the table is being concurrently modified. Again, * you can ensure interference-free iteration by enclosing the * iteration in a synchronized block. *

* As much ancillary code as possible from java.util.Hashtable is used * in this class, in order to help ensure behavioral compatibility * (except for concurrency control matters) across all operations. * *

[ Introduction to this package. ] **/ public class CRHashtable extends AbstractMap implements Map, Cloneable, Serializable { /* The basic strategy is an optimistic-style scheme based on the guarantee that the hash table and its lists are always in a consistent enough state to be read without locking: * Read operations first proceed without locking, by traversing the apparently correct list of the apparently correct bin. If an entry is found, but not invalidated (value field null), it is returned. If not found, operations must recheck (after a memory barrier) to make sure they are using both the right list and the right table (which can change under resizes). If invalidated, reads must acquire main update lock to wait out the update, and then re-traverse. * All list additions are at the front of each bin, making it easy to check changes, and also fast to traverse. Entry next pointers are never assigned. Remove() builds new nodes when necessary to preserve this. * Remove() (also clear()) invalidates removed nodes to alert read operations that they must wait out the full modifications. */ /** * The default initial number of table slots for this table (31). * Used when not otherwise specified in constructor. **/ public static int DEFAULT_INITIAL_CAPACITY = 31; // 101 /** * The default load factor for this table (1.0). * Used when not otherwise specified in constructor. **/ public static final float DEFAULT_LOAD_FACTOR = 1.0f; // 0.75f; /** * The hash table data. */ protected transient Entry[] table; protected synchronized Entry[] getTableUnderLock() { return table; } /** * The total number of mappings in the hash table. */ protected transient int count; /** * The table is rehashed when its size exceeds this threshold. (The * value of this field is (int)(capacity * loadFactor).) * * @serial */ protected int threshold; /** * The load factor for the hash table. * * @serial */ protected float loadFactor; /* Current mechanics to obtain barriers is to write 0 to a volatile on updates, and to read that zero on retrievals. */ static final boolean USE_SYNCH_FOR_BARRIER = false; protected volatile int aVolatile = 0; protected final void writeBarrier() { if (!USE_SYNCH_FOR_BARRIER) { aVolatile = 0; } } protected final void readBarrier() { if (USE_SYNCH_FOR_BARRIER) { synchronized(this) {} } else { int junk = aVolatile; } } /** * Constructs a new, empty map with the specified initial * capacity and the specified load factor. * * @param initialCapacity the initial capacity of the CRHashtable. * @param loadFactor the load factor of the CRHashtable * @throws IllegalArgumentException if the initial capacity is less * than zero, or if the load factor is nonpositive. */ public CRHashtable(int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException("Illegal Initial Capacity: "+ initialCapacity); if (loadFactor <= 0) throw new IllegalArgumentException("Illegal Load factor: "+ loadFactor); if (initialCapacity==0) initialCapacity = 1; this.loadFactor = loadFactor; table = new Entry[initialCapacity]; threshold = (int)(initialCapacity * loadFactor); } /** * Constructs a new, empty map with the specified initial capacity * and default load factor. * * @param initialCapacity the initial capacity of the * CRHashtable. * @throws IllegalArgumentException if the initial capacity is less * than zero. */ public CRHashtable(int initialCapacity) { this(initialCapacity, DEFAULT_LOAD_FACTOR); } /** * Constructs a new, empty map with a default capacity and load * factor. */ public CRHashtable() { this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR); } /** * Constructs a new map with the same mappings as the given map. The * map is created with a capacity of twice the number of mappings in * the given map or 11 (whichever is greater), and a default load factor. */ public CRHashtable(Map t) { this(Math.max(2*t.size(), 11), DEFAULT_LOAD_FACTOR); putAll(t); } /** * Returns the number of key-value mappings in this map. * * @return the number of key-value mappings in this map. */ public synchronized int size() { return count; } /** * Returns true if this map contains no key-value mappings. * * @return true if this map contains no key-value mappings. */ public synchronized boolean isEmpty() { return count == 0; } /** * Returns the value to which the specified key is mapped in this hashtable. * * @param key a key in the hashtable. * @return the value to which the key is mapped in this hashtable; * null if the key is not mapped to any value in * this hashtable. * @exception NullPointerException if the key is * null. * @see #put(Object, Object) */ public Object get(Object key) { int hash = key.hashCode(); // throw null pointer exception if key null /* Start off at the apparently correct bin. If entry is found, we need to check after a barrier (via inspection of volatile value) anyway. If not found, we need a barrier to check if we are actually in right bin. So either way, we encounter only one barrier unless we need to retry. And we only need to fully synchronize if there have been concurrent modifications. */ Entry[] tab = table; int index = (hash & 0x7FFFFFFF) % tab.length; Entry first = tab[index]; Entry e = first; for (;;) { if (e == null) { // If key apparently not there, check to // make sure this was the good read readBarrier(); tab = table; if (first == tab[index]) return null; else { // Wrong list -- must restart traversal at new first e = first = tab[index = (hash & 0x7FFFFFFF) % tab.length]; } } // checking for pointer equality first wins in most applications else if (key == e.key || (e.hash == hash && key.equals(e.key))) { Object value = e.value; if (value != null) return value; // Entry was invalidated during deletion. But it could // have been re-inserted, so we must retraverse. // To avoid useless contention, get lock to wait out modifications // before retraversing. tab = getTableUnderLock(); e = first = tab[index = (hash & 0x7FFFFFFF) % tab.length]; } else e = e.next; } } /** * Tests if the specified object is a key in this hashtable. * * @param key possible key. * @return true if and only if the specified object * is a key in this hashtable, as determined by the * equals method; false otherwise. * @exception NullPointerException if the key is * null. * @see #contains(Object) */ public boolean containsKey(Object key) { return get(key) != null; } /** * Maps the specified key to the specified * value in this hashtable. Neither the key nor the * value can be null.

* * The value can be retrieved by calling the get method * with a key that is equal to the original key. * * @param key the hashtable key. * @param value the value. * @return the previous value of the specified key in this hashtable, * or null if it did not have one. * @exception NullPointerException if the key or value is * null. * @see Object#equals(Object) * @see #get(Object) */ public Object put(Object key, Object value) { if (value == null) throw new NullPointerException(); int hash = key.hashCode(); Entry[] tab = table; int index = (hash & 0x7FFFFFFF) % tab.length; Entry first = tab[index]; Entry e = first; for (;;) { if (e == null) { // not found, so add synchronized(this) { tab = table; // make sure we are adding to correct list if (first == tab[index]) { // Add to front of list tab[index] = new Entry(hash, key, value, first); if (++count >= threshold) rehash(); writeBarrier(); return null; } else { // wrong list -- retry e = first = tab[index = (hash & 0x7FFFFFFF) % tab.length]; } } } else if (key == e.key || (e.hash == hash && key.equals(e.key))) { // synch to avoid race with remove and to // ensure proper serialization of multiple replaces synchronized(this) { tab = table; Object oldValue = e.value; if (first == tab[index] && oldValue != null) { e.value = value; return oldValue; } else { // retry if wrong list or lost race against concurrent remove e = first = tab[index = (hash & 0x7FFFFFFF) % tab.length]; } } } else e = e.next; } } /** * Rehashes the contents of this map into a new table * with a larger capacity. This method is called automatically when the * number of keys in this map exceeds its capacity and load factor. */ protected void rehash() { // PRE: synch lock held if (count < threshold) return; Entry[] oldMap = table; int oldCapacity = oldMap.length; int newCapacity = oldCapacity * 2 + 1; Entry[] newMap = new Entry[newCapacity]; threshold = (int)(newCapacity * loadFactor); /* We need to guarantee that any existing reads of oldMap can proceed. So we cannot yet null out each oldMap bin. */ for (int i = 0; i < oldCapacity ; ++i) { Entry first = oldMap[i]; for (Entry e = first; e != null; e = e.next) { int index = (e.hash & 0x7FFFFFFF) % newCapacity; // reuse the node if safe to do so if (e.next == null && newMap[index] == null) newMap[index] = e; else newMap[index] = new Entry(e.hash, e.key, e.value, newMap[index]); } } table = newMap; writeBarrier(); } /** * Removes the key (and its corresponding value) from this * hashtable. This method does nothing if the key is not in the hashtable. * * @param key the key that needs to be removed. * @return the value to which the key had been mapped in this hashtable, * or null if the key did not have a mapping. * @exception NullPointerException if the key is * null. */ public Object remove(Object key) { int hash = key.hashCode(); Entry[] tab = table; int index = (hash & 0x7FFFFFFF) % tab.length; Entry first = tab[index]; Entry e = first; for (;;) { // failure case works same as get() if (e == null) { readBarrier(); tab = table; if (first == tab[index]) return null; else { // Wrong list -- must restart traversal at new first e = first = tab[index = (hash & 0x7FFFFFFF) % tab.length]; } } else if (key == e.key || (e.hash == hash && key.equals(e.key))) { synchronized(this) { tab = table; Object oldValue = e.value; // re-find under synch if wrong list if (first != tab[index] || oldValue == null) { e = first = tab[index = (hash & 0x7FFFFFFF) % tab.length]; for (;;) { if (e == null) return null; else if (key == e.key || (e.hash == hash && key.equals(e.key))){ oldValue = e.value; break; } else e = e.next; } } e.value = null; /* All entries following removed node can stay in list, but all preceeding ones need to be cloned. Traversals rely on this strategy to ensure that elements will not be repeated during iteration. */ Entry head = e.next; for (Entry p = first; p != e; p = p.next) head = new Entry(p.hash, p.key, p.value, head); tab[index] = head; count--; writeBarrier(); return oldValue; } } else e = e.next; } } /** * Returns true if this map maps one or more keys to the * specified value. Note: This method requires a full internal * traversal of the hash table, and so is much slower than * method containsKey. * * @param value value whose presence in this map is to be tested. * @return true if this map maps one or more keys to the * specified value. * @exception NullPointerException if the value is null. */ public boolean containsValue(Object value) { if (value == null) throw new NullPointerException(); Entry tab[] = getTableUnderLock(); for (int i = 0 ; i < tab.length; ++i) { for (Entry e = tab[i] ; e != null ; e = e.next) { Object v = e.value; if (v != null && value.equals(v)) return true; } } return false; } /** * Tests if some key maps into the specified value in this hashtable. * This operation is more expensive than the containsKey * method.

* * Note that this method is identical in functionality to containsValue, * (which is part of the Map interface in the collections framework). * * @param value a value to search for. * @return true if and only if some key maps to the * value argument in this hashtable as * determined by the equals method; * false otherwise. * @exception NullPointerException if the value is null. * @see #containsKey(Object) * @see #containsValue(Object) * @see Map */ public boolean contains(Object value) { return containsValue(value); } /** * Copies all of the mappings from the specified map to this one. * * These mappings replace any mappings that this map had for any of the * keys currently in the specified Map. * * @param t Mappings to be stored in this map. */ public synchronized void putAll(Map t) { for (Iterator it = t.entrySet().iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); Object key = entry.getKey(); Object value = entry.getValue(); if (value == null) throw new NullPointerException(); int hash = key.hashCode(); Entry[] tab = table; int index = (hash & 0x7FFFFFFF) % tab.length; Entry first = tab[index]; Entry e = first; for (;;) { if (e == null) { tab[index] = new Entry(hash, key, value, first); if (++count >= threshold) rehash(); break; } else if (key == e.key || (e.hash == hash && key.equals(e.key))) break; else e = e.next; } } writeBarrier(); } /** * Removes all mappings from this map. */ public synchronized void clear() { Entry tab[] = table; for (int i = 0; i < tab.length ; ++i) { // must invalidate all to force concurrent get's to wait and then retry for (Entry e = tab[i]; e != null; e = e.next) e.value = null; tab[i] = null; } count = 0; } /** * Returns a shallow copy of this * CRHashtable instance: the keys and * values themselves are not cloned. * * @return a shallow copy of this map. */ public synchronized Object clone() { try { CRHashtable t = (CRHashtable)super.clone(); t.keySet = null; t.entrySet = null; t.values = null; Entry[] tab = table; t.table = new Entry[tab.length]; Entry[] ttab = t.table; for (int i = 0; i < tab.length; ++i) { Entry first = tab[i]; if (first != null) ttab[i] = (Entry)(first.clone()); } return t; } catch (CloneNotSupportedException e) { // this shouldn't happen, since we are Cloneable throw new InternalError(); } } // Views protected transient Set keySet = null; protected transient Set entrySet = null; protected transient Collection values = null; /** * Returns a set view of the keys contained in this map. The set is * backed by the map, so changes to the map are reflected in the set, and * vice-versa. The set supports element removal, which removes the * corresponding mapping from this map, via the Iterator.remove, * Set.remove, removeAll, retainAll, and * clear operations. It does not support the add or * addAll operations. * * @return a set view of the keys contained in this map. */ public Set keySet() { Set ks = keySet; if (ks != null) return ks; else { return keySet = new AbstractSet() { public Iterator iterator() { return new HashIterator(KEYS); } public int size() { return CRHashtable.this.size(); } public boolean contains(Object o) { return CRHashtable.this.containsKey(o); } public boolean remove(Object o) { return CRHashtable.this.remove(o) != null; } public void clear() { CRHashtable.this.clear(); } }; } } /** * Returns a collection view of the values contained in this map. The * collection is backed by the map, so changes to the map are reflected in * the collection, and vice-versa. The collection supports element * removal, which removes the corresponding mapping from this map, via the * Iterator.remove, Collection.remove, * removeAll, retainAll, and clear operations. * It does not support the add or addAll operations. * * @return a collection view of the values contained in this map. */ public Collection values() { Collection vs = values; if (vs != null) return vs; else { return values = new AbstractCollection() { public Iterator iterator() { return new HashIterator(VALUES); } public int size() { return CRHashtable.this.size(); } public boolean contains(Object o) { return CRHashtable.this.containsValue(o); } public void clear() { CRHashtable.this.clear(); } }; } } /** * Returns a collection view of the mappings contained in this map. Each * element in the returned collection is a Map.Entry. The * collection is backed by the map, so changes to the map are reflected in * the collection, and vice-versa. The collection supports element * removal, which removes the corresponding mapping from the map, via the * Iterator.remove, Collection.remove, * removeAll, retainAll, and clear operations. * It does not support the add or addAll operations. * * @return a collection view of the mappings contained in this map. * @see Map.Entry */ public Set entrySet() { Set es = entrySet; if (es != null) return es; else { return entrySet = new AbstractSet() { public Iterator iterator() { return new HashIterator(ENTRIES); } public boolean contains(Object o) { if (!(o instanceof Map.Entry)) return false; Map.Entry entry = (Map.Entry)o; Object key = entry.getKey(); Object v = CRHashtable.this.get(key); return v != null && v.equals(entry.getValue()); } public boolean remove(Object o) { if (!(o instanceof Map.Entry)) return false; return CRHashtable.this.findAndRemoveEntry((Map.Entry)o); } public int size() { return CRHashtable.this.size(); } public void clear() { CRHashtable.this.clear(); } }; } } /** * Helper method for entrySet.remove **/ protected synchronized boolean findAndRemoveEntry(Map.Entry entry) { Object key = entry.getKey(); Object v = get(key); if (v != null && v.equals(entry.getValue())) { remove(key); return true; } else return false; } /** * Returns an enumeration of the keys in this hashtable. * * @return an enumeration of the keys in this hashtable. * @see Enumeration * @see #elements() * @see #keySet() * @see Map */ public Enumeration keys() { return new HashIterator(KEYS); } /** * Returns an enumeration of the values in this hashtable. * Use the Enumeration methods on the returned object to fetch the elements * sequentially. * * @return an enumeration of the values in this hashtable. * @see java.util.Enumeration * @see #keys() * @see #values() * @see Map */ public Enumeration elements() { return new HashIterator(VALUES); } /** * CRHashtable collision list entry. */ protected static class Entry implements Map.Entry { /* The use of volatile for value field ensures that we can detect status changes without synchronization. The other fields are never changed, and are marked as final. The get, put, and remove code could be slightly reworked to avoid needing memory effects of final here, allowing final to be dropped. */ protected final int hash; protected final Object key; protected final Entry next; protected volatile Object value; Entry(int hash, Object key, Object value, Entry next) { this.hash = hash; this.key = key; this.value = value; this.next = next; } protected Object clone() { return new Entry(hash, key, value, (next==null ? null : (Entry)next.clone())); } // Map.Entry Ops public Object getKey() { return key; } /** * Get the value. * Note: In an entrySet or entrySet.iterator, * unless the set or iterator is * used under synchronization of the table as a whole (or * you can otherwise guarantee lack of concurrent modification), * getValue might return null, reflecting the fact that * the entry has been concurrently removed. However, * there are no assurances that concurrent removals will * be reflected using this method. * * @return the current value, or null if the entry has been * detectably removed. **/ public Object getValue() { return value; } /** * Set the value of this entry. * Note: In an entrySet or entrySet.iterator), * unless the set or iterator is * used under synchronization of the table as a whole (or * you can otherwise guarantee lack of concurrent modification), * setValue is not strictly guaranteed to actually replace * the value field obtained via the get operation * of the underlying hash table in multithreaded applications. * If iterator-wide synchronization is not used, and any * other concurrent put or remove operations * occur, sometimes even to other entries, then this change * is not guaranteed to be reflected in the hash table. (It might, * or it might not. There are no assurances either way.) * @param value the new value. * @return the previous value, or null if entry has been detectably * removed. * @exception NullPointerException if the value is null. * **/ public Object setValue(Object value) { if (value == null) throw new NullPointerException(); Object oldValue = this.value; this.value = value; return oldValue; } public boolean equals(Object o) { if (!(o instanceof Map.Entry)) return false; Map.Entry e = (Map.Entry)o; if (!key.equals(e.getKey())) return false; Object v = value; return (v == null) ? e.getValue() == null : v.equals(e.getValue()); } public int hashCode() { Object v = value; return hash ^ ((v == null) ? 0 : v.hashCode()); } public String toString() { return key + "=" + value; } } // Types of Iterators protected static final int KEYS = 0; protected static final int VALUES = 1; protected static final int ENTRIES = 2; protected class HashIterator implements Iterator, Enumeration { final Entry[] tab; int index; Entry entry = null; Entry lastReturned = null; Object currentKey; Object currentValue; final int type; HashIterator(int type) { this.type = type; tab = CRHashtable.this.getTableUnderLock(); index = tab.length - 1; } public boolean hasMoreElements() { return hasNext(); } public Object nextElement() { return next(); } public boolean hasNext() { /* currentkey and currentValue are set here to ensure that next() returns normally if hasNext() returns true. This avoids surprises especially when final element is removed during traversal -- instead, we just ignore the removal during current traversal. */ for (;;) { if (entry != null) { Object v = entry.value; if (v != null) { currentKey = entry.key; currentValue = v; return true; } else entry = entry.next; } while (entry == null && index >= 0) entry = tab[index--]; if (entry == null) { currentKey = currentValue = null; return false; } } } public Object next() { if (currentKey == null) { // bypass advance if already set if (!hasNext()) // resets currentKey throw new NoSuchElementException(); } lastReturned = entry; entry = entry.next; Object result = (type == KEYS) ? currentKey : (type == VALUES) ? currentValue : lastReturned; currentKey = currentValue = null; return result; } public void remove() { if (lastReturned == null) throw new IllegalStateException(); CRHashtable.this.remove(lastReturned.key); } } /** * Save the state of the CRHashtable * instance to a stream (i.e., * serialize it). * * @serialData The capacity of the * CRHashtable (the length of the * bucket array) is emitted (int), followed by the * size of the CRHashtable (the number of key-value * mappings), followed by the key (Object) and value (Object) * for each key-value mapping represented by the CRHashtable * The key-value mappings are emitted in no particular order. */ protected synchronized void writeObject(java.io.ObjectOutputStream s) throws IOException { // Write out the threshold, loadfactor, and any hidden stuff s.defaultWriteObject(); // Write out number of buckets s.writeInt(table.length); // Write out size (number of Mappings) s.writeInt(count); // Write out keys and values (alternating) for (int index = table.length-1; index >= 0; index--) { Entry entry = table[index]; while (entry != null) { s.writeObject(entry.key); s.writeObject(entry.value); entry = entry.next; } } } /** * Reconstitute the CRHashtable * instance from a stream (i.e., * deserialize it). */ protected synchronized void readObject(java.io.ObjectInputStream s) throws IOException, ClassNotFoundException { // Read in the threshold, loadfactor, and any hidden stuff s.defaultReadObject(); // Read in number of buckets and allocate the bucket array; int numBuckets = s.readInt(); table = new Entry[numBuckets]; // Read in size (number of Mappings) int size = s.readInt(); // Read the keys and values, and put the mappings in the table for (int i=0; i