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 TableName tableName = getName();
89 NavigableMap<HRegionInfo, ServerName> locations =
90 MetaScanner.allTableRegions(this.connection, tableName);
91 ArrayList<HRegionLocation> regions = new ArrayList<>(locations.size());
92 for (Entry<HRegionInfo, ServerName> entry : locations.entrySet()) {
93 regions.add(new HRegionLocation(entry.getKey(), entry.getValue()));
94 }
95 if (regions.size() > 0) {
96 connection.cacheLocation(tableName, new RegionLocations(regions));
97 }
98 return regions;
99 }
100
101
102
103
104 @Override
105 public byte[][] getStartKeys() throws IOException {
106 return getStartEndKeys().getFirst();
107 }
108
109
110
111
112 @Override
113 public byte[][] getEndKeys() throws IOException {
114 return getStartEndKeys().getSecond();
115 }
116
117
118
119
120 @Override
121 public Pair<byte[][], byte[][]> getStartEndKeys() throws IOException {
122 return getStartEndKeys(listRegionLocations());
123 }
124
125 @VisibleForTesting
126 Pair<byte[][], byte[][]> getStartEndKeys(List<RegionLocations> regions) {
127 final byte[][] startKeyList = new byte[regions.size()][];
128 final byte[][] endKeyList = new byte[regions.size()][];
129
130 for (int i = 0; i < regions.size(); i++) {
131 HRegionInfo region = regions.get(i).getRegionLocation().getRegionInfo();
132 startKeyList[i] = region.getStartKey();
133 endKeyList[i] = region.getEndKey();
134 }
135
136 return new Pair<>(startKeyList, endKeyList);
137 }
138
139 @Override
140 public TableName getName() {
141 return this.tableName;
142 }
143
144 @VisibleForTesting
145 List<RegionLocations> listRegionLocations() throws IOException {
146 return MetaScanner.listTableRegionLocations(getConfiguration(), this.connection, getName());
147 }
148
149 public Configuration getConfiguration() {
150 return connection.getConfiguration();
151 }
152 }