Rename LRU and segment locks for clarity

This commit is contained in:
Jason Tedor 2015-10-07 10:43:56 -04:00
parent 1f61384988
commit 44b471b008

View File

@ -167,10 +167,10 @@ public class Cache<K, V> {
*/ */
private static class CacheSegment<K, V> { private static class CacheSegment<K, V> {
// read/write lock protecting mutations to the segment // read/write lock protecting mutations to the segment
ReadWriteLock lock = new ReentrantReadWriteLock(); ReadWriteLock segmentLock = new ReentrantReadWriteLock();
ReleasableLock readLock = new ReleasableLock(lock.readLock()); ReleasableLock readLock = new ReleasableLock(segmentLock.readLock());
ReleasableLock writeLock = new ReleasableLock(lock.writeLock()); ReleasableLock writeLock = new ReleasableLock(segmentLock.writeLock());
Map<K, Entry<K, V>> map = new HashMap<>(); Map<K, Entry<K, V>> map = new HashMap<>();
SegmentStats segmentStats = new SegmentStats(); SegmentStats segmentStats = new SegmentStats();
@ -261,7 +261,7 @@ public class Cache<K, V> {
Entry<K, V> tail; Entry<K, V> tail;
// lock protecting mutations to the LRU list // lock protecting mutations to the LRU list
private ReleasableLock lock = new ReleasableLock(new ReentrantLock()); private ReleasableLock lruLock = new ReleasableLock(new ReentrantLock());
/** /**
* Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. * Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
@ -332,7 +332,7 @@ public class Cache<K, V> {
CacheSegment<K, V> segment = getCacheSegment(key); CacheSegment<K, V> segment = getCacheSegment(key);
Tuple<Entry<K, V>, Entry<K, V>> tuple = segment.put(key, value, now); Tuple<Entry<K, V>, Entry<K, V>> tuple = segment.put(key, value, now);
boolean replaced = false; boolean replaced = false;
try (ReleasableLock ignored = lock.acquire()) { try (ReleasableLock ignored = lruLock.acquire()) {
if (tuple.v2() != null && tuple.v2().state == State.EXISTING) { if (tuple.v2() != null && tuple.v2().state == State.EXISTING) {
if (unlink(tuple.v2())) { if (unlink(tuple.v2())) {
replaced = true; replaced = true;
@ -355,7 +355,7 @@ public class Cache<K, V> {
CacheSegment<K, V> segment = getCacheSegment(key); CacheSegment<K, V> segment = getCacheSegment(key);
Entry<K, V> entry = segment.remove(key); Entry<K, V> entry = segment.remove(key);
if (entry != null) { if (entry != null) {
try (ReleasableLock ignored = lock.acquire()) { try (ReleasableLock ignored = lruLock.acquire()) {
delete(entry, RemovalNotification.RemovalReason.INVALIDATED); delete(entry, RemovalNotification.RemovalReason.INVALIDATED);
} }
} }
@ -367,8 +367,8 @@ public class Cache<K, V> {
*/ */
public void invalidateAll() { public void invalidateAll() {
Entry<K, V> h = head; Entry<K, V> h = head;
Arrays.stream(segments).forEach(segment -> segment.lock.writeLock().lock()); Arrays.stream(segments).forEach(segment -> segment.segmentLock.writeLock().lock());
try (ReleasableLock ignored = lock.acquire()) { try (ReleasableLock ignored = lruLock.acquire()) {
Arrays.stream(segments).forEach(segment -> segment.map = new HashMap<>()); Arrays.stream(segments).forEach(segment -> segment.map = new HashMap<>());
Entry<K, V> current = head; Entry<K, V> current = head;
while (current != null) { while (current != null) {
@ -379,7 +379,7 @@ public class Cache<K, V> {
count = 0; count = 0;
weight = 0; weight = 0;
} }
Arrays.stream(segments).forEach(segment -> segment.lock.writeLock().unlock()); Arrays.stream(segments).forEach(segment -> segment.segmentLock.writeLock().unlock());
while (h != null) { while (h != null) {
removalListener.onRemoval(new RemovalNotification<>(h.key, h.value, RemovalNotification.RemovalReason.INVALIDATED)); removalListener.onRemoval(new RemovalNotification<>(h.key, h.value, RemovalNotification.RemovalReason.INVALIDATED));
h = h.after; h = h.after;
@ -391,7 +391,7 @@ public class Cache<K, V> {
*/ */
public void refresh() { public void refresh() {
long now = now(); long now = now();
try (ReleasableLock ignored = lock.acquire()) { try (ReleasableLock ignored = lruLock.acquire()) {
evict(now); evict(now);
} }
} }
@ -488,7 +488,7 @@ public class Cache<K, V> {
if (entry != null) { if (entry != null) {
CacheSegment<K, V> segment = getCacheSegment(entry.key); CacheSegment<K, V> segment = getCacheSegment(entry.key);
segment.remove(entry.key); segment.remove(entry.key);
try (ReleasableLock ignored = lock.acquire()) { try (ReleasableLock ignored = lruLock.acquire()) {
current = null; current = null;
delete(entry, RemovalNotification.RemovalReason.INVALIDATED); delete(entry, RemovalNotification.RemovalReason.INVALIDATED);
} }
@ -540,7 +540,7 @@ public class Cache<K, V> {
private boolean promote(Entry<K, V> entry, long now) { private boolean promote(Entry<K, V> entry, long now) {
boolean promoted = true; boolean promoted = true;
try (ReleasableLock ignored = lock.acquire()) { try (ReleasableLock ignored = lruLock.acquire()) {
switch (entry.state) { switch (entry.state) {
case DELETED: case DELETED:
promoted = false; promoted = false;
@ -560,7 +560,7 @@ public class Cache<K, V> {
} }
private void evict(long now) { private void evict(long now) {
assert lock.isHeldByCurrentThread(); assert lruLock.isHeldByCurrentThread();
while (tail != null && shouldPrune(tail, now)) { while (tail != null && shouldPrune(tail, now)) {
CacheSegment<K, V> segment = getCacheSegment(tail.key); CacheSegment<K, V> segment = getCacheSegment(tail.key);
@ -573,7 +573,7 @@ public class Cache<K, V> {
} }
private void delete(Entry<K, V> entry, RemovalNotification.RemovalReason removalReason) { private void delete(Entry<K, V> entry, RemovalNotification.RemovalReason removalReason) {
assert lock.isHeldByCurrentThread(); assert lruLock.isHeldByCurrentThread();
if (unlink(entry)) { if (unlink(entry)) {
removalListener.onRemoval(new RemovalNotification<>(entry.key, entry.value, removalReason)); removalListener.onRemoval(new RemovalNotification<>(entry.key, entry.value, removalReason));
@ -594,7 +594,7 @@ public class Cache<K, V> {
} }
private boolean unlink(Entry<K, V> entry) { private boolean unlink(Entry<K, V> entry) {
assert lock.isHeldByCurrentThread(); assert lruLock.isHeldByCurrentThread();
if (entry.state == State.EXISTING) { if (entry.state == State.EXISTING) {
final Entry<K, V> before = entry.before; final Entry<K, V> before = entry.before;
@ -634,7 +634,7 @@ public class Cache<K, V> {
} }
private void linkAtHead(Entry<K, V> entry) { private void linkAtHead(Entry<K, V> entry) {
assert lock.isHeldByCurrentThread(); assert lruLock.isHeldByCurrentThread();
Entry<K, V> h = head; Entry<K, V> h = head;
entry.before = null; entry.before = null;
@ -652,7 +652,7 @@ public class Cache<K, V> {
} }
private void relinkAtHead(Entry<K, V> entry) { private void relinkAtHead(Entry<K, V> entry) {
assert lock.isHeldByCurrentThread(); assert lruLock.isHeldByCurrentThread();
if (head != entry) { if (head != entry) {
unlink(entry); unlink(entry);