SampledRowKeysAdapter { public List<HRegionLocation> adaptResponse(List<KeyOffset> responses) { List<HRegionLocation> regions = new ArrayList<>(); byte[] startKey = HConstants.EMPTY_START_ROW; for (KeyOffset response : responses) { byte[] endKey = response.getKey().toByteArray(); if (Bytes.equals(startKey, endKey)) { continue; } regions.add(createRegionLocation(startKey, endKey)); startKey = endKey; } byte[] endKey = HConstants.EMPTY_END_ROW; if (regions.isEmpty() || !Bytes.equals(startKey, endKey)) { regions.add(createRegionLocation(startKey, endKey)); } return regions; } SampledRowKeysAdapter(TableName tableName, ServerName serverName); List<HRegionLocation> adaptResponse(List<KeyOffset> responses);  }
@Test public void testEmptyRowList() { List<KeyOffset> rowKeys = new ArrayList<>(); List<HRegionLocation> locations = adapter.adaptResponse(rowKeys); Assert.assertEquals(1, locations.size()); HRegionLocation location = locations.get(0); Assert.assertArrayEquals(HConstants.EMPTY_START_ROW, location.getRegionInfo().getStartKey()); Assert.assertArrayEquals(HConstants.EMPTY_END_ROW, location.getRegionInfo().getEndKey()); Assert.assertEquals("host", location.getHostname()); Assert.assertEquals(123, location.getPort()); }
@Test public void testOneRow() { byte[] rowKey = Bytes.toBytes("row"); List<KeyOffset> responses = new ArrayList<>(); responses.add(KeyOffset.create(ByteString.copyFrom(rowKey), 0)); List<HRegionLocation> locations = adapter.adaptResponse(responses); Assert.assertEquals(2, locations.size()); HRegionLocation location = locations.get(0); Assert.assertArrayEquals(HConstants.EMPTY_START_ROW, location.getRegionInfo().getStartKey()); Assert.assertArrayEquals(rowKey, location.getRegionInfo().getEndKey()); location = locations.get(1); Assert.assertArrayEquals(rowKey, location.getRegionInfo().getStartKey()); Assert.assertArrayEquals(HConstants.EMPTY_END_ROW, location.getRegionInfo().getEndKey()); }
@Test public void testEmptyRow() { byte[] rowKey = new byte[0]; List<KeyOffset> responses = new ArrayList<>(); responses.add(KeyOffset.create(ByteString.copyFrom(rowKey), 0)); List<HRegionLocation> locations = adapter.adaptResponse(responses); Assert.assertEquals(1, locations.size()); HRegionLocation location = locations.get(0); Assert.assertArrayEquals(HConstants.EMPTY_START_ROW, location.getRegionInfo().getStartKey()); Assert.assertArrayEquals(HConstants.EMPTY_END_ROW, location.getRegionInfo().getEndKey()); }