Skip to content

Instantly share code, notes, and snippets.

@spullara
Created December 31, 2009 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spullara/266951 to your computer and use it in GitHub Desktop.
Save spullara/266951 to your computer and use it in GitHub Desktop.
private static float search(final HTablePool pool, byte[] woeid, Position pos, Set<Integer> container, float smallestArea) throws IOException, JAXBException {
final HTable placesTable = getTable(PLACES);
Get get = new Get(woeid);
get.addFamily(CHILDREN);
Result children = placesTable.get(get);
searchCounter.getAndIncrement();
try {
NavigableMap<byte[], byte[]> map = children.getFamilyMap(CHILDREN);
ExecutorCompletionService<Result> service = new ExecutorCompletionService<Result>(es);
if (map != null) {
Set<byte[]> set = map.keySet();
int size = set.size();
for (final byte[] child : set) {
service.submit(new Callable<Result>() {
@Override
public Result call() throws Exception {
HTable placesTable = getTable(PLACES);
try {
int childInt = Bytes.toInt(child);
Get get = new Get(woeid(childInt));
get.addColumn(SHAPE, SWLAT);
get.addColumn(SHAPE, SWLON);
get.addColumn(SHAPE, NELAT);
get.addColumn(SHAPE, NELON);
searchCounter.getAndIncrement();
return placesTable.get(get);
} finally {
pool.putTable(placesTable);
}
}
});
}
for (int i = 0; i < size; i++) {
Result result = service.take().get();
byte[] value = result.getValue(SHAPE, SWLAT);
Region region;
byte[] row = result.getRow();
if (value == null) {
BoundingBox boundingBox = lookupShape(pool, row);
region = new Region(
boundingBox.getSouthWest().getLatitude().floatValue(),
boundingBox.getSouthWest().getLongitude().floatValue(),
boundingBox.getNorthEast().getLatitude().floatValue(),
boundingBox.getNorthEast().getLongitude().floatValue()
);
} else {
region = new Region(
Bytes.toFloat(value),
Bytes.toFloat(result.getValue(SHAPE, SWLON)),
Bytes.toFloat(result.getValue(SHAPE, NELAT)),
Bytes.toFloat(result.getValue(SHAPE, NELON))
);
}
if (region.bound(pos)) {
float area = region.area();
if (area < smallestArea) {
smallestArea = area;
container.clear();
}
if (area <= smallestArea) {
container.add(Bytes.toInt(row));
}
smallestArea = search(pool, row, pos, container, smallestArea);
}
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} finally {
pool.putTable(placesTable);
}
return smallestArea;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment