|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 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 | +package org.eclipse.rdf4j.sail.lmdb.join; |
| 12 | + |
| 13 | +import static org.assertj.core.api.Assertions.assertThat; |
| 14 | + |
| 15 | +import java.util.Arrays; |
| 16 | + |
| 17 | +import org.eclipse.rdf4j.model.impl.SimpleValueFactory; |
| 18 | +import org.eclipse.rdf4j.query.algebra.StatementPattern; |
| 19 | +import org.eclipse.rdf4j.query.algebra.Var; |
| 20 | +import org.eclipse.rdf4j.sail.lmdb.IdBindingInfo; |
| 21 | +import org.eclipse.rdf4j.sail.lmdb.RecordIterator; |
| 22 | +import org.eclipse.rdf4j.sail.lmdb.model.LmdbValue; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.api.parallel.Isolated; |
| 25 | + |
| 26 | +@Isolated |
| 27 | +class LmdbIdMergeJoinIteratorTest { |
| 28 | + |
| 29 | + @Test |
| 30 | + void mergeJoinProducesIdRecordsWithoutMaterialization() throws Exception { |
| 31 | + StatementPattern leftPattern = new StatementPattern( |
| 32 | + new Var("x"), |
| 33 | + new Var("pl", SimpleValueFactory.getInstance().createIRI("urn:p:left")), |
| 34 | + new Var("ol", SimpleValueFactory.getInstance().createIRI("urn:o:left"))); |
| 35 | + StatementPattern rightPattern = new StatementPattern( |
| 36 | + new Var("x"), |
| 37 | + new Var("pr", SimpleValueFactory.getInstance().createIRI("urn:p:right")), |
| 38 | + new Var("or", SimpleValueFactory.getInstance().createIRI("urn:o:right"))); |
| 39 | + |
| 40 | + LmdbIdJoinIterator.PatternInfo leftInfo = LmdbIdJoinIterator.PatternInfo.create(leftPattern); |
| 41 | + LmdbIdJoinIterator.PatternInfo rightInfo = LmdbIdJoinIterator.PatternInfo.create(rightPattern); |
| 42 | + IdBindingInfo bindingInfo = IdBindingInfo.combine(IdBindingInfo.fromFirstPattern(leftInfo), rightInfo, null); |
| 43 | + |
| 44 | + long[] leftRecord = new long[] { 7L, LmdbValue.UNKNOWN_ID, LmdbValue.UNKNOWN_ID, 0L }; |
| 45 | + long[] rightRecord = new long[] { 7L, LmdbValue.UNKNOWN_ID, LmdbValue.UNKNOWN_ID, 0L }; |
| 46 | + |
| 47 | + RecordIterator leftIterator = new ArrayRecordIterator(leftRecord); |
| 48 | + RecordIterator rightIterator = new ArrayRecordIterator(rightRecord); |
| 49 | + |
| 50 | + LmdbIdMergeJoinIterator iterator = new LmdbIdMergeJoinIterator(leftIterator, rightIterator, leftInfo, rightInfo, |
| 51 | + "x", bindingInfo); |
| 52 | + try { |
| 53 | + Object record = iterator.next(); |
| 54 | + assertThat(record).isInstanceOf(long[].class); |
| 55 | + assertThat((long[]) record).containsExactly(7L, LmdbValue.UNKNOWN_ID, LmdbValue.UNKNOWN_ID, 0L); |
| 56 | + } finally { |
| 57 | + iterator.close(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private static final class ArrayRecordIterator implements RecordIterator { |
| 62 | + private final long[][] data; |
| 63 | + private int index; |
| 64 | + |
| 65 | + private ArrayRecordIterator(long[]... records) { |
| 66 | + this.data = Arrays.stream(records) |
| 67 | + .map(arr -> Arrays.copyOf(arr, arr.length)) |
| 68 | + .toArray(long[][]::new); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public long[] next() { |
| 73 | + if (index >= data.length) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + long[] source = data[index++]; |
| 77 | + return Arrays.copyOf(source, source.length); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void close() { |
| 82 | + // nothing to do |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments