1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }