Skip to content

Commit

Permalink
Fix build error
Browse files Browse the repository at this point in the history
  • Loading branch information
dungba88 committed Nov 8, 2024
1 parent b97aadb commit 972dbfb
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ public void mergeOneField(FieldInfo fieldInfo, MergeState mergeState) throws IOE
new OffHeapByteVectorValues.DenseOffHeapVectorValues(
fieldInfo.getVectorDimension(),
docsWithField.cardinality(),
vectorDataInput,
vectorDataInput.toRandomAccessInput(),
byteSize,
defaultFlatVectorScorer,
fieldInfo.getVectorSimilarityFunction()));
Expand All @@ -462,7 +462,7 @@ public void mergeOneField(FieldInfo fieldInfo, MergeState mergeState) throws IOE
new OffHeapFloatVectorValues.DenseOffHeapVectorValues(
fieldInfo.getVectorDimension(),
docsWithField.cardinality(),
vectorDataInput,
vectorDataInput.toRandomAccessInput(),
byteSize,
defaultFlatVectorScorer,
fieldInfo.getVectorSimilarityFunction()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public float binaryDotProductMemSeg() throws IOException {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public CloseableRandomVectorScorerSupplier mergeOneFieldToIndex(
new OffHeapByteVectorValues.DenseOffHeapVectorValues(
fieldInfo.getVectorDimension(),
docsWithField.cardinality(),
finalVectorDataInput,
finalVectorDataInput.toRandomAccessInput(),
fieldInfo.getVectorDimension() * Byte.BYTES,
vectorsScorer,
fieldInfo.getVectorSimilarityFunction()));
Expand All @@ -313,7 +313,7 @@ public CloseableRandomVectorScorerSupplier mergeOneFieldToIndex(
new OffHeapFloatVectorValues.DenseOffHeapVectorValues(
fieldInfo.getVectorDimension(),
docsWithField.cardinality(),
finalVectorDataInput,
finalVectorDataInput.toRandomAccessInput(),
fieldInfo.getVectorDimension() * Float.BYTES,
vectorsScorer,
fieldInfo.getVectorSimilarityFunction()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ private ScalarQuantizedCloseableRandomVectorScorerSupplier mergeOneFieldToIndex(
compress,
fieldInfo.getVectorSimilarityFunction(),
vectorsScorer,
quantizationDataInput)));
quantizationDataInput.toRandomAccessInput())));
} finally {
if (success == false) {
IOUtils.closeWhileHandlingException(tempQuantizedVectorData, quantizationDataInput);
Expand Down
17 changes: 17 additions & 0 deletions lucene/core/src/java/org/apache/lucene/store/IndexInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ protected String getFullSliceDescription(String sliceDescription) {
}
}

/** 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.
*
Expand Down Expand Up @@ -214,6 +222,15 @@ public void prefetch(long offset, long length) throws IOException {
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() + ")";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;

Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ public void readLongs(long[] dst, int offset, int length) throws IOException {
}
}

@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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,13 @@ public void testCheckFloatDimensions() throws IOException {
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[]. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,13 @@ public void testWithFloatValues() throws IOException {
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
Expand Down

0 comments on commit 972dbfb

Please sign in to comment.