Java 类com.google.common.cache.LocalCache.ValueReference 实例源码

项目:guava-mock    文件:LocalCacheTest.java   
public void testNewEntry() {
  for (CacheBuilder<Object, Object> builder : allEntryTypeMakers()) {
    LocalCache<Object, Object> map = makeLocalCache(builder);

    Object keyOne = new Object();
    Object valueOne = new Object();
    int hashOne = map.hash(keyOne);
    ReferenceEntry<Object, Object> entryOne = map.newEntry(keyOne, hashOne, null);
    ValueReference<Object, Object> valueRefOne = map.newValueReference(entryOne, valueOne, 1);
    assertSame(valueOne, valueRefOne.get());
    entryOne.setValueReference(valueRefOne);

    assertSame(keyOne, entryOne.getKey());
    assertEquals(hashOne, entryOne.getHash());
    assertNull(entryOne.getNext());
    assertSame(valueRefOne, entryOne.getValueReference());

    Object keyTwo = new Object();
    Object valueTwo = new Object();
    int hashTwo = map.hash(keyTwo);
    ReferenceEntry<Object, Object> entryTwo = map.newEntry(keyTwo, hashTwo, entryOne);
    ValueReference<Object, Object> valueRefTwo = map.newValueReference(entryTwo, valueTwo, 1);
    assertSame(valueTwo, valueRefTwo.get());
    entryTwo.setValueReference(valueRefTwo);

    assertSame(keyTwo, entryTwo.getKey());
    assertEquals(hashTwo, entryTwo.getHash());
    assertSame(entryOne, entryTwo.getNext());
    assertSame(valueRefTwo, entryTwo.getValueReference());
  }
}
项目:guava-mock    文件:LocalCacheTest.java   
public void testExpand() {
  LocalCache<Object, Object> map =
      makeLocalCache(createCacheBuilder().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;
  ReferenceEntry<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 = map.newEntry(key, hash, entry);
    ValueReference<Object, Object> valueRef = map.newValueReference(entry, value, 1);
    entry.setValueReference(valueRef);
  }
  segment.table.set(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, 0));
    assertEquals(originalCount, segment.count);
    assertEquals(originalMap, map);
  }
}
项目:guava-mock    文件:LocalCacheTest.java   
public void testEvictEntries() {
  int maxSize = 10;
  LocalCache<Object, Object> map =
      makeLocalCache(createCacheBuilder().concurrencyLevel(1).maximumSize(maxSize));
  Segment<Object, Object> segment = map.segments[0];

  // manually add elements to avoid eviction
  int originalCount = 1024;
  ReferenceEntry<Object, Object> entry = null;
  LinkedHashMap<Object, Object> originalMap = Maps.newLinkedHashMap();
  for (int i = 0; i < originalCount; i++) {
    Object key = new Object();
    Object value = new Object();
    AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
    int hash = map.hash(key);
    int index = hash & (table.length() - 1);
    ReferenceEntry<Object, Object> first = table.get(index);
    entry = map.newEntry(key, hash, first);
    ValueReference<Object, Object> valueRef = map.newValueReference(entry, value, 1);
    entry.setValueReference(valueRef);
    segment.recordWrite(entry, 1, map.ticker.read());
    table.set(index, entry);
    originalMap.put(key, value);
  }
  segment.count = originalCount;
  segment.totalWeight = originalCount;
  assertEquals(originalCount, map.size());
  assertEquals(originalMap, map);

  Iterator<Object> it = originalMap.keySet().iterator();
  for (int i = 0; i < originalCount - maxSize; i++) {
    it.next();
    it.remove();
  }
  segment.evictEntries(entry);
  assertEquals(maxSize, map.size());
  assertEquals(originalMap, map);
}
项目:guava-mock    文件:LocalCacheTest.java   
public void testDrainValueReferenceQueueOnWrite() {
  for (CacheBuilder<Object, Object> builder : allKeyValueStrengthMakers()) {
    LocalCache<Object, Object> map =
        makeLocalCache(builder.concurrencyLevel(1));
    if (map.usesValueReferences()) {
      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);
      ReferenceEntry<Object, Object> entry = segment.getEntry(keyOne, hashOne);
      ValueReference<Object, Object> valueReference = entry.getValueReference();

      @SuppressWarnings("unchecked")
      Reference<Object> reference = (Reference) valueReference;
      reference.enqueue();

      map.put(keyTwo, valueTwo);
      assertFalse(map.containsKey(keyOne));
      assertFalse(map.containsValue(valueOne));
      assertNull(map.get(keyOne));
      assertEquals(1, map.size());
      assertNull(segment.valueReferenceQueue.poll());
    }
  }
}
项目:guava-mock    文件:LocalCacheTest.java   
public void testDrainValueReferenceQueueOnRead() {
  for (CacheBuilder<Object, Object> builder : allKeyValueStrengthMakers()) {
    LocalCache<Object, Object> map =
        makeLocalCache(builder.concurrencyLevel(1));
    if (map.usesValueReferences()) {
      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);
      ReferenceEntry<Object, Object> entry = segment.getEntry(keyOne, hashOne);
      ValueReference<Object, Object> valueReference = entry.getValueReference();

      @SuppressWarnings("unchecked")
      Reference<Object> reference = (Reference) valueReference;
      reference.enqueue();

      for (int i = 0; i < SMALL_MAX_SIZE; i++) {
        map.get(keyTwo);
      }
      assertFalse(map.containsKey(keyOne));
      assertFalse(map.containsValue(valueOne));
      assertNull(map.get(keyOne));
      assertEquals(0, map.size());
      assertNull(segment.valueReferenceQueue.poll());
    }
  }
}
项目:guava-mock    文件:CacheTesting.java   
/**
 * Poke into the Cache internals to simulate garbage collection of the value associated with the
 * given key. This assumes that the associated entry is a WeakValueReference or a
 * SoftValueReference (and not a LoadingValueReference), and throws an IllegalStateException
 * if that assumption does not hold.
 */
@SuppressWarnings("unchecked")  // the instanceof check and the cast generate this warning
static <K, V> void simulateValueReclamation(Cache<K, V> cache, K key) {
  ReferenceEntry<K, V> entry = getReferenceEntry(cache, key);
  if (entry != null) {
    ValueReference<K, V> valueRef = entry.getValueReference();
    // fail on strong/computing refs
    Preconditions.checkState(valueRef instanceof Reference);
    Reference<V> ref = (Reference<V>) valueRef;
    if (ref != null) {
      ref.clear();
    }
  }
}
项目:googles-monorepo-demo    文件:LocalCacheTest.java   
public void testNewEntry() {
  for (CacheBuilder<Object, Object> builder : allEntryTypeMakers()) {
    LocalCache<Object, Object> map = makeLocalCache(builder);

    Object keyOne = new Object();
    Object valueOne = new Object();
    int hashOne = map.hash(keyOne);
    ReferenceEntry<Object, Object> entryOne = map.newEntry(keyOne, hashOne, null);
    ValueReference<Object, Object> valueRefOne = map.newValueReference(entryOne, valueOne, 1);
    assertSame(valueOne, valueRefOne.get());
    entryOne.setValueReference(valueRefOne);

    assertSame(keyOne, entryOne.getKey());
    assertEquals(hashOne, entryOne.getHash());
    assertNull(entryOne.getNext());
    assertSame(valueRefOne, entryOne.getValueReference());

    Object keyTwo = new Object();
    Object valueTwo = new Object();
    int hashTwo = map.hash(keyTwo);
    ReferenceEntry<Object, Object> entryTwo = map.newEntry(keyTwo, hashTwo, entryOne);
    ValueReference<Object, Object> valueRefTwo = map.newValueReference(entryTwo, valueTwo, 1);
    assertSame(valueTwo, valueRefTwo.get());
    entryTwo.setValueReference(valueRefTwo);

    assertSame(keyTwo, entryTwo.getKey());
    assertEquals(hashTwo, entryTwo.getHash());
    assertSame(entryOne, entryTwo.getNext());
    assertSame(valueRefTwo, entryTwo.getValueReference());
  }
}
项目:googles-monorepo-demo    文件:LocalCacheTest.java   
public void testExpand() {
  LocalCache<Object, Object> map =
      makeLocalCache(createCacheBuilder().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;
  ReferenceEntry<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 = map.newEntry(key, hash, entry);
    ValueReference<Object, Object> valueRef = map.newValueReference(entry, value, 1);
    entry.setValueReference(valueRef);
  }
  segment.table.set(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, 0));
    assertEquals(originalCount, segment.count);
    assertEquals(originalMap, map);
  }
}
项目:googles-monorepo-demo    文件:LocalCacheTest.java   
public void testEvictEntries() {
  int maxSize = 10;
  LocalCache<Object, Object> map =
      makeLocalCache(createCacheBuilder().concurrencyLevel(1).maximumSize(maxSize));
  Segment<Object, Object> segment = map.segments[0];

  // manually add elements to avoid eviction
  int originalCount = 1024;
  ReferenceEntry<Object, Object> entry = null;
  LinkedHashMap<Object, Object> originalMap = Maps.newLinkedHashMap();
  for (int i = 0; i < originalCount; i++) {
    Object key = new Object();
    Object value = new Object();
    AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
    int hash = map.hash(key);
    int index = hash & (table.length() - 1);
    ReferenceEntry<Object, Object> first = table.get(index);
    entry = map.newEntry(key, hash, first);
    ValueReference<Object, Object> valueRef = map.newValueReference(entry, value, 1);
    entry.setValueReference(valueRef);
    segment.recordWrite(entry, 1, map.ticker.read());
    table.set(index, entry);
    originalMap.put(key, value);
  }
  segment.count = originalCount;
  segment.totalWeight = originalCount;
  assertEquals(originalCount, map.size());
  assertEquals(originalMap, map);

  Iterator<Object> it = originalMap.keySet().iterator();
  for (int i = 0; i < originalCount - maxSize; i++) {
    it.next();
    it.remove();
  }
  segment.evictEntries(entry);
  assertEquals(maxSize, map.size());
  assertEquals(originalMap, map);
}
项目:googles-monorepo-demo    文件:LocalCacheTest.java   
public void testDrainValueReferenceQueueOnWrite() {
  for (CacheBuilder<Object, Object> builder : allKeyValueStrengthMakers()) {
    LocalCache<Object, Object> map =
        makeLocalCache(builder.concurrencyLevel(1));
    if (map.usesValueReferences()) {
      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);
      ReferenceEntry<Object, Object> entry = segment.getEntry(keyOne, hashOne);
      ValueReference<Object, Object> valueReference = entry.getValueReference();

      @SuppressWarnings("unchecked")
      Reference<Object> reference = (Reference) valueReference;
      reference.enqueue();

      map.put(keyTwo, valueTwo);
      assertFalse(map.containsKey(keyOne));
      assertFalse(map.containsValue(valueOne));
      assertNull(map.get(keyOne));
      assertEquals(1, map.size());
      assertNull(segment.valueReferenceQueue.poll());
    }
  }
}
项目:googles-monorepo-demo    文件:LocalCacheTest.java   
public void testDrainValueReferenceQueueOnRead() {
  for (CacheBuilder<Object, Object> builder : allKeyValueStrengthMakers()) {
    LocalCache<Object, Object> map =
        makeLocalCache(builder.concurrencyLevel(1));
    if (map.usesValueReferences()) {
      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);
      ReferenceEntry<Object, Object> entry = segment.getEntry(keyOne, hashOne);
      ValueReference<Object, Object> valueReference = entry.getValueReference();

      @SuppressWarnings("unchecked")
      Reference<Object> reference = (Reference) valueReference;
      reference.enqueue();

      for (int i = 0; i < SMALL_MAX_SIZE; i++) {
        map.get(keyTwo);
      }
      assertFalse(map.containsKey(keyOne));
      assertFalse(map.containsValue(valueOne));
      assertNull(map.get(keyOne));
      assertEquals(0, map.size());
      assertNull(segment.valueReferenceQueue.poll());
    }
  }
}
项目:googles-monorepo-demo    文件:CacheTesting.java   
/**
 * Poke into the Cache internals to simulate garbage collection of the value associated with the
 * given key. This assumes that the associated entry is a WeakValueReference or a
 * SoftValueReference (and not a LoadingValueReference), and throws an IllegalStateException
 * if that assumption does not hold.
 */
@SuppressWarnings("unchecked")  // the instanceof check and the cast generate this warning
static <K, V> void simulateValueReclamation(Cache<K, V> cache, K key) {
  ReferenceEntry<K, V> entry = getReferenceEntry(cache, key);
  if (entry != null) {
    ValueReference<K, V> valueRef = entry.getValueReference();
    // fail on strong/computing refs
    Preconditions.checkState(valueRef instanceof Reference);
    Reference<V> ref = (Reference<V>) valueRef;
    if (ref != null) {
      ref.clear();
    }
  }
}
项目:guava-libraries    文件:LocalCacheTest.java   
public void testNewEntry() {
  for (CacheBuilder<Object, Object> builder : allEntryTypeMakers()) {
    LocalCache<Object, Object> map = makeLocalCache(builder);

    Object keyOne = new Object();
    Object valueOne = new Object();
    int hashOne = map.hash(keyOne);
    ReferenceEntry<Object, Object> entryOne = map.newEntry(keyOne, hashOne, null);
    ValueReference<Object, Object> valueRefOne = map.newValueReference(entryOne, valueOne, 1);
    assertSame(valueOne, valueRefOne.get());
    entryOne.setValueReference(valueRefOne);

    assertSame(keyOne, entryOne.getKey());
    assertEquals(hashOne, entryOne.getHash());
    assertNull(entryOne.getNext());
    assertSame(valueRefOne, entryOne.getValueReference());

    Object keyTwo = new Object();
    Object valueTwo = new Object();
    int hashTwo = map.hash(keyTwo);
    ReferenceEntry<Object, Object> entryTwo = map.newEntry(keyTwo, hashTwo, entryOne);
    ValueReference<Object, Object> valueRefTwo = map.newValueReference(entryTwo, valueTwo, 1);
    assertSame(valueTwo, valueRefTwo.get());
    entryTwo.setValueReference(valueRefTwo);

    assertSame(keyTwo, entryTwo.getKey());
    assertEquals(hashTwo, entryTwo.getHash());
    assertSame(entryOne, entryTwo.getNext());
    assertSame(valueRefTwo, entryTwo.getValueReference());
  }
}
项目:guava-libraries    文件:LocalCacheTest.java   
public void testExpand() {
  LocalCache<Object, Object> map =
      makeLocalCache(createCacheBuilder().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;
  ReferenceEntry<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 = map.newEntry(key, hash, entry);
    ValueReference<Object, Object> valueRef = map.newValueReference(entry, value, 1);
    entry.setValueReference(valueRef);
  }
  segment.table.set(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, 0));
    assertEquals(originalCount, segment.count);
    assertEquals(originalMap, map);
  }
}
项目:guava-libraries    文件:LocalCacheTest.java   
public void testEvictEntries() {
  int maxSize = 10;
  LocalCache<Object, Object> map =
      makeLocalCache(createCacheBuilder().concurrencyLevel(1).maximumSize(maxSize));
  Segment<Object, Object> segment = map.segments[0];

  // manually add elements to avoid eviction
  int originalCount = 1024;
  ReferenceEntry<Object, Object> entry = null;
  LinkedHashMap<Object, Object> originalMap = Maps.newLinkedHashMap();
  for (int i = 0; i < originalCount; i++) {
    Object key = new Object();
    Object value = new Object();
    AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
    int hash = map.hash(key);
    int index = hash & (table.length() - 1);
    ReferenceEntry<Object, Object> first = table.get(index);
    entry = map.newEntry(key, hash, first);
    ValueReference<Object, Object> valueRef = map.newValueReference(entry, value, 1);
    entry.setValueReference(valueRef);
    segment.recordWrite(entry, 1, map.ticker.read());
    table.set(index, entry);
    originalMap.put(key, value);
  }
  segment.count = originalCount;
  segment.totalWeight = originalCount;
  assertEquals(originalCount, map.size());
  assertEquals(originalMap, map);

  Iterator<Object> it = originalMap.keySet().iterator();
  for (int i = 0; i < originalCount - maxSize; i++) {
    it.next();
    it.remove();
  }
  segment.evictEntries();
  assertEquals(maxSize, map.size());
  assertEquals(originalMap, map);
}
项目:guava-libraries    文件:LocalCacheTest.java   
public void testDrainValueReferenceQueueOnWrite() {
  for (CacheBuilder<Object, Object> builder : allKeyValueStrengthMakers()) {
    LocalCache<Object, Object> map =
        makeLocalCache(builder.concurrencyLevel(1));
    if (map.usesValueReferences()) {
      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);
      ReferenceEntry<Object, Object> entry = segment.getEntry(keyOne, hashOne);
      ValueReference<Object, Object> valueReference = entry.getValueReference();

      @SuppressWarnings("unchecked")
      Reference<Object> reference = (Reference) valueReference;
      reference.enqueue();

      map.put(keyTwo, valueTwo);
      assertFalse(map.containsKey(keyOne));
      assertFalse(map.containsValue(valueOne));
      assertNull(map.get(keyOne));
      assertEquals(1, map.size());
      assertNull(segment.valueReferenceQueue.poll());
    }
  }
}
项目:guava-libraries    文件:LocalCacheTest.java   
public void testDrainValueReferenceQueueOnRead() {
  for (CacheBuilder<Object, Object> builder : allKeyValueStrengthMakers()) {
    LocalCache<Object, Object> map =
        makeLocalCache(builder.concurrencyLevel(1));
    if (map.usesValueReferences()) {
      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);
      ReferenceEntry<Object, Object> entry = segment.getEntry(keyOne, hashOne);
      ValueReference<Object, Object> valueReference = entry.getValueReference();

      @SuppressWarnings("unchecked")
      Reference<Object> reference = (Reference) valueReference;
      reference.enqueue();

      for (int i = 0; i < SMALL_MAX_SIZE; i++) {
        map.get(keyTwo);
      }
      assertFalse(map.containsKey(keyOne));
      assertFalse(map.containsValue(valueOne));
      assertNull(map.get(keyOne));
      assertEquals(0, map.size());
      assertNull(segment.valueReferenceQueue.poll());
    }
  }
}
项目:guava-libraries    文件:CacheTesting.java   
/**
 * Poke into the Cache internals to simulate garbage collection of the value associated with the
 * given key. This assumes that the associated entry is a WeakValueReference or a
 * SoftValueReference (and not a LoadingValueReference), and throws an IllegalStateException
 * if that assumption does not hold.
 */
