@Override public E intern(E sample) { while (true) { // trying to read the canonical... InternalEntry<E, Dummy, ?> entry = map.getEntry(sample); if (entry != null) { E canonical = entry.getKey(); if (canonical != null) { // only matters if weak/soft keys are used return canonical; } } // didn't see it, trying to put it instead... Dummy sneaky = map.putIfAbsent(sample, Dummy.VALUE); if (sneaky == null) { return sample; } else { /* Someone beat us to it! Trying again... * * Technically this loop not guaranteed to terminate, so theoretically (extremely * unlikely) this thread might starve, but even then, there is always going to be another * thread doing progress here. */ } } }
public void testClear() { MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(createMapMaker().concurrencyLevel(1).initialCapacity(1)); Segment<Object, Object, ?, ?> segment = map.segments[0]; AtomicReferenceArray<? extends InternalEntry<Object, Object, ?>> table = segment.table; assertEquals(1, table.length()); Object key = new Object(); Object value = new Object(); int hash = map.hash(key); InternalEntry<Object, Object, ?> entry = segment.newEntryForTesting(key, hash, null); segment.setValueForTesting(entry, value); segment.setTableEntryForTesting(0, entry); segment.readCount.incrementAndGet(); segment.count = 1; assertSame(entry, table.get(0)); segment.clear(); assertNull(table.get(0)); assertEquals(0, segment.readCount.get()); assertEquals(0, segment.count); }
public void testRemoveEntry() { MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(createMapMaker().concurrencyLevel(1).initialCapacity(1)); Segment<Object, Object, ?, ?> segment = map.segments[0]; AtomicReferenceArray<? extends InternalEntry<Object, Object, ?>> table = segment.table; assertEquals(1, table.length()); Object key = new Object(); Object value = new Object(); int hash = map.hash(key); InternalEntry<Object, Object, ?> entry = segment.newEntryForTesting(key, hash, null); segment.setValueForTesting(entry, value); // remove absent assertFalse(segment.removeTableEntryForTesting(entry)); segment.setTableEntryForTesting(0, entry); segment.count = 1; assertTrue(segment.removeTableEntryForTesting(entry)); assertEquals(0, segment.count); assertNull(table.get(0)); }
public void testNewEntry() { for (MapMaker maker : allWeakValueStrengthMakers()) { MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(maker); Segment<Object, Object, ?, ?> segment = map.segments[0]; Object keyOne = new Object(); Object valueOne = new Object(); int hashOne = map.hash(keyOne); InternalEntry<Object, Object, ?> entryOne = segment.newEntryForTesting(keyOne, hashOne, null); WeakValueReference<Object, Object, ?> valueRefOne = segment.newWeakValueReferenceForTesting(entryOne, valueOne); assertSame(valueOne, valueRefOne.get()); segment.setWeakValueReferenceForTesting(entryOne, valueRefOne); assertSame(keyOne, entryOne.getKey()); assertEquals(hashOne, entryOne.getHash()); assertNull(entryOne.getNext()); assertSame(valueRefOne, segment.getWeakValueReferenceForTesting(entryOne)); Object keyTwo = new Object(); Object valueTwo = new Object(); int hashTwo = map.hash(keyTwo); InternalEntry<Object, Object, ?> entryTwo = segment.newEntryForTesting(keyTwo, hashTwo, entryOne); WeakValueReference<Object, Object, ?> valueRefTwo = segment.newWeakValueReferenceForTesting(entryTwo, valueTwo); assertSame(valueTwo, valueRefTwo.get()); segment.setWeakValueReferenceForTesting(entryTwo, valueRefTwo); assertSame(keyTwo, entryTwo.getKey()); assertEquals(hashTwo, entryTwo.getHash()); assertSame(entryOne, entryTwo.getNext()); assertSame(valueRefTwo, segment.getWeakValueReferenceForTesting(entryTwo)); } }
public void testCopyEntry() { for (MapMaker maker : allWeakValueStrengthMakers()) { MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(maker); Segment<Object, Object, ?, ?> segment = map.segments[0]; Object keyOne = new Object(); Object valueOne = new Object(); int hashOne = map.hash(keyOne); InternalEntry<Object, Object, ?> entryOne = segment.newEntryForTesting(keyOne, hashOne, null); segment.setValueForTesting(entryOne, valueOne); Object keyTwo = new Object(); Object valueTwo = new Object(); int hashTwo = map.hash(keyTwo); InternalEntry<Object, Object, ?> entryTwo = segment.newEntryForTesting(keyTwo, hashTwo, null); segment.setValueForTesting(entryTwo, valueTwo); InternalEntry<Object, Object, ?> copyOne = segment.copyForTesting(entryOne, null); assertSame(keyOne, entryOne.getKey()); assertEquals(hashOne, entryOne.getHash()); assertNull(entryOne.getNext()); assertSame(valueOne, copyOne.getValue()); InternalEntry<Object, Object, ?> copyTwo = segment.copyForTesting(entryTwo, copyOne); assertSame(keyTwo, copyTwo.getKey()); assertEquals(hashTwo, copyTwo.getHash()); assertSame(copyOne, copyTwo.getNext()); assertSame(valueTwo, copyTwo.getValue()); } }
public void testSegmentReplace() { MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(createMapMaker().concurrencyLevel(1).weakValues()); Segment<Object, Object, ?, ?> segment = map.segments[0]; // TODO(fry): check recency ordering Object key = new Object(); int hash = map.hash(key); Object oldValue = new Object(); Object newValue = new Object(); AtomicReferenceArray<? extends InternalEntry<Object, Object, ?>> table = segment.table; int index = hash & (table.length() - 1); InternalEntry<Object, Object, ?> entry = segment.newEntryForTesting(key, hash, null); WeakValueReference<Object, Object, ?> oldValueRef = segment.newWeakValueReferenceForTesting(entry, oldValue); segment.setWeakValueReferenceForTesting(entry, oldValueRef); // no entry assertNull(segment.replace(key, hash, newValue)); assertEquals(0, segment.count); // same key segment.setTableEntryForTesting(index, entry); segment.count++; assertEquals(1, segment.count); assertSame(oldValue, segment.get(key, hash)); assertSame(oldValue, segment.replace(key, hash, newValue)); assertEquals(1, segment.count); assertSame(newValue, segment.get(key, hash)); // cleared segment.setWeakValueReferenceForTesting(entry, oldValueRef); oldValueRef.clear(); assertNull(segment.replace(key, hash, newValue)); assertEquals(0, segment.count); assertNull(segment.get(key, hash)); }
public void testSegmentPut() { MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(createMapMaker().concurrencyLevel(1).weakValues()); Segment<Object, Object, ?, ?> segment = map.segments[0]; // TODO(fry): check recency ordering Object key = new Object(); int hash = map.hash(key); Object oldValue = new Object(); Object newValue = new Object(); // no entry assertEquals(0, segment.count); assertNull(segment.put(key, hash, oldValue, false)); assertEquals(1, segment.count); // same key assertSame(oldValue, segment.put(key, hash, newValue, false)); assertEquals(1, segment.count); assertSame(newValue, segment.get(key, hash)); // cleared InternalEntry<Object, Object, ?> entry = segment.getEntry(key, hash); WeakValueReference<Object, Object, ?> oldValueRef = segment.newWeakValueReferenceForTesting(entry, oldValue); segment.setWeakValueReferenceForTesting(entry, oldValueRef); assertSame(oldValue, segment.get(key, hash)); oldValueRef.clear(); assertNull(segment.put(key, hash, newValue, false)); assertEquals(1, segment.count); assertSame(newValue, segment.get(key, hash)); }
public void testSegmentPutIfAbsent() { MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(createMapMaker().concurrencyLevel(1).weakValues()); Segment<Object, Object, ?, ?> segment = map.segments[0]; // TODO(fry): check recency ordering Object key = new Object(); int hash = map.hash(key); Object oldValue = new Object(); Object newValue = new Object(); // no entry assertEquals(0, segment.count); assertNull(segment.put(key, hash, oldValue, true)); assertEquals(1, segment.count); // same key assertSame(oldValue, segment.put(key, hash, newValue, true)); assertEquals(1, segment.count); assertSame(oldValue, segment.get(key, hash)); // cleared InternalEntry<Object, Object, ?> entry = segment.getEntry(key, hash); WeakValueReference<Object, Object, ?> oldValueRef = segment.newWeakValueReferenceForTesting(entry, oldValue); segment.setWeakValueReferenceForTesting(entry, oldValueRef); assertSame(oldValue, segment.get(key, hash)); oldValueRef.clear(); assertNull(segment.put(key, hash, newValue, true)); assertEquals(1, segment.count); assertSame(newValue, segment.get(key, hash)); }
public void testSegmentRemove() { MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(createMapMaker().concurrencyLevel(1).weakValues()); Segment<Object, Object, ?, ?> segment = map.segments[0]; Object key = new Object(); int hash = map.hash(key); Object oldValue = new Object(); AtomicReferenceArray<? extends InternalEntry<Object, Object, ?>> table = segment.table; int index = hash & (table.length() - 1); InternalEntry<Object, Object, ?> entry = segment.newEntryForTesting(key, hash, null); WeakValueReference<Object, Object, ?> oldValueRef = segment.newWeakValueReferenceForTesting(entry, oldValue); segment.setWeakValueReferenceForTesting(entry, oldValueRef); // no entry assertEquals(0, segment.count); assertNull(segment.remove(key, hash)); assertEquals(0, segment.count); // same key segment.setTableEntryForTesting(index, entry); segment.count++; assertEquals(1, segment.count); assertSame(oldValue, segment.get(key, hash)); assertSame(oldValue, segment.remove(key, hash)); assertEquals(0, segment.count); assertNull(segment.get(key, hash)); // cleared segment.setTableEntryForTesting(index, entry); segment.count++; assertEquals(1, segment.count); assertSame(oldValue, segment.get(key, hash)); oldValueRef.clear(); assertNull(segment.remove(key, hash)); assertEquals(0, segment.count); assertNull(segment.get(key, hash)); }
public void testExpand() { MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(createMapMaker().concurrencyLevel(1).initialCapacity(1)); Segment<Object, Object, ?, ?> segment = map.segments[0]; assertEquals(1, segment.table.length()); // manually add elements to avoid expansion int originalCount = 1024; InternalEntry<Object, Object, ?> entry = null; for (int i = 0; i < originalCount; i++) { Object key = new Object(); Object value = new Object(); int hash = map.hash(key); // chain all entries together as we only have a single bucket entry = segment.newEntryForTesting(key, hash, entry); segment.setValueForTesting(entry, value); } segment.setTableEntryForTesting(0, entry); segment.count = originalCount; ImmutableMap<Object, Object> originalMap = ImmutableMap.copyOf(map); assertEquals(originalCount, originalMap.size()); assertEquals(originalMap, map); for (int i = 1; i <= originalCount * 2; i *= 2) { if (i > 1) { segment.expand(); } assertEquals(i, segment.table.length()); assertEquals(originalCount, countLiveEntries(map)); assertEquals(originalCount, segment.count); assertEquals(originalMap, map); } }
private static <K, V> int countLiveEntries(MapMakerInternalMap<K, V, ?, ?> map) { int result = 0; for (Segment<K, V, ?, ?> segment : map.segments) { AtomicReferenceArray<? extends InternalEntry<K, V, ?>> table = segment.table; for (int i = 0; i < table.length(); i++) { for (InternalEntry<K, V, ?> e = table.get(i); e != null; e = e.getNext()) { if (map.isLiveForTesting(e)) { result++; } } } } return result; }
public void testClearValue() { MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(createMapMaker().concurrencyLevel(1).initialCapacity(1).weakValues()); Segment<Object, Object, ?, ?> segment = map.segments[0]; AtomicReferenceArray<? extends InternalEntry<Object, Object, ?>> table = segment.table; assertEquals(1, table.length()); Object key = new Object(); Object value = new Object(); int hash = map.hash(key); InternalEntry<Object, Object, ?> entry = segment.newEntryForTesting(key, hash, null); segment.setValueForTesting(entry, value); WeakValueReference<Object, Object, ?> valueRef = segment.getWeakValueReferenceForTesting(entry); // clear absent assertFalse(segment.clearValueForTesting(key, hash, valueRef)); segment.setTableEntryForTesting(0, entry); // don't increment count; this is used during computation assertTrue(segment.clearValueForTesting(key, hash, valueRef)); // no notification sent with clearValue assertEquals(0, segment.count); assertNull(table.get(0)); // clear wrong value reference segment.setTableEntryForTesting(0, entry); WeakValueReference<Object, Object, ?> otherValueRef = segment.newWeakValueReferenceForTesting(entry, value); segment.setWeakValueReferenceForTesting(entry, otherValueRef); assertFalse(segment.clearValueForTesting(key, hash, valueRef)); segment.setWeakValueReferenceForTesting(entry, valueRef); assertTrue(segment.clearValueForTesting(key, hash, valueRef)); }
public void testDrainKeyReferenceQueueOnWrite() { for (MapMaker maker : allWeakKeyStrengthMakers()) { MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(maker.concurrencyLevel(1)); if (maker.getKeyStrength() == Strength.WEAK) { Segment<Object, Object, ?, ?> segment = map.segments[0]; Object keyOne = new Object(); int hashOne = map.hash(keyOne); Object valueOne = new Object(); Object keyTwo = new Object(); Object valueTwo = new Object(); map.put(keyOne, valueOne); InternalEntry<Object, Object, ?> entry = segment.getEntry(keyOne, hashOne); @SuppressWarnings("unchecked") Reference<Object> reference = (Reference) entry; reference.enqueue(); map.put(keyTwo, valueTwo); assertFalse(map.containsKey(keyOne)); assertFalse(map.containsValue(valueOne)); assertNull(map.get(keyOne)); assertEquals(1, map.size()); assertNull(segment.getKeyReferenceQueueForTesting().poll()); } } }
public void testDrainKeyReferenceQueueOnRead() { for (MapMaker maker : allWeakKeyStrengthMakers()) { MapMakerInternalMap<Object, Object, ?, ?> map = makeMap(maker.concurrencyLevel(1)); if (maker.getKeyStrength() == Strength.WEAK) { Segment<Object, Object, ?, ?> segment = map.segments[0]; Object keyOne = new Object(); int hashOne = map.hash(keyOne); Object valueOne = new Object(); Object keyTwo = new Object(); map.put(keyOne, valueOne); InternalEntry<Object, Object, ?> entry = segment.getEntry(keyOne, hashOne); @SuppressWarnings("unchecked") Reference<Object> reference = (Reference) entry; reference.enqueue(); for (int i = 0; i < SMALL_MAX_SIZE; i++) { Object unused = map.get(keyTwo); } assertFalse(map.containsKey(keyOne)); assertFalse(map.containsValue(valueOne)); assertNull(map.get(keyOne)); assertEquals(0, map.size()); assertNull(segment.getKeyReferenceQueueForTesting().poll()); } } }