/** * Create the EncodingManager from the provided GraphHopper location. Throws an * IllegalStateException if it fails. Used if no EncodingManager specified on load. */ public static EncodingManager create(FlagEncoderFactory factory, String ghLoc) { Directory dir = new RAMDirectory(ghLoc, true); StorableProperties properties = new StorableProperties(dir); if (!properties.loadExisting()) throw new IllegalStateException("Cannot load properties to fetch EncodingManager configuration at: " + dir.getLocation()); // check encoding for compatibility properties.checkVersions(false); String acceptStr = properties.get("graph.flag_encoders"); if (acceptStr.isEmpty()) throw new IllegalStateException("EncodingManager was not configured. And no one was found in the graph: " + dir.getLocation()); int bytesForFlags = 4; if ("8".equals(properties.get("graph.bytes_for_flags"))) bytesForFlags = 8; return new EncodingManager(factory, acceptStr, bytesForFlags); }
@Test public void testStore() { String dir = "./target/test"; Helper.removeDir(new File(dir)); StorableProperties instance = new StorableProperties(createDir(dir, true)); instance.create(1000); instance.put("test.min", 123); instance.put("test.max", 321); instance.flush(); instance.close(); instance = new StorableProperties(createDir(dir, true)); assertTrue(instance.loadExisting()); assertEquals("123", instance.get("test.min")); assertEquals("321", instance.get("test.max")); instance.close(); Helper.removeDir(new File(dir)); }
@Test public void testVersionCheck() { StorableProperties instance = new StorableProperties(createDir("", false)); instance.putCurrentVersions(); assertTrue(instance.checkVersions(true)); instance.put("nodes.version", 0); assertFalse(instance.checkVersions(true)); try { instance.checkVersions(false); assertTrue(false); } catch (Exception ex) { } instance.close(); }
/** * 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"); dir.create(); long initSize = Math.max(byteCount, 100); properties.create(100); properties.put("graph.bytes_for_flags", encodingManager.getBytesForFlags()); properties.put("graph.flag_encoders", encodingManager.toDetailsString()); properties.put("graph.byte_order", dir.getByteOrder()); properties.put("graph.dimension", baseGraph.nodeAccess.getDimension()); properties.putCurrentVersions(); baseGraph.create(initSize); for (CHGraphImpl cg : chGraphs) { cg.create(byteCount); } properties.put("graph.ch.weightings", getCHWeightings().toString()); return this; }
@Override public boolean loadExisting() { baseGraph.checkInit(); if (properties.loadExisting()) { properties.checkVersions(false); // check encoding for compatibility String flagEncodersStr = properties.get("graph.flag_encoders"); if (!flagEncodersStr.isEmpty() && !encodingManager.toDetailsString().equalsIgnoreCase(flagEncodersStr)) { throw new IllegalStateException("Encoding does not match:" + "\nGraphhopper config: " + encodingManager.toDetailsString() + "\nGraph: " + flagEncodersStr + "\nChange configuration to match the graph or delete " + dir.getLocation()); } String byteOrder = properties.get("graph.byte_order"); if (!byteOrder.equalsIgnoreCase("" + dir.getByteOrder())) throw new IllegalStateException("Configured graph.byte_order (" + dir.getByteOrder() + ") is not equal to loaded " + byteOrder + ""); String bytesForFlags = properties.get("graph.bytes_for_flags"); if (!bytesForFlags.equalsIgnoreCase("" + encodingManager.getBytesForFlags())) throw new IllegalStateException("Configured graph.bytes_for_flags (" + encodingManager.getBytesForFlags() + ") is not equal to loaded " + bytesForFlags); String dim = properties.get("graph.dimension"); baseGraph.loadExisting(dim); checkIfConfiguredAndLoadedWeightingsCompatible(); for (CHGraphImpl cg : chGraphs) { if (!cg.loadExisting()) throw new IllegalStateException("Cannot load " + cg); } return true; } return false; }
@Test public void testLoad() { StorableProperties instance = new StorableProperties(createDir("", false)); // an in-memory storage does not load anything assertFalse(instance.loadExisting()); instance = new StorableProperties(createDir("", true)); assertFalse(instance.loadExisting()); instance.close(); }
public synchronized void putCurrentVersions() { put("nodes.version", Constants.VERSION_NODE); put("edges.version", Constants.VERSION_EDGE); put("geometry.version", Constants.VERSION_GEOMETRY); put("location_index.version", Constants.VERSION_LOCATION_IDX); put("name_index.version", Constants.VERSION_NAME_IDX); put("shortcuts.version", Constants.VERSION_SHORTCUT); }
@Override public void run() { String errorKey = Landmark.PREPARE + "error." + name; try { Thread.currentThread().setName(name); properties.put(errorKey, "LM preparation incomplete"); plm.doWork(); properties.remove(errorKey); properties.put(Landmark.PREPARE + "date." + name, Helper.createFormatter().format(new Date())); } catch (Exception ex) { LOGGER.error("Problem while LM preparation " + name, ex); properties.put(errorKey, ex.getMessage()); } } });
@Override public void close() { properties.close(); baseGraph.close(); for (CHGraphImpl cg : chGraphs) { cg.close(); } }
public GraphHopperStorage(List<? extends Weighting> chWeightings, Directory dir, final EncodingManager encodingManager, boolean withElevation, GraphExtension extendedStorage) { if (extendedStorage == null) throw new IllegalArgumentException("GraphExtension cannot be null, use NoOpExtension"); if (encodingManager == null) throw new IllegalArgumentException("EncodingManager needs to be non-null since 0.7. Create one using new EncodingManager or EncodingManager.create(flagEncoderFactory, ghLocation)"); this.encodingManager = encodingManager; this.dir = dir; this.properties = new StorableProperties(dir); InternalGraphEventListener listener = new InternalGraphEventListener() { @Override public void initStorage() { for (CHGraphImpl cg : chGraphs) { cg.initStorage(); } } @Override public void freeze() { for (CHGraphImpl cg : chGraphs) { cg._freeze(); } } }; this.baseGraph = new BaseGraph(dir, encodingManager, withElevation, listener, extendedStorage); for (Weighting w : chWeightings) { chGraphs.add(new CHGraphImpl(w, dir, this.baseGraph)); } }
@Override public void flush() { for (CHGraphImpl cg : chGraphs) { cg.setEdgesHeader(); cg.flush(); } baseGraph.flush(); properties.flush(); }
public synchronized boolean checkVersions(boolean silent) { if (!check("nodes", Constants.VERSION_NODE, silent)) return false; if (!check("edges", Constants.VERSION_EDGE, silent)) return false; if (!check("geometry", Constants.VERSION_GEOMETRY, silent)) return false; if (!check("location_index", Constants.VERSION_LOCATION_IDX, silent)) return false; if (!check("name_index", Constants.VERSION_NAME_IDX, silent)) return false; if (!check("shortcuts", Constants.VERSION_SHORTCUT, silent)) return false; // The check for the encoder version is done in EncoderManager, as this class does not know about the // registered encoders and their version return true; }
@Override public boolean loadExisting() { baseGraph.checkInit(); if (properties.loadExisting()) { properties.checkVersions(false); String flagEncodersStr = properties.get("graph.flag_encoders"); String byteOrder = properties.get("graph.byte_order"); if (!byteOrder.equalsIgnoreCase("" + dir.getByteOrder())) throw new IllegalStateException("Configured graph.byte_order (" + dir.getByteOrder() + ") is not equal to loaded " + byteOrder + ""); String bytesForFlags = properties.get("graph.bytes_for_flags"); if (!bytesForFlags.equalsIgnoreCase("" + encodingManager.getBytesForFlags())) throw new IllegalStateException("Configured graph.bytes_for_flags (" + encodingManager.getBytesForFlags() + ") is not equal to loaded " + bytesForFlags); String dim = properties.get("graph.dimension"); baseGraph.loadExisting(dim); String loadedCHWeightings = properties.get("graph.ch.weightings"); String configuredCHWeightings = getCHWeightings().toString(); if (!loadedCHWeightings.equals(configuredCHWeightings))
boolean check(String key, int vers, boolean silent) { String str = get(key + ".version"); if (!str.equals(vers + "")) { if (silent) return false; throw new IllegalStateException("Version of " + key + " unsupported: " + str + ", expected:" + vers + ". " + "Make sure you are using the same GraphHopper version for reading the files that was used for creating them. " + "See https://discuss.graphhopper.com/t/722"); } return true; }
/** * 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"); dir.create(); long initSize = Math.max(byteCount, 100); properties.create(100); properties.put("graph.bytes_for_flags", encodingManager.getBytesForFlags()); properties.put("graph.flag_encoders", encodingManager.toDetailsString()); properties.put("graph.byte_order", dir.getByteOrder()); properties.put("graph.dimension", baseGraph.nodeAccess.getDimension()); properties.putCurrentVersions(); baseGraph.create(initSize); for (CHGraphImpl cg : chGraphs) { cg.create(byteCount); } properties.put("graph.ch.weightings", getCHWeightings().toString()); return this; }
@Override public void run() { // toString is not taken into account so we need to cheat, see http://stackoverflow.com/q/6113746/194609 for other options Thread.currentThread().setName(name); prepare.doWork(); properties.put(CH.PREPARE + "date." + name, createFormatter().format(new Date())); } }, name);
@Override public void run() { String errorKey = CH.PREPARE + "error." + name; try { // toString is not taken into account so we need to cheat, see http://stackoverflow.com/q/6113746/194609 for other options Thread.currentThread().setName(name); properties.put(errorKey, "CH preparation incomplete"); prepare.doWork(); properties.remove(errorKey); properties.put(CH.PREPARE + "date." + name, Helper.createFormatter().format(new Date())); } catch (Exception ex) { LOGGER.error("Problem while CH preparation " + name, ex); properties.put(errorKey, ex.getMessage()); } } });
@Override public void close() { properties.close(); baseGraph.close(); for (CHGraphImpl cg : chGraphs) { cg.close(); } }
public GraphHopperStorage(List<? extends Weighting> chWeightings, Directory dir, final EncodingManager encodingManager, boolean withElevation, GraphExtension extendedStorage) { if (extendedStorage == null) throw new IllegalArgumentException("GraphExtension cannot be null, use NoOpExtension"); if (encodingManager == null) throw new IllegalArgumentException("EncodingManager needs to be non-null since 0.7. Create one using new EncodingManager or EncodingManager.create(flagEncoderFactory, ghLocation)"); this.encodingManager = encodingManager; this.dir = dir; this.properties = new StorableProperties(dir); InternalGraphEventListener listener = new InternalGraphEventListener() { @Override public void initStorage() { for (CHGraphImpl cg : chGraphs) { cg.initStorage(); } } @Override public void freeze() { for (CHGraphImpl cg : chGraphs) { cg._freeze(); } } }; this.baseGraph = new BaseGraph(dir, encodingManager, withElevation, listener, extendedStorage); for (Weighting w : chWeightings) { chGraphs.add(new CHGraphImpl(w, dir, this.baseGraph)); } }