Fix build error

This commit is contained in:
Anh Dung Bui 2024-11-08 17:31:28 +09:00
parent b97aadb925
commit 972dbfb174
10 changed files with 46 additions and 17 deletions

View File

@ -450,7 +450,7 @@ public final class Lucene95HnswVectorsWriter extends KnnVectorsWriter {
new OffHeapByteVectorValues.DenseOffHeapVectorValues(
fieldInfo.getVectorDimension(),
docsWithField.cardinality(),
vectorDataInput,
vectorDataInput.toRandomAccessInput(),
byteSize,
defaultFlatVectorScorer,
fieldInfo.getVectorSimilarityFunction()));
@ -462,7 +462,7 @@ public final class Lucene95HnswVectorsWriter extends KnnVectorsWriter {
new OffHeapFloatVectorValues.DenseOffHeapVectorValues(
fieldInfo.getVectorDimension(),
docsWithField.cardinality(),
vectorDataInput,
vectorDataInput.toRandomAccessInput(),
byteSize,
defaultFlatVectorScorer,
fieldInfo.getVectorSimilarityFunction()));

View File

@ -98,7 +98,7 @@ public class VectorScorerBenchmark {
static KnnVectorValues vectorValues(
int dims, int size, IndexInput in, VectorSimilarityFunction sim) throws IOException {
return new OffHeapByteVectorValues.DenseOffHeapVectorValues(
dims, size, in.slice("test", 0, in.length()), dims, new ThrowingFlatVectorScorer(), sim);
dims, size, in.toRandomAccessInput(), dims, new ThrowingFlatVectorScorer(), sim);
}
static final class ThrowingFlatVectorScorer implements FlatVectorsScorer {

View File

@ -303,7 +303,7 @@ public final class Lucene99FlatVectorsWriter extends FlatVectorsWriter {
new OffHeapByteVectorValues.DenseOffHeapVectorValues(
fieldInfo.getVectorDimension(),
docsWithField.cardinality(),
finalVectorDataInput,
finalVectorDataInput.toRandomAccessInput(),
fieldInfo.getVectorDimension() * Byte.BYTES,
vectorsScorer,
fieldInfo.getVectorSimilarityFunction()));
@ -313,7 +313,7 @@ public final class Lucene99FlatVectorsWriter extends FlatVectorsWriter {
new OffHeapFloatVectorValues.DenseOffHeapVectorValues(
fieldInfo.getVectorDimension(),
docsWithField.cardinality(),
finalVectorDataInput,
finalVectorDataInput.toRandomAccessInput(),
fieldInfo.getVectorDimension() * Float.BYTES,
vectorsScorer,
fieldInfo.getVectorSimilarityFunction()));

View File

@ -524,7 +524,7 @@ public final class Lucene99ScalarQuantizedVectorsWriter extends FlatVectorsWrite
compress,
fieldInfo.getVectorSimilarityFunction(),
vectorsScorer,
quantizationDataInput)));
quantizationDataInput.toRandomAccessInput())));
} finally {
if (success == false) {
IOUtils.closeWhileHandlingException(tempQuantizedVectorData, quantizationDataInput);

View File

@ -152,6 +152,14 @@ public abstract class IndexInput extends DataInput implements Closeable {
}
}
/** Convert this IndexInput a RandomAccessInput. */
public RandomAccessInput toRandomAccessInput() throws IOException {
if (this instanceof RandomAccessInput) {
return (RandomAccessInput) this;
}
return randomAccessSlice(0, length());
}
/**
* Creates a random-access slice of this index input, with the given offset and length.
*
@ -214,6 +222,15 @@ public abstract class IndexInput extends DataInput implements Closeable {
slice.prefetch(offset, length);
}
@Override
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
throw new Error("This cannot happen: Failing to clone RandomAccessInput", e);
}
}
@Override
public String toString() {
return "RandomAccessInput(" + IndexInput.this.toString() + ")";

View File

@ -25,6 +25,7 @@ import org.apache.lucene.index.VectorSimilarityFunction;
import org.apache.lucene.store.FilterIndexInput;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.MemorySegmentAccessInput;
import org.apache.lucene.store.RandomAccessInput;
import org.apache.lucene.util.hnsw.RandomVectorScorer;
abstract sealed class Lucene99MemorySegmentByteVectorScorer
@ -40,8 +41,14 @@ abstract sealed class Lucene99MemorySegmentByteVectorScorer
* returned.
*/
public static Optional<Lucene99MemorySegmentByteVectorScorer> create(
VectorSimilarityFunction type, IndexInput input, KnnVectorValues values, byte[] queryVector) {
VectorSimilarityFunction type,
RandomAccessInput slice,
KnnVectorValues values,
byte[] queryVector) {
assert values instanceof ByteVectorValues;
if (!(slice instanceof IndexInput input)) {
return Optional.empty();
}
input = FilterIndexInput.unwrapOnlyTest(input);
if (!(input instanceof MemorySegmentAccessInput msInput)) {
return Optional.empty();

View File

@ -25,6 +25,7 @@ import org.apache.lucene.index.VectorSimilarityFunction;
import org.apache.lucene.store.FilterIndexInput;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.MemorySegmentAccessInput;
import org.apache.lucene.store.RandomAccessInput;
import org.apache.lucene.util.hnsw.RandomVectorScorer;
import org.apache.lucene.util.hnsw.RandomVectorScorerSupplier;
@ -42,8 +43,11 @@ public abstract sealed class Lucene99MemorySegmentByteVectorScorerSupplier
* optional is returned.
*/
static Optional<RandomVectorScorerSupplier> create(
VectorSimilarityFunction type, IndexInput input, KnnVectorValues values) {
VectorSimilarityFunction type, RandomAccessInput slice, KnnVectorValues values) {
assert values instanceof ByteVectorValues;
if (!(slice instanceof IndexInput input)) {
return Optional.empty();
}
input = FilterIndexInput.unwrapOnlyTest(input);
if (!(input instanceof MemorySegmentAccessInput msInput)) {
return Optional.empty();

View File

@ -232,6 +232,12 @@ abstract class MemorySegmentIndexInput extends IndexInput
}
}
@Override
public void readFloats(long pos, float[] dst, int offset, int len) throws IOException {
seek(pos);
readFloats(dst, offset, len);
}
@Override
public void readFloats(float[] dst, int offset, int length) throws IOException {
try {

View File

@ -178,18 +178,13 @@ public class TestFlatVectorScorer extends LuceneTestCase {
ByteVectorValues byteVectorValues(int dims, int size, IndexInput in, VectorSimilarityFunction sim)
throws IOException {
return new OffHeapByteVectorValues.DenseOffHeapVectorValues(
dims, size, in.slice("byteValues", 0, in.length()), dims, flatVectorsScorer, sim);
dims, size, in.toRandomAccessInput(), dims, flatVectorsScorer, sim);
}
FloatVectorValues floatVectorValues(
int dims, int size, IndexInput in, VectorSimilarityFunction sim) throws IOException {
return new OffHeapFloatVectorValues.DenseOffHeapVectorValues(
dims,
size,
in.slice("floatValues", 0, in.length()),
dims * Float.BYTES,
flatVectorsScorer,
sim);
dims, size, in.toRandomAccessInput(), dims * Float.BYTES, flatVectorsScorer, sim);
}
/** Concatenates float arrays as byte[]. */

View File

@ -381,13 +381,13 @@ public class TestVectorScorer extends LuceneTestCase {
KnnVectorValues vectorValues(int dims, int size, IndexInput in, VectorSimilarityFunction sim)
throws IOException {
return new OffHeapByteVectorValues.DenseOffHeapVectorValues(
dims, size, in.slice("byteValues", 0, in.length()), dims, MEMSEG_SCORER, sim);
dims, size, in.toRandomAccessInput(), dims, MEMSEG_SCORER, sim);
}
KnnVectorValues floatVectorValues(int dims, int size, IndexInput in, VectorSimilarityFunction sim)
throws IOException {
return new OffHeapFloatVectorValues.DenseOffHeapVectorValues(
dims, size, in.slice("floatValues", 0, in.length()), dims, MEMSEG_SCORER, sim);
dims, size, in.toRandomAccessInput(), dims, MEMSEG_SCORER, sim);
}
// creates the vector based on the given ordinal, which is reproducible given the ord and dims