public ConcurrentLongHashMap(int expectedItems, int concurrencyLevel) { checkArgument(expectedItems > 0); checkArgument(concurrencyLevel > 0); checkArgument(expectedItems >= concurrencyLevel); int numSections = concurrencyLevel; int perSectionExpectedItems = expectedItems / numSections; int perSectionCapacity = (int) (perSectionExpectedItems / MapFillFactor); this.sections = (Section<V>[]) new Section[numSections]; for (int i = 0; i < numSections; i++) { sections[i] = new Section<>(perSectionCapacity); } }
public V put(long key, V value) { checkNotNull(value); long h = hash(key); return getSection(h).put(key, value, (int) h, false, null); }
V put(long key, V value, int keyHash, boolean onlyIfAbsent, LongFunction<V> valueProvider) { int bucket = keyHash; long stamp = writeLock(); int capacity = this.capacity; if (usedBuckets >= resizeThreshold) { try { rehash(); } finally { unlockWrite(stamp); unlockWrite(stamp);
V get(long key, int keyHash) { int bucket = keyHash; long stamp = tryOptimisticRead(); boolean acquiredLock = false; V storedValue = values[bucket]; if (!acquiredLock && validate(stamp)) { stamp = readLock(); acquiredLock = true; storedKey = keys[bucket]; unlockRead(stamp);
public void forEach(EntryProcessor<V> processor) { long stamp = tryOptimisticRead(); if (!validate(stamp)) { stamp = readLock(); acquiredReadLock = true; V storedValue = values[bucket]; if (!acquiredReadLock && !validate(stamp)) { stamp = readLock(); acquiredReadLock = true; unlockRead(stamp);
private void rehash() { // Expand the hashmap int newCapacity = capacity * 2; long[] newKeys = new long[newCapacity]; V[] newValues = (V[]) new Object[newCapacity]; // Re-hash table for (int i = 0; i < keys.length; i++) { long storedKey = keys[i]; V storedValue = values[i]; if (storedValue != EmptyValue && storedValue != DeletedValue) { insertKeyValueNoLock(newKeys, newValues, storedKey, storedValue); } } keys = newKeys; values = newValues; capacity = newCapacity; usedBuckets = size; resizeThreshold = (int) (capacity * MapFillFactor); }
public V get(long key) { long h = hash(key); return getSection(h).get(key, (int) h); }
public V computeIfAbsent(long key, LongFunction<V> provider) { checkNotNull(provider); long h = hash(key); return getSection(h).put(key, null, (int) h, true, provider); }
public V putIfAbsent(long key, V value) { checkNotNull(value); long h = hash(key); return getSection(h).put(key, value, (int) h, true, null); }
public void forEach(EntryProcessor<V> processor) { for (Section<V> s : sections) { s.forEach(processor); } }
public void clear() { for (Section<V> s : sections) { s.clear(); } }
public ConcurrentLongHashMap(int expectedItems, int concurrencyLevel) { checkArgument(expectedItems > 0); checkArgument(concurrencyLevel > 0); checkArgument(expectedItems >= concurrencyLevel); int numSections = concurrencyLevel; int perSectionExpectedItems = expectedItems / numSections; int perSectionCapacity = (int) (perSectionExpectedItems / MapFillFactor); this.sections = (Section<V>[]) new Section[numSections]; for (int i = 0; i < numSections; i++) { sections[i] = new Section<>(perSectionCapacity); } }
public V put(long key, V value) { checkNotNull(value); long h = hash(key); return getSection(h).put(key, value, (int) h, false, null); }
V put(long key, V value, int keyHash, boolean onlyIfAbsent, LongFunction<V> valueProvider) { int bucket = keyHash; long stamp = writeLock(); int capacity = this.capacity; if (usedBuckets >= resizeThreshold) { try { rehash(); } finally { unlockWrite(stamp); unlockWrite(stamp);
V get(long key, int keyHash) { int bucket = keyHash; long stamp = tryOptimisticRead(); boolean acquiredLock = false; V storedValue = values[bucket]; if (!acquiredLock && validate(stamp)) { stamp = readLock(); acquiredLock = true; storedKey = keys[bucket]; unlockRead(stamp);
public void forEach(EntryProcessor<V> processor) { long stamp = tryOptimisticRead(); if (!validate(stamp)) { stamp = readLock(); acquiredReadLock = true; V storedValue = values[bucket]; if (!acquiredReadLock && !validate(stamp)) { stamp = readLock(); acquiredReadLock = true; unlockRead(stamp);
private void rehash() { // Expand the hashmap int newCapacity = capacity * 2; long[] newKeys = new long[newCapacity]; V[] newValues = (V[]) new Object[newCapacity]; // Re-hash table for (int i = 0; i < keys.length; i++) { long storedKey = keys[i]; V storedValue = values[i]; if (storedValue != EmptyValue && storedValue != DeletedValue) { insertKeyValueNoLock(newKeys, newValues, storedKey, storedValue); } } keys = newKeys; values = newValues; capacity = newCapacity; usedBuckets = size; resizeThreshold = (int) (capacity * MapFillFactor); }
public V get(long key) { long h = hash(key); return getSection(h).get(key, (int) h); }
public void clear() { for (Section<V> s : sections) { s.clear(); } }
public void forEach(EntryProcessor<V> processor) { for (Section<V> s : sections) { s.forEach(processor); } }