|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 Eclipse RDF4J contributors. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Distribution License v1.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: BSD-3-Clause |
| 10 | + *******************************************************************************/ |
| 11 | +// Some portions generated by Codex |
| 12 | +package org.eclipse.rdf4j.sail.lmdb; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 16 | + |
| 17 | +import java.io.File; |
| 18 | +import java.lang.reflect.Field; |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import java.nio.file.Files; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import org.apache.commons.io.FileUtils; |
| 25 | +import org.eclipse.rdf4j.common.transaction.IsolationLevels; |
| 26 | +import org.eclipse.rdf4j.model.IRI; |
| 27 | +import org.eclipse.rdf4j.model.impl.SimpleValueFactory; |
| 28 | +import org.eclipse.rdf4j.query.QueryLanguage; |
| 29 | +import org.eclipse.rdf4j.query.algebra.StatementPattern; |
| 30 | +import org.eclipse.rdf4j.query.algebra.evaluation.impl.EvaluationStatistics; |
| 31 | +import org.eclipse.rdf4j.query.algebra.helpers.collectors.StatementPatternCollector; |
| 32 | +import org.eclipse.rdf4j.query.parser.ParsedTupleQuery; |
| 33 | +import org.eclipse.rdf4j.query.parser.QueryParserUtil; |
| 34 | +import org.eclipse.rdf4j.repository.sail.SailRepository; |
| 35 | +import org.eclipse.rdf4j.repository.sail.SailRepositoryConnection; |
| 36 | +import org.eclipse.rdf4j.sail.base.SailStore; |
| 37 | +import org.eclipse.rdf4j.sail.lmdb.config.LmdbStoreConfig; |
| 38 | +import org.junit.jupiter.api.Test; |
| 39 | + |
| 40 | +class LmdbEvaluationStatisticsMemoizationTest { |
| 41 | + |
| 42 | + @Test |
| 43 | + void cachesEquivalentStatementPatternCardinalitiesByResolvedIds() throws Exception { |
| 44 | + File dataDir = Files.createTempDirectory("lmdb-eval-stats-memoization").toFile(); |
| 45 | + SailRepository repository = new SailRepository(new LmdbStore(dataDir, new LmdbStoreConfig())); |
| 46 | + try { |
| 47 | + loadData(repository); |
| 48 | + |
| 49 | + EvaluationStatistics statistics = extractEvaluationStatistics(repository); |
| 50 | + StatementPattern followsA = statementPattern("SELECT * WHERE { ?s <urn:test:follows> ?o . }"); |
| 51 | + StatementPattern followsB = statementPattern("SELECT * WHERE { ?x <urn:test:follows> ?y . }"); |
| 52 | + StatementPattern name = statementPattern("SELECT * WHERE { ?u <urn:test:name> ?label . }"); |
| 53 | + |
| 54 | + statistics.getCardinality(followsA); |
| 55 | + statistics.getCardinality(followsB); |
| 56 | + statistics.getCardinality(name); |
| 57 | + |
| 58 | + Map<?, ?> cache = cardinalityCache(statistics); |
| 59 | + assertEquals(2, cache.size(), "Equivalent follows patterns should share one memoized entry"); |
| 60 | + } finally { |
| 61 | + repository.shutDown(); |
| 62 | + FileUtils.deleteDirectory(dataDir); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private static void loadData(SailRepository repository) { |
| 67 | + SimpleValueFactory vf = SimpleValueFactory.getInstance(); |
| 68 | + IRI follows = vf.createIRI("urn:test:follows"); |
| 69 | + IRI name = vf.createIRI("urn:test:name"); |
| 70 | + try (SailRepositoryConnection connection = repository.getConnection()) { |
| 71 | + connection.begin(IsolationLevels.NONE); |
| 72 | + for (int i = 0; i < 8; i++) { |
| 73 | + IRI subject = vf.createIRI("urn:test:user:" + i); |
| 74 | + IRI object = vf.createIRI("urn:test:user:" + ((i + 1) % 8)); |
| 75 | + connection.add(subject, follows, object); |
| 76 | + connection.add(subject, name, vf.createLiteral("u" + i)); |
| 77 | + } |
| 78 | + connection.commit(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private static EvaluationStatistics extractEvaluationStatistics(SailRepository repository) throws Exception { |
| 83 | + Object sail = repository.getSail(); |
| 84 | + Method getSailStoreMethod = sail.getClass().getDeclaredMethod("getSailStore"); |
| 85 | + getSailStoreMethod.setAccessible(true); |
| 86 | + SailStore sailStore = (SailStore) getSailStoreMethod.invoke(sail); |
| 87 | + return sailStore.getEvaluationStatistics(); |
| 88 | + } |
| 89 | + |
| 90 | + private static StatementPattern statementPattern(String query) { |
| 91 | + ParsedTupleQuery parsed = QueryParserUtil.parseTupleQuery(QueryLanguage.SPARQL, query, null); |
| 92 | + List<StatementPattern> patterns = StatementPatternCollector.process(parsed.getTupleExpr()); |
| 93 | + return patterns.get(0); |
| 94 | + } |
| 95 | + |
| 96 | + private static Map<?, ?> cardinalityCache(EvaluationStatistics statistics) throws Exception { |
| 97 | + assertTrue(statistics.getClass().getName().endsWith("LmdbEvaluationStatistics")); |
| 98 | + Field field = statistics.getClass().getDeclaredField("cardinalityCache"); |
| 99 | + field.setAccessible(true); |
| 100 | + return (Map<?, ?>) field.get(statistics); |
| 101 | + } |
| 102 | +} |
0 commit comments