Skip to content

Commit

Permalink
avoid unneccessary geometry building during count requests
Browse files Browse the repository at this point in the history
  • Loading branch information
tyrasd committed Mar 22, 2021
1 parent f7cb2b7 commit a131d58
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,32 +80,28 @@ public Response aggregate() throws Exception {
final SortedMap<OSHDBTimestamp, ? extends Number> result;
MapReducer<OSMEntitySnapshot> mapRed = null;
mapRed = inputProcessor.processParameters();
var mapRedGeom = mapRed.map(OSMEntitySnapshot::getGeometry);
switch (requestResource) {
case COUNT:
result = mapRed.aggregateByTimestamp().count();
break;
case AREA:
result = mapRed.aggregateByTimestamp()
.sum((SerializableFunction<OSMEntitySnapshot, Number>) snapshot -> ExecutionUtils
.cacheInUserData(snapshot.getGeometry(), () -> Geo.areaOf(snapshot.getGeometry())));
break;
case LENGTH:
result = mapRed.aggregateByTimestamp()
.sum((SerializableFunction<OSMEntitySnapshot, Number>) snapshot -> ExecutionUtils
.cacheInUserData(snapshot.getGeometry(),
() -> Geo.lengthOf(snapshot.getGeometry())));
break;
case PERIMETER:
result = mapRed.aggregateByTimestamp()
.sum((SerializableFunction<OSMEntitySnapshot, Number>) snapshot -> {
if (snapshot.getGeometry() instanceof Polygonal) {
return ExecutionUtils.cacheInUserData(snapshot.getGeometry(),
() -> Geo.lengthOf(snapshot.getGeometry().getBoundary()));
} else {
result = mapRedGeom.aggregateByTimestamp()
.sum(geom -> {
if (!(geom instanceof Polygonal)) {
return 0.0;
}
return ExecutionUtils.cacheInUserData(geom, () -> Geo.lengthOf(geom.getBoundary()));
});
break;
case LENGTH:
result = mapRedGeom.aggregateByTimestamp()
.sum(geom -> ExecutionUtils.cacheInUserData(geom, () -> Geo.lengthOf(geom)));
break;
case AREA:
result = mapRedGeom.aggregateByTimestamp()
.sum(geom -> ExecutionUtils.cacheInUserData(geom, () -> Geo.areaOf(geom)));
break;
default:
throw new RuntimeException("Unsupported RequestResource type for this processing. "
+ "Only COUNT, LENGTH, PERIMETER, and AREA are permitted here");
Expand Down Expand Up @@ -341,7 +337,6 @@ private ElementsResult[] fillElementsResult(SortedMap<OSHDBTimestamp, ? extends
if (boundaryType == BoundaryType.NOBOUNDARY) {
throw new BadRequestException(ExceptionMessages.NO_BOUNDARY);
}
MapAggregator<OSHDBCombinedIndex<OSHDBTimestamp, Integer>, Geometry> preResult;
ArrayList<Geometry> arrGeoms = new ArrayList<>(processingData.getBoundaryList());
@SuppressWarnings("unchecked") // intentionally as check for P on Polygonal is already performed
Map<Integer, P> geoms = IntStream.range(0, arrGeoms.size()).boxed()
Expand All @@ -355,22 +350,22 @@ private ElementsResult[] fillElementsResult(SortedMap<OSHDBTimestamp, ? extends
if (filter.isPresent()) {
mapAgg = mapAgg.filter(filter.get());
}
preResult = mapAgg.map(OSMEntitySnapshot::getGeometry);
var mapAggGeom = mapAgg.map(OSMEntitySnapshot::getGeometry);
switch (requestResource) {
case COUNT:
return preResult.count();
return mapAgg.count();
case PERIMETER:
return preResult.sum(geom -> {
return mapAggGeom.sum(geom -> {
if (!(geom instanceof Polygonal)) {
return 0.0;
}
return ExecutionUtils.cacheInUserData(geom, () -> Geo.lengthOf(geom.getBoundary()));
});
case LENGTH:
return preResult
return mapAggGeom
.sum(geom -> ExecutionUtils.cacheInUserData(geom, () -> Geo.lengthOf(geom)));
case AREA:
return preResult.sum(geom -> ExecutionUtils.cacheInUserData(geom, () -> Geo.areaOf(geom)));
return mapAggGeom.sum(geom -> ExecutionUtils.cacheInUserData(geom, () -> Geo.areaOf(geom)));
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,10 @@ public static <P extends Geometry & Polygonal> Response aggregateGroupByBoundary
mapAgg = mapAgg.filter(filter.get());
}
ExecutionUtils exeUtils = new ExecutionUtils(processingData);
var preResult = mapAgg.map(f -> exeUtils.mapSnapshotToTags(keysInt, valuesInt, f))
.aggregateBy(Pair::getKey, zeroFill).map(Pair::getValue)
.aggregateByTimestamp(OSMEntitySnapshot::getTimestamp).map(OSMEntitySnapshot::getGeometry);
var result = exeUtils.computeNestedResult(requestResource, preResult);
var result = exeUtils.computeNestedResult(requestResource,
mapAgg.map(f -> exeUtils.mapSnapshotToTags(keysInt, valuesInt, f))
.aggregateBy(Pair::getKey, zeroFill).map(Pair::getValue)
.aggregateByTimestamp(OSMEntitySnapshot::getTimestamp));
var groupByResult = OSHDBCombinedIndex.nest(result);
GroupByResult[] resultSet = new GroupByResult[groupByResult.entrySet().size()];
InputProcessingUtils utils = inputProcessor.getUtils();
Expand Down Expand Up @@ -684,8 +684,8 @@ public static Response aggregateRatio(RequestResource requestResource,
mapRed = mapRed.filter(combinedFilter);
var preResult = mapRed.aggregateByTimestamp().aggregateBy(snapshot -> {
OSMEntity entity = snapshot.getEntity();
boolean matches1 = filterExpr1.applyOSMGeometry(entity, snapshot.getGeometry());
boolean matches2 = filterExpr2.applyOSMGeometry(entity, snapshot.getGeometry());
boolean matches1 = filterExpr1.applyOSMGeometry(entity, snapshot::getGeometry);
boolean matches2 = filterExpr2.applyOSMGeometry(entity, snapshot::getGeometry);
if (matches1 && matches2) {
return MatchType.MATCHESBOTH;
} else if (matches1) {
Expand Down Expand Up @@ -990,11 +990,11 @@ public static <P extends Geometry & Polygonal> Response aggregateRatioGroupByBou
arrGeoms.stream().collect(Collectors.toMap(arrGeoms::indexOf, geom -> (P) geom));
var mapRed2 = mapRed.aggregateByTimestamp().aggregateByGeometry(geoms);
mapRed2 = mapRed2.filter(combinedFilter);
var preResult =
var mapRed3 =
mapRed2.aggregateBy((SerializableFunction<OSMEntitySnapshot, MatchType>) snapshot -> {
OSMEntity entity = snapshot.getEntity();
boolean matches1 = filterExpr1.applyOSMGeometry(entity, snapshot.getGeometry());
boolean matches2 = filterExpr2.applyOSMGeometry(entity, snapshot.getGeometry());
boolean matches1 = filterExpr1.applyOSMGeometry(entity, snapshot::getGeometry);
boolean matches2 = filterExpr2.applyOSMGeometry(entity, snapshot::getGeometry);
if (matches1 && matches2) {
return MatchType.MATCHESBOTH;
} else if (matches1) {
Expand All @@ -1005,19 +1005,20 @@ public static <P extends Geometry & Polygonal> Response aggregateRatioGroupByBou
assert false : "MatchType matches none.";
}
return MatchType.MATCHESNONE;
}, EnumSet.allOf(MatchType.class)).map(OSMEntitySnapshot::getGeometry);
}, EnumSet.allOf(MatchType.class));
var mapRed3Geom = mapRed3.map(OSMEntitySnapshot::getGeometry);
SortedMap<OSHDBCombinedIndex<OSHDBCombinedIndex<OSHDBTimestamp, Integer>, MatchType>, ? extends
Number> result = null;
switch (requestResource) {
case COUNT:
result = preResult.count();
result = mapRed3.count();
break;
case LENGTH:
result =
preResult.sum(geom -> ExecutionUtils.cacheInUserData(geom, () -> Geo.lengthOf(geom)));
mapRed3Geom.sum(geom -> ExecutionUtils.cacheInUserData(geom, () -> Geo.lengthOf(geom)));
break;
case PERIMETER:
result = preResult.sum(geom -> {
result = mapRed3Geom.sum(geom -> {
if (!(geom instanceof Polygonal)) {
return 0.0;
}
Expand All @@ -1026,7 +1027,7 @@ public static <P extends Geometry & Polygonal> Response aggregateRatioGroupByBou
break;
case AREA:
result =
preResult.sum(geom -> ExecutionUtils.cacheInUserData(geom, () -> Geo.areaOf(geom)));
mapRed3Geom.sum(geom -> ExecutionUtils.cacheInUserData(geom, () -> Geo.areaOf(geom)));
break;
default:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,28 +433,26 @@ public org.wololo.geojson.Feature createOSMFeature(OSMEntity entity, Geometry ge
public <K extends Comparable<K> & Serializable, V extends Number>
SortedMap<OSHDBCombinedIndex<OSHDBTimestamp, K>, V> computeResult(
RequestResource requestResource,
MapAggregator<OSHDBCombinedIndex<OSHDBTimestamp, K>, OSMEntitySnapshot> preResult)
MapAggregator<OSHDBCombinedIndex<OSHDBTimestamp, K>, OSMEntitySnapshot> mapAgg)
throws Exception {
var mapAggGeom = mapAgg.map(OSMEntitySnapshot::getGeometry);
switch (requestResource) {
case COUNT:
return (SortedMap<OSHDBCombinedIndex<OSHDBTimestamp, K>, V>) preResult.count();
case LENGTH:
return (SortedMap<OSHDBCombinedIndex<OSHDBTimestamp, K>, V>) preResult
.sum((SerializableFunction<OSMEntitySnapshot, Number>) snapshot -> cacheInUserData(
snapshot.getGeometry(), () -> Geo.lengthOf(snapshot.getGeometry())));
return (SortedMap<OSHDBCombinedIndex<OSHDBTimestamp, K>, V>) mapAgg.count();
case PERIMETER:
return (SortedMap<OSHDBCombinedIndex<OSHDBTimestamp, K>, V>) preResult
.sum((SerializableFunction<OSMEntitySnapshot, Number>) snapshot -> {
if (snapshot.getGeometry() instanceof Polygonal) {
return cacheInUserData(snapshot.getGeometry(),
() -> Geo.lengthOf(snapshot.getGeometry().getBoundary()));
return (SortedMap<OSHDBCombinedIndex<OSHDBTimestamp, K>, V>) mapAggGeom
.sum(geom -> {
if (!(geom instanceof Polygonal)) {
return 0.0;
}
return 0.0;
return cacheInUserData(geom, () -> Geo.lengthOf(geom.getBoundary()));
});
case LENGTH:
return (SortedMap<OSHDBCombinedIndex<OSHDBTimestamp, K>, V>) mapAggGeom
.sum(geom -> cacheInUserData(geom, () -> Geo.lengthOf(geom)));
case AREA:
return (SortedMap<OSHDBCombinedIndex<OSHDBTimestamp, K>, V>) preResult
.sum((SerializableFunction<OSMEntitySnapshot, Number>) snapshot -> cacheInUserData(
snapshot.getGeometry(), () -> Geo.areaOf(snapshot.getGeometry())));
return (SortedMap<OSHDBCombinedIndex<OSHDBTimestamp, K>, V>) mapAggGeom
.sum(geom -> cacheInUserData(geom, () -> Geo.areaOf(geom)));
default:
return null;
}
Expand All @@ -469,26 +467,27 @@ SortedMap<OSHDBCombinedIndex<OSHDBTimestamp, K>, V> computeResult(
SortedMap<OSHDBCombinedIndex<OSHDBCombinedIndex<Integer, K>, OSHDBTimestamp>, V>
computeNestedResult(
RequestResource requestResource,
MapAggregator<OSHDBCombinedIndex<OSHDBCombinedIndex<Integer, K>, OSHDBTimestamp>, Geometry>
preResult) throws Exception {
MapAggregator<OSHDBCombinedIndex<OSHDBCombinedIndex<Integer, K>, OSHDBTimestamp>,
OSMEntitySnapshot> mapAgg) throws Exception {
var mapAggGeom = mapAgg.map(OSMEntitySnapshot::getGeometry);
switch (requestResource) {
case COUNT:
return (SortedMap<OSHDBCombinedIndex<OSHDBCombinedIndex<Integer, K>, OSHDBTimestamp>, V>)
preResult.count();
mapAgg.count();
case PERIMETER:
return (SortedMap<OSHDBCombinedIndex<OSHDBCombinedIndex<Integer, K>, OSHDBTimestamp>, V>)
preResult.sum(geom -> {
mapAggGeom.sum(geom -> {
if (!(geom instanceof Polygonal)) {
return 0.0;
}
return cacheInUserData(geom, () -> Geo.lengthOf(geom.getBoundary()));
});
case LENGTH:
return (SortedMap<OSHDBCombinedIndex<OSHDBCombinedIndex<Integer, K>, OSHDBTimestamp>, V>)
preResult.sum(geom -> cacheInUserData(geom, () -> Geo.lengthOf(geom)));
mapAggGeom.sum(geom -> cacheInUserData(geom, () -> Geo.lengthOf(geom)));
case AREA:
return (SortedMap<OSHDBCombinedIndex<OSHDBCombinedIndex<Integer, K>, OSHDBTimestamp>, V>)
preResult.sum(geom -> cacheInUserData(geom, () -> Geo.areaOf(geom)));
mapAggGeom.sum(geom -> cacheInUserData(geom, () -> Geo.areaOf(geom)));
default:
return null;
}
Expand Down

0 comments on commit a131d58

Please sign in to comment.