1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.client;
20
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.NavigableMap;
25 import java.util.Map.Entry;
26
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.hbase.HRegionInfo;
29 import org.apache.hadoop.hbase.HRegionLocation;
30 import org.apache.hadoop.hbase.RegionLocations;
31 import org.apache.hadoop.hbase.ServerName;
32 import org.apache.hadoop.hbase.TableName;
33 import org.apache.hadoop.hbase.classification.InterfaceAudience;
34 import org.apache.hadoop.hbase.classification.InterfaceStability;
35 import org.apache.hadoop.hbase.util.Pair;
36
37 import com.google.common.annotations.VisibleForTesting;
38
39
40
41
42
43
44
45
46
47 @InterfaceAudience.Private
48 @InterfaceStability.Stable
49 public class HRegionLocator implements RegionLocator {
50
51 private final TableName tableName;
52 private final ClusterConnection connection;
53
54 public HRegionLocator(TableName tableName, ClusterConnection connection) {
55 this.connection = connection;
56 this.tableName = tableName;
57 }
58
59
60
61
62 @Override
63 public void close() throws IOException {
64
65
66 }
67
68
69
70
71 @Override
72 public HRegionLocation getRegionLocation(final byte [] row)
73 throws IOException {
74 return connection.getRegionLocation(tableName, row, false);
75 }
76
77
78
79
80 @Override
81 public HRegionLocation getRegionLocation(final byte [] row, boolean reload)
82 throws IOException {
83 return connection.getRegionLocation(tableName, row, reload);
84 }
85
86 @Override
87 public List<HRegionLocation> getAllRegionLocations() throws IOException {
88 NavigableMap<HRegionInfo, ServerName> locations =
89 MetaScanner.allTableRegions(this.connection, getName());
90 ArrayList<HRegionLocation> regions = new ArrayList<>(locations.size());
91 for (Entry<HRegionInfo, ServerName> entry : locations.entrySet()) {
92 regions.add(new HRegionLocation(entry.getKey(), entry.getValue()));
93 }
94 return regions;
95 }
96
97
98
99
100 @Override
101 public byte[][] getStartKeys() throws IOException {
102 return getStartEndKeys().getFirst();
103 }
104
105
106
107
108 @Override
109 public byte[][] getEndKeys() throws IOException {
110 return getStartEndKeys().getSecond();
111 }
112
113
114
115
116 @Override
117 public Pair<byte[][], byte[][]> getStartEndKeys() throws IOException {
118 return getStartEndKeys(listRegionLocations());
119 }
120
121 @VisibleForTesting
122 Pair<byte[][], byte[][]> getStartEndKeys(List<RegionLocations> regions) {
123 final byte[][] startKeyList = new byte[regions.size()][];
124 final byte[][] endKeyList = new byte[regions.size()][];
125
126 for (int i = 0; i < regions.size(); i++) {
127 HRegionInfo region = regions.get(i).getRegionLocation().getRegionInfo();
128 startKeyList[i] = region.getStartKey();
129 endKeyList[i] = region.getEndKey();
130 }
131
132 return new Pair<>(startKeyList, endKeyList);
133 }
134
135 @Override
136 public TableName getName() {
137 return this.tableName;
138 }
139
140 @VisibleForTesting
141 List<RegionLocations> listRegionLocations() throws IOException {
142 return MetaScanner.listTableRegionLocations(getConfiguration(), this.connection, getName());
143 }
144
145 public Configuration getConfiguration() {
146 return connection.getConfiguration();
147 }
148 }