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 org.tikv.common.util.Pair;
22  import org.tikv.kvproto.Metapb;
23  
24  public class RequestKeyV1TxnCodec extends RequestKeyV1Codec implements RequestKeyCodec {
25    public RequestKeyV1TxnCodec() {}
26  
27    @Override
28    public ByteString encodePdQuery(ByteString key) {
29      return CodecUtils.encode(key);
30    }
31  
32    @Override
33    public Pair<ByteString, ByteString> encodePdQueryRange(ByteString start, ByteString end) {
34      if (!start.isEmpty()) {
35        start = CodecUtils.encode(start);
36      }
37  
38      if (!end.isEmpty()) {
39        end = CodecUtils.encode(end);
40      }
41  
42      return Pair.create(start, end);
43    }
44  
45    @Override
46    public Metapb.Region decodeRegion(Metapb.Region region) {
47      Metapb.Region.Builder builder = Metapb.Region.newBuilder().mergeFrom(region);
48      ByteString start = region.getStartKey();
49      ByteString end = region.getEndKey();
50  
51      if (!start.isEmpty()) {
52        start = CodecUtils.decode(start);
53      }
54  
55      if (!end.isEmpty()) {
56        end = CodecUtils.decode(end);
57      }
58  
59      return builder.setStartKey(start).setEndKey(end).build();
60    }
61  }