Unverified 提交 c0d55ffd authored 作者: Andrei Tokar's avatar Andrei Tokar 提交者: GitHub

Merge pull request #1687 from h2database/mvmap-refact

MVMap minor cleanup
......@@ -505,9 +505,12 @@ public class MVStore implements AutoCloseable {
M map;
if (id >= 0) {
map = openMap(id, builder);
assert builder.getKeyType() == null || map.getKeyType().getClass().equals(builder.getKeyType().getClass());
assert builder.getValueType() == null || map.getValueType().getClass().equals(builder.getValueType().getClass());
} else {
HashMap<String, Object> c = new HashMap<>();
id = lastMapId.incrementAndGet();
assert getMap(id) == null;
c.put("id", id);
c.put("createVersion", currentVersion);
map = builder.create(this, c);
......@@ -1250,7 +1253,7 @@ public class MVStore implements AutoCloseable {
ArrayList<Page> changed = new ArrayList<>();
for (Iterator<MVMap<?, ?>> iter = maps.values().iterator(); iter.hasNext(); ) {
MVMap<?, ?> map = iter.next();
MVMap.RootReference rootReference = map.setWriteVersion(version);
RootReference rootReference = map.setWriteVersion(version);
if (rootReference == null) {
assert map.isClosed();
assert map.getVersion() < getOldestVersionToKeep();
......@@ -1289,7 +1292,7 @@ public class MVStore implements AutoCloseable {
}
}
applyFreedSpace();
MVMap.RootReference metaRootReference = meta.setWriteVersion(version);
RootReference metaRootReference = meta.setWriteVersion(version);
assert metaRootReference != null;
assert metaRootReference.version == version : metaRootReference.version + " != " + version;
metaChanged = false;
......@@ -1445,9 +1448,9 @@ public class MVStore implements AutoCloseable {
try {
ChunkIdsCollector collector = new ChunkIdsCollector(meta.getId());
long oldestVersionToKeep = getOldestVersionToKeep();
MVMap.RootReference rootReference = meta.flushAndGetRoot();
RootReference rootReference = meta.flushAndGetRoot();
if (fast) {
MVMap.RootReference previous;
RootReference previous;
while (rootReference.version >= oldestVersionToKeep && (previous = rootReference.previous) != null) {
rootReference = previous;
}
......@@ -1482,7 +1485,7 @@ public class MVStore implements AutoCloseable {
* @param inspectedRoots set of page positions for map's roots already inspected
* or null if not to be used
*/
private void inspectVersion(MVMap.RootReference rootReference, ChunkIdsCollector collector,
private void inspectVersion(RootReference rootReference, ChunkIdsCollector collector,
ThreadPoolExecutor executorService,
AtomicInteger executingThreadCounter,
Set<Long> inspectedRoots) {
......@@ -2683,7 +2686,7 @@ public class MVStore implements AutoCloseable {
DataUtils.checkArgument(map != meta,
"Removing the meta map is not allowed");
map.close();
MVMap.RootReference rootReference = map.getRoot();
RootReference rootReference = map.getRoot();
updateCounter += rootReference.updateCounter;
updateAttemptCounter += rootReference.updateAttemptCounter;
......@@ -2983,11 +2986,11 @@ public class MVStore implements AutoCloseable {
public double getUpdateFailureRatio() {
long updateCounter = this.updateCounter;
long updateAttemptCounter = this.updateAttemptCounter;
MVMap.RootReference rootReference = meta.getRoot();
RootReference rootReference = meta.getRoot();
updateCounter += rootReference.updateCounter;
updateAttemptCounter += rootReference.updateAttemptCounter;
for (MVMap<?, ?> map : maps.values()) {
MVMap.RootReference root = map.getRoot();
RootReference root = map.getRoot();
updateCounter += root.updateCounter;
updateAttemptCounter += root.updateAttemptCounter;
}
......
......@@ -699,8 +699,8 @@ public class MVStoreTool {
@Override
public void write(WriteBuffer buff, Object[] obj, int len, boolean key) {
for (Object o : obj) {
write(buff, o);
for (int i = 0; i < len; i++) {
write(buff, obj[i]);
}
}
......@@ -717,12 +717,9 @@ public class MVStoreTool {
@Override
public void read(ByteBuffer buff, Object[] obj, int len, boolean key) {
for (int i = 0; i < obj.length; i++) {
for (int i = 0; i < len; i++) {
obj[i] = read(buff);
}
}
}
}
......@@ -468,7 +468,7 @@ public abstract class Page implements Cloneable
* @return the value or null
*/
int binarySearch(Object key) {
int low = 0, high = keys.length - 1;
int low = 0, high = getKeyCount() - 1;
// the cached index minus one, so that
// for the first time (when cachedCompare is 0),
// the default value is used
......@@ -550,7 +550,7 @@ public abstract class Page implements Cloneable
public abstract long getTotalCount();
/**
* Get the descendant counts for the given child.
* Get the number of key-value pairs for a given child.
*
* @param index the child index
* @return the descendant count
......@@ -621,7 +621,7 @@ public abstract class Page implements Cloneable
final void insertKey(int index, Object key) {
int keyCount = getKeyCount();
assert index <= keyCount : index + " > " + keyCount;
Object[] newKeys = new Object[keyCount + 1];
Object[] newKeys = createKeyStorage(keyCount + 1);
DataUtils.copyWithGap(keys, newKeys, keyCount, index);
keys = newKeys;
......@@ -647,7 +647,7 @@ public abstract class Page implements Cloneable
Object old = getKey(index);
addMemory(-MEMORY_POINTER - keyType.getMemory(old));
}
Object[] newKeys = new Object[keyCount - 1];
Object[] newKeys = createKeyStorage(keyCount - 1);
DataUtils.copyExcept(keys, newKeys, keyCount, index);
keys = newKeys;
}
......@@ -661,7 +661,7 @@ public abstract class Page implements Cloneable
private void read(ByteBuffer buff, int chunkId) {
int pageLength = buff.remaining() + 4; // size of int, since we've read page length already
int len = DataUtils.readVarInt(buff);
keys = new Object[len];
keys = createKeyStorage(len);
int type = buff.get();
if(isLeaf() != ((type & 1) == PAGE_TYPE_LEAF)) {
throw DataUtils.newIllegalStateException(
......@@ -728,7 +728,7 @@ public abstract class Page implements Cloneable
buff.put((byte) type);
writeChildren(buff, true);
int compressStart = buff.position();
map.getKeyType().write(buff, keys, getKeyCount(), true);
map.getKeyType().write(buff, keys, len, true);
writeValues(buff);
MVStore store = map.getStore();
int expLen = buff.position() - compressStart;
......@@ -889,10 +889,11 @@ public abstract class Page implements Cloneable
* @return memory in bytes
*/
protected int calculateMemory() {
int mem = keys.length * MEMORY_POINTER;
int keyCount = getKeyCount();
int mem = keyCount * MEMORY_POINTER;
DataType keyType = map.getKeyType();
for (Object key : keys) {
mem += keyType.getMemory(key);
for (int i = 0; i < keyCount; i++) {
mem += keyType.getMemory(keys[i]);
}
return mem;
}
......@@ -1472,6 +1473,7 @@ public abstract class Page implements Cloneable
long getCounts(int index) {
throw new UnsupportedOperationException();
}
@Override
public void setChild(int index, Page c) {
throw new UnsupportedOperationException();
......@@ -1510,6 +1512,7 @@ public abstract class Page implements Cloneable
}
}
}
@Override
public void insertNode(int index, Object key, Page childPage) {
throw new UnsupportedOperationException();
......@@ -1573,11 +1576,11 @@ public abstract class Page implements Cloneable
@Override
protected int calculateMemory() {
int mem = super.calculateMemory() + PAGE_LEAF_MEMORY +
values.length * MEMORY_POINTER;
int keyCount = getKeyCount();
int mem = super.calculateMemory() + PAGE_LEAF_MEMORY + keyCount * MEMORY_POINTER;
DataType valueType = map.getValueType();
for (Object value : values) {
mem += valueType.getMemory(value);
for (int i = 0; i < keyCount; i++) {
mem += valueType.getMemory(values[i]);
}
return mem;
}
......
/*
* Copyright 2004-2019 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;
/**
* Class RootReference is an immutable structure to represent state of the MVMap as a whole
* (not related to a particular B-Tree node).
* Single structure would allow for non-blocking atomic state change.
* The most important part of it is a reference to the root node.
*
* @author <a href='mailto:andrei.tokar@gmail.com'>Andrei Tokar</a>
*/
public final class RootReference
{
/**
* The root page.
*/
public final Page root;
/**
* The version used for writing.
*/
public final long version;
/**
* Indicator that map is locked for update.
*/
final boolean lockedForUpdate;
/**
* Reference to the previous root in the chain.
*/
public volatile RootReference previous;
/**
* Counter for successful root updates.
*/
final long updateCounter;
/**
* Counter for attempted root updates.
*/
final long updateAttemptCounter;
/**
* Size of the occupied part of the append buffer.
*/
final byte appendCounter;
// This one is used to set root initially and for r/o snapshots
RootReference(Page root, long version) {
this.root = root;
this.version = version;
this.previous = null;
this.updateCounter = 1;
this.updateAttemptCounter = 1;
this.lockedForUpdate = false;
this.appendCounter = 0;
}
RootReference(RootReference r, Page root, long updateAttemptCounter) {
this.root = root;
this.version = r.version;
this.previous = r.previous;
this.updateCounter = r.updateCounter + 1;
this.updateAttemptCounter = r.updateAttemptCounter + updateAttemptCounter;
this.lockedForUpdate = false;
this.appendCounter = r.appendCounter;
}
// This one is used for locking
RootReference(RootReference r, int attempt) {
this.root = r.root;
this.version = r.version;
this.previous = r.previous;
this.updateCounter = r.updateCounter + 1;
this.updateAttemptCounter = r.updateAttemptCounter + attempt;
this.lockedForUpdate = true;
this.appendCounter = r.appendCounter;
}
// This one is used for unlocking
RootReference(RootReference r, Page root, int appendCounter, boolean lockedForUpdate) {
this.root = root;
this.version = r.version;
this.previous = r.previous;
this.updateCounter = r.updateCounter;
this.updateAttemptCounter = r.updateAttemptCounter;
this.lockedForUpdate = lockedForUpdate;
this.appendCounter = (byte) appendCounter;
}
// This one is used for version change
RootReference(RootReference r, long version, int attempt) {
RootReference previous = r;
RootReference tmp;
while ((tmp = previous.previous) != null && tmp.root == r.root) {
previous = tmp;
}
this.root = r.root;
this.version = version;
this.previous = previous;
this.updateCounter = r.updateCounter + 1;
this.updateAttemptCounter = r.updateAttemptCounter + attempt;
this.lockedForUpdate = r.lockedForUpdate;
this.appendCounter = r.appendCounter;
}
int getAppendCounter() {
return appendCounter & 0xff;
}
public long getTotalCount() {
return root.getTotalCount() + getAppendCounter();
}
@Override
public String toString() {
return "RootReference(" + System.identityHashCode(root) + "," + version + "," + lockedForUpdate + ")";
}
}
......@@ -13,6 +13,7 @@ import org.h2.mvstore.CursorPos;
import org.h2.mvstore.DataUtils;
import org.h2.mvstore.MVMap;
import org.h2.mvstore.Page;
import org.h2.mvstore.RootReference;
import org.h2.mvstore.type.DataType;
/**
......
......@@ -9,6 +9,7 @@ import org.h2.mvstore.Cursor;
import org.h2.mvstore.DataUtils;
import org.h2.mvstore.MVMap;
import org.h2.mvstore.Page;
import org.h2.mvstore.RootReference;
import org.h2.mvstore.type.DataType;
import org.h2.value.VersionedValue;
......@@ -100,19 +101,19 @@ public class TransactionMap<K, V> extends AbstractMap<K, V> {
// In order to get such a "snapshot", we wait for a moment of silence,
// when none of the variables concurrently changes it's value.
BitSet committingTransactions;
MVMap.RootReference mapRootReference;
MVMap.RootReference[] undoLogRootReferences;
RootReference mapRootReference;
RootReference[] undoLogRootReferences;
long undoLogSize;
do {
committingTransactions = store.committingTransactions.get();
mapRootReference = map.flushAndGetRoot();
BitSet opentransactions = store.openTransactions.get();
undoLogRootReferences = new MVMap.RootReference[opentransactions.length()];
undoLogRootReferences = new RootReference[opentransactions.length()];
undoLogSize = 0;
for (int i = opentransactions.nextSetBit(0); i >= 0; i = opentransactions.nextSetBit(i+1)) {
MVMap<Long, Object[]> undoLog = store.undoLogs[i];
if (undoLog != null) {
MVMap.RootReference rootReference = undoLog.flushAndGetRoot();
RootReference rootReference = undoLog.flushAndGetRoot();
undoLogRootReferences[i] = rootReference;
undoLogSize += rootReference.getTotalCount();
}
......@@ -156,7 +157,7 @@ public class TransactionMap<K, V> extends AbstractMap<K, V> {
} else {
// The undo logs are much smaller than the map - scan all undo logs,
// and then lookup relevant map entry.
for (MVMap.RootReference undoLogRootReference : undoLogRootReferences) {
for (RootReference undoLogRootReference : undoLogRootReferences) {
if (undoLogRootReference != null) {
Cursor<Long, Object[]> cursor = new Cursor<>(undoLogRootReference.root, null);
while (cursor.hasNext()) {
......@@ -688,7 +689,7 @@ public class TransactionMap<K, V> extends AbstractMap<K, V> {
// In order to get such a "snapshot", we wait for a moment of silence,
// when neither of the variables concurrently changes it's value.
BitSet committingTransactions;
MVMap.RootReference mapRootReference;
RootReference mapRootReference;
do {
committingTransactions = store.committingTransactions.get();
mapRootReference = map.flushAndGetRoot();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论