View Javadoc
1   /*
2    * Copyright 2022 TiKV Project Authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  }