Skip to content

Commit

Permalink
Merge pull request #3081 from ibi-group/kryo-cached-elevations-dev1x
Browse files Browse the repository at this point in the history
Use Kryo library for cached elevation serialization/deserialization
  • Loading branch information
t2gran committed Jun 2, 2020
2 parents a334fcc + 6c5fbcc commit e48f8e8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ public static class Double extends PackedCoordinateSequence {
*/
double[] coords;

/**
* For use with Kryo library only. Kryo needs an no-arg constructor in order to deserialize data.
*/
public Double() {}

/**
* Builds a new packed coordinate sequence
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.opentripplanner.graph_builder.module.ned;

import com.fasterxml.jackson.databind.JsonNode;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.geotools.geometry.DirectPosition2D;
Expand Down Expand Up @@ -28,10 +30,9 @@
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -147,6 +148,16 @@ public ElevationModule(
this.multiThreadElevationCalculations = multiThreadElevationCalculations;
}

/**
* Use Kryo to serialize/deserialize cached elevations
*/
private Kryo makeKryo() {
Kryo kryo = new Kryo();
kryo.register(HashMap.class);
kryo.register(PackedCoordinateSequence.Double.class);
return kryo;
}

@Override
public void buildGraph(Graph graph, HashMap<Class<?>, Object> extra) {
this.graph = graph;
Expand All @@ -155,10 +166,11 @@ public void buildGraph(Graph graph, HashMap<Class<?>, Object> extra) {
if (readCachedElevations) {
// try to load in the cached elevation data
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(cachedElevationsFile));
cachedElevations = (HashMap<String, PackedCoordinateSequence>) in.readObject();
Kryo kryo = makeKryo();
Input input = new Input(new FileInputStream(cachedElevationsFile));
cachedElevations = (HashMap<String, PackedCoordinateSequence>) kryo.readClassAndObject(input);
log.info("Cached elevation data loaded into memory!");
} catch (IOException | ClassNotFoundException e) {
} catch (FileNotFoundException e) {
log.warn(graph.addBuilderAnnotation(new Graphwide(
String.format("Cached elevations file could not be read in due to error: %s!", e.getMessage()))));
}
Expand Down Expand Up @@ -225,9 +237,9 @@ public void buildGraph(Graph graph, HashMap<Class<?>, Object> extra) {
streetEdge.getElevationProfile());
}
try {
ObjectOutputStream out = new ObjectOutputStream(
new BufferedOutputStream(new FileOutputStream(cachedElevationsFile)));
out.writeObject(newCachedElevations);
Kryo kryo = makeKryo();
Output out = new Output(new BufferedOutputStream(new FileOutputStream(cachedElevationsFile)));
kryo.writeClassAndObject(out, newCachedElevations);
out.close();
} catch (IOException e) {
log.error(e.getMessage());
Expand Down

0 comments on commit e48f8e8

Please sign in to comment.