|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.lucene.codecs; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.List; |
| 21 | +import org.apache.lucene.index.ByteVectorValues; |
| 22 | +import org.apache.lucene.index.FloatVectorValues; |
| 23 | +import org.apache.lucene.index.MergeState; |
| 24 | +import org.apache.lucene.tests.util.LuceneTestCase; |
| 25 | + |
| 26 | +/** Tests for merged vector values to ensure lastOrd is properly incremented during iteration. */ |
| 27 | +public class TestMergedVectorValues extends LuceneTestCase { |
| 28 | + |
| 29 | + /** |
| 30 | + * Test that skipping vectors in MergedByteVectorValues via nextDoc() and then loading a |
| 31 | + * subsequent vector via vectorValue() works correctly. |
| 32 | + */ |
| 33 | + public void testSkipsInMergedByteVectorValues() throws IOException { |
| 34 | + // Data |
| 35 | + List<byte[]> vectors = List.of(new byte[] {0}, new byte[] {1}); |
| 36 | + |
| 37 | + // Setup |
| 38 | + KnnVectorsWriter.ByteVectorValuesSub sub = |
| 39 | + new KnnVectorsWriter.ByteVectorValuesSub(x -> x, ByteVectorValues.fromBytes(vectors, 1)); |
| 40 | + MergeState state = |
| 41 | + new MergeState( |
| 42 | + null, null, null, null, null, null, null, null, null, null, null, null, null, null, |
| 43 | + null, false); |
| 44 | + |
| 45 | + // Run the test |
| 46 | + ByteVectorValues values = |
| 47 | + new KnnVectorsWriter.MergedVectorValues.MergedByteVectorValues(List.of(sub), state); |
| 48 | + |
| 49 | + // Skip doc 0 and load doc 1 |
| 50 | + values.iterator().nextDoc(); // doc 0 |
| 51 | + values.iterator().nextDoc(); // doc 1 |
| 52 | + |
| 53 | + // Read vector for doc 1 |
| 54 | + assertArrayEquals(vectors.get(1), values.vectorValue(1)); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Test that skipping vectors in MergedFloat32VectorValues via nextDoc() and then loading a |
| 59 | + * subsequent vector via vectorValue() works correctly. |
| 60 | + */ |
| 61 | + public void testSkipsInMergedFloat32VectorValues() throws IOException { |
| 62 | + // Data |
| 63 | + List<float[]> vectors = List.of(new float[] {0.0f}, new float[] {1.0f}); |
| 64 | + |
| 65 | + // Setup |
| 66 | + KnnVectorsWriter.FloatVectorValuesSub sub = |
| 67 | + new KnnVectorsWriter.FloatVectorValuesSub(x -> x, FloatVectorValues.fromFloats(vectors, 1)); |
| 68 | + MergeState state = |
| 69 | + new MergeState( |
| 70 | + null, null, null, null, null, null, null, null, null, null, null, null, null, null, |
| 71 | + null, false); |
| 72 | + |
| 73 | + // Run the test |
| 74 | + FloatVectorValues values = |
| 75 | + new KnnVectorsWriter.MergedVectorValues.MergedFloat32VectorValues(List.of(sub), state); |
| 76 | + |
| 77 | + // Skip doc 0 and load doc 1 |
| 78 | + values.iterator().nextDoc(); // doc 0 |
| 79 | + values.iterator().nextDoc(); // doc 1 |
| 80 | + |
| 81 | + // Read vector for doc 1 |
| 82 | + assertArrayEquals(vectors.get(1), values.vectorValue(1), 0.0f); |
| 83 | + } |
| 84 | +} |
0 commit comments