@SuppressWarnings("unchecked")  // the instanceof check and the cast generate this warning
static <K, V> void simulateValueReclamation(Cache<K, V> cache, K key) {
  ReferenceEntry<K, V> entry = getReferenceEntry(cache, key);
  if (entry != null) {
    ValueReference<K, V> valueRef = entry.getValueReference();
    // fail on strong/computing refs
    Preconditions.checkState(valueRef instanceof Reference);
    Reference<V> ref = (Reference<V>) valueRef;
    if (ref != null) {
      ref.clear();
    }
  }
}
项目:guava    文件:LocalCacheTest.java   
public void testNewEntry() {
  for (CacheBuilder<Object, Object> builder : allEntryTypeMakers()) {
    LocalCache<Object, Object> map = makeLocalCache(builder);

    Object keyOne = new Object();
    Object valueOne = new Object();
    int hashOne = map.hash(keyOne);
    ReferenceEntry<Object, Object> entryOne = map.newEntry(keyOne, hashOne, null);
    ValueReference<Object, Object> valueRefOne = map.newValueReference(entryOne, valueOne, 1);
    assertSame(valueOne, valueRefOne.get());
    entryOne.setValueReference(valueRefOne);

    assertSame(keyOne, entryOne.getKey());
    assertEquals(hashOne, entryOne.getHash());
    assertNull(entryOne.getNext());
    assertSame(valueRefOne, entryOne.getValueReference());

    Object keyTwo = new Object();
    Object valueTwo = new Object();
    int hashTwo = map.hash(keyTwo);
    ReferenceEntry<Object, Object> entryTwo = map.newEntry(keyTwo, hashTwo, entryOne);
    ValueReference<Object, Object> valueRefTwo = map.newValueReference(entryTwo, valueTwo, 1);
    assertSame(valueTwo, valueRefTwo.get());
    entryTwo.setValueReference(valueRefTwo);

    assertSame(keyTwo, entryTwo.getKey());
    assertEquals(hashTwo, entryTwo.getHash());
    assertSame(entryOne, entryTwo.getNext());
    assertSame(valueRefTwo, entryTwo.getValueReference());
  }
}
项目:guava    文件:LocalCacheTest.java   
public void testExpand() {
  LocalCache<Object, Object> map =
      makeLocalCache(createCacheBuilder().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;
  ReferenceEntry<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 = map.newEntry(key, hash, entry);
    ValueReference<Object, Object> valueRef = map.newValueReference(entry, value, 1);
    entry.setValueReference(valueRef);
  }
  segment.table.set(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, 0));
    assertEquals(originalCount, segment.count);
    assertEquals(originalMap, map);
  }
}
项目:guava    文件:LocalCacheTest.java   
public void testEvictEntries() {
  int maxSize = 10;
  LocalCache<Object, Object> map =
      makeLocalCache(createCacheBuilder().concurrencyLevel(1).maximumSize(maxSize));
  Segment<Object, Object> segment = map.segments[0];

  // manually add elements to avoid eviction
  int originalCount = 1024;
  ReferenceEntry<Object, Object> entry = null;
  LinkedHashMap<Object, Object> originalMap = Maps.newLinkedHashMap();
  for (int i = 0; i < originalCount; i++) {
    Object key = new Object();
    Object value = new Object();
    AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
    int hash = map.hash(key);
    int index = hash & (table.length() - 1);
    ReferenceEntry<Object, Object> first = table.get(index);
    entry = map.newEntry(key, hash, first);
    ValueReference<Object, Object> valueRef = map.newValueReference(entry, value, 1);
    entry.setValueReference(valueRef);
    segment.recordWrite(entry, 1, map.ticker.read());
    table.set(index, entry);
    originalMap.put(key, value);
  }
  segment.count = originalCount;
  segment.totalWeight = originalCount;
  assertEquals(originalCount, map.size());
  assertEquals(originalMap, map);

  Iterator<Object> it = originalMap.keySet().iterator();
  for (int i = 0; i < originalCount - maxSize; i++) {
    it.next();
    it.remove();
  }
  segment.evictEntries(entry);
  assertEquals(maxSize, map.size());
  assertEquals(originalMap, map);
}
项目:guava    文件:LocalCacheTest.java   
public void testDrainValueReferenceQueueOnWrite() {
  for (CacheBuilder<Object, Object> builder : allKeyValueStrengthMakers()) {
    LocalCache<Object, Object> map = makeLocalCache(builder.concurrencyLevel(1));
    if (map.usesValueReferences()) {
      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);
      ReferenceEntry<Object, Object> entry = segment.getEntry(keyOne, hashOne);
      ValueReference<Object, Object> valueReference = entry.getValueReference();

      @SuppressWarnings("unchecked")
      Reference<Object> reference = (Reference) valueReference;
      reference.enqueue();

      map.put(keyTwo, valueTwo);
      assertFalse(map.containsKey(keyOne));
      assertFalse(map.containsValue(valueOne));
      assertNull(map.get(keyOne));
      assertEquals(1, map.size());
      assertNull(segment.valueReferenceQueue.poll());
    }
  }
}
项目:guava    文件:LocalCacheTest.java   
public void testDrainValueReferenceQueueOnRead() {
  for (CacheBuilder<Object, Object> builder : allKeyValueStrengthMakers()) {
    LocalCache<Object, Object> map = makeLocalCache(builder.concurrencyLevel(1));
    if (map.usesValueReferences()) {
      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);
      ReferenceEntry<Object, Object> entry = segment.getEntry(keyOne, hashOne);
      ValueReference<Object, Object> valueReference = entry.getValueReference();

      @SuppressWarnings("unchecked")
      Reference<Object> reference = (Reference) valueReference;
      reference.enqueue();

      for (int i = 0; i < SMALL_MAX_SIZE; i++) {
        map.get(keyTwo);
      }
      assertFalse(map.containsKey(keyOne));
      assertFalse(map.containsValue(valueOne));
      assertNull(map.get(keyOne));
      assertEquals(0, map.size());
      assertNull(segment.valueReferenceQueue.poll());
    }
  }
}
项目:guava    文件:CacheTesting.java   
/**
 * Poke into the Cache internals to simulate garbage collection of the value associated with the
 * given key. This assumes that the associated entry is a WeakValueReference or a
 * SoftValueReference (and not a LoadingValueReference), and throws an IllegalStateException if
 * that assumption does not hold.
 */
