提交 72e65d6f authored 作者: Andrei Tokar's avatar Andrei Tokar

consistency in using getKeyCount()

上级 f0fb7e00
...@@ -699,8 +699,8 @@ public class MVStoreTool { ...@@ -699,8 +699,8 @@ public class MVStoreTool {
@Override @Override
public void write(WriteBuffer buff, Object[] obj, int len, boolean key) { public void write(WriteBuffer buff, Object[] obj, int len, boolean key) {
for (Object o : obj) { for (int i = 0; i < len; i++) {
write(buff, o); write(buff, obj[i]);
} }
} }
...@@ -717,12 +717,9 @@ public class MVStoreTool { ...@@ -717,12 +717,9 @@ public class MVStoreTool {
@Override @Override
public void read(ByteBuffer buff, Object[] obj, int len, boolean key) { 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); obj[i] = read(buff);
} }
} }
} }
} }
...@@ -468,7 +468,7 @@ public abstract class Page implements Cloneable ...@@ -468,7 +468,7 @@ public abstract class Page implements Cloneable
* @return the value or null * @return the value or null
*/ */
int binarySearch(Object key) { int binarySearch(Object key) {
int low = 0, high = keys.length - 1; int low = 0, high = getKeyCount() - 1;
// the cached index minus one, so that // the cached index minus one, so that
// for the first time (when cachedCompare is 0), // for the first time (when cachedCompare is 0),
// the default value is used // the default value is used
...@@ -550,7 +550,7 @@ public abstract class Page implements Cloneable ...@@ -550,7 +550,7 @@ public abstract class Page implements Cloneable
public abstract long getTotalCount(); 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 * @param index the child index
* @return the descendant count * @return the descendant count
...@@ -621,7 +621,7 @@ public abstract class Page implements Cloneable ...@@ -621,7 +621,7 @@ public abstract class Page implements Cloneable
final void insertKey(int index, Object key) { final void insertKey(int index, Object key) {
int keyCount = getKeyCount(); int keyCount = getKeyCount();
assert index <= keyCount : index + " > " + keyCount; assert index <= keyCount : index + " > " + keyCount;
Object[] newKeys = new Object[keyCount + 1]; Object[] newKeys = createKeyStorage(keyCount + 1);
DataUtils.copyWithGap(keys, newKeys, keyCount, index); DataUtils.copyWithGap(keys, newKeys, keyCount, index);
keys = newKeys; keys = newKeys;
...@@ -647,7 +647,7 @@ public abstract class Page implements Cloneable ...@@ -647,7 +647,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 = createKeyStorage(keyCount - 1);
DataUtils.copyExcept(keys, newKeys, keyCount, index); DataUtils.copyExcept(keys, newKeys, keyCount, index);
keys = newKeys; keys = newKeys;
} }
...@@ -661,7 +661,7 @@ public abstract class Page implements Cloneable ...@@ -661,7 +661,7 @@ public abstract class Page implements Cloneable
private void read(ByteBuffer buff, int chunkId) { private void read(ByteBuffer buff, int chunkId) {
int pageLength = buff.remaining() + 4; // size of int, since we've read page length already int pageLength = buff.remaining() + 4; // size of int, since we've read page length already
int len = DataUtils.readVarInt(buff); int len = DataUtils.readVarInt(buff);
keys = new Object[len]; keys = createKeyStorage(len);
int type = buff.get(); int type = buff.get();
if(isLeaf() != ((type & 1) == PAGE_TYPE_LEAF)) { if(isLeaf() != ((type & 1) == PAGE_TYPE_LEAF)) {
throw DataUtils.newIllegalStateException( throw DataUtils.newIllegalStateException(
...@@ -728,7 +728,7 @@ public abstract class Page implements Cloneable ...@@ -728,7 +728,7 @@ public abstract class Page implements Cloneable
buff.put((byte) type); buff.put((byte) type);
writeChildren(buff, true); writeChildren(buff, true);
int compressStart = buff.position(); int compressStart = buff.position();
map.getKeyType().write(buff, keys, getKeyCount(), true); map.getKeyType().write(buff, keys, len, true);
writeValues(buff); writeValues(buff);
MVStore store = map.getStore(); MVStore store = map.getStore();
int expLen = buff.position() - compressStart; int expLen = buff.position() - compressStart;
...@@ -889,10 +889,11 @@ public abstract class Page implements Cloneable ...@@ -889,10 +889,11 @@ public abstract class Page implements Cloneable
* @return memory in bytes * @return memory in bytes
*/ */
protected int calculateMemory() { protected int calculateMemory() {
int mem = keys.length * MEMORY_POINTER; int keyCount = getKeyCount();
int mem = keyCount * MEMORY_POINTER;
DataType keyType = map.getKeyType(); DataType keyType = map.getKeyType();
for (Object key : keys) { for (int i = 0; i < keyCount; i++) {
mem += keyType.getMemory(key); mem += keyType.getMemory(keys[i]);
} }
return mem; return mem;
} }
...@@ -1472,6 +1473,7 @@ public abstract class Page implements Cloneable ...@@ -1472,6 +1473,7 @@ public abstract class Page implements Cloneable
long getCounts(int index) { long getCounts(int index) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override @Override
public void setChild(int index, Page c) { public void setChild(int index, Page c) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
...@@ -1510,6 +1512,7 @@ public abstract class Page implements Cloneable ...@@ -1510,6 +1512,7 @@ public abstract class Page implements Cloneable
} }
} }
} }
@Override @Override
public void insertNode(int index, Object key, Page childPage) { public void insertNode(int index, Object key, Page childPage) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
...@@ -1573,11 +1576,11 @@ public abstract class Page implements Cloneable ...@@ -1573,11 +1576,11 @@ public abstract class Page implements Cloneable
@Override @Override
protected int calculateMemory() { protected int calculateMemory() {
int mem = super.calculateMemory() + PAGE_LEAF_MEMORY + int keyCount = getKeyCount();
values.length * MEMORY_POINTER; int mem = super.calculateMemory() + PAGE_LEAF_MEMORY + keyCount * MEMORY_POINTER;
DataType valueType = map.getValueType(); DataType valueType = map.getValueType();
for (Object value : values) { for (int i = 0; i < keyCount; i++) {
mem += valueType.getMemory(value); mem += valueType.getMemory(values[i]);
} }
return mem; return mem;
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论