提交 3b16f12f authored 作者: Andrei Tokar's avatar Andrei Tokar

cherry-pick from fork-join

上级 5c4719de
...@@ -993,7 +993,7 @@ public class MVMap<K, V> extends AbstractMap<K, V> ...@@ -993,7 +993,7 @@ public class MVMap<K, V> extends AbstractMap<K, V>
* @param memory the number of bytes used for this page * @param memory the number of bytes used for this page
*/ */
protected final void removePage(long pos, int memory) { protected final void removePage(long pos, int memory) {
store.removePage(this, pos, memory); store.removePage(pos, memory);
} }
/** /**
...@@ -1190,10 +1190,10 @@ public class MVMap<K, V> extends AbstractMap<K, V> ...@@ -1190,10 +1190,10 @@ public class MVMap<K, V> extends AbstractMap<K, V>
int attempt = 0; int attempt = 0;
int keyCount; int keyCount;
while((keyCount = rootReference.getAppendCounter()) > 0) { while((keyCount = rootReference.getAppendCounter()) > 0) {
Page page = Page.create(this, Page page = Page.createLeaf(this,
Arrays.copyOf(keysBuffer, keyCount), Arrays.copyOf(keysBuffer, keyCount),
Arrays.copyOf(valuesBuffer, keyCount), Arrays.copyOf(valuesBuffer, keyCount),
null, keyCount, 0); 0);
CursorPos pos = rootReference.root.getAppendCursorPos(null); CursorPos pos = rootReference.root.getAppendCursorPos(null);
assert page.map == this; assert page.map == this;
assert pos != null; assert pos != null;
...@@ -1215,7 +1215,7 @@ public class MVMap<K, V> extends AbstractMap<K, V> ...@@ -1215,7 +1215,7 @@ public class MVMap<K, V> extends AbstractMap<K, V>
Page.PageReference children[] = new Page.PageReference[] { Page.PageReference children[] = new Page.PageReference[] {
new Page.PageReference(p), new Page.PageReference(p),
new Page.PageReference(page)}; new Page.PageReference(page)};
p = Page.create(this, keys, null, children, p.getTotalCount() + page.getTotalCount(), 0); p = Page.createNode(this, keys, children, p.getTotalCount() + page.getTotalCount(), 0);
} }
break; break;
} }
...@@ -1784,7 +1784,7 @@ public class MVMap<K, V> extends AbstractMap<K, V> ...@@ -1784,7 +1784,7 @@ public class MVMap<K, V> extends AbstractMap<K, V>
new Page.PageReference(p), new Page.PageReference(p),
new Page.PageReference(split) new Page.PageReference(split)
}; };
p = Page.create(this, keys, null, children, totalCount, 0); p = Page.createNode(this, keys, children, totalCount, 0);
break; break;
} }
Page c = p; Page c = p;
......
...@@ -1412,9 +1412,7 @@ public class MVStore { ...@@ -1412,9 +1412,7 @@ public class MVStore {
} }
public Set<Integer> getReferenced() { public Set<Integer> getReferenced() {
Set<Integer> set = new HashSet<>(); return new HashSet<>(referencedChunks.keySet());
set.addAll(referencedChunks.keySet());
return set;
} }
public void visit(Page page, ThreadPoolExecutor executorService, AtomicInteger executingThreadCounter) { public void visit(Page page, ThreadPoolExecutor executorService, AtomicInteger executingThreadCounter) {
...@@ -1426,7 +1424,8 @@ public class MVStore { ...@@ -1426,7 +1424,8 @@ public class MVStore {
if (count == 0) { if (count == 0) {
return; return;
} }
final ChunkIdsCollector childCollector = new ChunkIdsCollector(this); ChunkIdsCollector childCollector = DataUtils.isPageSaved(pos) && cacheChunkRef != null ?
new ChunkIdsCollector(this) : this;
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
Page childPage = page.getChildPageIfLoaded(i); Page childPage = page.getChildPageIfLoaded(i);
if (childPage != null) { if (childPage != null) {
...@@ -1436,7 +1435,7 @@ public class MVStore { ...@@ -1436,7 +1435,7 @@ public class MVStore {
} }
} }
// and cache resulting set of chunk ids // and cache resulting set of chunk ids
if (DataUtils.isPageSaved(pos) && cacheChunkRef != null) { if (childCollector != this) {
int[] chunkIds = childCollector.getChunkIds(); int[] chunkIds = childCollector.getChunkIds();
cacheChunkRef.put(pos, chunkIds, Constants.MEMORY_ARRAY + 4 * chunkIds.length); cacheChunkRef.put(pos, chunkIds, Constants.MEMORY_ARRAY + 4 * chunkIds.length);
} }
...@@ -1450,7 +1449,7 @@ public class MVStore { ...@@ -1450,7 +1449,7 @@ public class MVStore {
if (DataUtils.getPageType(pos) == DataUtils.PAGE_TYPE_LEAF) { if (DataUtils.getPageType(pos) == DataUtils.PAGE_TYPE_LEAF) {
return; return;
} }
int chunkIds[]; int[] chunkIds;
if (cacheChunkRef != null && (chunkIds = cacheChunkRef.get(pos)) != null) { if (cacheChunkRef != null && (chunkIds = cacheChunkRef.get(pos)) != null) {
// there is a cached set of chunk ids for this position // there is a cached set of chunk ids for this position
for (int chunkId : chunkIds) { for (int chunkId : chunkIds) {
...@@ -1490,7 +1489,7 @@ public class MVStore { ...@@ -1490,7 +1489,7 @@ public class MVStore {
} }
private int[] getChunkIds() { private int[] getChunkIds() {
int chunkIds[] = new int[referencedChunks.size()]; int[] chunkIds = new int[referencedChunks.size()];
int index = 0; int index = 0;
for (Integer chunkId : referencedChunks.keySet()) { for (Integer chunkId : referencedChunks.keySet()) {
chunkIds[index++] = chunkId; chunkIds[index++] = chunkId;
...@@ -2070,11 +2069,10 @@ public class MVStore { ...@@ -2070,11 +2069,10 @@ public class MVStore {
/** /**
* Remove a page. * Remove a page.
* *
* @param map the map the page belongs to
* @param pos the position of the page * @param pos the position of the page
* @param memory the memory usage * @param memory the memory usage
*/ */
void removePage(MVMap<?, ?> map, long pos, int memory) { void removePage(long pos, int memory) {
// we need to keep temporary pages, // we need to keep temporary pages,
// to support reading old versions and rollback // to support reading old versions and rollback
if (!DataUtils.isPageSaved(pos)) { if (!DataUtils.isPageSaved(pos)) {
...@@ -2086,19 +2084,6 @@ public class MVStore { ...@@ -2086,19 +2084,6 @@ public class MVStore {
return; return;
} }
// This could result in a cache miss if the operation is rolled back,
// but we don't optimize for rollback.
// We could also keep the page in the cache, as somebody
// could still read it (reading the old version).
/*
if (cache != null) {
if (DataUtils.getPageType(pos) == DataUtils.PAGE_TYPE_LEAF) {
// keep nodes in the cache, because they are still used for
// garbage collection
cache.remove(pos);
}
}
*/
int chunkId = DataUtils.getPageChunkId(pos); int chunkId = DataUtils.getPageChunkId(pos);
// synchronize, because pages could be freed concurrently // synchronize, because pages could be freed concurrently
synchronized (freedPageSpace) { synchronized (freedPageSpace) {
......
...@@ -131,7 +131,7 @@ public abstract class Page implements Cloneable ...@@ -131,7 +131,7 @@ public abstract class Page implements Cloneable
memory = source.memory; memory = source.memory;
} }
Page(MVMap<?, ?> map, Object keys[]) { Page(MVMap<?, ?> map, Object[] keys) {
this.map = map; this.map = map;
this.keys = keys; this.keys = keys;
} }
...@@ -143,37 +143,46 @@ public abstract class Page implements Cloneable ...@@ -143,37 +143,46 @@ public abstract class Page implements Cloneable
* @return the new page * @return the new page
*/ */
static Page createEmptyLeaf(MVMap<?, ?> map) { static Page createEmptyLeaf(MVMap<?, ?> map) {
Page page = new Leaf(map, EMPTY_OBJECT_ARRAY, EMPTY_OBJECT_ARRAY); return createLeaf(map, EMPTY_OBJECT_ARRAY, EMPTY_OBJECT_ARRAY, PAGE_LEAF_MEMORY);
page.initMemoryAccount(PAGE_LEAF_MEMORY);
return page;
} }
public static Page createEmptyNode(MVMap<?, ?> map) { static Page createEmptyNode(MVMap<?, ?> map) {
Page page = new NonLeaf(map, EMPTY_OBJECT_ARRAY, SINGLE_EMPTY, 0); return createNode(map, EMPTY_OBJECT_ARRAY, SINGLE_EMPTY, 0,
page.initMemoryAccount(PAGE_NODE_MEMORY + PAGE_NODE_MEMORY + MEMORY_POINTER + PAGE_MEMORY_CHILD); // there is always one child
MEMORY_POINTER + PAGE_MEMORY_CHILD); // there is always one child
return page;
} }
/** /**
* Create a new page. The arrays are not cloned. * Create a new non-leaf page. The arrays are not cloned.
* *
* @param map the map * @param map the map
* @param keys the keys * @param keys the keys
* @param values the values
* @param children the child page positions * @param children the child page positions
* @param totalCount the total number of keys * @param totalCount the total number of keys
* @param memory the memory used in bytes * @param memory the memory used in bytes
* @return the page * @return the page
*/ */
public static Page create(MVMap<?, ?> map, public static Page createNode(MVMap<?, ?> map, Object[] keys, PageReference[] children,
Object[] keys, Object[] values, PageReference[] children,
long totalCount, int memory) { long totalCount, int memory) {
assert keys != null; assert keys != null;
Page p = children == null ? new Leaf(map, keys, values) : Page page = new NonLeaf(map, keys, children, totalCount);
new NonLeaf(map, keys, children, totalCount); page.initMemoryAccount(memory);
p.initMemoryAccount(memory); return page;
return p; }
/**
* Create a new leaf page. The arrays are not cloned.
*
* @param map the map
* @param keys the keys
* @param values the values
* @param memory the memory used in bytes
* @return the page
*/
public static Page createLeaf(MVMap<?, ?> map, Object[] keys, Object[] values, int memory) {
assert keys != null;
Page page = new Leaf(map, keys, values);
page.initMemoryAccount(memory);
return page;
} }
private void initMemoryAccount(int memoryCount) { private void initMemoryAccount(int memoryCount) {
...@@ -532,8 +541,8 @@ public abstract class Page implements Cloneable ...@@ -532,8 +541,8 @@ public abstract class Page implements Cloneable
final Object[] splitKeys(int aCount, int bCount) { final Object[] splitKeys(int aCount, int bCount) {
assert aCount + bCount <= getKeyCount(); assert aCount + bCount <= getKeyCount();
Object aKeys[] = createKeyStorage(aCount); Object[] aKeys = createKeyStorage(aCount);
Object bKeys[] = createKeyStorage(bCount); Object[] bKeys = createKeyStorage(bCount);
System.arraycopy(keys, 0, aKeys, 0, aCount); System.arraycopy(keys, 0, aKeys, 0, aCount);
System.arraycopy(keys, getKeyCount() - bCount, bKeys, 0, bCount); System.arraycopy(keys, getKeyCount() - bCount, bKeys, 0, bCount);
keys = aKeys; keys = aKeys;
...@@ -639,7 +648,7 @@ public abstract class Page implements Cloneable ...@@ -639,7 +648,7 @@ public abstract class Page implements Cloneable
Object old = getKey(index); Object old = getKey(index);
addMemory(-MEMORY_POINTER - keyType.getMemory(old)); addMemory(-MEMORY_POINTER - keyType.getMemory(old));
} }
Object newKeys[] = new Object[keyCount - 1]; Object[] newKeys = new Object[keyCount - 1];
DataUtils.copyExcept(keys, newKeys, keyCount, index); DataUtils.copyExcept(keys, newKeys, keyCount, index);
keys = newKeys; keys = newKeys;
} }
...@@ -873,7 +882,7 @@ public abstract class Page implements Cloneable ...@@ -873,7 +882,7 @@ public abstract class Page implements Cloneable
memory += mem; memory += mem;
} }
protected final void recalculateMemory() { final void recalculateMemory() {
assert isPersistent(); assert isPersistent();
memory = calculateMemory(); memory = calculateMemory();
} }
...@@ -1005,13 +1014,13 @@ public abstract class Page implements Cloneable ...@@ -1005,13 +1014,13 @@ public abstract class Page implements Cloneable
super(map); super(map);
} }
private NonLeaf(MVMap<?, ?> map, NonLeaf source, PageReference children[], long totalCount) { private NonLeaf(MVMap<?, ?> map, NonLeaf source, PageReference[] children, long totalCount) {
super(map, source); super(map, source);
this.children = children; this.children = children;
this.totalCount = totalCount; this.totalCount = totalCount;
} }
NonLeaf(MVMap<?, ?> map, Object keys[], PageReference children[], long totalCount) { NonLeaf(MVMap<?, ?> map, Object[] keys, PageReference[] children, long totalCount) {
super(map, keys); super(map, keys);
this.children = children; this.children = children;
this.totalCount = totalCount; this.totalCount = totalCount;
...@@ -1058,11 +1067,10 @@ public abstract class Page implements Cloneable ...@@ -1058,11 +1067,10 @@ public abstract class Page implements Cloneable
} }
@Override @Override
@SuppressWarnings("SuspiciousSystemArraycopy")
public Page split(int at) { public Page split(int at) {
assert !isSaved(); assert !isSaved();
int b = getKeyCount() - at; int b = getKeyCount() - at;
Object bKeys[] = splitKeys(at, b - 1); Object[] bKeys = splitKeys(at, b - 1);
PageReference[] aChildren = new PageReference[at + 1]; PageReference[] aChildren = new PageReference[at + 1];
PageReference[] bChildren = new PageReference[b]; PageReference[] bChildren = new PageReference[b];
System.arraycopy(children, 0, aChildren, 0, at + 1); System.arraycopy(children, 0, aChildren, 0, at + 1);
...@@ -1078,7 +1086,7 @@ public abstract class Page implements Cloneable ...@@ -1078,7 +1086,7 @@ public abstract class Page implements Cloneable
for (PageReference x : bChildren) { for (PageReference x : bChildren) {
t += x.count; t += x.count;
} }
Page newPage = create(map, bKeys, null, bChildren, t, 0); Page newPage = createNode(map, bKeys, bChildren, t, 0);
if(isPersistent()) { if(isPersistent()) {
recalculateMemory(); recalculateMemory();
} }
...@@ -1132,7 +1140,7 @@ public abstract class Page implements Cloneable ...@@ -1132,7 +1140,7 @@ public abstract class Page implements Cloneable
int childCount = getRawChildPageCount(); int childCount = getRawChildPageCount();
insertKey(index, key); insertKey(index, key);
PageReference newChildren[] = new PageReference[childCount + 1]; PageReference[] newChildren = new PageReference[childCount + 1];
DataUtils.copyWithGap(children, newChildren, childCount, index); DataUtils.copyWithGap(children, newChildren, childCount, index);
children = newChildren; children = newChildren;
children[index] = new PageReference(childPage); children[index] = new PageReference(childPage);
...@@ -1151,7 +1159,7 @@ public abstract class Page implements Cloneable ...@@ -1151,7 +1159,7 @@ public abstract class Page implements Cloneable
addMemory(-MEMORY_POINTER - PAGE_MEMORY_CHILD); addMemory(-MEMORY_POINTER - PAGE_MEMORY_CHILD);
} }
totalCount -= children[index].count; totalCount -= children[index].count;
PageReference newChildren[] = new PageReference[childCount - 1]; PageReference[] newChildren = new PageReference[childCount - 1];
DataUtils.copyExcept(children, newChildren, childCount, index); DataUtils.copyExcept(children, newChildren, childCount, index);
children = newChildren; children = newChildren;
} }
...@@ -1190,7 +1198,7 @@ public abstract class Page implements Cloneable ...@@ -1190,7 +1198,7 @@ public abstract class Page implements Cloneable
protected void readPayLoad(ByteBuffer buff) { protected void readPayLoad(ByteBuffer buff) {
int keyCount = getKeyCount(); int keyCount = getKeyCount();
children = new PageReference[keyCount + 1]; children = new PageReference[keyCount + 1];
long p[] = new long[keyCount + 1]; long[] p = new long[keyCount + 1];
for (int i = 0; i <= keyCount; i++) { for (int i = 0; i <= keyCount; i++) {
p[i] = buff.getLong(); p[i] = buff.getLong();
} }
...@@ -1282,7 +1290,7 @@ public abstract class Page implements Cloneable ...@@ -1282,7 +1290,7 @@ public abstract class Page implements Cloneable
/** /**
* The storage for values. * The storage for values.
*/ */
private Object values[]; private Object[] values;
Leaf(MVMap<?, ?> map) { Leaf(MVMap<?, ?> map) {
super(map); super(map);
...@@ -1293,7 +1301,7 @@ public abstract class Page implements Cloneable ...@@ -1293,7 +1301,7 @@ public abstract class Page implements Cloneable
this.values = source.values; this.values = source.values;
} }
Leaf(MVMap<?, ?> map, Object keys[], Object values[]) { Leaf(MVMap<?, ?> map, Object[] keys, Object[] values) {
super(map, keys); super(map, keys);
this.values = values; this.values = values;
} }
...@@ -1327,19 +1335,18 @@ public abstract class Page implements Cloneable ...@@ -1327,19 +1335,18 @@ public abstract class Page implements Cloneable
} }
@Override @Override
@SuppressWarnings("SuspiciousSystemArraycopy")
public Page split(int at) { public Page split(int at) {
assert !isSaved(); assert !isSaved();
int b = getKeyCount() - at; int b = getKeyCount() - at;
Object bKeys[] = splitKeys(at, b); Object[] bKeys = splitKeys(at, b);
Object bValues[] = createValueStorage(b); Object[] bValues = createValueStorage(b);
if(values != null) { if(values != null) {
Object aValues[] = createValueStorage(at); Object[] aValues = createValueStorage(at);
System.arraycopy(values, 0, aValues, 0, at); System.arraycopy(values, 0, aValues, 0, at);
System.arraycopy(values, at, bValues, 0, b); System.arraycopy(values, at, bValues, 0, b);
values = aValues; values = aValues;
} }
Page newPage = create(map, bKeys, bValues, null, b, 0); Page newPage = createLeaf(map, bKeys, bValues, 0);
if(isPersistent()) { if(isPersistent()) {
recalculateMemory(); recalculateMemory();
} }
...@@ -1384,7 +1391,7 @@ public abstract class Page implements Cloneable ...@@ -1384,7 +1391,7 @@ public abstract class Page implements Cloneable
insertKey(index, key); insertKey(index, key);
if(values != null) { if(values != null) {
Object newValues[] = createValueStorage(keyCount + 1); Object[] newValues = createValueStorage(keyCount + 1);
DataUtils.copyWithGap(values, newValues, keyCount, index); DataUtils.copyWithGap(values, newValues, keyCount, index);
values = newValues; values = newValues;
setValueInternal(index, value); setValueInternal(index, value);
...@@ -1407,7 +1414,7 @@ public abstract class Page implements Cloneable ...@@ -1407,7 +1414,7 @@ public abstract class Page implements Cloneable
Object old = getValue(index); Object old = getValue(index);
addMemory(-MEMORY_POINTER - map.getValueType().getMemory(old)); addMemory(-MEMORY_POINTER - map.getValueType().getMemory(old));
} }
Object newValues[] = createValueStorage(keyCount - 1); Object[] newValues = createValueStorage(keyCount - 1);
DataUtils.copyExcept(values, newValues, keyCount, index); DataUtils.copyExcept(values, newValues, keyCount, index);
values = newValues; values = newValues;
} }
......
...@@ -154,7 +154,7 @@ public final class MVRTreeMap<V> extends MVMap<SpatialKey, V> { ...@@ -154,7 +154,7 @@ public final class MVRTreeMap<V> extends MVMap<SpatialKey, V> {
new Page.PageReference(split), new Page.PageReference(split),
Page.PageReference.EMPTY Page.PageReference.EMPTY
}; };
p = Page.create(this, keys, null, children, totalCount, 0); p = Page.createNode(this, keys, children, totalCount, 0);
if(store.getFileStore() != null) { if(store.getFileStore() != null) {
store.registerUnsavedPage(p.getMemory()); store.registerUnsavedPage(p.getMemory());
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论