Search in sources :

Example 1 with ChangeGraphResponse

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();
    }
}
Also used : ChangeGraphHelper(com.graphhopper.storage.change.ChangeGraphHelper) ChangeGraphResponse(com.graphhopper.storage.change.ChangeGraphResponse) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Lock(java.util.concurrent.locks.Lock)

Example 2 with ChangeGraphResponse

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);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonFeatureCollection(com.graphhopper.json.geo.JsonFeatureCollection) ChangeGraphResponse(com.graphhopper.storage.change.ChangeGraphResponse) GraphHopper(com.graphhopper.GraphHopper) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) StopWatch(com.graphhopper.util.StopWatch)

Aggregations

ChangeGraphResponse (com.graphhopper.storage.change.ChangeGraphResponse)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 GraphHopper (com.graphhopper.GraphHopper)1 JsonFeatureCollection (com.graphhopper.json.geo.JsonFeatureCollection)1 ChangeGraphHelper (com.graphhopper.storage.change.ChangeGraphHelper)1 StopWatch (com.graphhopper.util.StopWatch)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Lock (java.util.concurrent.locks.Lock)1 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 ServletException (javax.servlet.ServletException)1