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