Skip to content

Instantly share code, notes, and snippets.

@spullara spullara/gist:266951
Created Dec 31, 2009

Embed
What would you like to do?
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
You can’t perform that action at this time.