提交 749d0823 authored 作者: Thomas Mueller's avatar Thomas Mueller

LIRS cache: concurrent and concurrent with long key

上级 f3864e40
......@@ -104,6 +104,7 @@ import org.h2.test.server.TestNestedLoop;
import org.h2.test.server.TestWeb;
import org.h2.test.server.TestInit;
import org.h2.test.store.TestCacheLIRS;
import org.h2.test.store.TestCacheLongKeyLIRS;
import org.h2.test.store.TestConcurrent;
import org.h2.test.store.TestDataUtils;
import org.h2.test.store.TestMVStore;
......@@ -668,6 +669,7 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1`
private void testUnit() {
// mv store
new TestCacheLIRS().runTest(this);
new TestCacheLongKeyLIRS().runTest(this);
new TestConcurrent().runTest(this);
new TestDataUtils().runTest(this);
new TestMVRTree().runTest(this);
......
......@@ -8,8 +8,8 @@ package org.h2.test.store;
import java.nio.ByteBuffer;
import org.h2.dev.store.btree.DataType;
import org.h2.dev.store.btree.MapFactory;
import org.h2.dev.store.btree.DataUtils;
import org.h2.dev.store.btree.MapFactory;
import org.h2.util.StringUtils;
/**
......
......@@ -8,7 +8,7 @@ package org.h2.test.store;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import org.h2.dev.store.btree.CacheLongKeyLIRS;
import org.h2.dev.store.cache.CacheLongKeyLIRS;
import org.h2.test.TestBase;
import org.h2.util.Task;
......
......@@ -10,7 +10,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Random;
import org.h2.dev.store.btree.CacheLIRS;
import org.h2.dev.store.cache.CacheLIRS;
import org.h2.test.TestBase;
import org.h2.util.New;
......@@ -29,6 +29,10 @@ public class TestCacheLIRS extends TestBase {
}
public void test() throws Exception {
testCache();
}
private void testCache() {
testEdgeCases();
testSize();
testClear();
......@@ -42,7 +46,7 @@ public class TestCacheLIRS extends TestBase {
}
private void testEdgeCases() {
CacheLIRS<Integer, Integer> test = CacheLIRS.newInstance(1, 1);
CacheLIRS<Integer, Integer> test = createCache(1);
test.put(1, 10, 100);
assertEquals(10, test.get(1).intValue());
try {
......@@ -82,19 +86,19 @@ public class TestCacheLIRS extends TestBase {
verifyMapSize(769, 2048);
CacheLIRS<Integer, Integer> test;
test = CacheLIRS.newInstance(3, 10);
test = createCache(3, 10);
test.put(0, 0, 9);
test.put(1, 10, 9);
test.put(2, 20, 9);
test.put(3, 30, 9);
test.put(4, 40, 9);
test = CacheLIRS.newInstance(1, 1);
test = createCache(1, 1);
test.put(1, 10);
test.put(0, 0);
test.get(0);
test = CacheLIRS.newInstance(1000, 1);
test = createCache(1000);
for (int j = 0; j < 2000; j++) {
test.put(j, j);
}
......@@ -106,18 +110,18 @@ public class TestCacheLIRS extends TestBase {
assertEquals(968, test.sizeNonResident());
}
private void verifyMapSize(int elements, int mapSize) {
private void verifyMapSize(int elements, int expectedMapSize) {
CacheLIRS<Integer, Integer> test;
test = CacheLIRS.newInstance(elements - 1, 1);
assertTrue(mapSize > test.sizeMapArray());
test = CacheLIRS.newInstance(elements, 1);
assertEquals(mapSize, test.sizeMapArray());
test = CacheLIRS.newInstance(elements * 100, 100);
assertEquals(mapSize, test.sizeMapArray());
test = createCache(elements - 1);
assertTrue(test.sizeMapArray() < expectedMapSize);
test = createCache(elements);
assertEquals(expectedMapSize, test.sizeMapArray());
test = createCache(elements * 100, 100);
assertEquals(expectedMapSize, test.sizeMapArray());
}
private void testGetPutPeekRemove() {
CacheLIRS<Integer, Integer> test = CacheLIRS.newInstance(4, 1);
CacheLIRS<Integer, Integer> test = createCache(4);
test.put(1, 10);
test.put(2, 20);
test.put(3, 30);
......@@ -234,7 +238,7 @@ public class TestCacheLIRS extends TestBase {
}
private void testPruneStack() {
CacheLIRS<Integer, Integer> test = CacheLIRS.newInstance(5, 1);
CacheLIRS<Integer, Integer> test = createCache(5);
for (int i = 0; i < 7; i++) {
test.put(i, i * 10);
}
......@@ -253,7 +257,7 @@ public class TestCacheLIRS extends TestBase {
}
private void testClear() {
CacheLIRS<Integer, Integer> test = CacheLIRS.newInstance(40, 10);
CacheLIRS<Integer, Integer> test = createCache(40, 10);
for (int i = 0; i < 5; i++) {
test.put(i, 10 * i, 9);
}
......@@ -302,7 +306,7 @@ public class TestCacheLIRS extends TestBase {
}
private void testLimitHot() {
CacheLIRS<Integer, Integer> test = CacheLIRS.newInstance(100, 1);
CacheLIRS<Integer, Integer> test = createCache(100);
for (int i = 0; i < 300; i++) {
test.put(i, 10 * i);
}
......@@ -312,7 +316,7 @@ public class TestCacheLIRS extends TestBase {
}
private void testLimitNonResident() {
CacheLIRS<Integer, Integer> test = CacheLIRS.newInstance(4, 1);
CacheLIRS<Integer, Integer> test = createCache(4);
for (int i = 0; i < 20; i++) {
test.put(i, 10 * i);
}
......@@ -347,7 +351,7 @@ public class TestCacheLIRS extends TestBase {
}
CacheLIRS<BadHash, Integer> test = CacheLIRS.newInstance(size * 2, 1);
CacheLIRS<BadHash, Integer> test = createCache(size * 2);
for (int i = 0; i < size; i++) {
test.put(new BadHash(i), i);
}
......@@ -386,7 +390,7 @@ public class TestCacheLIRS extends TestBase {
boolean log = false;
int size = 20;
// cache size 11 (10 hot, 1 cold)
CacheLIRS<Integer, Integer> test = CacheLIRS.newInstance(size / 2 + 1, 1);
CacheLIRS<Integer, Integer> test = createCache(size / 2 + 1);
// init the cache with some dummy entries
for (int i = 0; i < size; i++) {
test.put(-i, -i * 10);
......@@ -440,7 +444,7 @@ public class TestCacheLIRS extends TestBase {
int size = 10;
Random r = new Random(1);
for (int j = 0; j < 100; j++) {
CacheLIRS<Integer, Integer> test = CacheLIRS.newInstance(size / 2, 1);
CacheLIRS<Integer, Integer> test = createCache(size / 2);
HashMap<Integer, Integer> good = New.hashMap();
for (int i = 0; i < 10000; i++) {
int key = r.nextInt(size);
......@@ -524,4 +528,12 @@ public class TestCacheLIRS extends TestBase {
}
}
private static <K, V> CacheLIRS<K, V> createCache(int maxElements) {
return createCache(maxElements, 1);
}
private static <K, V> CacheLIRS<K, V> createCache(int maxSize, int averageSize) {
return CacheLIRS.newInstance(maxSize, averageSize, 1, 0);
}
}
......@@ -10,7 +10,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Random;
import org.h2.dev.store.btree.CacheLongKeyLIRS;
import org.h2.dev.store.cache.CacheLongKeyLIRS;
import org.h2.test.TestBase;
import org.h2.util.New;
......@@ -29,19 +29,23 @@ public class TestCacheLongKeyLIRS extends TestBase {
}
public void test() throws Exception {
testCache();
}
private void testCache() {
testEdgeCases();
testSize();
testClear();
// testGetPutPeekRemove();
testGetPutPeekRemove();
testPruneStack();
testLimitHot();
testLimitNonResident();
// testScanResistance();
testScanResistance();
testRandomOperations();
}
private void testEdgeCases() {
CacheLongKeyLIRS<Integer> test = CacheLongKeyLIRS.newInstance(1);
CacheLongKeyLIRS<Integer> test = createCache(1);
test.put(1, 10, 100);
assertEquals(10, test.get(1).intValue());
try {
......@@ -65,41 +69,52 @@ public class TestCacheLongKeyLIRS extends TestBase {
}
private void testSize() {
int todo;
// verifyMapSize(7, 16);
// verifyMapSize(13, 32);
// verifyMapSize(25, 64);
// verifyMapSize(49, 128);
// verifyMapSize(97, 256);
// verifyMapSize(193, 512);
// verifyMapSize(385, 1024);
// verifyMapSize(769, 2048);
//
// CacheConcurrentLongKeyLIRS<Integer> test;
// test = CacheConcurrentLongKeyLIRS.newInstance(1000, 1);
// for (int j = 0; j < 2000; j++) {
// test.put(j, j);
// }
// // for a cache of size 1000,
// // there are 62 cold entries (about 6.25%).
// assertEquals(62, test.size() - test.sizeHot());
// // at most as many non-resident elements
// // as there are entries in the stack
// assertEquals(968, test.sizeNonResident());
verifyMapSize(7, 16);
verifyMapSize(13, 32);
verifyMapSize(25, 64);
verifyMapSize(49, 128);
verifyMapSize(97, 256);
verifyMapSize(193, 512);
verifyMapSize(385, 1024);
verifyMapSize(769, 2048);
CacheLongKeyLIRS<Integer> test;
test = createCache(3, 10);
test.put(0, 0, 9);
test.put(1, 10, 9);
test.put(2, 20, 9);
test.put(3, 30, 9);
test.put(4, 40, 9);
test = createCache(1, 1);
test.put(1, 10);
test.put(0, 0);
test.get(0);
test = createCache(1000);
for (int j = 0; j < 2000; j++) {
test.put(j, j);
}
// for a cache of size 1000,
// there are 62 cold entries (about 6.25%).
assertEquals(62, test.size() - test.sizeHot());
// at most as many non-resident elements
// as there are entries in the stack
assertEquals(968, test.sizeNonResident());
}
private void verifyMapSize(int elements, int mapSize) {
private void verifyMapSize(int elements, int expectedMapSize) {
CacheLongKeyLIRS<Integer> test;
test = CacheLongKeyLIRS.newInstance(elements - 1);
assertTrue(mapSize > test.sizeMapArray());
test = CacheLongKeyLIRS.newInstance(elements);
assertEquals(mapSize, test.sizeMapArray());
test = CacheLongKeyLIRS.newInstance(elements * 100, 100, 16, 10);
assertEquals(mapSize, test.sizeMapArray());
test = createCache(elements - 1);
assertTrue(test.sizeMapArray() < expectedMapSize);
test = createCache(elements);
assertEquals(expectedMapSize, test.sizeMapArray());
test = createCache(elements * 100, 100);
assertEquals(expectedMapSize, test.sizeMapArray());
}
private void testGetPutPeekRemove() {
CacheLongKeyLIRS<Integer> test = CacheLongKeyLIRS.newInstance(4);
CacheLongKeyLIRS<Integer> test = createCache(4);
test.put(1, 10);
test.put(2, 20);
test.put(3, 30);
......@@ -216,7 +231,7 @@ public class TestCacheLongKeyLIRS extends TestBase {
}
private void testPruneStack() {
CacheLongKeyLIRS<Integer> test = CacheLongKeyLIRS.newInstance(5);
CacheLongKeyLIRS<Integer> test = createCache(5);
for (int i = 0; i < 7; i++) {
test.put(i, i * 10);
}
......@@ -235,7 +250,7 @@ public class TestCacheLongKeyLIRS extends TestBase {
}
private void testClear() {
CacheLongKeyLIRS<Integer> test = CacheLongKeyLIRS.newInstance(40, 10, 16, 1);
CacheLongKeyLIRS<Integer> test = createCache(40, 10);
for (int i = 0; i < 5; i++) {
test.put(i, 10 * i, 9);
}
......@@ -252,10 +267,10 @@ public class TestCacheLongKeyLIRS extends TestBase {
}
assertEquals(40, test.getMaxMemory());
assertEquals(10, test.getAverageMemory());
// assertEquals(36, test.getUsedMemory());
// assertEquals(4, test.size());
// assertEquals(3, test.sizeHot());
// assertEquals(1, test.sizeNonResident());
assertEquals(36, test.getUsedMemory());
assertEquals(4, test.size());
assertEquals(3, test.sizeHot());
assertEquals(1, test.sizeNonResident());
assertFalse(test.isEmpty());
// changing the limit is not supposed to modify the map
......@@ -284,17 +299,17 @@ public class TestCacheLongKeyLIRS extends TestBase {
}
private void testLimitHot() {
CacheLongKeyLIRS<Integer> test = CacheLongKeyLIRS.newInstance(100);
CacheLongKeyLIRS<Integer> test = createCache(100);
for (int i = 0; i < 300; i++) {
test.put(i, 10 * i);
}
// assertEquals(100, test.size());
// assertEquals(99, test.sizeNonResident());
// assertEquals(93, test.sizeHot());
assertEquals(100, test.size());
assertEquals(99, test.sizeNonResident());
assertEquals(93, test.sizeHot());
}
private void testLimitNonResident() {
CacheLongKeyLIRS<Integer> test = CacheLongKeyLIRS.newInstance(4);
CacheLongKeyLIRS<Integer> test = createCache(4);
for (int i = 0; i < 20; i++) {
test.put(i, 10 * i);
}
......@@ -305,7 +320,7 @@ public class TestCacheLongKeyLIRS extends TestBase {
boolean log = false;
int size = 20;
// cache size 11 (10 hot, 1 cold)
CacheLongKeyLIRS<Integer> test = CacheLongKeyLIRS.newInstance(size / 2 + 1);
CacheLongKeyLIRS<Integer> test = createCache(size / 2 + 1);
// init the cache with some dummy entries
for (int i = 0; i < size; i++) {
test.put(-i, -i * 10);
......@@ -359,7 +374,7 @@ public class TestCacheLongKeyLIRS extends TestBase {
int size = 10;
Random r = new Random(1);
for (int j = 0; j < 100; j++) {
CacheLongKeyLIRS<Integer> test = CacheLongKeyLIRS.newInstance(size / 2);
CacheLongKeyLIRS<Integer> test = createCache(size / 2);
HashMap<Integer, Integer> good = New.hashMap();
for (int i = 0; i < 10000; i++) {
int key = r.nextInt(size);
......@@ -400,7 +415,7 @@ public class TestCacheLongKeyLIRS extends TestBase {
}
}
private static <V> String toString(CacheLongKeyLIRS<V> cache) {
private static <K, V> String toString(CacheLongKeyLIRS<V> cache) {
StringBuilder buff = new StringBuilder();
buff.append("mem: " + cache.getUsedMemory());
buff.append(" stack:");
......@@ -418,10 +433,10 @@ public class TestCacheLongKeyLIRS extends TestBase {
return buff.toString();
}
private <V> void verify(CacheLongKeyLIRS<V> cache, String expected) {
private <K, V> void verify(CacheLongKeyLIRS<V> cache, String expected) {
if (expected != null) {
String got = toString(cache);
// assertEquals(expected, got);
assertEquals(expected, got);
}
int mem = 0;
for (long k : cache.keySet()) {
......@@ -437,10 +452,18 @@ public class TestCacheLongKeyLIRS extends TestBase {
hot.removeAll(nonResident);
assertEquals(hot.size(), cache.sizeHot());
assertEquals(hot.size() + cold.size(), cache.size());
// if (stack.size() > 0) {
// long lastStack = stack.get(stack.size() - 1);
// assertTrue(hot.contains(lastStack));
// }
if (stack.size() > 0) {
long lastStack = stack.get(stack.size() - 1);
assertTrue(hot.contains(lastStack));
}
}
private static <V> CacheLongKeyLIRS<V> createCache(int maxElements) {
return createCache(maxElements, 1);
}
private static <V> CacheLongKeyLIRS<V> createCache(int maxSize, int averageSize) {
return CacheLongKeyLIRS.newInstance(maxSize, averageSize, 1, 0);
}
}
......@@ -15,9 +15,10 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.h2.compress.CompressLZF;
import org.h2.compress.Compressor;
import org.h2.dev.store.FilePathCache;
import org.h2.dev.store.cache.CacheLongKeyLIRS;
import org.h2.store.fs.FilePath;
import org.h2.store.fs.FileUtils;
import org.h2.util.New;
......@@ -90,7 +91,8 @@ public class MVStore {
private int blockSize = 4 * 1024;
private long rootChunkStart;
private Map<Long, Page> cache = CacheLIRS.newInstance(readCacheSize, 2048);
private CacheLongKeyLIRS<Page> cache = CacheLongKeyLIRS.newInstance(
readCacheSize, 2048, 16, readCacheSize / 100);
private int lastChunkId;
private HashMap<Integer, Chunk> chunks = New.hashMap();
......@@ -124,7 +126,9 @@ public class MVStore {
private MVStore(String fileName, MapFactory mapFactory) {
this.fileName = fileName;
this.mapFactory = mapFactory;
this.compressor = mapFactory.buildCompressor();
this.compressor = mapFactory == null ?
new CompressLZF() :
mapFactory.buildCompressor();
}
/**
......
/*
* Copyright 2012 H2 Group (http://h2database.com).
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2004-2012 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.dev.store.btree;
package org.h2.dev.store.cache;
import java.util.AbstractMap;
import java.util.ArrayList;
......@@ -23,7 +13,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
/**
* A scan resistant cache. It is meant to cache objects that are relatively
......@@ -54,7 +43,7 @@ import java.util.concurrent.ConcurrentMap;
* @param <K> the key type
* @param <V> the value type
*/
public class CacheConcurrentLIRS<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K, V> {
public class CacheLIRS<K, V> extends AbstractMap<K, V> implements Map<K, V> {
/**
* The maximum memory this cache should use.
......@@ -73,7 +62,7 @@ public class CacheConcurrentLIRS<K, V> extends AbstractMap<K, V> implements Conc
private int segmentMask;
private final int stackMoveDistance;
private CacheConcurrentLIRS(long maxMemory, int averageMemory, int segmentCount, int stackMoveDistance) {
private CacheLIRS(long maxMemory, int averageMemory, int segmentCount, int stackMoveDistance) {
setMaxMemory(maxMemory);
setAverageMemory(averageMemory);
if (Integer.bitCount(segmentCount) != 1) {
......@@ -89,8 +78,9 @@ public class CacheConcurrentLIRS<K, V> extends AbstractMap<K, V> implements Conc
segmentMask = segmentCount - 1;
segments = new Segment[segmentCount];
for (int i = 0; i < segmentCount; i++) {
long max = Math.max(1, maxMemory / segmentCount);
segments[i] = new Segment<K, V>(
1 + maxMemory / segmentCount, averageMemory, stackMoveDistance);
max, averageMemory, stackMoveDistance);
}
segmentShift = Integer.numberOfTrailingZeros(segments[0].sizeMapArray());
}
......@@ -150,26 +140,6 @@ public class CacheConcurrentLIRS<K, V> extends AbstractMap<K, V> implements Conc
return put(key, value, averageMemory);
}
public V putIfAbsent(K key, V value) {
int hash = getHash(key);
return getSegment(hash).putIfAbsent(key, hash, value);
}
public boolean remove(Object key, Object value) {
int hash = getHash(key);
return getSegment(hash).remove(key, hash, value);
}
public boolean replace(K key, V oldValue, V newValue) {
int hash = getHash(key);
return getSegment(hash).replace(key, hash, oldValue, newValue);
}
public V replace(K key, V value) {
int hash = getHash(key);
return getSegment(hash).replace(key, hash, value);
}
/**
* Remove an entry. Both resident and non-resident entries can be removed.
*
......@@ -233,38 +203,39 @@ public class CacheConcurrentLIRS<K, V> extends AbstractMap<K, V> implements Conc
}
/**
* Set the average memory used per entry. It is used to calculate the length
* of the internal array.
* Set the maximum memory this cache should use. This will not immediately
* cause entries to get removed however; it will only change the limit. To
* resize the internal array, call the clear method.
*
* @param averageMemory the average memory used (1 or larger)
* @param maxMemory the maximum size (1 or larger)
*/
public void setAverageMemory(int averageMemory) {
if (averageMemory <= 0) {
throw new IllegalArgumentException("Average memory must be larger than 0");
public void setMaxMemory(long maxMemory) {
if (maxMemory <= 0) {
throw new IllegalArgumentException("Max memory must be larger than 0");
}
this.averageMemory = averageMemory;
this.maxMemory = maxMemory;
if (segments != null) {
long max = 1 + maxMemory / segments.length;
for (Segment<K, V> s : segments) {
s.setAverageMemory(averageMemory);
s.setMaxMemory(max);
}
}
}
/**
* Set the maximum memory this cache should use. This will not immediately
* cause entries to get removed however; it will only change the limit. To
* resize the internal array, call the clear method.
* Set the average memory used per entry. It is used to calculate the length
* of the internal array.
*
* @param maxMemory the maximum size (1 or larger)
* @param averageMemory the average memory used (1 or larger)
*/
public void setMaxMemory(long maxMemory) {
if (maxMemory <= 0) {
throw new IllegalArgumentException("Max memory must be larger than 0");
public void setAverageMemory(int averageMemory) {
if (averageMemory <= 0) {
throw new IllegalArgumentException("Average memory must be larger than 0");
}
this.maxMemory = maxMemory;
this.averageMemory = averageMemory;
if (segments != null) {
for (Segment<K, V> s : segments) {
s.setMaxMemory(1 + maxMemory / segments.length);
s.setAverageMemory(averageMemory);
}
}
}
......@@ -295,8 +266,8 @@ public class CacheConcurrentLIRS<K, V> extends AbstractMap<K, V> implements Conc
* @param maxEntries the maximum number of entries
* @return the cache
*/
public static <K, V> CacheConcurrentLIRS<K, V> newInstance(int maxEntries) {
return new CacheConcurrentLIRS<K, V>(maxEntries, 1, 16, maxEntries / 100);
public static <K, V> CacheLIRS<K, V> newInstance(int maxEntries) {
return new CacheLIRS<K, V>(maxEntries, 1, 16, maxEntries / 100);
}
/**
......@@ -309,9 +280,9 @@ public class CacheConcurrentLIRS<K, V> extends AbstractMap<K, V> implements Conc
* of the stack before the current item is moved
* @return the cache
*/
public static <K, V> CacheConcurrentLIRS<K, V> newInstance(int maxMemory,
int averageMemory, int segmentCount, int stackMoveDistance) {
return new CacheConcurrentLIRS<K, V>(maxMemory, averageMemory, segmentCount, stackMoveDistance);
public static <K, V> CacheLIRS<K, V> newInstance(int maxMemory, int averageMemory,
int segmentCount, int stackMoveDistance) {
return new CacheLIRS<K, V>(maxMemory, averageMemory, segmentCount, stackMoveDistance);
}
/**
......@@ -645,50 +616,6 @@ public class CacheConcurrentLIRS<K, V> extends AbstractMap<K, V> implements Conc
return old;
}
synchronized V putIfAbsent(K key, int hash, V value) {
Entry<K, V> e = find(key, hash);
if (e != null && e.value != null) {
return e.value;
}
return put(key, hash, value, averageMemory);
}
synchronized boolean remove(Object key, int hash, Object value) {
Entry<K, V> e = find(key, hash);
if (e != null) {
V x = e.value;
if (x != null && x.equals(value)) {
remove(key, hash);
return true;
}
}
return false;
}
synchronized boolean replace(K key, int hash, V oldValue, V newValue) {
Entry<K, V> e = find(key, hash);
if (e != null) {
V x = e.value;
if (x != null && x.equals(oldValue)) {
put(key, hash, newValue, averageMemory);
return true;
}
}
return false;
}
synchronized V replace(K key, int hash, V value) {
Entry<K, V> e = find(key, hash);
if (e != null) {
V x = e.value;
if (x != null) {
put(key, hash, value, averageMemory);
return x;
}
}
return null;
}
synchronized V remove(Object key, int hash) {
int index = hash & mask;
Entry<K, V> e = entries[index];
......@@ -891,15 +818,6 @@ public class CacheConcurrentLIRS<K, V> extends AbstractMap<K, V> implements Conc
return set;
}
synchronized Set<Map.Entry<K, V>> entrySet() {
HashMap<K, V> map = new HashMap<K, V>();
for (K k : keySet()) {
int hash = getHash(k);
map.put(k, find(k, hash).value);
}
return map.entrySet();
}
int sizeHot() {
return mapSize - queueSize - queue2Size;
}
......@@ -923,10 +841,6 @@ public class CacheConcurrentLIRS<K, V> extends AbstractMap<K, V> implements Conc
this.maxMemory = maxMemory;
}
long getMaxMemory() {
return maxMemory;
}
void setAverageMemory(int averageMemory) {
if (averageMemory <= 0) {
throw new IllegalArgumentException("Average memory must be larger than 0");
......
/*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Copyright 2004-2012 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.dev.store.btree;
package org.h2.dev.store.cache;
import java.util.ArrayList;
import java.util.HashMap;
......@@ -31,9 +31,11 @@ import java.util.Set;
* prevent unbound memory usage. The maximum size of this queue is at most the
* size of the rest of the stack. About 6.25% of the mapped entries are cold.
* <p>
* Internally, the cache is split into 16 segments, and each segment is an
* individual LIRS cache. Accessed entries are only moved to the top of the
* stack if at least 20 other entries have been moved to the front. Write access
* Internally, the cache is split into a number of segments, and each segment is
* an individual LIRS cache.
* <p>
* Accessed entries are only moved to the top of the stack if at least a number
* of other entries have been moved to the front (1% by default). Write access
* and moving entries to the top of the stack is synchronized per segment.
*
* @author Thomas Mueller
......@@ -74,8 +76,9 @@ public class CacheLongKeyLIRS<V> {
segmentMask = segmentCount - 1;
segments = new Segment[segmentCount];
for (int i = 0; i < segmentCount; i++) {
long max = Math.max(1, maxMemory / segmentCount);
segments[i] = new Segment<V>(
1 + maxMemory / segmentCount, averageMemory, stackMoveDistance);
max, averageMemory, stackMoveDistance);
}
segmentShift = Integer.numberOfTrailingZeros(segments[0].sizeMapArray());
}
......@@ -266,12 +269,13 @@ public class CacheLongKeyLIRS<V> {
}
/**
* Create a new cache with the given memory size. To just limit the number
* of entries, use the required number as the maximum memory, and an average
* size of 1.
* Create a new cache with the given memory size.
*
* @param maxMemory the maximum memory to use (1 or larger)
* @param averageMemory the average memory (1 or larger)
* @param segmentCount the number of cache segments (must be a power of 2)
* @param stackMoveDistance how many other item are to be moved to the top
* of the stack before the current item is moved
* @return the cache
*/
public static <V> CacheLongKeyLIRS<V> newInstance(int maxMemory, int averageMemory,
......@@ -279,6 +283,32 @@ public class CacheLongKeyLIRS<V> {
return new CacheLongKeyLIRS<V>(maxMemory, averageMemory, segmentCount, stackMoveDistance);
}
/**
* Get the entry set for all resident entries.
*
* @return the entry set
*/
public synchronized Set<Map.Entry<Long, V>> entrySet() {
HashMap<Long, V> map = new HashMap<Long, V>();
for (long k : keySet()) {
map.put(k, find(k).value);
}
return map.entrySet();
}
/**
* Get the set of keys for resident entries.
*
* @return the set of keys
*/
public synchronized Set<Long> keySet() {
HashSet<Long> set = new HashSet<Long>();
for (Segment<V> s : segments) {
set.addAll(s.keySet());
}
return set;
}
/**
* Get the number of non-resident entries in the cache.
*
......@@ -306,16 +336,45 @@ public class CacheLongKeyLIRS<V> {
}
/**
* Get the entry set for all resident entries.
* Get the number of hot entries in the cache.
*
* @return the entry set
* @return the number of hot entries
*/
public Set<Long> keySet() {
HashSet<Long> set = new HashSet<Long>();
public int sizeHot() {
int x = 0;
for (Segment<V> s : segments) {
set.addAll(s.keySet());
x += s.sizeHot();
}
return set;
return x;
}
/**
* Get the number of resident entries.
*
* @return the number of entries
*/
public int size() {
int x = 0;
for (Segment<V> s : segments) {
x += s.size();
}
return x;
}
/**
* Get the list of keys. This method allows to read the internal state of
* the cache.
*
* @param cold if true, only keys for the cold entries are returned
* @param nonResident true for non-resident entries
* @return the key list
*/
public synchronized List<Long> keys(boolean cold, boolean nonResident) {
ArrayList<Long> keys = new ArrayList<Long>();
for (Segment<V> s : segments) {
keys.addAll(s.keys(cold, nonResident));
}
return keys;
}
/**
......@@ -338,15 +397,6 @@ public class CacheLongKeyLIRS<V> {
return size() == 0;
}
/**
* Get the entry set for all resident entries.
*
* @return the entry set
*/
public Set<Map.Entry<Long, V>> entrySet() {
return getMap().entrySet();
}
public boolean containsValue(Object value) {
return getMap().containsValue(value);
}
......@@ -362,32 +412,6 @@ public class CacheLongKeyLIRS<V> {
return map;
}
/**
* Get the number of hot entries in the cache.
*
* @return the number of hot entries
*/
public int sizeHot() {
int x = 0;
for (Segment<V> s : segments) {
x += s.sizeHot();
}
return x;
}
/**
* Get the number of resident entries.
*
* @return the number of entries
*/
public int size() {
int x = 0;
for (Segment<V> s : segments) {
x += s.size();
}
return x;
}
public void putAll(Map<Long, ? extends V> m) {
for (Map.Entry<Long, ? extends V> e : m.entrySet()) {
// copy only non-null entries
......@@ -395,22 +419,6 @@ public class CacheLongKeyLIRS<V> {
}
}
/**
* Get the list of keys. This method allows to read the internal state of
* the cache.
*
* @param cold if true, only keys for the cold entries are returned
* @param nonResident true for non-resident entries
* @return the key list
*/
public synchronized List<Long> keys(boolean cold, boolean nonResident) {
ArrayList<Long> keys = new ArrayList<Long>();
for (Segment<V> s : segments) {
keys.addAll(s.keys(cold, nonResident));
}
return keys;
}
/**
* A cache segment
*
......@@ -838,7 +846,7 @@ public class CacheLongKeyLIRS<V> {
return e != null && e.value != null;
}
Set<Long> keySet() {
synchronized Set<Long> keySet() {
HashSet<Long> set = new HashSet<Long>();
for (Entry<V> e = stack.stackNext; e != stack; e = e.stackNext) {
set.add(e.key);
......@@ -872,10 +880,6 @@ public class CacheLongKeyLIRS<V> {
this.maxMemory = maxMemory;
}
long getMaxMemory() {
return maxMemory;
}
void setAverageMemory(int averageMemory) {
if (averageMemory <= 0) {
throw new IllegalArgumentException("Average memory must be larger than 0");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论