use of org.opentripplanner.routing.services.GraphService in project OpenTripPlanner by opentripplanner.
the class GraphServiceTest method testGraphServiceAutoscan.
@Test
public final void testGraphServiceAutoscan() throws IOException {
// Check for no graphs
GraphService graphService = new GraphService(false);
GraphScanner graphScanner = new GraphScanner(graphService, basePath, true);
graphScanner.startup();
assertEquals(0, graphService.getRouterIds().size());
System.out.println("------------------------------------------");
// Add a single default graph
InputStreamGraphSource.FileFactory graphSourceFactory = new InputStreamGraphSource.FileFactory(basePath);
graphSourceFactory.save("", new ByteArrayInputStream(smallGraphData));
// Check that the single graph is there
graphService = new GraphService(false);
graphScanner = new GraphScanner(graphService, basePath, true);
graphScanner.startup();
assertEquals(1, graphService.getRouterIds().size());
assertEquals("", graphService.getRouter().graph.routerId);
assertEquals("", graphService.getRouter("").graph.routerId);
System.out.println("------------------------------------------");
// Add another graph in a sub-directory
graphSourceFactory.save("A", new ByteArrayInputStream(smallGraphData));
graphService = new GraphService(false);
graphScanner = new GraphScanner(graphService, basePath, true);
graphScanner.startup();
assertEquals(2, graphService.getRouterIds().size());
assertEquals("", graphService.getRouter().graph.routerId);
assertEquals("A", graphService.getRouter("A").graph.routerId);
System.out.println("------------------------------------------");
// Remove default Graph
new File(basePath, InputStreamGraphSource.GRAPH_FILENAME).delete();
// Check that default is A this time
graphService = new GraphService(false);
graphScanner = new GraphScanner(graphService, basePath, true);
graphScanner.startup();
assertEquals(1, graphService.getRouterIds().size());
assertEquals("A", graphService.getRouter().graph.routerId);
assertEquals("A", graphService.getRouter("A").graph.routerId);
}
use of org.opentripplanner.routing.services.GraphService in project OpenTripPlanner by opentripplanner.
the class GraphServiceTest method testGraphServiceMemoryRouterConfig.
@Test
public final void testGraphServiceMemoryRouterConfig() throws IOException {
ObjectMapper mapper = new ObjectMapper();
JsonNode buildConfig = MissingNode.getInstance();
ObjectNode routerConfig = mapper.createObjectNode();
routerConfig.put("timeout", 8);
EmbedConfig embedConfig = new EmbedConfig(buildConfig, routerConfig);
embedConfig.buildGraph(emptyGraph, null);
GraphService graphService = new GraphService();
graphService.registerGraph("A", new MemoryGraphSource("A", emptyGraph));
assertEquals(1, graphService.getRouterIds().size());
Graph graph = graphService.getRouter("A").graph;
assertNotNull(graph);
assertEquals(emptyGraph, graph);
assertEquals("A", emptyGraph.routerId);
JsonNode graphRouterConfig = mapper.readTree(graph.routerConfig);
assertEquals(graphRouterConfig, routerConfig);
assertEquals(graphRouterConfig.get("timeout"), routerConfig.get("timeout"));
}
use of org.opentripplanner.routing.services.GraphService in project OpenTripPlanner by opentripplanner.
the class GraphServiceTest method testGraphServiceFile.
@Test
public final void testGraphServiceFile() throws IOException {
// Create a GraphService and a GraphSourceFactory
GraphService graphService = new GraphService();
InputStreamGraphSource.FileFactory graphSourceFactory = new InputStreamGraphSource.FileFactory(basePath);
graphSourceFactory.save("A", new ByteArrayInputStream(emptyGraphData));
// Check if the graph has been saved
assertTrue(new File(new File(basePath, "A"), InputStreamGraphSource.GRAPH_FILENAME).canRead());
// Register this empty graph, reloading it from disk
boolean registered = graphService.registerGraph("A", graphSourceFactory.createGraphSource("A"));
assertTrue(registered);
// Check if the loaded graph is the one we saved earlier
Graph graph = graphService.getRouter("A").graph;
assertNotNull(graph);
assertEquals("A", graph.routerId);
assertEquals(0, graph.getVertices().size());
assertEquals(1, graphService.getRouterIds().size());
// Save A again, with more data this time
int verticesCount = smallGraph.getVertices().size();
int edgesCount = smallGraph.getEdges().size();
graphSourceFactory.save("A", new ByteArrayInputStream(smallGraphData));
// Force a reload, get again the graph
graphService.reloadGraphs(false, true);
graph = graphService.getRouter("A").graph;
// Check if loaded graph is the one modified
assertEquals(verticesCount, graph.getVertices().size());
assertEquals(edgesCount, graph.getEdges().size());
// Remove the file from disk and reload
boolean deleted = new File(new File(basePath, "A"), InputStreamGraphSource.GRAPH_FILENAME).delete();
assertTrue(deleted);
graphService.reloadGraphs(false, true);
// Check that the graph have been evicted
assertEquals(0, graphService.getRouterIds().size());
// Register it manually
registered = graphService.registerGraph("A", graphSourceFactory.createGraphSource("A"));
// Check that it fails again (file still deleted)
assertFalse(registered);
assertEquals(0, graphService.getRouterIds().size());
// Re-save the graph file, register it again
graphSourceFactory.save("A", new ByteArrayInputStream(smallGraphData));
registered = graphService.registerGraph("A", graphSourceFactory.createGraphSource("A"));
// This time registering is OK
assertTrue(registered);
assertEquals(1, graphService.getRouterIds().size());
// Evict the graph, should be OK
boolean evicted = graphService.evictRouter("A");
assertTrue(evicted);
assertEquals(0, graphService.getRouterIds().size());
}
use of org.opentripplanner.routing.services.GraphService in project OpenTripPlanner by opentripplanner.
the class Routers method putGraphId.
/**
* Load the graph for the specified routerId from disk.
* @param preEvict before reloading each graph, evict the existing graph. This will prevent
* memory usage from increasing during the reload, but routing will be unavailable on this
* routerId for the duration of the operation.
*/
@RolesAllowed({ "ROUTERS" })
@PUT
@Path("{routerId}")
@Produces({ MediaType.TEXT_PLAIN })
public Response putGraphId(@PathParam("routerId") String routerId, @QueryParam("preEvict") @DefaultValue("true") boolean preEvict) {
LOG.debug("Attempting to load graph '{}' from server's local filesystem.", routerId);
GraphService graphService = otpServer.getGraphService();
if (graphService.getRouterIds().contains(routerId)) {
boolean success = graphService.reloadGraph(routerId, preEvict, false);
if (success)
return Response.status(201).entity("graph already registered, reloaded.\n").build();
else
return Response.status(404).entity("graph already registered, but reload failed.\n").build();
} else {
boolean success = graphService.registerGraph(routerId, graphService.getGraphSourceFactory().createGraphSource(routerId));
if (success)
return Response.status(201).entity("graph registered.\n").build();
else
return Response.status(404).entity("graph not found or other error.\n").build();
}
}
use of org.opentripplanner.routing.services.GraphService in project OpenTripPlanner by opentripplanner.
the class Routers method postGraphOverWire.
/**
* Deserialize a graph sent with the HTTP request as POST data, associating it with the given
* routerId.
*/
@RolesAllowed({ "ROUTERS" })
@POST
@Path("{routerId}")
@Produces({ MediaType.TEXT_PLAIN })
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public Response postGraphOverWire(@PathParam("routerId") String routerId, @QueryParam("preEvict") @DefaultValue("true") boolean preEvict, @QueryParam("loadLevel") @DefaultValue("FULL") LoadLevel level, InputStream is) {
if (preEvict) {
LOG.debug("pre-evicting graph");
otpServer.getGraphService().evictRouter(routerId);
}
LOG.debug("deserializing graph from POST data stream...");
Graph graph;
try {
graph = Graph.load(is, level);
GraphService graphService = otpServer.getGraphService();
graphService.registerGraph(routerId, new MemoryGraphSource(routerId, graph));
return Response.status(Status.CREATED).entity(graph.toString() + "\n").build();
} catch (Exception e) {
return Response.status(Status.BAD_REQUEST).entity(e.toString() + "\n").build();
}
}
Aggregations