Skip to content

Commit

Permalink
Initial integration with OS 2.0 Alpha1
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Gaievski <[email protected]>
  • Loading branch information
martin-gaievski committed Mar 21, 2022
1 parent a45dc81 commit 12f9bec
Show file tree
Hide file tree
Showing 31 changed files with 1,226 additions and 36 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ buildscript {
// "<opensearch_version before first '-'>.0-<build.version_qualifier>-<SNAPSHOT optional>"
opensearch_version = System.getProperty("opensearch.version", "2.0.0-SNAPSHOT")
knn_bwc_version = System.getProperty("bwc.version", "1.2.0.0-SNAPSHOT")
version_qualifier = System.getProperty("build.version_qualifier", "")
version_qualifier = System.getProperty("build.version_qualifier", "alpha1")
opensearch_bwc_version = "${knn_bwc_version}" - ".0-SNAPSHOT"
opensearch_group = "org.opensearch"

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/opensearch/knn/index/KNNQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
import org.apache.lucene.search.ScoreMode;
import org.apache.lucene.search.Weight;

Expand Down Expand Up @@ -59,6 +60,11 @@ public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float bo
return new KNNWeight(this, boost);
}

@Override
public void visit(QueryVisitor visitor) {

}

@Override
public String toString(String field) {
return field;
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/org/opensearch/knn/index/KNNWeight.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ public Explanation explain(LeafReaderContext context, int doc) {
return Explanation.match(1.0f, "No Explanation");
}

@Override
public void extractTerms(Set<Term> terms) {
}

@Override
public Scorer scorer(LeafReaderContext context) throws IOException {
SegmentReader reader = (SegmentReader) FilterLeafReader.unwrap(context.reader());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.lucene.codecs.CompoundFormat;
import org.apache.lucene.codecs.DocValuesFormat;
import org.apache.lucene.codecs.FieldInfosFormat;
import org.apache.lucene.codecs.KnnVectorsFormat;
import org.apache.lucene.codecs.LiveDocsFormat;
import org.apache.lucene.codecs.NormsFormat;
import org.apache.lucene.codecs.PointsFormat;
Expand Down Expand Up @@ -112,4 +113,9 @@ public CompoundFormat compoundFormat() {
public PointsFormat pointsFormat() {
return getDelegatee().pointsFormat();
}

@Override
public KnnVectorsFormat knnVectorsFormat() {
throw new UnsupportedOperationException("Codec does not support knn vector format");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package org.opensearch.knn.index.codec.KNN80Codec;

import org.apache.lucene.codecs.lucene50.Lucene50CompoundFormat;
import org.apache.lucene.backward_codecs.lucene50.Lucene50CompoundFormat;
import org.opensearch.knn.common.KNNConstants;
import org.apache.lucene.codecs.CompoundDirectory;
import org.apache.lucene.codecs.CompoundFormat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.apache.lucene.codecs.DocValuesConsumer;
import org.apache.lucene.codecs.DocValuesFormat;
import org.apache.lucene.codecs.DocValuesProducer;
import org.apache.lucene.codecs.lucene80.Lucene80DocValuesFormat;
import org.apache.lucene.backward_codecs.lucene80.Lucene80DocValuesFormat;
import org.apache.lucene.index.SegmentReadState;
import org.apache.lucene.index.SegmentWriteState;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package org.opensearch.knn.index.codec.KNN84Codec;

import org.apache.lucene.codecs.KnnVectorsFormat;
import org.opensearch.knn.index.codec.KNN80Codec.KNN80CompoundFormat;
import org.opensearch.knn.index.codec.KNN80Codec.KNN80DocValuesFormat;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -116,4 +117,9 @@ public CompoundFormat compoundFormat() {
public PointsFormat pointsFormat() {
return getDelegatee().pointsFormat();
}

@Override
public KnnVectorsFormat knnVectorsFormat() {
return getDelegatee().knnVectorsFormat();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package org.opensearch.knn.index.codec.KNN86Codec;

import org.apache.lucene.codecs.KnnVectorsFormat;
import org.opensearch.knn.index.codec.KNN80Codec.KNN80CompoundFormat;
import org.opensearch.knn.index.codec.KNN80Codec.KNN80DocValuesFormat;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -125,4 +126,9 @@ public CompoundFormat compoundFormat() {
public PointsFormat pointsFormat() {
return getDelegatee().pointsFormat();
}

@Override
public KnnVectorsFormat knnVectorsFormat() {
return getDelegatee().knnVectorsFormat();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package org.opensearch.knn.index.codec.KNN87Codec;

import org.apache.lucene.codecs.FilterCodec;
import org.apache.lucene.codecs.lucene87.Lucene87Codec;
import org.apache.lucene.backward_codecs.lucene87.Lucene87Codec;
import org.opensearch.knn.index.codec.KNN80Codec.KNN80CompoundFormat;
import org.opensearch.knn.index.codec.KNN80Codec.KNN80DocValuesFormat;
import org.apache.lucene.codecs.Codec;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.knn.index.codec.KNN91Codec;

import org.apache.lucene.index.BinaryDocValues;
import org.apache.lucene.index.DocIDMerger;
import org.apache.lucene.util.BytesRef;
import org.opensearch.knn.index.codec.util.BinaryDocValuesSub;

import java.io.IOException;

/**
* A per-document kNN numeric value.
*/
class KNN91BinaryDocValues extends BinaryDocValues {

private DocIDMerger<BinaryDocValuesSub> docIDMerger;

KNN91BinaryDocValues(DocIDMerger<BinaryDocValuesSub> docIdMerger) {
this.docIDMerger = docIdMerger;
}

private BinaryDocValuesSub current;
private int docID = -1;

@Override
public int docID() {
return docID;
}

@Override
public int nextDoc() throws IOException {
current = docIDMerger.next();
if (current == null) {
docID = NO_MORE_DOCS;
} else {
docID = current.mappedDocID;
}
return docID;
}

@Override
public int advance(int target) throws IOException {
throw new UnsupportedOperationException();
}

@Override
public boolean advanceExact(int target) throws IOException {
throw new UnsupportedOperationException();
}

@Override
public long cost() {
throw new UnsupportedOperationException();
}

@Override
public BytesRef binaryValue() throws IOException {
return current.getValues().binaryValue();
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.knn.index.codec.KNN91Codec;

import org.apache.lucene.codecs.Codec;
import org.apache.lucene.codecs.CompoundFormat;
import org.apache.lucene.codecs.DocValuesFormat;
import org.apache.lucene.codecs.FilterCodec;
import org.apache.lucene.codecs.lucene91.Lucene91Codec;

/**
* Extends the Codec to support a new file format for KNN index
* based on the mappings.
*
*/
public final class KNN91Codec extends FilterCodec {

private final DocValuesFormat docValuesFormat;
private final CompoundFormat compoundFormat;

public static final String KNN_91 = "KNN91Codec";

/**
* No arg constructor that uses Lucene91 as the delegate
*/
public KNN91Codec() {
this(new Lucene91Codec());
}
/**
* Constructor that takes a Codec delegate to delegate all methods this code does not implement to.
*
* @param delegate codec that will perform all operations this codec does not override
*/
public KNN91Codec(Codec delegate) {
super(KNN_91, delegate);
this.docValuesFormat = new KNN91DocValuesFormat(delegate.docValuesFormat());
this.compoundFormat = new KNN91CompoundFormat(delegate.compoundFormat());
}

@Override
public DocValuesFormat docValuesFormat() {
return this.docValuesFormat;
}

@Override
public CompoundFormat compoundFormat() {
return this.compoundFormat;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.knn.index.codec.KNN91Codec;

import org.apache.lucene.codecs.CompoundDirectory;
import org.apache.lucene.codecs.CompoundFormat;
import org.apache.lucene.index.SegmentInfo;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IOContext;
import org.opensearch.knn.common.KNNConstants;
import org.opensearch.knn.index.util.KNNEngine;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Class to encode/decode compound file
*/
public class KNN91CompoundFormat extends CompoundFormat {

private final CompoundFormat delegate;

/**
* Constructor that takes a delegate to handle non-overridden methods
*
* @param delegate CompoundFormat that will handle non-overridden methods
*/
public KNN91CompoundFormat(CompoundFormat delegate) {
this.delegate = delegate;
}

@Override
public CompoundDirectory getCompoundReader(Directory dir, SegmentInfo si, IOContext context) throws IOException {
return delegate.getCompoundReader(dir, si, context);
}

@Override
public void write(Directory dir, SegmentInfo si, IOContext context) throws IOException {
for (KNNEngine knnEngine : KNNEngine.values()) {
writeEngineFiles(dir, si, context, knnEngine.getExtension());
}
delegate.write(dir, si, context);
}

private void writeEngineFiles(Directory dir, SegmentInfo si, IOContext context, String engineExtension) throws IOException {
/*
* If engine file present, remove it from the compounding file list to avoid header/footer checks
* and create a new compounding file format with extension engine + c.
*/
Set<String> engineFiles = si.files().stream().filter(file -> file.endsWith(engineExtension)).collect(Collectors.toSet());

Set<String> segmentFiles = new HashSet<>(si.files());

if (!engineFiles.isEmpty()) {
for (String engineFile : engineFiles) {
String engineCompoundFile = engineFile + KNNConstants.COMPOUND_EXTENSION;
dir.copyFrom(dir, engineFile, engineCompoundFile, context);
}
segmentFiles.removeAll(engineFiles);
si.setFiles(segmentFiles);
}
}
}
Loading

0 comments on commit 12f9bec

Please sign in to comment.