Heuristic:AKSW RDFUnit Query Caching For Repeated Execution
| Knowledge Sources | |
|---|---|
| Domains | Optimization, SPARQL, Test_Execution |
| Last Updated | 2026-10-16 00:00 GMT |
Overview
Cache parsed SPARQL query objects with a Guava LoadingCache to avoid re-parsing the same test case queries when executing a test suite across multiple datasets.
Description
When RDFUnit executes a test suite, each test case is converted to a SPARQL query string and then parsed by Jena's `QueryFactory.create()` into a `Query` object. Parsing is relatively expensive, especially for large test suites with hundreds or thousands of test cases. The `QueryGenerationFactoryCache` decorator wraps a `QueryGenerationFactory` with a Guava `LoadingCache` that stores the parsed `Query` keyed by `TestCase`. When the same test suite is run against multiple datasets (a common pattern in batch validation), the cache eliminates redundant query parsing on subsequent runs.
The cache uses weak references for keys (`weakKeys()`), a configurable maximum size (default 10,000 entries), and a time-based expiration (default 30 minutes after last access).
Usage
Use this heuristic when you are running the same test suite against multiple datasets or when test execution performance is critical. Use `TestExecutorFactory.createTestExecutorWithCache()` instead of `createTestExecutor()` to enable caching. Avoid when running a single validation pass, as the cache adds memory overhead with no benefit.
The Insight (Rule of Thumb)
- Action: Use `TestExecutorFactory.createTestExecutorWithCache(executionType, cacheSize)` instead of `createTestExecutor(executionType)`.
- Value: Default cache size = 10,000 entries; default expiration = 30 minutes after last access.
- Trade-off: Reduces query parsing overhead on repeated runs at the cost of additional memory for the cache. Cache uses weak keys so entries can be garbage-collected under memory pressure.
- Compatibility: Works with all `TestCaseExecutionType` values. The cache is transparent—the executor API is identical.
Reasoning
SPARQL query parsing via `QueryFactory.create()` involves lexing, parsing, and building an algebra representation. For large test suites (e.g., 500+ auto-generated test cases), this parsing cost dominates on the first run. On subsequent runs against different datasets, the same queries are reused unchanged. The Guava `LoadingCache` with `weakKeys()` ensures the cache does not prevent garbage collection of test cases that go out of scope, while `expireAfterAccess()` bounds staleness.
Code Evidence
Cache configuration from `QueryGenerationFactoryCache.java:15-40`:
private static final int DEFAULT_CACHE_SIZE = 10000;
private static final int DEFAULT_EXPIRATION_MINUTES = 30;
private final LoadingCache<TestCase, Query> queryCache;
this.queryCache = CacheBuilder.newBuilder()
.maximumSize(cache_size)
.weakKeys()
.expireAfterAccess(expire_after_minutes, TimeUnit.MINUTES)
.build(new CacheLoader<TestCase, Query>() {
public Query load(TestCase testCase) {
return delegate.getSparqlQuery(testCase);
}
});
Factory method from `TestExecutorFactory.java:35-40`:
public static TestExecutor createTestExecutorWithCache(TestCaseExecutionType executionType,
int cacheSize) {
QueryGenerationFactory qgf = createQueryGeneration(executionType);
QueryGenerationFactory cached = new QueryGenerationFactoryCache(qgf, cacheSize);
return createTestExecutor(executionType, cached);
}