View Javadoc
1   /*
2    * Copyright 2021 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.util;
19  
20  import com.google.protobuf.ByteString;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  import org.tikv.common.region.TiRegion;
25  
26  /** A Batch containing the region, a list of keys and/or values to send */
27  public class Batch {
28    private final BackOffer backOffer;
29    private final TiRegion region;
30    private final List<ByteString> keys;
31    private final List<ByteString> values;
32    private final Map<ByteString, ByteString> map;
33  
34    public Batch(BackOffer backOffer, TiRegion region, List<ByteString> keys) {
35      this.backOffer = ConcreteBackOffer.create(backOffer);
36      this.region = region;
37      this.keys = keys;
38      this.values = null;
39      this.map = null;
40    }
41  
42    public Batch(
43        BackOffer backOffer, TiRegion region, List<ByteString> keys, List<ByteString> values) {
44      this.backOffer = ConcreteBackOffer.create(backOffer);
45      this.region = region;
46      this.keys = keys;
47      this.values = values;
48      this.map = toMap(keys, values);
49    }
50  
51    private Map<ByteString, ByteString> toMap(List<ByteString> keys, List<ByteString> values) {
52      assert keys.size() == values.size();
53      Map<ByteString, ByteString> kvMap = new HashMap<>();
54      for (int i = 0; i < keys.size(); i++) {
55        kvMap.put(keys.get(i), values.get(i));
56      }
57      return kvMap;
58    }
59  
60    public BackOffer getBackOffer() {
61      return ConcreteBackOffer.create(backOffer);
62    }
63  
64    public TiRegion getRegion() {
65      return region;
66    }
67  
68    public List<ByteString> getKeys() {
69      return keys;
70    }
71  
72    public List<ByteString> getValues() {
73      return values;
74    }
75  
76    public Map<ByteString, ByteString> getMap() {
77      return map;
78    }
79  }