Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix key incorrect next() meaning #648

Merged
merged 1 commit into from Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 12 additions & 5 deletions tikv-client/src/main/java/com/pingcap/tikv/key/Key.java
Expand Up @@ -108,16 +108,23 @@ public String toString() {
}

/**
* The next key for bytes domain It first plus one at LSB and if LSB overflows, a zero byte is
* appended at the end Original bytes will be reused if possible
* Next key simply append a zero byte to previous key.
*
* @return encoded results
* @return next key with a zero byte appended
*/
public Key next() {
return toRawKey(nextValue(value));
return toRawKey(Arrays.copyOf(value, value.length + 1));
}

static byte[] nextValue(byte[] value) {
/**
* The prefixNext key for bytes domain
*
* <p>It first plus one at LSB and if LSB overflows, a zero byte is appended at the end Original
* bytes will be reused if possible
*
* @return encoded results
*/
static byte[] prefixNext(byte[] value) {
int i;
byte[] newVal = Arrays.copyOf(value, value.length);
for (i = newVal.length - 1; i >= 0; i--) {
Expand Down
2 changes: 1 addition & 1 deletion tikv-client/src/main/java/com/pingcap/tikv/key/RowKey.java
Expand Up @@ -80,7 +80,7 @@ private static byte[] encode(long tableId, long handle) {
}

private static byte[] encodeBeyondMaxHandle(long tableId) {
return nextValue(encode(tableId, Long.MAX_VALUE));
return prefixNext(encode(tableId, Long.MAX_VALUE));
}

@Override
Expand Down
9 changes: 5 additions & 4 deletions tikv-client/src/main/java/com/pingcap/tikv/key/TypedKey.java
Expand Up @@ -44,8 +44,9 @@ public static TypedKey toTypedKey(Object val, DataType type) {
}

/**
* Map a typed value into TypedKey, only encoding first prefixLength bytes When prefixLength is
* DataType.UNSPECIFIED_LEN, encode full length of value
* Map a typed value into TypedKey, only encoding first prefixLength bytes
*
* <p>When prefixLength is DataType.UNSPECIFIED_LEN, encode full length of value
*
* @param val value
* @param type type of value
Expand All @@ -66,9 +67,9 @@ private static byte[] encodeKey(Object val, DataType type, int prefixLength) {
public TypedKey next(int prefixLength) {
Object val = getValue();
if (val instanceof String) {
return toTypedKey(nextValue(((String) val).getBytes()), type, prefixLength);
return toTypedKey(prefixNext(((String) val).getBytes()), type, prefixLength);
} else if (val instanceof byte[]) {
return toTypedKey(nextValue(((byte[]) val)), type, prefixLength);
return toTypedKey(prefixNext(((byte[]) val)), type, prefixLength);
} else {
throw new TypeException(
"Type for TypedKey in next() function must be either String or Byte array");
Expand Down
Expand Up @@ -25,6 +25,7 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Range;
import com.google.protobuf.ByteString;
import com.pingcap.tikv.codec.KeyUtils;
import com.pingcap.tikv.expression.ColumnRef;
import com.pingcap.tikv.expression.Constant;
import com.pingcap.tikv.expression.Expression;
Expand Down Expand Up @@ -147,7 +148,7 @@ public void buildIndexScanKeyRangeTest() {
ByteString.copyFrom(
new byte[] {
116, -128, 0, 0, 0, 0, 0, 0, 6, 95, 105, -128, 0, 0, 0, 0, 0, 0, 5, 3, -128, 0, 0, 0,
0, 0, 0, 1
0, 0, 0, 0, 0
}),
keyRange.getEnd());

Expand Down Expand Up @@ -175,7 +176,7 @@ public void buildIndexScanKeyRangeTest() {
ByteString.copyFrom(
new byte[] {
116, -128, 0, 0, 0, 0, 0, 0, 6, 95, 105, -128, 0, 0, 0, 0, 0, 0, 5, 3, -128, 0, 0, 0,
0, 0, 0, 0, 1, 119, 116, 102, 0, 0, 0, 0, 0, -5
0, 0, 0, 0, 1, 119, 116, 102, 0, 0, 0, 0, 0, -6, 0
}),
keyRange.getEnd());
}
Expand Down