1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.tikv.common.apiversion;
19
20 import com.google.protobuf.ByteString;
21 import java.util.List;
22 import java.util.stream.Collectors;
23 import org.tikv.common.util.Pair;
24 import org.tikv.kvproto.Kvrpcpb.KvPair;
25 import org.tikv.kvproto.Kvrpcpb.Mutation;
26 import org.tikv.kvproto.Metapb;
27 import org.tikv.kvproto.Pdpb;
28
29 public interface RequestKeyCodec {
30 ByteString encodeKey(ByteString key);
31
32 default List<ByteString> encodeKeys(List<ByteString> keys) {
33 return keys.stream().map(this::encodeKey).collect(Collectors.toList());
34 }
35
36 default List<Mutation> encodeMutations(List<Mutation> mutations) {
37 return mutations
38 .stream()
39 .map(mut -> Mutation.newBuilder().mergeFrom(mut).setKey(encodeKey(mut.getKey())).build())
40 .collect(Collectors.toList());
41 }
42
43 ByteString decodeKey(ByteString key);
44
45 default KvPair decodeKvPair(KvPair pair) {
46 return KvPair.newBuilder().mergeFrom(pair).setKey(decodeKey(pair.getKey())).build();
47 }
48
49 default List<KvPair> decodeKvPairs(List<KvPair> pairs) {
50 return pairs.stream().map(this::decodeKvPair).collect(Collectors.toList());
51 }
52
53 Pair<ByteString, ByteString> encodeRange(ByteString start, ByteString end);
54
55 ByteString encodePdQuery(ByteString key);
56
57 Pair<ByteString, ByteString> encodePdQueryRange(ByteString start, ByteString end);
58
59 Metapb.Region decodeRegion(Metapb.Region region);
60
61 default List<Pdpb.Region> decodePdRegions(List<Pdpb.Region> regions) {
62 return regions
63 .stream()
64 .map(
65 r ->
66 Pdpb.Region.newBuilder()
67 .mergeFrom(r)
68 .setRegion(this.decodeRegion(r.getRegion()))
69 .build())
70 .collect(Collectors.toList());
71 }
72 }