Skip to content

Commit

Permalink
Fixed another geoindex-related bug where the first query would fail b…
Browse files Browse the repository at this point in the history
…ecuase the QueryExec's context did not yet contain the spatial index.
  • Loading branch information
Aklakan committed Oct 22, 2023
1 parent 7cd2543 commit 93b740d
Showing 1 changed file with 41 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
import org.aksw.sparql_integrate.cli.cmd.CmdSparqlIntegrateMain;
import org.aksw.sparql_integrate.cli.cmd.CmdSparqlIntegrateMain.OutputSpec;
import org.apache.jena.geosparql.configuration.GeoSPARQLConfig;
import org.apache.jena.geosparql.spatial.SpatialIndex;
import org.apache.jena.irix.IRIx;
import org.apache.jena.query.ARQ;
import org.apache.jena.query.Dataset;
Expand Down Expand Up @@ -525,12 +526,24 @@ public void beforeExec() {
r = QueryExecWrapperTxn.wrap(r, cb);

if (cmd.arqConfig.geoindex) {

// Spatial index creation must happen outside of a transaction!
r = new QueryExecWrapperBase<>(r) {
@Override
public void beforeExec() {
// Txn.executeWrite(finalDataset, () -> {
updateSpatialIndexIfDirty(finalDataset);
// });
Context finalDatasetCxt = finalDataset.getContext();
if (finalDatasetCxt != null && finalDatasetCxt.isFalseOrUndef(SPATIAL_INDEX_IS_CLEAN)) {
updateSpatialIndex(finalDataset);

// The the spatial index symbol is in the dataset's context
// copy it into the query exec's context.
// TODO This is hacky; can we avoid the copy?
Context execCxt = getContext();
if (execCxt != null) {
execCxt.set(SpatialIndex.SPATIAL_INDEX_SYMBOL,
finalDataset.getContext().get(SpatialIndex.SPATIAL_INDEX_SYMBOL));
}
}
}
};
}
Expand Down Expand Up @@ -567,9 +580,20 @@ public void beforeExec() {

@Override
public void beforeExec() {
spatialUpdateNeeded = isSpatialIndexUpdateImmediatelyRequired(ur);
if (spatialUpdateNeeded) {
updateSpatialIndexIfDirty(finalDataset);
Context finalDatasetCxt = finalDataset.getContext();
if (finalDatasetCxt != null && finalDatasetCxt.isFalseOrUndef(SPATIAL_INDEX_IS_CLEAN)) {
spatialUpdateNeeded = isSpatialIndexUpdateImmediatelyRequired(ur);
if (spatialUpdateNeeded) {
updateSpatialIndex(finalDataset);

// The the spatial index symbol is in the dataset's context
// copy it into the query exec's context.
Context execCxt = getContext();
if (execCxt != null) {
execCxt.set(SpatialIndex.SPATIAL_INDEX_SYMBOL,
finalDataset.getContext().getAsString(SpatialIndex.SPATIAL_INDEX_SYMBOL));
}
}
}
}

Expand All @@ -581,7 +605,7 @@ public void afterExec() {
// Defer update
if (false && spatialUpdateNeeded) {
// Note: The update runs in the same transaction as the update!
updateSpatialIndexIfDirty(finalDataset);
updateSpatialIndex(finalDataset);
}
}
};
Expand Down Expand Up @@ -718,19 +742,17 @@ public void afterExec() {
}

/** Be careful not to call within a read transaction! */
public static void updateSpatialIndexIfDirty(Dataset dataset) {
public static void updateSpatialIndex(Dataset dataset) {
Context cxt = dataset.getContext();
if (cxt != null && cxt.isFalseOrUndef(SPATIAL_INDEX_IS_CLEAN)) {
logger.info("(Re-)computing geo index");
try {
GeoSPARQLConfig.setupSpatialIndex(dataset);
cxt.setTrue(SPATIAL_INDEX_IS_CLEAN);
} catch (Exception e) {
if (e.getMessage().toLowerCase().contains("no srs found")) {
// ignore - assuming simply no geodata present
} else {
logger.error("Failed to udpate geo index after update", e);
}
logger.info("(Re-)computing geo index");
try {
GeoSPARQLConfig.setupSpatialIndex(dataset);
cxt.setTrue(SPATIAL_INDEX_IS_CLEAN);
} catch (Exception e) {
if (e.getMessage().toLowerCase().contains("no srs found")) {
// ignore - assuming simply no geodata present
} else {
logger.error("Failed to udpate geo index after update", e);
}
}
}
Expand Down Expand Up @@ -838,8 +860,6 @@ public static void execStmt(

}



/**
* Checks whether transactions with the given transactional
* are thread-independent.
Expand Down

0 comments on commit 93b740d

Please sign in to comment.