add accessOrder parameter to our LinkedHashMap

This commit is contained in:
peter
2013-11-10 21:23:59 +01:00
parent 73f7ab54bc
commit 29a4f604ff
6 changed files with 21 additions and 57 deletions
@@ -48,7 +48,7 @@ public class SLRUMap<K,V> {
myProtectedQueueSize = protectedQueueSize * FACTOR;
myProbationalQueueSize = probationalQueueSize * FACTOR;
myProtectedQueue = new LinkedHashMap<K,V>(10, 0.6f, hashingStrategy) {
myProtectedQueue = new LinkedHashMap<K,V>(10, 0.6f, hashingStrategy, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest, K key, V value) {
if (size() > myProtectedQueueSize) {
@@ -58,15 +58,9 @@ public class SLRUMap<K,V> {
return false;
}
@Override
protected boolean shouldMoveEntryToTopWhenReading() {
return true;
}
};
myProbationalQueue = new LinkedHashMap<K,V>(10, 0.6f, hashingStrategy) {
myProbationalQueue = new LinkedHashMap<K,V>(10, 0.6f, hashingStrategy, true) {
@Override
protected boolean removeEldestEntry(final Map.Entry<K, V> eldest, K key, V value) {
if (size() > myProbationalQueueSize) {
@@ -75,12 +69,6 @@ public class SLRUMap<K,V> {
}
return false;
}
@Override
protected boolean shouldMoveEntryToTopWhenReading() {
return true;
}
};
}
@@ -29,7 +29,7 @@ public class LinkedHashMap<K, V> extends AbstractMap<K, V> implements Map<K, V>
private int size;
private final float loadFactor;
private final EqualityPolicy<K> hashingStrategy;
private final boolean accessOrder;
public LinkedHashMap() {
this(0);
@@ -38,19 +38,30 @@ public class LinkedHashMap<K, V> extends AbstractMap<K, V> implements Map<K, V>
public LinkedHashMap(int capacity) {
this(capacity, HashUtil.DEFAULT_LOAD_FACTOR);
}
public LinkedHashMap(int capacity, boolean accessOrder) {
this(capacity, HashUtil.DEFAULT_LOAD_FACTOR, accessOrder);
}
public LinkedHashMap(int capacity, float loadFactor) {
this(capacity, loadFactor, (EqualityPolicy)EqualityPolicy.CANONICAL);
}
public LinkedHashMap(int capacity, float loadFactor, boolean accessOrder) {
this(capacity, loadFactor, (EqualityPolicy)EqualityPolicy.CANONICAL, accessOrder);
}
public LinkedHashMap(EqualityPolicy hashingStrategy) {
this(0, HashUtil.DEFAULT_LOAD_FACTOR, hashingStrategy);
}
public LinkedHashMap(int capacity, float loadFactor, EqualityPolicy<K> hashingStrategy) {
this(capacity, loadFactor, hashingStrategy, false);
}
public LinkedHashMap(int capacity, float loadFactor, EqualityPolicy<K> hashingStrategy, boolean accessOrder) {
this.loadFactor = loadFactor;
this.hashingStrategy = hashingStrategy;
clear(capacity);
this.accessOrder = accessOrder;
}
@Override
@@ -200,12 +211,8 @@ public class LinkedHashMap<K, V> extends AbstractMap<K, V> implements Map<K, V>
size = 0;
}
protected boolean shouldMoveEntryToTopWhenReading() {
return false;
}
private void moveToTop(final Entry<K, V> e) {
if (!shouldMoveEntryToTopWhenReading()) {
if (!accessOrder) {
return;
}
@@ -45,7 +45,7 @@ public class PagePool {
private PoolPageKey lastFinalizedKey = null;
public PagePool(final int protectedPagesLimit, final int probationalPagesLimit) {
myProbationalQueue = new LinkedHashMap<PoolPageKey,Page>(probationalPagesLimit * 2, 0.6f) {
myProbationalQueue = new LinkedHashMap<PoolPageKey,Page>(probationalPagesLimit * 2, 0.6f, true) {
@Override
protected boolean removeEldestEntry(final Map.Entry<PoolPageKey, Page> eldest) {
if (size() > probationalPagesLimit) {
@@ -54,14 +54,9 @@ public class PagePool {
}
return false;
}
@Override
protected boolean shouldMoveEntryToTopWhenReading() {
return true;
}
};
myProtectedQueue = new LinkedHashMap<PoolPageKey, Page>(protectedPagesLimit, 0.6f) {
myProtectedQueue = new LinkedHashMap<PoolPageKey, Page>(protectedPagesLimit, 0.6f, true) {
@Override
protected boolean removeEldestEntry(final Map.Entry<PoolPageKey, Page> eldest) {
if (size() > protectedPagesLimit) {
@@ -70,10 +65,6 @@ public class PagePool {
}
return false;
}
@Override
protected boolean shouldMoveEntryToTopWhenReading() {
return true;
}
};
}
@@ -484,17 +484,12 @@ public class PagedFileStorage implements Forceable {
myDefaultStorageLockContext = new StorageLockContext(this, checkThreadAccess);
mySizeLimit = UPPER_LIMIT;
mySegments = new LinkedHashMap<Integer, ByteBufferWrapper>(10, 0.75f) {
mySegments = new LinkedHashMap<Integer, ByteBufferWrapper>(10, 0.75f, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<Integer, ByteBufferWrapper> eldest) {
return mySize > mySizeLimit;
}
@Override
protected boolean shouldMoveEntryToTopWhenReading() {
return true;
}
@Nullable
@Override
public ByteBufferWrapper remove(Object key) {
@@ -135,17 +135,11 @@ public class LinkedHashMapTest {
@Test
public void lru2() {
final LinkedHashMap<Integer, String> tested = new LinkedHashMap<Integer, String>() {
final LinkedHashMap<Integer, String> tested = new LinkedHashMap<Integer, String>(0, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<Integer, String> eldest) {
return size() > 1000;
}
@Override
protected boolean shouldMoveEntryToTopWhenReading() {
return true;
}
};
for (int i = 0; i < 1000; ++i) {
tested.put(i, Integer.toString(i));
@@ -161,17 +155,11 @@ public class LinkedHashMapTest {
@Test
public void lru3() {
final LinkedHashMap<Integer, String> tested = new LinkedHashMap<Integer, String>() {
final LinkedHashMap<Integer, String> tested = new LinkedHashMap<Integer, String>(0, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<Integer, String> eldest) {
return size() > 1000;
}
@Override
protected boolean shouldMoveEntryToTopWhenReading() {
return true;
}
};
for (int i = 0; i < 1000; ++i) {
tested.put(i, Integer.toString(i));
@@ -56,15 +56,10 @@ public class YouTrackIntellisense {
private final int myMaxSize;
private SizeLimitedCache(int max) {
super((int)(max / 0.75) + 1);
super((int)(max / 0.75) + 1, true);
myMaxSize = max;
}
@Override
protected boolean shouldMoveEntryToTopWhenReading() {
return true;
}
@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest, K key, V value) {
return size() > myMaxSize;