@SuppressWarnings("unchecked") // the instanceof check and the cast generate this warning
static <K, V> void simulateValueReclamation(Cache<K, V> cache, K key) {
  ReferenceEntry<K, V> entry = getReferenceEntry(cache, key);
  if (entry != null) {
    ValueReference<K, V> valueRef = entry.getValueReference();
    // fail on strong/computing refs
    Preconditions.checkState(valueRef instanceof Reference);
    Reference<V> ref = (Reference<V>) valueRef;
    if (ref != null) {
      ref.clear();
    }
  }
}
项目:guava    文件:LocalCacheTest.java   
public void testNewEntry() {
  for (CacheBuilder<Object, Object> builder : allEntryTypeMakers()) {
    LocalCache<Object, Object> map = makeLocalCache(builder);

    Object keyOne = new Object();
    Object valueOne = new Object();
    int hashOne = map.hash(keyOne);
    ReferenceEntry<Object, Object> entryOne = map.newEntry(keyOne, hashOne, null);
    ValueReference<Object, Object> valueRefOne = map.newValueReference(entryOne, valueOne, 1);
    assertSame(valueOne, valueRefOne.get());
    entryOne.setValueReference(valueRefOne);

    assertSame(keyOne, entryOne.getKey());
    assertEquals(hashOne, entryOne.getHash());
    assertNull(entryOne.getNext());
    assertSame(valueRefOne, entryOne.getValueReference());

    Object keyTwo = new Object();
    Object valueTwo = new Object();
    int hashTwo = map.hash(keyTwo);
    ReferenceEntry<Object, Object> entryTwo = map.newEntry(keyTwo, hashTwo, entryOne);
    ValueReference<Object, Object> valueRefTwo = map.newValueReference(entryTwo, valueTwo, 1);
    assertSame(valueTwo, valueRefTwo.get());
    entryTwo.setValueReference(valueRefTwo);

    assertSame(keyTwo, entryTwo.getKey());
    assertEquals(hashTwo, entryTwo.getHash());
    assertSame(entryOne, entryTwo.getNext());
    assertSame(valueRefTwo, entryTwo.getValueReference());
  }
}
项目:guava    文件:LocalCacheTest.java   
public void testExpand() {
  LocalCache<Object, Object> map =
      makeLocalCache(createCacheBuilder().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;
  ReferenceEntry<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 = map.newEntry(key, hash, entry);
    ValueReference<Object, Object> valueRef = map.newValueReference(entry, value, 1);
    entry.setValueReference(valueRef);
  }
  segment.table.set(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, 0));
    assertEquals(originalCount, segment.count);
    assertEquals(originalMap, map);
  }
}
项目:guava    文件:LocalCacheTest.java   
public void testEvictEntries() {
  int maxSize = 10;
  LocalCache<Object, Object> map =
      makeLocalCache(createCacheBuilder().concurrencyLevel(1).maximumSize(maxSize));
  Segment<Object, Object> segment = map.segments[0];

  // manually add elements to avoid eviction
  int originalCount = 1024;
  ReferenceEntry<Object, Object> entry = null;
  LinkedHashMap<Object, Object> originalMap = Maps.newLinkedHashMap();
  for (int i = 0; i < originalCount; i++) {
    Object key = new Object();
    Object value = new Object();
    AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
    int hash = map.hash(key);
    int index = hash & (table.length() - 1);
    ReferenceEntry<Object, Object> first = table.get(index);
    entry = map.newEntry(key, hash, first);
    ValueReference<Object, Object> valueRef = map.newValueReference(entry, value, 1);
    entry.setValueReference(valueRef);
    segment.recordWrite(entry, 1, map.ticker.read());
    table.set(index, entry);
    originalMap.put(key, value);
  }
  segment.count = originalCount;
  segment.totalWeight = originalCount;
  assertEquals(originalCount, map.size());
  assertEquals(originalMap, map);

  Iterator<Object> it = originalMap.keySet().iterator();
  for (int i = 0; i < originalCount - maxSize; i++) {
    it.next();
    it.remove();
  }
  segment.evictEntries(entry);
  assertEquals(maxSize, map.size());
  assertEquals(originalMap, map);
}
项目:guava    文件:LocalCacheTest.java   
public void testDrainValueReferenceQueueOnWrite() {
  for (CacheBuilder<Object, Object> builder : allKeyValueStrengthMakers()) {
    LocalCache<Object, Object> map = makeLocalCache(builder.concurrencyLevel(1));
    if (map.usesValueReferences()) {
      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);
      ReferenceEntry<Object, Object> entry = segment.getEntry(keyOne, hashOne);
      ValueReference<Object, Object> valueReference = entry.getValueReference();

      @SuppressWarnings("unchecked")
      Reference<Object> reference = (Reference) valueReference;
      reference.enqueue();

      map.put(keyTwo, valueTwo);
      assertFalse(map.containsKey(keyOne));
      assertFalse(map.containsValue(valueOne));
      assertNull(map.get(keyOne));
      assertEquals(1, map.size());
      assertNull(segment.valueReferenceQueue.poll());
    }
  }
}
项目:guava    文件:LocalCacheTest.java   
public void testDrainValueReferenceQueueOnRead() {
  for (CacheBuilder<Object, Object> builder : allKeyValueStrengthMakers()) {
    LocalCache<Object, Object> map = makeLocalCache(builder.concurrencyLevel(1));
    if (map.usesValueReferences()) {
      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);
      ReferenceEntry<Object, Object> entry = segment.getEntry(keyOne, hashOne);
      ValueReference<Object, Object> valueReference = entry.getValueReference();

      @SuppressWarnings("unchecked")
      Reference<Object> reference = (Reference) valueReference;
      reference.enqueue();

      for (int i = 0; i < SMALL_MAX_SIZE; i++) {
        map.get(keyTwo);
      }
      assertFalse(map.containsKey(keyOne));
      assertFalse(map.containsValue(valueOne));
      assertNull(map.get(keyOne));
      assertEquals(0, map.size());
      assertNull(segment.valueReferenceQueue.poll());
    }
  }
}
项目:guava    文件:CacheTesting.java   
/**
 * Poke into the Cache internals to simulate garbage collection of the value associated with the
 * given key. This assumes that the associated entry is a WeakValueReference or a
 * SoftValueReference (and not a LoadingValueReference), and throws an IllegalStateException if
 * that assumption does not hold.
 */
