Lorenz Bühmann created JENA-2310:
------------------------------------
Summary: NPE with geospatial property function due to query
rewrite index issue
Key: JENA-2310
URL: https://issues.apache.org/jira/browse/JENA-2310
Project: Apache Jena
Issue Type: Bug
Components: GeoSPARQL
Affects Versions: Jena 4.4.0
Environment: {code:java}
// code placeholder
{code}
{code:java}
// Some comments here
public String getFoo()
{
return foo;
}
{code}
Reporter: Lorenz Bühmann
Using a GeoSPARQL query with a geospatial property function, e.g.
{code:java}
SELECT * {
:x geo:hasGeometry ?geo1 .
?s2 geo:hasGeometry ?geo2 .
?geo1 geo:sfContains ?geo2
}{code}
leads to an nondeterministic NPE when we're doing this query a a larger dataset.
The reason is explained here:
- evaluation of the property function leads to {{GenericPropertyFunction}}
class with
{code:java}
private QueryIterator bothBound(Binding binding, Node subject, Node predicate,
Node object, ExecutionContext execCxt) {
Graph graph = execCxt.getActiveGraph();
QueryRewriteIndex queryRewriteIndex =
QueryRewriteIndex.retrieve(execCxt);
Boolean isPositiveResult = queryRewrite(graph, subject, predicate,
object, queryRewriteIndex);
if (isPositiveResult) {
{code}
which leads to the query rewrite part and in {{QueryRewriteIndex}} class we have
{code:java}
if (index.containsKey(key)) {
result = index.get(key);
} else {
result = propertyFunction.testFilterFunction(subjectGeometryLiteral,
objectGeometryLiteral);
index.put(key, result);
}
{code}
{{index}} is an {{ExpiringMap}} which extends {{ConcurrentHashMap}} with an
additional {{TimerTask}} clearing the map periodically - and this is why we get
an NPE sometimes. Have a look at the lines above, in particular
{code:java}
if (index.containsKey(key)){
result = index.get(key);
}
{code}
that part isn't atomic, thus, while {{containsKey}} could be true, the
{{TimerTask}} might clear the map before we go to get - this will lead to a
{{null}} value returned which will lead to an NPE in the check
Since Java 8 the atomic way is to use {{computeIfAbsent}}, i.e. we can do
{code:java}
Boolean result = index.computeIfAbsent(key, k ->
propertyFunction.testFilterFunction(subjectGeometryLiteral,
objectGeometryLiteral));{code}
--
This message was sent by Atlassian Jira
(v8.20.1#820001)