use of com.graphhopper.storage.change.ChangeGraphResponse in project graphhopper by graphhopper.
the class GraphHopper method changeGraph.
/**
* This method applies the changes to the graph specified as feature collection. It does so by locking the routing
* to avoid concurrent changes which could result in incorrect routing (like when done while a Dijkstra search) or
* also while just reading one edge row (inconsistent edge properties).
*/
public ChangeGraphResponse changeGraph(Collection<JsonFeature> collection) {
// TODO allow calling this method if called before CH preparation
if (getCHFactoryDecorator().isEnabled())
throw new IllegalArgumentException("To use the changeGraph API you need to turn off CH");
Lock writeLock = readWriteLock.writeLock();
writeLock.lock();
try {
ChangeGraphHelper overlay = createChangeGraphHelper(ghStorage, locationIndex);
long updateCount = overlay.applyChanges(encodingManager, collection);
return new ChangeGraphResponse(updateCount);
} finally {
writeLock.unlock();
}
}
use of com.graphhopper.storage.change.ChangeGraphResponse in project graphhopper by graphhopper.
the class ChangeGraphServlet method doPost.
@Override
protected void doPost(HttpServletRequest httpReq, HttpServletResponse httpRes) throws ServletException, IOException {
String infoStr = httpReq.getRemoteAddr() + " " + httpReq.getPathInfo() + " " + httpReq.getQueryString();
float took = -1;
StopWatch sw = new StopWatch().start();
try {
JsonFeatureCollection collection = ghJson.fromJson(new InputStreamReader(httpReq.getInputStream(), Helper.UTF_CS), JsonFeatureCollection.class);
// TODO put changeGraph on GraphHopperAPI interface and remove cast (or some other solution)
if (!(graphHopper instanceof GraphHopper)) {
throw new IllegalStateException("Graph change API not supported with public transit.");
}
// TODO make asynchronous!
ChangeGraphResponse rsp = ((GraphHopper) graphHopper).changeGraph(collection.getFeatures());
ObjectNode resObject = objectMapper.createObjectNode();
resObject.put("updates", rsp.getUpdateCount());
// prepare the consumer to get some changes not immediately when returning after POST
resObject.put("scheduled_updates", 0);
httpRes.setHeader("X-GH-Took", "" + Math.round(took * 1000));
writeJson(httpReq, httpRes, resObject);
took = sw.stop().getSeconds();
logger.info(infoStr + " " + took);
} catch (IllegalArgumentException ex) {
took = sw.stop().getSeconds();
logger.warn(infoStr + " " + took + ", " + ex.getMessage());
writeError(httpRes, 400, "Wrong arguments for endpoint /change, " + infoStr);
} catch (Exception ex) {
took = sw.stop().getSeconds();
logger.error(infoStr + " " + took, ex);
writeError(httpRes, 500, "Error at endpoint /change, " + infoStr);
}
}
Aggregations