[close #606]: change the default prefix for API v2 (#607)

This commit is contained in:
iosmanthus 2022-06-06 21:22:03 +08:00 committed by GitHub
parent 9ce52d3401
commit aacbb8c849
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 16 deletions

View File

@ -23,15 +23,15 @@ import org.tikv.kvproto.Metapb;
import org.tikv.kvproto.Metapb.Region; import org.tikv.kvproto.Metapb.Region;
public class RequestKeyV2Codec implements RequestKeyCodec { public class RequestKeyV2Codec implements RequestKeyCodec {
protected static final ByteString RAW_KEY_PREFIX = ByteString.copyFromUtf8("r"); protected static final ByteString RAW_DEFAULT_PREFIX =
protected static final ByteString RAW_END_KEY = ByteString.copyFrom(new byte[] {'r', 0, 0, 0});
ByteString.copyFrom(new byte[] {(byte) (RAW_KEY_PREFIX.toByteArray()[0] + 1)}); protected static final ByteString RAW_DEFAULT_END =
ByteString.copyFrom(new byte[] {'r', 0, 0, 1});
protected static final ByteString TXN_KEY_PREFIX = ByteString.copyFromUtf8("x"); protected static final ByteString TXN_DEFAULT_PREFIX =
protected static final ByteString TXN_END_KEY = ByteString.copyFrom(new byte[] {'x', 0, 0, 0});
ByteString.copyFrom(new byte[] {(byte) (TXN_KEY_PREFIX.toByteArray()[0] + 1)}); protected static final ByteString TXN_DEFAULT_END =
ByteString.copyFrom(new byte[] {'x', 0, 0, 1});
protected ByteString keyPrefix; protected ByteString keyPrefix;
protected ByteString infiniteEndKey; protected ByteString infiniteEndKey;
@Override @Override
@ -49,7 +49,7 @@ public class RequestKeyV2Codec implements RequestKeyCodec {
throw new IllegalArgumentException("key corrupted, wrong prefix"); throw new IllegalArgumentException("key corrupted, wrong prefix");
} }
return key.substring(1); return key.substring(keyPrefix.size());
} }
@Override @Override

View File

@ -21,7 +21,7 @@ public class RequestKeyV2RawCodec extends RequestKeyV2Codec {
public RequestKeyV2RawCodec() { public RequestKeyV2RawCodec() {
super(); super();
this.keyPrefix = RAW_KEY_PREFIX; this.keyPrefix = RAW_DEFAULT_PREFIX;
this.infiniteEndKey = RAW_END_KEY; this.infiniteEndKey = RAW_DEFAULT_END;
} }
} }

View File

@ -21,7 +21,7 @@ public class RequestKeyV2TxnCodec extends RequestKeyV2Codec {
public RequestKeyV2TxnCodec() { public RequestKeyV2TxnCodec() {
super(); super();
this.keyPrefix = TXN_KEY_PREFIX; this.keyPrefix = TXN_DEFAULT_PREFIX;
this.infiniteEndKey = TXN_END_KEY; this.infiniteEndKey = TXN_DEFAULT_END;
} }
} }

View File

@ -164,9 +164,10 @@ public class RequestKeyCodecTest {
end = ByteString.EMPTY; end = ByteString.EMPTY;
range = v2.encodeRange(start, end); range = v2.encodeRange(start, end);
assertEquals(v2.encodeKey(start), range.first); assertEquals(v2.encodeKey(start), range.first);
assertArrayEquals(
new byte[] {(byte) (v2.encodeKey(ByteString.EMPTY).byteAt(0) + 1)}, byte[] max = v2.encodeKey(ByteString.EMPTY).toByteArray();
range.second.toByteArray()); max[max.length - 1] += 1;
assertArrayEquals(max, range.second.toByteArray());
region = region =
Region.newBuilder() Region.newBuilder()