/** After configuring this storage you need to create it explicitly. */ @Override public GraphHopperStorage create(long byteCount) { baseGraph.checkInit(); if (encodingManager == null) { throw new IllegalStateException("EncodingManager can only be null if you call loadExisting"); } long initSize = Math.max(byteCount, 100); properties.create(100); properties.put("graph.bytesForFlags", encodingManager.getBytesForFlags()); properties.put("graph.flagEncoders", encodingManager.toDetailsString()); properties.put("graph.byteOrder", dir.getByteOrder()); properties.put("graph.dimension", baseGraph.nodeAccess.getDimension()); properties.putCurrentVersions(); baseGraph.create(initSize); for (CHGraphImpl cg : chGraphs) { cg.create(byteCount); } properties.put("graph.chWeightings", getCHWeightings().toString()); return this; }
@Test public void internalDisconnect() { GraphHopperStorage storage = createGHStorage(); BaseGraph graph = (BaseGraph) storage.getGraph(Graph.class); EdgeIteratorState iter0 = graph.edge(0, 1, 10, true); EdgeIteratorState iter2 = graph.edge(1, 2, 10, true); EdgeIteratorState iter3 = graph.edge(0, 3, 10, true); EdgeExplorer explorer = graph.createEdgeExplorer(); assertEquals(GHUtility.asSet(3, 1), GHUtility.getNeighbors(explorer.setBaseNode(0))); assertEquals(GHUtility.asSet(2, 0), GHUtility.getNeighbors(explorer.setBaseNode(1))); // remove edge "1-2" but only from 1 not from 2 graph.edgeAccess.internalEdgeDisconnect( iter2.getEdge(), -1, iter2.getBaseNode(), iter2.getAdjNode()); assertEquals(GHUtility.asSet(0), GHUtility.getNeighbors(explorer.setBaseNode(1))); assertEquals(GHUtility.asSet(1), GHUtility.getNeighbors(explorer.setBaseNode(2))); // let 0 unchanged -> no side effects assertEquals(GHUtility.asSet(3, 1), GHUtility.getNeighbors(explorer.setBaseNode(0))); // remove edge "0-1" but only from 0 graph.edgeAccess.internalEdgeDisconnect( iter0.getEdge(), (long) iter3.getEdge() * graph.edgeEntryBytes, iter0.getBaseNode(), iter0.getAdjNode()); assertEquals(GHUtility.asSet(3), GHUtility.getNeighbors(explorer.setBaseNode(0))); assertEquals(GHUtility.asSet(0), GHUtility.getNeighbors(explorer.setBaseNode(3))); assertEquals(GHUtility.asSet(0), GHUtility.getNeighbors(explorer.setBaseNode(1))); storage.close(); }
@Override public boolean loadExisting() { baseGraph.checkInit(); if (properties.loadExisting()) { properties.checkVersions(false); // check encoding for compatiblity String acceptStr = properties.get("graph.flagEncoders"); if (encodingManager == null) { if (acceptStr.isEmpty()) throw new IllegalStateException( "No EncodingManager was configured. And no one was found in the graph: " + dir.getLocation()); int bytesForFlags = 4; if ("8".equals(properties.get("graph.bytesForFlags"))) bytesForFlags = 8; encodingManager = new EncodingManager(acceptStr, bytesForFlags); } else if (!acceptStr.isEmpty() && !encodingManager.toDetailsString().equalsIgnoreCase(acceptStr)) { throw new IllegalStateException( "Encoding does not match:\nGraphhopper config: " + encodingManager.toDetailsString() + "\nGraph: " + acceptStr + ", dir:" + dir.getLocation()); } String byteOrder = properties.get("graph.byteOrder"); if (!byteOrder.equalsIgnoreCase("" + dir.getByteOrder())) throw new IllegalStateException( "Configured graph.byteOrder (" + dir.getByteOrder() + ") is not equal to loaded " + byteOrder + ""); String bytesForFlags = properties.get("graph.bytesForFlags"); if (!bytesForFlags.equalsIgnoreCase("" + encodingManager.getBytesForFlags())) throw new IllegalStateException( "Configured graph.bytesForFlags (" + encodingManager.getBytesForFlags() + ") is not equal to loaded " + bytesForFlags); String dim = properties.get("graph.dimension"); baseGraph.loadExisting(dim); for (CHGraphImpl cg : chGraphs) { if (!cg.loadExisting()) throw new IllegalStateException("Cannot load " + cg); } return true; } return false; }
@Override public void setSegmentSize(int bytes) { baseGraph.setSegmentSize(bytes); for (CHGraphImpl cg : chGraphs) { cg.setSegmentSize(bytes); } }
@Override public long getCapacity() { long cnt = baseGraph.getCapacity() + properties.getCapacity(); for (CHGraphImpl cg : chGraphs) { cnt += cg.getCapacity(); } return cnt; }
@Override public void close() { properties.close(); baseGraph.close(); for (CHGraphImpl cg : chGraphs) { cg.close(); } }
@Override public String toDetailsString() { String str = baseGraph.toDetailsString(); for (CHGraphImpl cg : chGraphs) { str += ", " + cg.toDetailsString(); } return str; }
@Override public void optimize() { if (isFrozen()) { throw new IllegalStateException("do not optimize after graph was frozen"); } int delNodes = baseGraph.getRemovedNodes().getCardinality(); if (delNodes <= 0) { return; } // Deletes only nodes. // It reduces the fragmentation of the node space but introduces new unused edges. baseGraph.inPlaceNodeRemove(delNodes); // Reduce memory usage baseGraph.trimToSize(); }
@Override public void flush() { for (CHGraphImpl cg : chGraphs) { cg.setEdgesHeader(); cg.flush(); } baseGraph.flush(); properties.flush(); }
@Override public final AllEdgesIterator getAllEdges() { return baseGraph.getAllEdges(); }
@Override public final EdgeIteratorState getEdgeIteratorState(int edgeId, int adjNode) { return baseGraph.getEdgeIteratorState(edgeId, adjNode); }
@Override public final EdgeIteratorState edge(int a, int b, double distance, boolean bothDirections) { return baseGraph.edge(a, b, distance, bothDirections); }
@Override public final EdgeIteratorState edge(int a, int b) { return baseGraph.edge(a, b); }
@Override public final BBox getBounds() { return baseGraph.getBounds(); }
@Override public final NodeAccess getNodeAccess() { return baseGraph.getNodeAccess(); }
@Override public final int getNodes() { return baseGraph.getNodes(); }
public void setAdditionalEdgeField(long edgePointer, int value) { baseGraph.setAdditionalEdgeField(edgePointer, value); }
@Override public final Graph copyTo(Graph g) { return baseGraph.copyTo(g); }
@Override public final EdgeExplorer createEdgeExplorer(EdgeFilter filter) { return baseGraph.createEdgeExplorer(filter); }
@Override public void markNodeRemoved(int index) { baseGraph.getRemovedNodes().add(index); }
@Override public boolean isNodeRemoved(int index) { return baseGraph.getRemovedNodes().contains(index); }
@Override public final EdgeExplorer createEdgeExplorer() { return baseGraph.createEdgeExplorer(); }
/** * Avoid that edges and nodes of the base graph are further modified. Necessary as hook for e.g. * ch graphs on top to initilize themself */ public void freeze() { if (!baseGraph.isFrozen()) { baseGraph.freeze(); } }
@Override public final GraphExtension getExtension() { return baseGraph.getExtension(); }
boolean isFrozen() { return baseGraph.isFrozen(); }