-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimizes totalLiveDocs calculation for merge in
NativeEnginesKNNVectorsWriter Currently vector values are iterated irrespective of whether there are deleted docs in the segment. This makes sure they aren't Signed-off-by: Tejas Shah <[email protected]>
- Loading branch information
Showing
14 changed files
with
399 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/main/java/org/opensearch/knn/index/vectorvalues/KNNMergeVectorValues.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.knn.index.vectorvalues; | ||
|
||
import org.apache.lucene.codecs.KnnVectorsReader; | ||
import org.apache.lucene.index.ByteVectorValues; | ||
import org.apache.lucene.index.DocIDMerger; | ||
import org.apache.lucene.index.FieldInfo; | ||
import org.apache.lucene.index.FloatVectorValues; | ||
import org.apache.lucene.index.MergeState; | ||
import org.apache.lucene.index.VectorEncoding; | ||
import org.apache.lucene.search.DocIdSetIterator; | ||
import org.apache.lucene.util.Bits; | ||
import org.apache.lucene.util.FixedBitSet; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public final class KNNMergeVectorValues { | ||
|
||
public static List<KNNVectorValuesSub<FloatVectorValues>> mergeFloatVectorValues(FieldInfo fieldInfo, MergeState mergeState) | ||
throws IOException { | ||
assert fieldInfo != null && fieldInfo.hasVectorValues(); | ||
if (fieldInfo.getVectorEncoding() != VectorEncoding.FLOAT32) { | ||
throw new UnsupportedOperationException("Cannot merge vectors encoded as [" + fieldInfo.getVectorEncoding() + "] as FLOAT32"); | ||
} | ||
final List<KNNVectorValuesSub<FloatVectorValues>> subs = new ArrayList<>(); | ||
for (int i = 0; i < mergeState.knnVectorsReaders.length; i++) { | ||
KnnVectorsReader knnVectorsReader = mergeState.knnVectorsReaders[i]; | ||
if (knnVectorsReader != null) { | ||
FloatVectorValues values = knnVectorsReader.getFloatVectorValues(fieldInfo.name); | ||
if (values != null) { | ||
final Bits liveDocs = mergeState.liveDocs[i]; | ||
final int liveDocsInt; | ||
if (liveDocs != null) { | ||
if (liveDocs instanceof FixedBitSet) { | ||
liveDocsInt = ((FixedBitSet) liveDocs).cardinality(); | ||
} else { | ||
liveDocsInt = computeLiveDocs(values, liveDocs); | ||
values = knnVectorsReader.getFloatVectorValues(fieldInfo.name); | ||
} | ||
} else { | ||
liveDocsInt = Math.toIntExact(values.cost()); | ||
} | ||
subs.add(new KNNVectorValuesSub<>(mergeState.docMaps[i], values, liveDocsInt)); | ||
} | ||
} | ||
} | ||
return subs; | ||
} | ||
|
||
public static List<KNNVectorValuesSub<ByteVectorValues>> mergeByteVectorValues(FieldInfo fieldInfo, MergeState mergeState) | ||
throws IOException { | ||
assert fieldInfo != null && fieldInfo.hasVectorValues(); | ||
if (fieldInfo.getVectorEncoding() != VectorEncoding.BYTE) { | ||
throw new UnsupportedOperationException("Cannot merge vectors encoded as [" + fieldInfo.getVectorEncoding() + "] as BYTE"); | ||
} | ||
final List<KNNVectorValuesSub<ByteVectorValues>> subs = new ArrayList<>(); | ||
for (int i = 0; i < mergeState.knnVectorsReaders.length; i++) { | ||
KnnVectorsReader knnVectorsReader = mergeState.knnVectorsReaders[i]; | ||
if (knnVectorsReader != null) { | ||
ByteVectorValues values = knnVectorsReader.getByteVectorValues(fieldInfo.name); | ||
if (values != null) { | ||
final Bits liveDocs = mergeState.liveDocs[i]; | ||
final int liveDocsInt; | ||
if (liveDocs != null) { | ||
if (liveDocs instanceof FixedBitSet) { | ||
liveDocsInt = ((FixedBitSet) liveDocs).cardinality(); | ||
} else { | ||
liveDocsInt = computeLiveDocs(values, liveDocs); | ||
values = knnVectorsReader.getByteVectorValues(fieldInfo.name); | ||
} | ||
} else { | ||
liveDocsInt = Math.toIntExact(values.cost()); | ||
} | ||
subs.add(new KNNVectorValuesSub<>(mergeState.docMaps[i], values, liveDocsInt)); | ||
} | ||
} | ||
} | ||
return subs; | ||
} | ||
|
||
private static int computeLiveDocs(final DocIdSetIterator values, Bits liveDocs) throws IOException { | ||
int count = 0; | ||
if (liveDocs != null) { | ||
while (values.docID() != DocIdSetIterator.NO_MORE_DOCS) { | ||
count++; | ||
values.nextDoc(); | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
static class KNNVectorValuesSub<T extends DocIdSetIterator> extends DocIDMerger.Sub { | ||
final T values; | ||
final int liveDocs; | ||
|
||
KNNVectorValuesSub(MergeState.DocMap docMap, T values, int liveDocs) { | ||
super(docMap); | ||
this.values = values; | ||
this.liveDocs = liveDocs; | ||
} | ||
|
||
@Override | ||
public int nextDoc() throws IOException { | ||
return values.nextDoc(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.