use of com.google.common.cache.LocalCache.ReferenceEntry in project guava by google.
the class LocalCacheTest method testRecordRead.
public void testRecordRead() {
for (CacheBuilder<Object, Object> builder : allEvictingMakers()) {
LocalCache<Object, Object> map = makeLocalCache(builder.concurrencyLevel(1));
Segment<Object, Object> segment = map.segments[0];
List<ReferenceEntry<Object, Object>> writeOrder = Lists.newLinkedList();
List<ReferenceEntry<Object, Object>> readOrder = Lists.newLinkedList();
for (int i = 0; i < DRAIN_THRESHOLD * 2; i++) {
Object key = new Object();
int hash = map.hash(key);
Object value = new Object();
ReferenceEntry<Object, Object> entry = createDummyEntry(key, hash, value, null);
// must recordRead for drainRecencyQueue to believe this entry is live
segment.recordWrite(entry, 1, map.ticker.read());
writeOrder.add(entry);
readOrder.add(entry);
}
checkEvictionQueues(map, segment, readOrder, writeOrder);
checkExpirationTimes(map);
// access some of the elements
Random random = new Random();
List<ReferenceEntry<Object, Object>> reads = Lists.newArrayList();
Iterator<ReferenceEntry<Object, Object>> i = readOrder.iterator();
while (i.hasNext()) {
ReferenceEntry<Object, Object> entry = i.next();
if (random.nextBoolean()) {
segment.recordRead(entry, map.ticker.read());
reads.add(entry);
i.remove();
}
}
checkAndDrainRecencyQueue(map, segment, reads);
readOrder.addAll(reads);
checkEvictionQueues(map, segment, readOrder, writeOrder);
checkExpirationTimes(map);
}
}
use of com.google.common.cache.LocalCache.ReferenceEntry in project guava by google.
the class LocalCacheTest method testRecordReadOnGet.
public void testRecordReadOnGet() {
for (CacheBuilder<Object, Object> builder : allEvictingMakers()) {
LocalCache<Object, Object> map = makeLocalCache(builder.concurrencyLevel(1));
Segment<Object, Object> segment = map.segments[0];
List<ReferenceEntry<Object, Object>> writeOrder = Lists.newLinkedList();
List<ReferenceEntry<Object, Object>> readOrder = Lists.newLinkedList();
for (int i = 0; i < DRAIN_THRESHOLD * 2; i++) {
Object key = new Object();
int hash = map.hash(key);
Object value = new Object();
map.put(key, value);
ReferenceEntry<Object, Object> entry = segment.getEntry(key, hash);
writeOrder.add(entry);
readOrder.add(entry);
}
checkEvictionQueues(map, segment, readOrder, writeOrder);
checkExpirationTimes(map);
assertTrue(segment.recencyQueue.isEmpty());
// access some of the elements
Random random = new Random();
List<ReferenceEntry<Object, Object>> reads = Lists.newArrayList();
Iterator<ReferenceEntry<Object, Object>> i = readOrder.iterator();
while (i.hasNext()) {
ReferenceEntry<Object, Object> entry = i.next();
if (random.nextBoolean()) {
map.get(entry.getKey());
reads.add(entry);
i.remove();
assertTrue(segment.recencyQueue.size() <= DRAIN_THRESHOLD);
}
}
int undrainedIndex = reads.size() - segment.recencyQueue.size();
checkAndDrainRecencyQueue(map, segment, reads.subList(undrainedIndex, reads.size()));
readOrder.addAll(reads);
checkEvictionQueues(map, segment, readOrder, writeOrder);
checkExpirationTimes(map);
}
}
use of com.google.common.cache.LocalCache.ReferenceEntry in project guava by google.
the class LocalCacheTest method testComputePartiallyCollectedValue.
public void testComputePartiallyCollectedValue() throws ExecutionException {
CacheBuilder<Object, Object> builder = createCacheBuilder().concurrencyLevel(1);
CountingLoader loader = new CountingLoader();
LocalCache<Object, Object> map = makeLocalCache(builder);
Segment<Object, Object> segment = map.segments[0];
AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
assertEquals(0, loader.getCount());
Object key = new Object();
int hash = map.hash(key);
Object value = new Object();
int index = hash & (table.length() - 1);
DummyEntry<Object, Object> entry = DummyEntry.create(key, hash, null);
DummyValueReference<Object, Object> valueRef = DummyValueReference.create(value);
entry.setValueReference(valueRef);
table.set(index, entry);
segment.count++;
assertSame(value, map.get(key, loader));
assertEquals(0, loader.getCount());
assertEquals(1, segment.count);
valueRef.clear();
assertNotSame(value, map.get(key, loader));
assertEquals(1, loader.getCount());
assertEquals(1, segment.count);
}
use of com.google.common.cache.LocalCache.ReferenceEntry in project guava by google.
the class LocalCacheTest method testComputePartiallyCollectedKey.
public void testComputePartiallyCollectedKey() throws ExecutionException {
CacheBuilder<Object, Object> builder = createCacheBuilder().concurrencyLevel(1);
CountingLoader loader = new CountingLoader();
LocalCache<Object, Object> map = makeLocalCache(builder);
Segment<Object, Object> segment = map.segments[0];
AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
assertEquals(0, loader.getCount());
Object key = new Object();
int hash = map.hash(key);
Object value = new Object();
int index = hash & (table.length() - 1);
DummyEntry<Object, Object> entry = DummyEntry.create(key, hash, null);
DummyValueReference<Object, Object> valueRef = DummyValueReference.create(value);
entry.setValueReference(valueRef);
table.set(index, entry);
segment.count++;
assertSame(value, map.get(key, loader));
assertEquals(0, loader.getCount());
assertEquals(1, segment.count);
entry.clearKey();
assertNotSame(value, map.get(key, loader));
assertEquals(1, loader.getCount());
assertEquals(2, segment.count);
}
use of com.google.common.cache.LocalCache.ReferenceEntry in project guava by google.
the class LocalCacheTest method testRecordReadOnCompute.
public void testRecordReadOnCompute() throws ExecutionException {
CountingLoader loader = new CountingLoader();
for (CacheBuilder<Object, Object> builder : allEvictingMakers()) {
LocalCache<Object, Object> map = makeLocalCache(builder.concurrencyLevel(1));
Segment<Object, Object> segment = map.segments[0];
List<ReferenceEntry<Object, Object>> writeOrder = Lists.newLinkedList();
List<ReferenceEntry<Object, Object>> readOrder = Lists.newLinkedList();
for (int i = 0; i < SMALL_MAX_SIZE; i++) {
Object key = new Object();
int hash = map.hash(key);
map.get(key, loader);
ReferenceEntry<Object, Object> entry = segment.getEntry(key, hash);
writeOrder.add(entry);
readOrder.add(entry);
}
checkEvictionQueues(map, segment, readOrder, writeOrder);
checkExpirationTimes(map);
assertTrue(segment.recencyQueue.isEmpty());
// access some of the elements
Random random = new Random();
List<ReferenceEntry<Object, Object>> reads = Lists.newArrayList();
Iterator<ReferenceEntry<Object, Object>> i = readOrder.iterator();
while (i.hasNext()) {
ReferenceEntry<Object, Object> entry = i.next();
if (random.nextBoolean()) {
map.get(entry.getKey(), loader);
reads.add(entry);
i.remove();
assertTrue(segment.recencyQueue.size() <= DRAIN_THRESHOLD);
}
}
int undrainedIndex = reads.size() - segment.recencyQueue.size();
checkAndDrainRecencyQueue(map, segment, reads.subList(undrainedIndex, reads.size()));
readOrder.addAll(reads);
checkEvictionQueues(map, segment, readOrder, writeOrder);
checkExpirationTimes(map);
}
}
Aggregations