Skip to content

Commit

Permalink
Advanced caching + some refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
pietervdvn committed Jul 24, 2018
1 parent 0938fe0 commit bf451d8
Show file tree
Hide file tree
Showing 13 changed files with 234 additions and 116 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ OpenSeaLab/cache/*
OpenSeaLab/www/.DS_Store
**.DS_Store
OpenSeaLab/www/map.html
OpenSeaLab/geology.json
Binary file modified OpenSeaLab/VLIZ-Server.jar
Binary file not shown.
24 changes: 21 additions & 3 deletions OpenSeaLab/prod.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,33 @@ seabed=http://213.122.160.75/scripts/mapserv.exe?map=D:/Websites/MeshAtlantic/ma

seabed-default-type=EUSM2016_simplified200

seabed-min-lat=23
seabed-max-lat=75
seabed-min-lon=-33
seabed-max-lon=46

# geology
geology=http://drive.emodnet-geology.eu/geoserver/EMODnetGeology/wfs?service=WFS&version=1.1.1&request=GetFeature&typeName={type}&outputFormat=application/json
geology=file:///home/pietervdvn/git/vliz/OpenSeaLab/geology.json
geology-url=http://drive.emodnet-geology.eu/geoserver/EMODnetGeology/wfs?service=WFS&version=1.1.1&request=GetFeature&typeName={type}&srsName=EPSG:4326&outputFormat=application/json

geology-default-type=seabed_substrate250k
geology-other-type=EMODnetGeology:marine_aggregate_deposits_pts

geology-default-type=EMODnetGeology:marine_aggregate_deposits_pts
geology-min-lat=23
geology-max-lat=75
geology-min-lon=-33
geology-max-lon=46

# physics
physics=http://geoserver.emodnet-physics.eu/geoserver/emodnet/ows?service=WFS&version=1.0.0&request=GetFeature&typeName={type}&bbox={bbox}

physics-default-type=emodnet:PlatformAll

physics-min-lat=23
physics-max-lat=75
physics-min-lon=-33
physics-max-lon=46


#bathymetry
bathymetry=http://rest.emodnet-bathymetry.eu/depth/profile?geom=LINESTRING
bathymetry=http://rest.emodnet-bathymetry.eu/depth/profile?geom=LINESTRING
22 changes: 20 additions & 2 deletions OpenSeaLab/src/feature/GeometryFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,31 @@
import java.util.ArrayList;
import java.util.List;

import exceptions.FatalException;

public class GeometryFactory {
public static Geometry newPoint(List<Double> point) {
public static Geometry newPoint(List<?> point) {
if (point == null || point.size() == 0) {
return null;
}
// Geojson specs : lon lat
return new Point(point.get(1), point.get(0));
double lat = getDouble(point.get(1));
double lon = getDouble(point.get(0));
if(lat > 90 || lon > 180)
{
throw new FatalException("Wrong coordinate system. Latitude is above 90 degrees or longitude above 180");
}



return new Point(lat, lon);
}

private static double getDouble(Object val) {
if (val instanceof Long) {
return ((Long) val).doubleValue();
}
return (double) val;
}

public static Geometry newPoint(String s) {
Expand Down
22 changes: 19 additions & 3 deletions OpenSeaLab/src/feature/Rectangle.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package feature;

import main.AppContext;

public class Rectangle extends Geometry {

private static final long serialVersionUID = 1L;
Expand All @@ -19,6 +21,20 @@ public Rectangle(String minLat, String minLong, String maxLat, String maxLong) {
validateArgument("maxLat", maxLat), validateArgument("maxLong", maxLong));
}

/**
* Constructs a rectangle based on the min-lat/max-lat as defined in the
* properties
*
* @param layerName
* @param ctx
*/
public Rectangle(AppContext ctx, String layerName) {
this(Integer.parseInt(ctx.getProperty(layerName + "-min-lat")), //
Integer.parseInt(ctx.getProperty(layerName + "-min-lon")), //
Integer.parseInt(ctx.getProperty(layerName + "-max-lat")), //
Integer.parseInt(ctx.getProperty(layerName + "-max-lon")));
}

