Skip to content

Commit

Permalink
JENA-901 Avoid exposing the Guava cache as protected
Browse files Browse the repository at this point in the history
.. but just in case (and to match the rest of the class)
add protected methods for manipulating the cache
  • Loading branch information
stain committed Jun 24, 2016
1 parent a9b8dc1 commit 529fc96
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 40 deletions.
2 changes: 1 addition & 1 deletion jena-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-shadowed-guava</artifactId>
<artifactId>jena-shaded-guava</artifactId>
<version>3.1.1-SNAPSHOT</version>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ public class LPBRuleEngine {

/** Table mapping tabled goals to generators for those goals.
* This is here so that partial goal state can be shared across multiple queries.
*
* Note: Do no expose as protected/public, as this depends on
* the shadowed org.apache.jena.ext.com.google.common.*
*/
protected Cache<TriplePattern, Generator> tabledGoals = CacheBuilder.newBuilder()
Cache<TriplePattern, Generator> tabledGoals = CacheBuilder.newBuilder()
.maximumSize(MAX_CACHED_TABLED_GOALS).weakValues().build();

/** Set of generators waiting to be run */
Expand Down Expand Up @@ -130,7 +133,7 @@ public synchronized ExtendedIterator<Triple> find(TriplePattern goal) {
*/
public synchronized void reset() {
checkSafeToUpdate();
tabledGoals.invalidateAll();
clearCachedTabledGoals();
agenda.clear();
}

Expand Down Expand Up @@ -277,30 +280,23 @@ public synchronized void tablePredicate(Node predicate) {
* @param clauses the precomputed set of code blocks used to implement the goal
*/
public synchronized Generator generatorFor(final TriplePattern goal, final List<RuleClauseCode> clauses) {
try {
return tabledGoals.get(goal, new Callable<Generator>() {
@Override
public Generator call() {
/** FIXME: Unify with #generatorFor(TriplePattern) - but investigate what about
* the edge case that this method might have been called with the of goal == null
* or goal.size()==0 -- which gives different behaviour in
* LPInterpreter constructor than through the route of
* generatorFor(TriplePattern) which calls a different LPInterpreter constructor
* which would fill in from RuleStore.
*/
LPInterpreter interpreter = new LPInterpreter(LPBRuleEngine.this, goal, clauses, false);
activeInterpreters.add(interpreter);
Generator generator = new Generator(interpreter, goal);
schedule(generator);
return generator;
}
});
} catch (ExecutionException e) {
if (e.getCause() instanceof RuntimeException) {
throw (RuntimeException)e.getCause();
}
throw new RuntimeException(e);
}
return getCachedTabledGoal(goal, new Callable<Generator>() {
@Override
public Generator call() {
/** FIXME: Unify with #generatorFor(TriplePattern) - but investigate what about
* the edge case that this method might have been called with the of goal == null
* or goal.size()==0 -- which gives different behaviour in
* LPInterpreter constructor than through the route of
* generatorFor(TriplePattern) which calls a different LPInterpreter constructor
* which would fill in from RuleStore.
*/
LPInterpreter interpreter = new LPInterpreter(LPBRuleEngine.this, goal, clauses, false);
activeInterpreters.add(interpreter);
Generator generator = new Generator(interpreter, goal);
schedule(generator);
return generator;
}
});
}

/**
Expand All @@ -309,29 +305,38 @@ public Generator call() {
* @param goal the goal whose results are to be generated
*/
public synchronized Generator generatorFor(final TriplePattern goal) {
try {
return tabledGoals.get(goal, new Callable<Generator>() {
@Override
public Generator call() {
LPInterpreter interpreter = new LPInterpreter(LPBRuleEngine.this, goal, false);
activeInterpreters.add(interpreter);
Generator generator = new Generator(interpreter, goal);
schedule(generator);
return generator;
}
});
return getCachedTabledGoal(goal, new Callable<Generator>() {
@Override
public Generator call() {
LPInterpreter interpreter = new LPInterpreter(LPBRuleEngine.this, goal, false);
activeInterpreters.add(interpreter);
Generator generator = new Generator(interpreter, goal);
schedule(generator);
return generator;
}
});
}

protected Generator getCachedTabledGoal(TriplePattern goal,
Callable<Generator> callable) {
try {
return tabledGoals.get(goal, callable);
} catch (ExecutionException e) {
if (e.getCause() instanceof RuntimeException) {
throw (RuntimeException)e.getCause();
}
throw new RuntimeException(e);
}
}
}

long cachedTabledGoals() {
protected long cachedTabledGoals() {
return tabledGoals.size();
}

protected void clearCachedTabledGoals() {
tabledGoals.invalidateAll();
}

/**
* Register that a generator or specific generator state (Consumer choice point)
* is now ready to run.
Expand Down

0 comments on commit 529fc96

Please sign in to comment.