@SuppressWarnings("unchecked") // the instanceof check and the cast generate this warning
static <K, V> void simulateValueReclamation(Cache<K, V> cache, K key) {
  ReferenceEntry<K, V> entry = getReferenceEntry(cache, key);
  if (entry != null) {
    ValueReference<K, V> valueRef = entry.getValueReference();
    // fail on strong/computing refs
    Preconditions.checkState(valueRef instanceof Reference);
    Reference<V> ref = (Reference<V>) valueRef;
    if (ref != null) {
      ref.clear();
    }
  }
}
项目:guava-mock    文件:LocalCacheTest.java   
public void testSegmentGetAndContains() {
  FakeTicker ticker = new FakeTicker();
  LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder()
      .concurrencyLevel(1)
      .ticker(ticker)
      .expireAfterAccess(1, TimeUnit.NANOSECONDS));
  Segment<Object, Object> segment = map.segments[0];
  // TODO(fry): check recency ordering

  Object key = new Object();
  int hash = map.hash(key);
  Object value = new Object();
  AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
  int index = hash & (table.length() - 1);

  ReferenceEntry<Object, Object> entry = map.newEntry(key, hash, null);
  ValueReference<Object, Object> valueRef = map.newValueReference(entry, value, 1);
  entry.setValueReference(valueRef);

  assertNull(segment.get(key, hash));

  // count == 0
  table.set(index, entry);
  assertNull(segment.get(key, hash));
  assertFalse(segment.containsKey(key, hash));
  assertFalse(segment.containsValue(value));

  // count == 1
  segment.count++;
  assertSame(value, segment.get(key, hash));
  assertTrue(segment.containsKey(key, hash));
  assertTrue(segment.containsValue(value));
  // don't see absent values now that count > 0
  assertNull(segment.get(new Object(), hash));

  // null key
  DummyEntry<Object, Object> nullEntry = DummyEntry.create(null, hash, entry);
  Object nullValue = new Object();
  ValueReference<Object, Object> nullValueRef = map.newValueReference(nullEntry, nullValue, 1);
  nullEntry.setValueReference(nullValueRef);
  table.set(index, nullEntry);
  // skip the null key
  assertSame(value, segment.get(key, hash));
  assertTrue(segment.containsKey(key, hash));
  assertTrue(segment.containsValue(value));
  assertFalse(segment.containsValue(nullValue));

  // hash collision
  DummyEntry<Object, Object> dummy = DummyEntry.create(new Object(), hash, entry);
  Object dummyValue = new Object();
  ValueReference<Object, Object> dummyValueRef = map.newValueReference(dummy, dummyValue, 1);
  dummy.setValueReference(dummyValueRef);
  table.set(index, dummy);
  assertSame(value, segment.get(key, hash));
  assertTrue(segment.containsKey(key, hash));
  assertTrue(segment.containsValue(value));
  assertTrue(segment.containsValue(dummyValue));

  // key collision
  dummy = DummyEntry.create(key, hash, entry);
  dummyValue = new Object();
  dummyValueRef = map.newValueReference(dummy, dummyValue, 1);
  dummy.setValueReference(dummyValueRef);
  table.set(index, dummy);
  // returns the most recent entry
  assertSame(dummyValue, segment.get(key, hash));
  assertTrue(segment.containsKey(key, hash));
  assertTrue(segment.containsValue(value));
  assertTrue(segment.containsValue(dummyValue));

  // expired
  dummy.setAccessTime(ticker.read() - 2);
  assertNull(segment.get(key, hash));
  assertFalse(segment.containsKey(key, hash));
  assertTrue(segment.containsValue(value));
  assertFalse(segment.containsValue(dummyValue));
}
项目:guava-mock    文件:LocalCacheTest.java   
public void testExpand_cleanup() {
  LocalCache<Object, Object> map =
      makeLocalCache(createCacheBuilder().concurrencyLevel(1).initialCapacity(1));
  Segment<Object, Object> segment = map.segments[0];
  assertEquals(1, segment.table.length());

  // manually add elements to avoid expansion
  // 1/3 null keys, 1/3 null values
  int originalCount = 1024;
  ReferenceEntry<Object, Object> entry = null;
  for (int i = 0; i < originalCount; i++) {
    Object key = new Object();
    Object value = (i % 3 == 0) ? null : new Object();
    int hash = map.hash(key);
    if (i % 3 == 1) {
      key = null;
    }
    // chain all entries together as we only have a single bucket
    entry = DummyEntry.create(key, hash, entry);
    ValueReference<Object, Object> valueRef = DummyValueReference.create(value);
    entry.setValueReference(valueRef);
  }
  segment.table.set(0, entry);
  segment.count = originalCount;
  int liveCount = originalCount / 3;
  assertEquals(1, segment.table.length());
  assertEquals(liveCount, countLiveEntries(map, 0));
  ImmutableMap<Object, Object> originalMap = ImmutableMap.copyOf(map);
  assertEquals(liveCount, originalMap.size());
  // can't compare map contents until cleanup occurs

  for (int i = 1; i <= originalCount * 2; i *= 2) {
    if (i > 1) {
      segment.expand();
    }
    assertEquals(i, segment.table.length());
    assertEquals(liveCount, countLiveEntries(map, 0));
    // expansion cleanup is sloppy, with a goal of avoiding unnecessary copies
    assertTrue(segment.count >= liveCount);
    assertTrue(segment.count <= originalCount);
    assertEquals(originalMap, ImmutableMap.copyOf(map));
  }
}
项目:guava-mock    文件:LocalCacheTest.java   
@Override
public ValueReference<K, V> getValueReference() {
  return valueReference;
}
项目:guava-mock    文件:LocalCacheTest.java   
@Override
public void setValueReference(ValueReference<K, V> valueReference) {
  this.valueReference = valueReference;
}
项目:guava-mock    文件:LocalCacheTest.java   
@Override
public ValueReference<K, V> copyFor(
    ReferenceQueue<V> queue, V value, ReferenceEntry<K, V> entry) {
  return this;
}
项目:googles-monorepo-demo    文件:LocalCacheTest.java   
public void testSegmentGetAndContains() {
  FakeTicker ticker = new FakeTicker();
  LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder()
      .concurrencyLevel(1)
      .ticker(ticker)
      .expireAfterAccess(1, TimeUnit.NANOSECONDS));
  Segment<Object, Object> segment = map.segments[0];
  // TODO(fry): check recency ordering

  Object key = new Object();
  int hash = map.hash(key);
  Object value = new Object();
  AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
  int index = hash & (table.length() - 1);

  ReferenceEntry<Object, Object> entry = map.newEntry(key, hash, null);
  ValueReference<Object, Object> valueRef = map.newValueReference(entry, value, 1);
  entry.setValueReference(valueRef);

  assertNull(segment.get(key, hash));

  // count == 0
  table.set(index, entry);
  assertNull(segment.get(key, hash));
  assertFalse(segment.containsKey(key, hash));
  assertFalse(segment.containsValue(value));

  // count == 1
  segment.count++;
  assertSame(value, segment.get(key, hash));
  assertTrue(segment.containsKey(key, hash));
  assertTrue(segment.containsValue(value));
  // don't see absent values now that count > 0
  assertNull(segment.get(new Object(), hash));

  // null key
  DummyEntry<Object, Object> nullEntry = DummyEntry.create(null, hash, entry);
  Object nullValue = new Object();
  ValueReference<Object, Object> nullValueRef = map.newValueReference(nullEntry, nullValue, 1);
  nullEntry.setValueReference(nullValueRef);
  table.set(index, nullEntry);
  // skip the null key
  assertSame(value, segment.get(key, hash));
  assertTrue(segment.containsKey(key, hash));
  assertTrue(segment.containsValue(value));
  assertFalse(segment.containsValue(nullValue));

  // hash collision
  DummyEntry<Object, Object> dummy = DummyEntry.create(new Object(), hash, entry);
  Object dummyValue = new Object();
  ValueReference<Object, Object> dummyValueRef = map.newValueReference(dummy, dummyValue, 1);
  dummy.setValueReference(dummyValueRef);
  table.set(index, dummy);
  assertSame(value, segment.get(key, hash));
  assertTrue(segment.containsKey(key, hash));
  assertTrue(segment.containsValue(value));
  assertTrue(segment.containsValue(dummyValue));

  // key collision
  dummy = DummyEntry.create(key, hash, entry);
  dummyValue = new Object();
  dummyValueRef = map.newValueReference(dummy, dummyValue, 1);
  dummy.setValueReference(dummyValueRef);
  table.set(index, dummy);
  // returns the most recent entry
  assertSame(dummyValue, segment.get(key, hash));
  assertTrue(segment.containsKey(key, hash));
  assertTrue(segment.containsValue(value));
  assertTrue(segment.containsValue(dummyValue));

  // expired
  dummy.setAccessTime(ticker.read() - 2);
  assertNull(segment.get(key, hash));
  assertFalse(segment.containsKey(key, hash));
  assertTrue(segment.containsValue(value));
  assertFalse(segment.containsValue(dummyValue));
}
项目:googles-monorepo-demo    文件:LocalCacheTest.java   
public void testExpand_cleanup() {
  LocalCache<Object, Object> map =
      makeLocalCache(createCacheBuilder().concurrencyLevel(1).initialCapacity(1));
  Segment<Object, Object> segment = map.segments[0];
  assertEquals(1, segment.table.length());

  // manually add elements to avoid expansion
  // 1/3 null keys, 1/3 null values
  int originalCount = 1024;
  ReferenceEntry<Object, Object> entry = null;
  for (int i = 0; i < originalCount; i++) {
    Object key = new Object();
    Object value = (i % 3 == 0) ? null : new Object();
    int hash = map.hash(key);
    if (i % 3 == 1) {
      key = null;
    }
    // chain all entries together as we only have a single bucket
    entry = DummyEntry.create(key, hash, entry);
    ValueReference<Object, Object> valueRef = DummyValueReference.create(value);
    entry.setValueReference(valueRef);
  }
  segment.table.set(0, entry);
  segment.count = originalCount;
  int liveCount = originalCount / 3;
  assertEquals(1, segment.table.length());
  assertEquals(liveCount, countLiveEntries(map, 0));
  ImmutableMap<Object, Object> originalMap = ImmutableMap.copyOf(map);
  assertEquals(liveCount, originalMap.size());
  // can't compare map contents until cleanup occurs

  for (int i = 1; i <= originalCount * 2; i *= 2) {
    if (i > 1) {
      segment.expand();
    }
    assertEquals(i, segment.table.length());
    assertEquals(liveCount, countLiveEntries(map, 0));
    // expansion cleanup is sloppy, with a goal of avoiding unnecessary copies
    assertTrue(segment.count >= liveCount);
    assertTrue(segment.count <= originalCount);
    assertEquals(originalMap, ImmutableMap.copyOf(map));
  }
}
项目:googles-monorepo-demo    文件:LocalCacheTest.java   
@Override
public ValueReference<K, V> getValueReference() {
  return valueReference;
}
项目:googles-monorepo-demo    文件:LocalCacheTest.java   
@Override
public void setValueReference(ValueReference<K, V> valueReference) {
  this.valueReference = valueReference;
}
项目:googles-monorepo-demo    文件:LocalCacheTest.java   
@Override
public ValueReference<K, V> copyFor(
    ReferenceQueue<V> queue, V value, ReferenceEntry<K, V> entry) {
  return this;
}