@Override
public String getCoordinates() {
return "[[ [" + minLon + ", " + minLat + "], [" + maxLon + ", " + maxLat + "] ]]";
Expand Down Expand Up @@ -100,9 +116,9 @@ public Rectangle bboxWith(Rectangle r) {
return new Rectangle(Math.min(this.minLat, r.minLat), Math.min(this.minLon, r.minLon),
Math.max(this.maxLat, r.maxLat), Math.max(this.maxLon, r.maxLon));
}

public boolean edgePoint(int lat, int lon) {
return (Math.floor(this.minLat) == lat || Math.ceil(this.maxLat) == lat +1
|| Math.floor(this.minLon) == lon || Math.ceil(this.maxLon) == lon+1);
return (Math.floor(this.minLat) == lat || Math.ceil(this.maxLat) == lat + 1 || Math.floor(this.minLon) == lon
|| Math.ceil(this.maxLon) == lon + 1);
}
}
9 changes: 9 additions & 0 deletions OpenSeaLab/src/feature/Square.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package feature;

public class Square extends Rectangle{

public Square(double lat, double lon) {
super(lat, lon, lat+1, lon+1);
}

}
32 changes: 22 additions & 10 deletions OpenSeaLab/src/main/CachingManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,18 @@ public class CachingManager {
private final String layerName;

public CachingManager(String layerName, String cache, String pattern) throws IOException {
this.cache = cache;
this.cache = cache + "/" + layerName;
this.pattern = pattern;
this.layerName = layerName;
Path cacheDir = FileSystems.getDefault().getPath(cache);
if (!Files.exists(cacheDir) || !Files.isDirectory(cacheDir)) {
Files.createDirectory(cacheDir);
LOGGER.log(Level.FINE, "Caching directory created ");
}
}

public void store(String data, Rectangle bbox, String type) {
Path p = getPath(bbox, type);
initCacheDir(p);
try (Writer writer = Files.newBufferedWriter(p, StandardCharsets.UTF_8)) {
if (!Files.exists(p.getParent())) {
Files.createDirectories(p.getParent());
}
writer.write(data);
LOGGER.log(Level.FINE, "Cache file " + p + " created");
} catch (IOException io) {
Expand All @@ -45,6 +44,7 @@ public void store(String data, Rectangle bbox, String type) {

public void store(Serializable ser, Rectangle bbox, String type) {
Path p = getPath(bbox, type);
initCacheDir(p);
try (ObjectOutputStream fileOut = new ObjectOutputStream(Files.newOutputStream(p))) {
fileOut.writeObject(ser);
fileOut.flush();
Expand All @@ -58,7 +58,7 @@ public <T> T restore(Rectangle bbox, String type) {
try (ObjectInputStream in = new ObjectInputStream(Files.newInputStream(getPath(bbox, type)))) {
return (T) in.readObject();
} catch (Exception e) {
System.out.println("Could not load "+getPath(bbox, type)+", purging it from cache");
System.out.println("Could not load " + getPath(bbox, type) + ", purging it from cache");
new File(getPath(bbox, type).toString()).delete();
return null;
}
Expand All @@ -74,15 +74,27 @@ public Path getPath(Rectangle bbox, String type) {

private String getId(Rectangle bbox, String type) {
if (type == null) {
type = "";
return layerName + "_" + bbox.getMinLat() + "_" + bbox.getMinLon() + "_" + bbox.getMaxLat() + "_"
+ bbox.getMaxLon();
} else {
type += "_";
return type + "/" + type + "_" + bbox.getMinLat() + "_" + bbox.getMinLon() + "_"
+ bbox.getMaxLat() + "_" + bbox.getMaxLon();

}
return layerName + "_" + type + bbox.getMinLat() + "_" + bbox.getMinLon() + "_" + bbox.getMaxLat() + "_" + bbox.getMaxLon();
}

public boolean isInCache(Rectangle bbox, String type) {
return Files.exists(getPath(bbox, type));
}

private static void initCacheDir(Path p) {
if (!Files.exists(p.getParent())) {
try {
Files.createDirectories(p.getParent());
} catch (IOException e) {
throw new FatalException(e);
}
}
}

}
58 changes: 42 additions & 16 deletions OpenSeaLab/src/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,43 @@
import bathymetry.BathymetryDAO;
import bathymetry.BathymetryServlet;
import bathymetry.UCCBathymetry;
import feature.Rectangle;
import vectorLayers.VectorLayersDAO;
import vectorLayers.VectorLayersServlet;
import vectorLayers.UCCVectorLayers;

public class Main {
private static final Logger LOGGER = Logger.getLogger(Main.class.getName());

public static void main(String[] args) throws Exception {
System.out.println("VLIZ Server 0.1");
System.out.println("VLIZ Server 0.2");
try {
LOGGER.info("Loading app configuration...");
AppContext appContext = new AppContext();
AppContext.configLogger("log.properties");


if(args.length != 0 && args[0].equals("--populate-cache")) {
appContext.loadProperties("prod.properties");
populateCache(appContext, "geology");
return;
}
appContext.loadProperties(args.length == 0 ? "prod.properties" : args[0]);
BathymetryDAO bathymetryDAO = new BathymetryDAO(appContext.getProperty("bathymetry"),
appContext.getProperty("cache-dir"), appContext.getProperty("bathymetry-stat"));
UCCBathymetry uccBathymetry = new UCCBathymetry(bathymetryDAO);

startServer(appContext, uccBathymetry);
startServer(appContext);
} catch (Exception exc) {
LOGGER.log(Level.SEVERE, "App configuration failed !", exc);
}
}

private static void startServer(AppContext appContext, UCCBathymetry uccBathymetry) throws Exception {
private static void startServer(AppContext appContext) throws Exception {
LOGGER.info("Starting the server...");
Server server = new Server(Integer.parseInt(appContext.getProperty(("port"))));
WebAppContext context = new WebAppContext();

BathymetryDAO bathymetryDAO = new BathymetryDAO(appContext.getProperty("bathymetry"),
appContext.getProperty("cache-dir"), appContext.getProperty("bathymetry-stat"));
UCCBathymetry uccBathymetry = new UCCBathymetry(bathymetryDAO);

context.setResourceBase("www");
context.addServlet(new ServletHolder(new DefaultServlet()), "/");

Expand All @@ -58,22 +66,40 @@ private static void startServer(AppContext appContext, UCCBathymetry uccBathymet
LOGGER.info("The server is listening...");
}

private static void initVectorLayerServlet(String layerName, AppContext appContext, WebAppContext context) throws IOException {
String defaultType = appContext.getProperty(layerName+"-default-type");
private static void populateCache(AppContext appContext, String layerName) throws IOException {
populateCache(appContext, "geology", new Rectangle(appContext, "geology"),
appContext.getProperty(layerName + "-default-type"));

}

VectorLayersDAO vectorLayersDAO = new VectorLayersDAO(layerName,appContext.getProperty(layerName),defaultType);
UCCVectorLayers uccVectorLayers = new UCCVectorLayers(vectorLayersDAO);
private static void populateCache(AppContext appContext, String layerName, Rectangle bbox, String type)
throws IOException {
PiecedCachingManager pcm = createPCM(layerName, appContext);
pcm.loadAndCacheAll(bbox, type);
}

private static void initVectorLayerServlet(String layerName, AppContext appContext, WebAppContext context)
throws IOException {
String defaultType = appContext.getProperty(layerName + "-default-type");

PiecedCachingManager pcm = createPCM(layerName, appContext);
HttpServlet seabedServlet = new VectorLayersServlet(pcm, defaultType);

context.addServlet(new ServletHolder(seabedServlet), "/" + layerName);
}

private static PiecedCachingManager createPCM(String layerName, AppContext appContext) throws IOException {
String defaultType = appContext.getProperty(layerName + "-default-type");

VectorLayersDAO vectorLayersDAO = new VectorLayersDAO(layerName, appContext.getProperty(layerName),
defaultType);
CachingManager dataCache = new CachingManager(layerName, appContext.getProperty("cache-dir"),
"data-{id}.FeatureCollection");
CachingManager statsCache = new CachingManager(layerName, appContext.getProperty("cache-dir"),
"stats-{id}.SurfaceCount");

PiecedCachingManager pcm = new PiecedCachingManager(layerName,uccVectorLayers, dataCache, statsCache);

HttpServlet seabedServlet = new VectorLayersServlet(pcm, defaultType);

context.addServlet(new ServletHolder(seabedServlet), "/"+layerName);
PiecedCachingManager pcm = new PiecedCachingManager(vectorLayersDAO, dataCache, statsCache);
return pcm;
}

}
Loading

0 comments on commit bf451d8

Please sign in to comment.