提交 1e391a6c authored 作者: Thomas Mueller's avatar Thomas Mueller

MVStore: simplify the cache API a bit

上级 606aa497
......@@ -203,7 +203,7 @@ public class MVStore {
if (fileName != null) {
Object s = config.get("cacheSize");
int mb = s == null ? 16 : Integer.parseInt(s.toString());
cache = CacheLongKeyLIRS.newInstance(
cache = new CacheLongKeyLIRS<Page>(
mb * 1024 * 1024, 2048, 16, mb * 1024 * 1024 / 2048 * 2 / 100);
} else {
cache = null;
......
......@@ -61,8 +61,28 @@ public class CacheLongKeyLIRS<V> {
private final int segmentMask;
private final int stackMoveDistance;
/**
* Create a new cache with the given number of entries, and the default
* settings (an average size of 1 per entry, 16 segments, and stack move
* distance equals to the maximum number of entries divided by 100).
*
* @param maxEntries the maximum number of entries
*/
public CacheLongKeyLIRS(int maxEntries) {
this(maxEntries, 1, 16, maxEntries / 100);
}
/**
* 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
*/
@SuppressWarnings("unchecked")
private CacheLongKeyLIRS(long maxMemory, int averageMemory, int segmentCount, int stackMoveDistance) {
public CacheLongKeyLIRS(long maxMemory, int averageMemory, int segmentCount, int stackMoveDistance) {
setMaxMemory(maxMemory);
setAverageMemory(averageMemory);
if (Integer.bitCount(segmentCount) != 1) {
......@@ -269,33 +289,6 @@ public class CacheLongKeyLIRS<V> {
return maxMemory;
}
/**
* Create a new cache with the given number of entries, and the default
* settings (an average size of 1 per entry, 16 segments, and stack move
* distance equals to the maximum number of entries divided by 100).
*
* @param maxEntries the maximum number of entries
* @return the cache
*/
public static <K, V> CacheLongKeyLIRS<V> newInstance(int maxEntries) {
return new CacheLongKeyLIRS<V>(maxEntries, 1, 16, maxEntries / 100);
}
/**
* 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,
int segmentCount, int stackMoveDistance) {
return new CacheLongKeyLIRS<V>(maxMemory, averageMemory, segmentCount, stackMoveDistance);
}
/**
* Get the entry set for all resident entries.
*
......@@ -458,7 +451,7 @@ public class CacheLongKeyLIRS<V> {
*
* @param <V> the value type
*/
static class Segment<V> {
private static class Segment<V> {
/**
* The number of (hot, cold, and non-resident) entries in the map.
......
......@@ -39,7 +39,7 @@ public class FilePathCache extends FilePathWrapper {
private final FileChannel base;
// 1 MB (256 * 4 * 1024)
private final CacheLongKeyLIRS<ByteBuffer> cache =
CacheLongKeyLIRS.newInstance(256);
new CacheLongKeyLIRS<ByteBuffer>(256);
FileCache(FileChannel base) {
this.base = base;
......
......@@ -31,7 +31,7 @@ public class TestCacheConcurrentLIRS extends TestBase {
}
private void testConcurrent() {
final CacheLongKeyLIRS<Integer> test = CacheLongKeyLIRS.newInstance(100);
final CacheLongKeyLIRS<Integer> test = new CacheLongKeyLIRS<Integer>(100);
int threadCount = 8;
final CountDownLatch wait = new CountDownLatch(1);
final AtomicBoolean stopped = new AtomicBoolean();
......
......@@ -533,7 +533,7 @@ public class TestCacheLIRS extends TestBase {
}
private static <K, V> CacheLIRS<K, V> createCache(int maxSize, int averageSize) {
return CacheLIRS.newInstance(maxSize, averageSize, 1, 0);
return new CacheLIRS<K, V>(maxSize, averageSize, 1, 0);
}
}
......@@ -463,7 +463,7 @@ public class TestCacheLongKeyLIRS extends TestBase {
}
private static <V> CacheLongKeyLIRS<V> createCache(int maxSize, int averageSize) {
return CacheLongKeyLIRS.newInstance(maxSize, averageSize, 1, 0);
return new CacheLongKeyLIRS<V>(maxSize, averageSize, 1, 0);
}
}
......@@ -62,8 +62,29 @@ public class CacheLIRS<K, V> extends AbstractMap<K, V> implements Map<K, V> {
private final int segmentMask;
private final int stackMoveDistance;
/**
* Create a new cache with the given number of entries, and the default
* settings (an average size of 1 per entry, 16 segments, and stack move
* distance equals to the maximum number of entries divided by 100).
*
* @param maxEntries the maximum number of entries
*/
public CacheLIRS(int maxEntries) {
this(maxEntries, 1, 16, maxEntries / 100);
}
/**
* 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
*/
@SuppressWarnings("unchecked")
private CacheLIRS(long maxMemory, int averageMemory, int segmentCount, int stackMoveDistance) {
public CacheLIRS(long maxMemory, int averageMemory, int segmentCount, int stackMoveDistance) {
setMaxMemory(maxMemory);
setAverageMemory(averageMemory);
if (Integer.bitCount(segmentCount) != 1) {
......@@ -270,33 +291,6 @@ public class CacheLIRS<K, V> extends AbstractMap<K, V> implements Map<K, V> {
return maxMemory;
}
/**
* Create a new cache with the given number of entries, and the default
* settings (an average size of 1 per entry, 16 segments, and stack move
* distance equals to the maximum number of entries divided by 100).
*
* @param maxEntries the maximum number of entries
* @return the cache
*/
public static <K, V> CacheLIRS<K, V> newInstance(int maxEntries) {
return new CacheLIRS<K, V>(maxEntries, 1, 16, maxEntries / 100);
}
/**
* 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 <K, V> CacheLIRS<K, V> newInstance(int maxMemory, int averageMemory,
int segmentCount, int stackMoveDistance) {
return new CacheLIRS<K, V>(maxMemory, averageMemory, segmentCount, stackMoveDistance);
}
/**
* Get the entry set for all resident entries.
*
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论