Author: manik.surtani(a)jboss.com
Date: 2008-07-29 13:17:18 -0400 (Tue, 29 Jul 2008)
New Revision: 6420
Modified:
core/trunk/src/main/java/org/jboss/cache/PessimisticUnversionedNode.java
core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java
core/trunk/src/main/java/org/jboss/cache/util/FastCopyHashMap.java
Log:
Added copy constructor to FCHM and updated UnversionedNode and PUN to use this instead of
a JDK HM.
Modified: core/trunk/src/main/java/org/jboss/cache/PessimisticUnversionedNode.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/PessimisticUnversionedNode.java 2008-07-29
10:21:39 UTC (rev 6419)
+++ core/trunk/src/main/java/org/jboss/cache/PessimisticUnversionedNode.java 2008-07-29
17:17:18 UTC (rev 6420)
@@ -7,10 +7,10 @@
import org.jboss.cache.lock.LockStrategyFactory;
import org.jboss.cache.marshall.MarshalledValue;
import org.jboss.cache.transaction.GlobalTransaction;
+import org.jboss.cache.util.FastCopyHashMap;
import org.jboss.cache.util.ImmutableSetCopy;
import java.util.Collections;
-import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@@ -42,7 +42,7 @@
if (data != null && !data.isEmpty())
setInternalState(data);
else
- this.data = new HashMap<K, V>();
+ this.data = new FastCopyHashMap<K, V>();
setLockForChildInsertRemove(cache != null && cache.getConfiguration() !=
null && cache.getConfiguration().isLockParentForChildInsertRemove());
Modified: core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java 2008-07-29 10:21:39 UTC
(rev 6419)
+++ core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java 2008-07-29 17:17:18 UTC
(rev 6420)
@@ -13,6 +13,7 @@
import org.jboss.cache.commands.write.CreateNodeCommand;
import org.jboss.cache.invocation.InvocationContext;
import org.jboss.cache.marshall.MarshalledValue;
+import org.jboss.cache.util.FastCopyHashMap;
import org.jboss.cache.util.ImmutableSetCopy;
import org.jboss.cache.util.concurrent.SelfInitializingConcurrentHashMap;
@@ -42,7 +43,7 @@
/**
* Map of general data keys to values.
*/
- protected HashMap<K, V> data;
+ protected FastCopyHashMap<K, V> data;
protected NodeSPI<K, V> delegate;
CommandsFactory commandsFactory;
protected NodeFactory<K, V> nodeFactory;
@@ -90,7 +91,7 @@
public UnversionedNode(Fqn fqn, CacheSPI<K, V> cache, boolean
lockForChildInsertRemove, Map<K, V> data)
{
this(fqn, cache, lockForChildInsertRemove);
- if (data != null) this.data = new HashMap<K, V>(data);
+ if (data != null) this.data = new FastCopyHashMap<K, V>(data);
}
/**
@@ -135,7 +136,7 @@
// does not need to be synchronized since this will only be accessed by a single
thread in MVCC thanks to the write lock.
private void initDataMap()
{
- if (data == null) data = new HashMap<K, V>();
+ if (data == null) data = new FastCopyHashMap<K, V>();
}
public CacheSPI<K, V> getCache()
@@ -612,7 +613,7 @@
public InternalNode<K, V> copy()
{
UnversionedNode<K, V> n = new UnversionedNode<K, V>(fqn, cache,
isFlagSet(LOCK_FOR_CHILD_INSERT_REMOVE));
- if (data != null) n.data = (HashMap<K, V>) data.clone();
+ if (data != null) n.data = (FastCopyHashMap<K, V>) data.clone();
copyInternals(n);
return n;
}
@@ -631,7 +632,7 @@
{
if (data == null)
{
- data = state == null ? new HashMap<K, V>() : new HashMap<K,
V>(state);
+ data = state == null ? new FastCopyHashMap<K, V>() : new
FastCopyHashMap<K, V>(state);
}
else
{
Modified: core/trunk/src/main/java/org/jboss/cache/util/FastCopyHashMap.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/util/FastCopyHashMap.java 2008-07-29 10:21:39
UTC (rev 6419)
+++ core/trunk/src/main/java/org/jboss/cache/util/FastCopyHashMap.java 2008-07-29 17:17:18
UTC (rev 6420)
@@ -42,28 +42,44 @@
{
private static final long serialVersionUID = 10929568968762L;
- /** Same default as HashMap, must be a power of 2 */
+ /**
+ * Same default as HashMap, must be a power of 2
+ */
private static final int DEFAULT_CAPACITY = 16;
- /** MAX_INT - 1 */
+ /**
+ * MAX_INT - 1
+ */
private static final int MAXIMUM_CAPACITY = 1 << 30;
- /** 67%, just like IdentityHashMap */
+ /**
+ * 67%, just like IdentityHashMap
+ */
private static final float DEFAULT_LOAD_FACTOR = 0.67f;
- /** The open-addressed table */
- private transient Entry<K,V>[] table;
+ /**
+ * The open-addressed table
+ */
+ private transient Entry<K, V>[] table;
- /** The current number of key-value pairs */
+ /**
+ * The current number of key-value pairs
+ */
private transient int size;
- /** The next resize */
+ /**
+ * The next resize
+ */
private transient int threshold;
- /** The user defined load factor which defines when to resize */
+ /**
+ * The user defined load factor which defines when to resize
+ */
private final float loadFactor;
- /** Counter used to detech changes made outside of an iterator */
+ /**
+ * Counter used to detech changes made outside of an iterator
+ */
private transient int modCount;
// Cached views
@@ -79,23 +95,29 @@
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
- if (! (loadFactor > 0F && loadFactor <= 1F))
+ if (!(loadFactor > 0F && loadFactor <= 1F))
throw new IllegalArgumentException("Load factor must be greater than 0 and
less than or equal to 1");
this.loadFactor = loadFactor;
init(initialCapacity, loadFactor);
}
+ public FastCopyHashMap(Map<K, V> data)
+ {
+ this();
+ putAll(data);
+ }
+
private void init(int initialCapacity, float loadFactor)
{
int c = 1;
- for (; c < initialCapacity; c <<= 1);
+ for (; c < initialCapacity; c <<= 1) ;
@SuppressWarnings("unchecked")
- Entry<K,V>[] table = (Entry<K,V>[]) new Entry[c];
+ Entry<K, V>[] table = (Entry<K, V>[]) new Entry[c];
this.table = table;
- threshold = (int)(c * loadFactor);
+ threshold = (int) (c * loadFactor);
}
public FastCopyHashMap(int initialCapacity)
@@ -113,7 +135,7 @@
{
int h = key.hashCode();
h ^= (h >>> 20) ^ (h >>> 12);
- return h ^ (h >>> 7) ^ (h >>> 4);
+ return h ^ (h >>> 7) ^ (h >>> 4);
}
private int nextIndex(int index, int length)
@@ -151,7 +173,7 @@
int length = table.length;
int index = index(hash, length);
- for (;;)
+ for (; ;)
{
Entry<K, V> e = table[index];
if (e == null)
@@ -173,7 +195,7 @@
int length = table.length;
int index = index(hash, length);
- for (;;)
+ for (; ;)
{
Entry<K, V> e = table[index];
if (e == null)
@@ -208,7 +230,7 @@
int index = start;
- for (;;)
+ for (; ;)
{
Entry<K, V> e = table[index];
if (e == null)
@@ -243,10 +265,10 @@
return;
@SuppressWarnings("unchecked")
- Entry<K,V>[] newTable = new Entry[newLength];
- Entry<K,V>[] old = table;
+ Entry<K, V>[] newTable = new Entry[newLength];
+ Entry<K, V>[] old = table;
- for (Entry<K,V> e : old)
+ for (Entry<K, V> e : old)
{
if (e == null)
continue;
@@ -258,7 +280,7 @@
newTable[index] = e;
}
- threshold = (int)(loadFactor * newLength);
+ threshold = (int) (loadFactor * newLength);
table = newTable;
}
@@ -274,7 +296,7 @@
size = MAXIMUM_CAPACITY;
int length = table.length;
- for (; length < size; length <<= 1);
+ for (; length < size; length <<= 1) ;
resize(length);
}
@@ -288,12 +310,12 @@
if (key == null)
throw new IllegalArgumentException("Null keys are not allowed");
- Entry<K,V>[] table = this.table;
+ Entry<K, V>[] table = this.table;
int length = table.length;
int hash = hash(key);
int start = index(hash, length);
- for (int index = start;;)
+ for (int index = start; ;)
{
Entry<K, V> e = table[index];
if (e == null)
@@ -322,7 +344,7 @@
int length = table.length;
int current = nextIndex(start, length);
- for(;;)
+ for (; ;)
{
Entry<K, V> e = table[current];
if (e == null)
@@ -333,7 +355,7 @@
// entries to their optimal positions once a gap is created.
int prefer = index(e.hash, length);
if ((current < prefer && (prefer <= start || start <=
current))
- || (prefer <= start && start <= current))
+ || (prefer <= start && start <= current))
{
table[start] = e;
table[current] = null;
@@ -359,7 +381,7 @@
try
{
@SuppressWarnings("unchecked")
- FastCopyHashMap<K,V> clone = (FastCopyHashMap<K,V>) super.clone();
+ FastCopyHashMap<K, V> clone = (FastCopyHashMap<K, V>)
super.clone();
clone.table = table.clone();
clone.entrySet = null;
clone.values = null;
@@ -379,9 +401,9 @@
int total = 0;
int totalSkew = 0;
int maxSkew = 0;
- for (int i = 0; i< table.length; i++)
+ for (int i = 0; i < table.length; i++)
{
- Entry<K,V> e = table[i];
+ Entry<K, V> e = table[i];
if (e != null)
{
@@ -401,13 +423,13 @@
System.out.println(" Size: " + size);
System.out.println(" Real Size: " + total);
- System.out.println(" Optimal: " + optimal + " (" +
(float) optimal * 100 / total + "%)");
+ System.out.println(" Optimal: " + optimal + " (" +
(float) optimal * 100 / total + "%)");
System.out.println(" Average Distnce: " + ((float) totalSkew / (total -
optimal)));
System.out.println(" Max Distance: " + maxSkew);
}
- public Set<Map.Entry<K,V>> entrySet()
+ public Set<Map.Entry<K, V>> entrySet()
{
if (entrySet == null)
entrySet = new EntrySet();
@@ -489,7 +511,8 @@
final int hash;
final V value;
- Entry(K key, int hash, V value) {
+ Entry(K key, int hash, V value)
+ {
this.key = key;
this.hash = hash;
this.value = value;
@@ -509,7 +532,7 @@
if (hasNext == true)
return true;
- Entry<K,V> table[] = this.table;
+ Entry<K, V> table[] = this.table;
for (int i = next; i < table.length; i++)
{
if (table[i] != null)
@@ -523,7 +546,7 @@
return false;
}
- protected Entry<K,V> nextEntry()
+ protected Entry<K, V> nextEntry()
{
if (modCount != expectedCount)
throw new ConcurrentModificationException();
@@ -554,7 +577,7 @@
// Start were we relocate
next = delete;
- Entry<K,V>[] table = this.table;
+ Entry<K, V>[] table = this.table;
if (table != FastCopyHashMap.this.table)
{
FastCopyHashMap.this.remove(table[delete].key);
@@ -570,7 +593,7 @@
table[delete] = null;
size--;
- for (;;)
+ for (; ;)
{
i = nextIndex(i, length);
Entry<K, V> e = table[i];
@@ -579,7 +602,7 @@
int prefer = index(e.hash, length);
if ((i < prefer && (prefer <= delete || delete <= i))
- || (prefer <= delete && delete <= i))
+ || (prefer <= delete && delete <= i))
{
// Snapshot the unseen portion of the table if we have
// to relocate an entry that was already seen by this iterator
@@ -587,7 +610,7 @@
{
int remaining = length - current;
@SuppressWarnings("unchecked")
- Entry<K, V>[] newTable = (Entry<K, V>[])new
Entry[remaining];
+ Entry<K, V>[] newTable = (Entry<K, V>[]) new
Entry[remaining];
System.arraycopy(table, current, newTable, 0, remaining);
// Replace iterator's table.
@@ -622,7 +645,7 @@
}
}
- private class EntryIterator extends
FasyCopyHashMapIterator<Map.Entry<K,V>>
+ private class EntryIterator extends FasyCopyHashMapIterator<Map.Entry<K,
V>>
{
private class WriteThroughEntry extends SimpleEntry<K, V>
{
@@ -640,9 +663,9 @@
}
}
- public Map.Entry<K,V> next()
+ public Map.Entry<K, V> next()
{
- Entry<K,V> e = nextEntry();
+ Entry<K, V> e = nextEntry();
return new WriteThroughEntry(e.key, e.value);
}
@@ -696,9 +719,9 @@
}
}
- private class EntrySet extends AbstractSet<Map.Entry<K,V>>
+ private class EntrySet extends AbstractSet<Map.Entry<K, V>>
{
- public Iterator<Map.Entry<K,V>> iterator()
+ public Iterator<Map.Entry<K, V>> iterator()
{
return new EntryIterator();
}
@@ -708,7 +731,7 @@
if (!(o instanceof Map.Entry))
return false;
- Map.Entry<?,?> entry = (Map.Entry<?,?>)o;
+ Map.Entry<?, ?> entry = (Map.Entry<?, ?>) o;
Object value = get(entry.getKey());
return eq(entry.getValue(), value);
}
@@ -729,7 +752,7 @@
}
}
- protected static class SimpleEntry<K,V> implements Map.Entry<K, V>
+ protected static class SimpleEntry<K, V> implements Map.Entry<K, V>
{
private K key;
private V value;
@@ -740,7 +763,7 @@
this.value = value;
}
- SimpleEntry(Map.Entry<K,V> entry)
+ SimpleEntry(Map.Entry<K, V> entry)
{
this.key = entry.getKey();
this.value = entry.getValue();
@@ -770,14 +793,14 @@
if (!(o instanceof Map.Entry))
return false;
- Map.Entry<?,?> e = (Map.Entry<?,?>) o;
+ Map.Entry<?, ?> e = (Map.Entry<?, ?>) o;
return eq(key, e.getKey()) && eq(value, e.getValue());
}
public int hashCode()
{
return hash(key) ^
- (value == null ? 0 : hash(value));
+ (value == null ? 0 : hash(value));
}
public String toString()