Commit 3fd293bc authored by Daniel Eggert's avatar Daniel Eggert
Browse files

revised spatial index access.

parent c38d1852
......@@ -109,29 +109,30 @@ public class SceneDatabase {
INDEX_PORT = Integer.parseInt(props.getProperty(PROP_KEY_INDEX_PORT, Integer.toString(MediatorConstants.PORT)));
}
private static final Charset CHARSET = Charset.forName("US-ASCII");
private static SceneDatabase instance = null;
private Connection con;
private final PreparedStatement pstDatasetsQuery;
private final PreparedStatement pstVirtualSensorsQuery;
private final PreparedStatement pstScenesSearchQuery;
private final PreparedStatement pstScenesCountQuery;
private final PreparedStatement pstScenesCountSpatOnlyQuery;
private final PreparedStatement pstScenesQuery;
private final PreparedStatement pstCountScenesQuery;
private final PreparedStatement pstMeasurementsQuery;
private final PreparedStatement pstSceneEntityIDQuery;
private final PreparedStatement pstSceneAndDatasetInfoQuery;
private final PreparedStatement pstSentinelUUIDQuery;
private final PreparedStatement pstSceneFilenameAndProcLevelUpdateQuery;
private final PreparedStatement pstGranuleProcLevelUpdateQuery;
private final PreparedStatement pstUpdateProcLevelQuery;
private final PreparedStatement pstProcessJobQuery;
private static final Charset CHARSET = Charset.forName("US-ASCII");
private static SceneDatabase instance = null;
private Connection con;
private final PreparedStatement pstDatasetsQuery;
private final PreparedStatement pstVirtualSensorsQuery;
private final PreparedStatement pstScenesSearchQuery;
private final PreparedStatement pstScenesCountQuery;
private final PreparedStatement pstScenesCountSpatOnlyQuery;
private final PreparedStatement pstScenesQuery;
private final PreparedStatement pstCountScenesQuery;
private final PreparedStatement pstMeasurementsQuery;
private final PreparedStatement pstSceneEntityIDQuery;
private final PreparedStatement pstSceneAndDatasetInfoQuery;
private final PreparedStatement pstSentinelUUIDQuery;
private final PreparedStatement pstSceneFilenameAndProcLevelUpdateQuery;
private final PreparedStatement pstGranuleProcLevelUpdateQuery;
private final PreparedStatement pstUpdateProcLevelQuery;
private final PreparedStatement pstProcessJobQuery;
// private final PreparedStatement pstUpdateJobStatus;
private SpatialIndexMediator spatialIndexMediator;
private final SpatialIndexMediator spatialIndex;
// private SpatialIndexMediator spatialIndex;
private SceneDatabase() {
LOG.debug("initializing scenedatabase connector");
......@@ -151,9 +152,6 @@ public class SceneDatabase {
pstGranuleProcLevelUpdateQuery = prepareGranuleProcLevelUpdateStatement();
pstUpdateProcLevelQuery = prepareUpdateProcLevelQuery();
pstProcessJobQuery = prepareProcessJobQuery();
System.out.println("connecting to index mediator server at: " + INDEX_HOST + ":" + INDEX_PORT);
spatialIndex = SpatialIndexMediator.connect(INDEX_HOST, INDEX_PORT);
}
/**
......@@ -742,6 +740,20 @@ public class SceneDatabase {
}
}
private SpatialIndexMediator getSpatialIndex() {
if (spatialIndexMediator == null) {
// try connect
try {
spatialIndexMediator = SpatialIndexMediator.connect(INDEX_HOST, INDEX_PORT);
} catch (Throwable t) {
LOG.error("Unable to connect to spatial index mediator", t);
spatialIndexMediator = null;
}
}
return spatialIndexMediator;
}
/**
* Returns a compact representation of the resulting count query. The returned buffer has the following schema:<br>
* [short][2byte] number of datasets<br>
......@@ -756,8 +768,11 @@ public class SceneDatabase {
* @param polygon
* @return
*/
public void getCompactSceneCountFromIndex(final Timestamp start, final Timestamp end, final int seasonCode, final byte maxCloudcover,
final byte minProclevel, final double[] polygon, ByteBuffer buffer) {
public void getCompactSceneCountFromIndex(final Timestamp start, final Timestamp end, final int seasonCode, final byte maxCloudcover, final byte minProclevel,
final double[] polygon, ByteBuffer buffer) {
SpatialIndexMediator spatialIndex = getSpatialIndex();
if (spatialIndex == null) {
// no spatial index - use the database
// getCompactSceneCount(start, end, polygon, buffer);
......@@ -771,8 +786,11 @@ public class SceneDatabase {
// calculate the area, width and height of the region of interest polygon
// getCompactMeasurementsForPolygon(polygon, buffer);
spatialIndex.countScenesGroupedByDatasets(e, start, end, seasonCode, maxCloudcover, minProclevel, buffer);
try {
spatialIndex.countScenesGroupedByDatasets(e, start, end, seasonCode, maxCloudcover, minProclevel, buffer);
} catch (Throwable t) {
spatialIndexMediator = null;
}
}
}
......@@ -820,11 +838,14 @@ public class SceneDatabase {
*/
public void getCompactSceneListFromIndex(final short datasetid, final Timestamp start, final Timestamp end, final int seasonCode, final byte minCloudcover,
final byte maxCloudcover, final double[] polygon, final byte minProclevel, final byte maxProclevel, ByteBuffer buffer) {
SpatialIndexMediator spatialIndex = getSpatialIndex();
if (spatialIndex == null) {
// no spatial index - use the database
// getCompactScenes(datasetid, start, end, polygon)
// getCompactSceneCount(start, end, polygon, buffer);
throw new RuntimeException("spatial index mediator not available and no fallback query implemented");
// throw new RuntimeException("spatial index mediator not available and no fallback query implemented");
LOG.error("spatial index mediator not available and no fallback query implemented");
} else {
Envelope e = new Envelope();
......@@ -832,7 +853,11 @@ public class SceneDatabase {
e.expandToInclude(polygon[i], polygon[i + 1]);
}
spatialIndex.getFullSceneDataForDataset(e, start, end, seasonCode, minCloudcover, maxCloudcover, datasetid, minProclevel, maxProclevel, buffer);
try {
spatialIndex.getFullSceneDataForDataset(e, start, end, seasonCode, minCloudcover, maxCloudcover, datasetid, minProclevel, maxProclevel, buffer);
} catch (Throwable t) {
spatialIndexMediator = null;
}
}
}
......@@ -842,13 +867,19 @@ public class SceneDatabase {
* @return
*/
public String getSpatialIndexVersionInfo() {
if (spatialIndex == null) {
return "unable to obtain version of spatial index: not connected";
} else {
return spatialIndex.getServerVersionInfo();
SpatialIndexMediator spatialIndex = getSpatialIndex();
if (spatialIndex != null) {
try {
return spatialIndex.getServerVersionInfo();
} catch (Throwable t) {
spatialIndexMediator = null;
}
}
return "unable to obtain version of spatial index: not connected";
}
public String getVersionInfo() {
return Version.getVersionInfo();
}
......@@ -865,8 +896,10 @@ public class SceneDatabase {
* [long] timestamp<br>
* [byte] cloudcover
*/
public void getCompactSceneMetaDataListFromIndex(final short datasetid, final Timestamp start, final Timestamp end, final int seasonCode,
final byte minCloudcover, final byte maxCloudcover, final double[] polygon, final byte minProclevel, final byte maxProclevel, ByteBuffer buffer) {
public void getCompactSceneMetaDataListFromIndex(final short datasetid, final Timestamp start, final Timestamp end, final int seasonCode, final byte minCloudcover,
final byte maxCloudcover, final double[] polygon, final byte minProclevel, final byte maxProclevel, ByteBuffer buffer) {
SpatialIndexMediator spatialIndex = getSpatialIndex();
if (spatialIndex == null) {
// no spatial index - use the database
// getCompactScenes(datasetid, start, end, polygon)
......@@ -879,15 +912,21 @@ public class SceneDatabase {
e.expandToInclude(polygon[i], polygon[i + 1]);
}
spatialIndex.getSceneMetaDataForDataset(e, start, end, seasonCode, minCloudcover, maxCloudcover, datasetid, minProclevel, maxProclevel, buffer);
try {
spatialIndex.getSceneMetaDataForDataset(e, start, end, seasonCode, minCloudcover, maxCloudcover, datasetid, minProclevel, maxProclevel, buffer);
} catch (Throwable t) {
spatialIndexMediator = null;
}
}
}
/**
* Stores a compact representation of the resulting scene query in the given buffer. Here only the scene bounds are stored in the buffer.
*/
public void getCompactSceneBoundsListFromIndex(final short datasetid, final Timestamp start, final Timestamp end, final int seasonCode,
final byte minCloudcover, final byte maxCloudcover, final double[] polygon, final byte minProclevel, final byte maxProclevel, ByteBuffer buffer) {
public void getCompactSceneBoundsListFromIndex(final short datasetid, final Timestamp start, final Timestamp end, final int seasonCode, final byte minCloudcover,
final byte maxCloudcover, final double[] polygon, final byte minProclevel, final byte maxProclevel, ByteBuffer buffer) {
SpatialIndexMediator spatialIndex = getSpatialIndex();
if (spatialIndex == null) {
// no spatial index - use the database
// getCompactScenes(datasetid, start, end, polygon)
......@@ -900,7 +939,11 @@ public class SceneDatabase {
e.expandToInclude(polygon[i], polygon[i + 1]);
}
spatialIndex.getSceneBoundsForDataset(e, start, end, seasonCode, minCloudcover, maxCloudcover, datasetid, minProclevel, maxProclevel, buffer);
try {
spatialIndex.getSceneBoundsForDataset(e, start, end, seasonCode, minCloudcover, maxCloudcover, datasetid, minProclevel, maxProclevel, buffer);
} catch (Throwable t) {
spatialIndexMediator = null;
}
}
}
......@@ -1112,8 +1155,7 @@ public class SceneDatabase {
if (datasetid != 249 && affectedRows > 1) {
throw new IllegalStateException("More than one row (" + affectedRows + ") was affected by the filename update");
} else if (affectedRows == 0) {
throw new IllegalArgumentException(
"The update did not affect any row, wrong sceneid? (" + sceneid + " | " + filename + " | " + proc_level.name() + ")");
throw new IllegalArgumentException("The update did not affect any row, wrong sceneid? (" + sceneid + " | " + filename + " | " + proc_level.name() + ")");
}
} catch (SQLException e) {
e.printStackTrace();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment