提交 c9404560 authored 作者: Thomas Mueller's avatar Thomas Mueller

MVStore: maintain separate child count field (might be removed later on)

上级 5818aa0b
...@@ -136,8 +136,10 @@ public class MVMap<K, V> extends AbstractMap<K, V> ...@@ -136,8 +136,10 @@ public class MVMap<K, V> extends AbstractMap<K, V>
long[] children = { p.getPos(), split.getPos() }; long[] children = { p.getPos(), split.getPos() };
Page[] childrenPages = { p, split }; Page[] childrenPages = { p, split };
long[] counts = { p.getTotalCount(), split.getTotalCount() }; long[] counts = { p.getTotalCount(), split.getTotalCount() };
p = Page.create(this, writeVersion, 1, p = Page.create(this, writeVersion,
keys, null, children, childrenPages, counts, totalCount, 0, 0); 1, keys, null,
2, children, childrenPages, counts,
totalCount, 0, 0);
return p; return p;
} }
......
...@@ -47,6 +47,11 @@ public class Page { ...@@ -47,6 +47,11 @@ public class Page {
* The number of keys. * The number of keys.
*/ */
private int keyCount; private int keyCount;
/**
* The number of children.
*/
private int childCount;
/** /**
* The last result of a find operation is cached. * The last result of a find operation is cached.
...@@ -109,9 +114,10 @@ public class Page { ...@@ -109,9 +114,10 @@ public class Page {
* @return the new page * @return the new page
*/ */
public static Page createEmpty(MVMap<?, ?> map, long version) { public static Page createEmpty(MVMap<?, ?> map, long version) {
return create(map, version, 0, return create(map, version,
EMPTY_OBJECT_ARRAY, EMPTY_OBJECT_ARRAY, 0, EMPTY_OBJECT_ARRAY, EMPTY_OBJECT_ARRAY,
null, null, null, 0, 0, DataUtils.PAGE_MEMORY); 0, null, null, null,
0, 0, DataUtils.PAGE_MEMORY);
} }
/** /**
...@@ -131,14 +137,15 @@ public class Page { ...@@ -131,14 +137,15 @@ public class Page {
* @return the page * @return the page
*/ */
public static Page create(MVMap<?, ?> map, long version, public static Page create(MVMap<?, ?> map, long version,
int keyCount, Object[] keys, int keyCount, Object[] keys, Object[] values,
Object[] values, long[] children, Page[] childrenPages, long[] counts, int childCount, long[] children, Page[] childrenPages, long[] counts,
long totalCount, int sharedFlags, int memory) { long totalCount, int sharedFlags, int memory) {
Page p = new Page(map, version); Page p = new Page(map, version);
// the position is 0 // the position is 0
p.keys = keys;
p.keyCount = keyCount; p.keyCount = keyCount;
p.keys = keys;
p.values = values; p.values = values;
p.childCount = childCount;
p.children = children; p.children = children;
p.childrenPages = childrenPages; p.childrenPages = childrenPages;
p.counts = counts; p.counts = counts;
...@@ -275,8 +282,8 @@ public class Page { ...@@ -275,8 +282,8 @@ public class Page {
*/ */
public Page copy(long version) { public Page copy(long version) {
Page newPage = create(map, version, Page newPage = create(map, version,
keyCount, keys, values, children, childrenPages, keyCount, keys, values,
counts, totalCount, childCount, children, childrenPages, counts, totalCount,
SHARED_KEYS | SHARED_VALUES | SHARED_CHILDREN | SHARED_COUNTS, SHARED_KEYS | SHARED_VALUES | SHARED_CHILDREN | SHARED_COUNTS,
memory); memory);
// mark the old as deleted // mark the old as deleted
...@@ -298,6 +305,9 @@ public class Page { ...@@ -298,6 +305,9 @@ public class Page {
*/ */
public int binarySearch(Object key) { public int binarySearch(Object key) {
int low = 0, high = keyCount - 1; int low = 0, high = keyCount - 1;
// the cached index minus one, so that
// for the first time (when cachedCompare is 0),
// the default value is used
int x = cachedCompare - 1; int x = cachedCompare - 1;
if (x < 0 || x > high) { if (x < 0 || x > high) {
x = (low + high) >>> 1; x = (low + high) >>> 1;
...@@ -360,8 +370,9 @@ public class Page { ...@@ -360,8 +370,9 @@ public class Page {
values = aValues; values = aValues;
sharedFlags &= ~(SHARED_KEYS | SHARED_VALUES); sharedFlags &= ~(SHARED_KEYS | SHARED_VALUES);
totalCount = a; totalCount = a;
Page newPage = create(map, version, b, Page newPage = create(map, version,
bKeys, bValues, null, null, null, b, bKeys, bValues,
0, null, null, null,
bKeys.length, 0, 0); bKeys.length, 0, 0);
memory = calculateMemory(); memory = calculateMemory();
newPage.memory = newPage.calculateMemory(); newPage.memory = newPage.calculateMemory();
...@@ -395,6 +406,7 @@ public class Page { ...@@ -395,6 +406,7 @@ public class Page {
System.arraycopy(counts, 0, aCounts, 0, a + 1); System.arraycopy(counts, 0, aCounts, 0, a + 1);
System.arraycopy(counts, a + 1, bCounts, 0, b); System.arraycopy(counts, a + 1, bCounts, 0, b);
counts = aCounts; counts = aCounts;
childCount = a + 1;
sharedFlags &= ~(SHARED_KEYS | SHARED_CHILDREN | SHARED_COUNTS); sharedFlags &= ~(SHARED_KEYS | SHARED_CHILDREN | SHARED_COUNTS);
long t = 0; long t = 0;
...@@ -406,9 +418,10 @@ public class Page { ...@@ -406,9 +418,10 @@ public class Page {
for (long x : bCounts) { for (long x : bCounts) {
t += x; t += x;
} }
Page newPage = create(map, version, b - 1, Page newPage = create(map, version,
bKeys, null, bChildren, bChildrenPages, b - 1, bKeys, null,
bCounts, t, 0, 0); b, bChildren, bChildrenPages, bCounts,
t, 0, 0);
memory = calculateMemory(); memory = calculateMemory();
newPage.memory = newPage.calculateMemory(); newPage.memory = newPage.calculateMemory();
return newPage; return newPage;
...@@ -539,7 +552,7 @@ public class Page { ...@@ -539,7 +552,7 @@ public class Page {
*/ */
void removeAllRecursive() { void removeAllRecursive() {
if (children != null) { if (children != null) {
for (int i = 0, size = children.length; i < size; i++) { for (int i = 0, size = childCount; i < size; i++) {
Page p = childrenPages[i]; Page p = childrenPages[i];
if (p != null) { if (p != null) {
p.removeAllRecursive(); p.removeAllRecursive();
...@@ -601,22 +614,25 @@ public class Page { ...@@ -601,22 +614,25 @@ public class Page {
DataUtils.copyWithGap(keys, newKeys, keyCount, index); DataUtils.copyWithGap(keys, newKeys, keyCount, index);
newKeys[index] = key; newKeys[index] = key;
keys = newKeys; keys = newKeys;
keyCount++; keyCount++;
long[] newChildren = new long[children.length + 1]; long[] newChildren = new long[childCount + 1];
DataUtils.copyWithGap(children, newChildren, children.length, index); DataUtils.copyWithGap(children, newChildren, childCount, index);
newChildren[index] = childPage.getPos(); newChildren[index] = childPage.getPos();
children = newChildren; children = newChildren;
Page[] newChildrenPages = new Page[childrenPages.length + 1]; Page[] newChildrenPages = new Page[childCount + 1];
DataUtils.copyWithGap(childrenPages, newChildrenPages, childrenPages.length, index); DataUtils.copyWithGap(childrenPages, newChildrenPages, childCount, index);
newChildrenPages[index] = childPage; newChildrenPages[index] = childPage;
childrenPages = newChildrenPages; childrenPages = newChildrenPages;
long[] newCounts = new long[counts.length + 1]; long[] newCounts = new long[childCount + 1];
DataUtils.copyWithGap(counts, newCounts, counts.length, index); DataUtils.copyWithGap(counts, newCounts, childCount, index);
newCounts[index] = childPage.totalCount; newCounts[index] = childPage.totalCount;
counts = newCounts; counts = newCounts;
childCount++;
sharedFlags &= ~(SHARED_KEYS | SHARED_CHILDREN | SHARED_COUNTS); sharedFlags &= ~(SHARED_KEYS | SHARED_CHILDREN | SHARED_COUNTS);
totalCount += childPage.totalCount; totalCount += childPage.totalCount;
...@@ -666,21 +682,23 @@ public class Page { ...@@ -666,21 +682,23 @@ public class Page {
memory -= DataUtils.PAGE_MEMORY_CHILD; memory -= DataUtils.PAGE_MEMORY_CHILD;
long countOffset = counts[index]; long countOffset = counts[index];
long[] newChildren = new long[children.length - 1]; long[] newChildren = new long[childCount - 1];
DataUtils.copyExcept(children, newChildren, children.length, index); DataUtils.copyExcept(children, newChildren, childCount, index);
children = newChildren; children = newChildren;
Page[] newChildrenPages = new Page[childrenPages.length - 1]; Page[] newChildrenPages = new Page[childCount - 1];
DataUtils.copyExcept(childrenPages, newChildrenPages, DataUtils.copyExcept(childrenPages, newChildrenPages,
childrenPages.length, index); childCount, index);
childrenPages = newChildrenPages; childrenPages = newChildrenPages;
long[] newCounts = new long[counts.length - 1]; long[] newCounts = new long[childCount - 1];
DataUtils.copyExcept(counts, newCounts, counts.length, index); DataUtils.copyExcept(counts, newCounts, childCount, index);
counts = newCounts; counts = newCounts;
sharedFlags &= ~(SHARED_CHILDREN | SHARED_COUNTS); sharedFlags &= ~(SHARED_CHILDREN | SHARED_COUNTS);
totalCount -= countOffset; totalCount -= countOffset;
childCount--;
} }
} }
...@@ -740,6 +758,7 @@ public class Page { ...@@ -740,6 +758,7 @@ public class Page {
keys[i] = k; keys[i] = k;
} }
if (node) { if (node) {
childCount = len + 1;
children = new long[len + 1]; children = new long[len + 1];
for (int i = 0; i <= len; i++) { for (int i = 0; i <= len; i++) {
children[i] = buff.getLong(); children[i] = buff.getLong();
...@@ -846,7 +865,7 @@ public class Page { ...@@ -846,7 +865,7 @@ public class Page {
return; return;
} }
if (!isLeaf()) { if (!isLeaf()) {
int len = children.length; int len = childCount;
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
Page p = childrenPages[i]; Page p = childrenPages[i];
if (p != null) { if (p != null) {
...@@ -863,7 +882,7 @@ public class Page { ...@@ -863,7 +882,7 @@ public class Page {
*/ */
void writeEnd() { void writeEnd() {
if (!isLeaf()) { if (!isLeaf()) {
int len = children.length; int len = childCount;
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
Page p = childrenPages[i]; Page p = childrenPages[i];
if (p != null) { if (p != null) {
...@@ -883,7 +902,7 @@ public class Page { ...@@ -883,7 +902,7 @@ public class Page {
} }
public int getChildPageCount() { public int getChildPageCount() {
return children.length; return childCount;
} }
@Override @Override
......
...@@ -235,8 +235,9 @@ public class MVRTreeMap<V> extends MVMap<SpatialKey, V> { ...@@ -235,8 +235,9 @@ public class MVRTreeMap<V> extends MVMap<SpatialKey, V> {
long[] children = { p.getPos(), split.getPos(), 0 }; long[] children = { p.getPos(), split.getPos(), 0 };
Page[] childrenPages = { p, split, null }; Page[] childrenPages = { p, split, null };
long[] counts = { p.getTotalCount(), split.getTotalCount(), 0 }; long[] counts = { p.getTotalCount(), split.getTotalCount(), 0 };
p = Page.create(this, v, 2, p = Page.create(this, v,
keys, null, children, childrenPages, counts, 2, keys, null,
3, children, childrenPages, counts,
totalCount, 0, 0); totalCount, 0, 0);
// now p is a node; continues // now p is a node; continues
} }
...@@ -432,8 +433,9 @@ public class MVRTreeMap<V> extends MVMap<SpatialKey, V> { ...@@ -432,8 +433,9 @@ public class MVRTreeMap<V> extends MVMap<SpatialKey, V> {
Object[] values = leaf ? new Object[4] : null; Object[] values = leaf ? new Object[4] : null;
long[] c = leaf ? null : new long[1]; long[] c = leaf ? null : new long[1];
Page[] cp = leaf ? null : new Page[1]; Page[] cp = leaf ? null : new Page[1];
return Page.create(this, writeVersion, 0, return Page.create(this, writeVersion,
new Object[4], values, c, cp, c, 0, 0, 0); 0, new Object[4], values,
leaf ? 0 : 1, c, cp, c, 0, 0, 0);
} }
private static void move(Page source, Page target, int sourceIndex) { private static void move(Page source, Page target, int sourceIndex) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论