|
| 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 | + |
| 13 | +package org.eclipse.rdf4j.sail.base; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 18 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 20 | + |
| 21 | +import java.lang.reflect.Field; |
| 22 | +import java.util.EnumMap; |
| 23 | + |
| 24 | +import org.eclipse.rdf4j.model.IRI; |
| 25 | +import org.eclipse.rdf4j.model.Statement; |
| 26 | +import org.eclipse.rdf4j.model.ValueFactory; |
| 27 | +import org.eclipse.rdf4j.model.impl.SimpleValueFactory; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +class SketchBasedJoinEstimatorMemoryLayoutTest { |
| 31 | + |
| 32 | + private static final ValueFactory VF = SimpleValueFactory.getInstance(); |
| 33 | + |
| 34 | + @Test |
| 35 | + void stateAllocatesDeleteOnlySketchContainers() throws Exception { |
| 36 | + SketchBasedJoinEstimator estimator = new SketchBasedJoinEstimator(new StubSailStore(), smallConfig()); |
| 37 | + Object state = privateFieldValue(estimator, "bufA"); |
| 38 | + |
| 39 | + assertNotNull(privateFieldValue(state, "delSingleTriples")); |
| 40 | + assertNotNull(privateFieldValue(state, "delSingles")); |
| 41 | + assertNotNull(privateFieldValue(state, "delPairs")); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void deleteUpdatesUseTombstoneSketches() { |
| 46 | + SketchBasedJoinEstimator estimator = new SketchBasedJoinEstimator(new StubSailStore(), smallConfig()); |
| 47 | + IRI predicate = VF.createIRI("urn:memory-layout:p"); |
| 48 | + Statement statement = VF.createStatement(VF.createIRI("urn:memory-layout:s"), predicate, |
| 49 | + VF.createIRI("urn:memory-layout:o")); |
| 50 | + |
| 51 | + estimator.addStatement(statement); |
| 52 | + estimator.debugFlushPendingIncremental(); |
| 53 | + |
| 54 | + assertTrue(estimator.cardinalitySingle(SketchBasedJoinEstimator.Component.P, predicate.stringValue()) > 0.0); |
| 55 | + |
| 56 | + estimator.deleteStatement(statement); |
| 57 | + estimator.debugFlushPendingIncremental(); |
| 58 | + |
| 59 | + assertEquals(0.0, estimator.cardinalitySingle(SketchBasedJoinEstimator.Component.P, predicate.stringValue()), |
| 60 | + 0.0001); |
| 61 | + assertTrue( |
| 62 | + estimator.debugResidentSketches() |
| 63 | + .stream() |
| 64 | + .anyMatch(SketchBasedJoinEstimator.DebugSketchAddress::isDelete), |
| 65 | + "Delete updates should create resident tombstone sketches"); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void pairSketchesUseSingleLevelPackedSparseArrays() throws Exception { |
| 70 | + SketchBasedJoinEstimator estimator = new SketchBasedJoinEstimator(new StubSailStore(), smallConfig()); |
| 71 | + Statement statement = VF.createStatement(VF.createIRI("urn:memory-layout:s"), |
| 72 | + VF.createIRI("urn:memory-layout:p"), VF.createIRI("urn:memory-layout:o"), |
| 73 | + VF.createIRI("urn:memory-layout:c")); |
| 74 | + |
| 75 | + estimator.addStatement(statement); |
| 76 | + estimator.debugFlushPendingIncremental(); |
| 77 | + |
| 78 | + Object state = privateFieldValue(estimator, "bufA"); |
| 79 | + EnumMap<?, ?> pairs = assertInstanceOf(EnumMap.class, privateFieldValue(state, "pairs")); |
| 80 | + Object spBuild = pairs.get(SketchBasedJoinEstimator.Pair.SP); |
| 81 | + |
| 82 | + assertThrows(NoSuchFieldException.class, () -> spBuild.getClass().getDeclaredField("rows"), |
| 83 | + "Pair sketches should not use row maps or a two-level sparse structure"); |
| 84 | + assertSingleLevelSparseArray(privateFieldValue(spBuild, "triples"), 3); |
| 85 | + assertSingleLevelSparseArray(privateFieldValue(spBuild, "comp1"), 3); |
| 86 | + assertSingleLevelSparseArray(privateFieldValue(spBuild, "comp2"), 3); |
| 87 | + } |
| 88 | + |
| 89 | + private static SketchBasedJoinEstimator.Config smallConfig() { |
| 90 | + return SketchBasedJoinEstimator.Config.defaults() |
| 91 | + .withNominalEntries(64) |
| 92 | + .withSubjectBucketCount(8) |
| 93 | + .withPredicateBucketCount(8) |
| 94 | + .withObjectBucketCount(8) |
| 95 | + .withContextBucketCount(4) |
| 96 | + .withThrottleEveryN(1) |
| 97 | + .withThrottleMillis(0); |
| 98 | + } |
| 99 | + |
| 100 | + private static Object privateFieldValue(Object target, String fieldName) throws Exception { |
| 101 | + Field field = target.getClass().getDeclaredField(fieldName); |
| 102 | + field.setAccessible(true); |
| 103 | + return field.get(target); |
| 104 | + } |
| 105 | + |
| 106 | + private static void assertSingleLevelSparseArray(Object sparseArray, int expectedYBits) throws Exception { |
| 107 | + assertNotNull(sparseArray); |
| 108 | + assertInstanceOf(long[].class, privateFieldValue(sparseArray, "keys")); |
| 109 | + assertTrue(((long[]) privateFieldValue(sparseArray, "keys")).length > 0); |
| 110 | + assertInstanceOf(Object[].class, privateFieldValue(sparseArray, "values")); |
| 111 | + assertEquals(expectedYBits, (int) privateFieldValue(sparseArray, "yBits")); |
| 112 | + } |
| 113 | +} |
0 commit comments