提交 5116b071 authored 作者: andrei's avatar andrei

move constants into Page, cleanup

上级 98365b17
...@@ -30,7 +30,6 @@ import org.h2.jdbc.JdbcConnection; ...@@ -30,7 +30,6 @@ import org.h2.jdbc.JdbcConnection;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.message.Trace; import org.h2.message.Trace;
import org.h2.message.TraceSystem; import org.h2.message.TraceSystem;
import org.h2.mvstore.MVStore;
import org.h2.mvstore.db.MVTable; import org.h2.mvstore.db.MVTable;
import org.h2.mvstore.db.MVTableEngine; import org.h2.mvstore.db.MVTableEngine;
import org.h2.mvstore.db.TransactionStore.Change; import org.h2.mvstore.db.TransactionStore.Change;
......
...@@ -138,20 +138,6 @@ public final class DataUtils { ...@@ -138,20 +138,6 @@ public final class DataUtils {
*/ */
public static final long COMPRESSED_VAR_LONG_MAX = 0x1ffffffffffffL; public static final long COMPRESSED_VAR_LONG_MAX = 0x1ffffffffffffL;
/**
* The estimated number of bytes used per page object.
*/
public static final int PAGE_MEMORY = Constants.MEMORY_OBJECT + 2 * Constants.MEMORY_POINTER + 22; //128;
public static final int PAGE_LEAF_MEMORY = PAGE_MEMORY + Constants.MEMORY_POINTER;
public static final int PAGE_NODE_MEMORY = PAGE_MEMORY + Constants.MEMORY_POINTER + 8;
public static final int PAGE_LEAF_EMPTY_MEMORY = PAGE_LEAF_MEMORY + 2 * Constants.MEMORY_ARRAY;
public static final int PAGE_NODE_EMPTY_MEMORY = PAGE_NODE_MEMORY + Constants.MEMORY_ARRAY;
/**
* The estimated number of bytes used per child entry.
*/
public static final int PAGE_MEMORY_CHILD = 16;
/** /**
* The marker size of a very large page. * The marker size of a very large page.
*/ */
......
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.mvstore;
import org.h2.mvstore.type.DataType;
import org.h2.mvstore.type.ObjectDataType;
import java.util.Map;
/**
* A class used for backward compatibility.
*
* @param <K> the key type
* @param <V> the value type
*/
public class MVMapConcurrent<K, V> extends MVMap<K, V> {
public MVMapConcurrent(Map<String, Object> config) {
super(config);
}
/**
* A builder for this class.
*
* @param <K> the key type
* @param <V> the value type
*/
public static class Builder<K, V> extends MVMap.Builder<K,V> {
/**
* Create a new builder with the default key and value data types.
*/
public Builder() {
// ignore
}
/**
* Set the key data type.
*
* @param keyType the key type
* @return this
*/
public Builder<K, V> keyType(DataType keyType) {
setKeyType(keyType);
return this;
}
/**
* Set the key data type.
*
* @param valueType the key type
* @return this
*/
public Builder<K, V> valueType(DataType valueType) {
setValueType(valueType);
return this;
}
@Override
protected MVMapConcurrent<K, V> create(Map<String, Object> config) {
return new MVMapConcurrent<>(config);
}
}
}
...@@ -226,7 +226,7 @@ public final class MVStore { ...@@ -226,7 +226,7 @@ public final class MVStore {
private Compressor compressorHigh; private Compressor compressorHigh;
public final UncaughtExceptionHandler backgroundExceptionHandler; private final UncaughtExceptionHandler backgroundExceptionHandler;
private volatile long currentVersion; private volatile long currentVersion;
...@@ -235,8 +235,19 @@ public final class MVStore { ...@@ -235,8 +235,19 @@ public final class MVStore {
*/ */
private long lastStoredVersion = INITIAL_VERSION; private long lastStoredVersion = INITIAL_VERSION;
/**
* Oldest store version in use. All version beyond this can be safely dropped
*/
private final AtomicLong oldestVersionToKeep = new AtomicLong(); private final AtomicLong oldestVersionToKeep = new AtomicLong();
/**
* Collection of all versions used by currently open transactions.
*/
private final Deque<TxCounter> versions = new LinkedList<>(); private final Deque<TxCounter> versions = new LinkedList<>();
/**
* Counter of open transactions for the latest (current) store version
*/
private volatile TxCounter currentTxCounter = new TxCounter(currentVersion); private volatile TxCounter currentTxCounter = new TxCounter(currentVersion);
/** /**
...@@ -2819,6 +2830,11 @@ public final class MVStore { ...@@ -2819,6 +2830,11 @@ public final class MVStore {
setOldestVersionToKeep(txCounter != null ? txCounter.version : currentTxCounter.version); setOldestVersionToKeep(txCounter != null ? txCounter.version : currentTxCounter.version);
} }
/**
* Class TxCounter is a simple data structure to hold version of the store
* along with the counter of open transactions,
* which are still operating on this version.
*/
public static final class TxCounter { public static final class TxCounter {
public final long version; public final long version;
public final AtomicInteger counter = new AtomicInteger(); public final AtomicInteger counter = new AtomicInteger();
......
...@@ -65,6 +65,18 @@ public abstract class Page implements Cloneable ...@@ -65,6 +65,18 @@ public abstract class Page implements Cloneable
*/ */
private volatile boolean removedInMemory; private volatile boolean removedInMemory;
/**
* The estimated number of bytes used per page object.
*/
private static final int PAGE_MEMORY = Constants.MEMORY_OBJECT + 2 * Constants.MEMORY_POINTER + Constants.MEMORY_ARRAY + 17;
protected static final int PAGE_NODE_MEMORY = PAGE_MEMORY + Constants.MEMORY_POINTER + 8 + Constants.MEMORY_ARRAY;
protected static final int PAGE_LEAF_MEMORY = PAGE_MEMORY + Constants.MEMORY_POINTER + Constants.MEMORY_ARRAY;
/**
* The estimated number of bytes used per child entry.
*/
protected static final int PAGE_MEMORY_CHILD = Constants.MEMORY_POINTER + 16; // 16 = two longs
/** /**
* An empty object array. * An empty object array.
*/ */
...@@ -100,13 +112,13 @@ public abstract class Page implements Cloneable ...@@ -100,13 +112,13 @@ public abstract class Page implements Cloneable
*/ */
static Page createEmptyLeaf(MVMap<?, ?> map) { static Page createEmptyLeaf(MVMap<?, ?> map) {
Page page = new Leaf(map, EMPTY_OBJECT_ARRAY, EMPTY_OBJECT_ARRAY); Page page = new Leaf(map, EMPTY_OBJECT_ARRAY, EMPTY_OBJECT_ARRAY);
page.initMemoryAccount(DataUtils.PAGE_LEAF_EMPTY_MEMORY); page.initMemoryAccount(PAGE_LEAF_MEMORY);
return page; return page;
} }
public static Page createEmptyNode(MVMap<?, ?> map) { public static Page createEmptyNode(MVMap<?, ?> map) {
Page page = new NonLeaf(map, EMPTY_OBJECT_ARRAY, SINGLE_EMPTY, 0); Page page = new NonLeaf(map, EMPTY_OBJECT_ARRAY, SINGLE_EMPTY, 0);
page.initMemoryAccount(DataUtils.PAGE_NODE_EMPTY_MEMORY); page.initMemoryAccount(PAGE_NODE_MEMORY);
return page; return page;
} }
...@@ -775,7 +787,7 @@ public abstract class Page implements Cloneable ...@@ -775,7 +787,7 @@ public abstract class Page implements Cloneable
protected void recalculateMemory() { protected void recalculateMemory() {
assert isPersistent(); assert isPersistent();
int mem = Constants.MEMORY_ARRAY; int mem = 0;
DataType keyType = map.getKeyType(); DataType keyType = map.getKeyType();
for (Object key : keys) { for (Object key : keys) {
mem += Constants.MEMORY_POINTER + keyType.getMemory(key); mem += Constants.MEMORY_POINTER + keyType.getMemory(key);
...@@ -999,7 +1011,7 @@ public abstract class Page implements Cloneable ...@@ -999,7 +1011,7 @@ public abstract class Page implements Cloneable
totalCount += childPage.getTotalCount(); totalCount += childPage.getTotalCount();
if (isPersistent()) { if (isPersistent()) {
addMemory(Constants.MEMORY_POINTER + DataUtils.PAGE_MEMORY_CHILD); addMemory(PAGE_MEMORY_CHILD);
} }
} }
...@@ -1008,7 +1020,7 @@ public abstract class Page implements Cloneable ...@@ -1008,7 +1020,7 @@ public abstract class Page implements Cloneable
int childCount = getRawChildPageCount(); int childCount = getRawChildPageCount();
super.remove(index); super.remove(index);
if(isPersistent()) { if(isPersistent()) {
addMemory(-(Constants.MEMORY_POINTER + DataUtils.PAGE_MEMORY_CHILD)); addMemory(-PAGE_MEMORY_CHILD);
} }
totalCount -= children[index].count; totalCount -= children[index].count;
PageReference newChildren[] = new PageReference[childCount - 1]; PageReference newChildren[] = new PageReference[childCount - 1];
...@@ -1116,8 +1128,8 @@ public abstract class Page implements Cloneable ...@@ -1116,8 +1128,8 @@ public abstract class Page implements Cloneable
@Override @Override
protected void recalculateMemory() { protected void recalculateMemory() {
super.recalculateMemory(); super.recalculateMemory();
int mem = DataUtils.PAGE_NODE_MEMORY + Constants.MEMORY_ARRAY + int mem = PAGE_NODE_MEMORY +
getRawChildPageCount() * (Constants.MEMORY_POINTER + DataUtils.PAGE_MEMORY_CHILD); getRawChildPageCount() * PAGE_MEMORY_CHILD;
addMemory(mem); addMemory(mem);
} }
...@@ -1310,7 +1322,7 @@ public abstract class Page implements Cloneable ...@@ -1310,7 +1322,7 @@ public abstract class Page implements Cloneable
@Override @Override
protected void recalculateMemory() { protected void recalculateMemory() {
super.recalculateMemory(); super.recalculateMemory();
int mem = DataUtils.PAGE_LEAF_MEMORY + Constants.MEMORY_ARRAY; int mem = PAGE_LEAF_MEMORY;
DataType valueType = map.getValueType(); DataType valueType = map.getValueType();
for (Object value : values) { for (Object value : values) {
mem += valueType.getMemory(value); mem += valueType.getMemory(value);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论