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  package org.tikv.common.replica;
18  
19  import static com.google.common.base.MoreObjects.toStringHelper;
20  
21  import java.util.Iterator;
22  import java.util.List;
23  import org.tikv.common.region.TiStore;
24  import org.tikv.kvproto.Metapb;
25  
26  public class Region {
27    private final Metapb.Region region;
28    private final Store[] stores;
29    private Store leaderStore;
30  
31    public Region(
32        final Metapb.Region region,
33        final Metapb.Peer leader,
34        final List<Metapb.Peer> peers,
35        final List<TiStore> stores) {
36      this.region = region;
37      this.stores = new Store[stores.size()];
38      Iterator<Metapb.Peer> peer = peers.iterator();
39      Iterator<TiStore> store = stores.iterator();
40      for (int idx = 0; idx < peers.size(); idx++) {
41        Metapb.Peer currentPeer = peer.next();
42        boolean isLeader = currentPeer.equals(leader);
43        this.stores[idx] = new Store(currentPeer, store.next().getStore(), isLeader);
44        if (isLeader) {
45          leaderStore = this.stores[idx];
46        }
47      }
48    }
49  
50    public Store[] getStores() {
51      return stores;
52    }
53  
54    public Store getLeader() {
55      return leaderStore;
56    }
57  
58    public long getId() {
59      return region.getId();
60    }
61  
62    public byte[] getStartKey() {
63      return region.getStartKey().toByteArray();
64    }
65  
66    public byte[] getEndKey() {
67      return region.getEndKey().toByteArray();
68    }
69  
70    public String toString() {
71      return toStringHelper(this).add("region", region).add("stores", stores).toString();
72    }
73  }