Skip to content

Commit

Permalink
Reorginize factory and facade code, minor review comments
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 22, 2022
1 parent 78b4e30 commit af79c68
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 162 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
/*
* Copyright OpenSearch Contributors
* 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;
import org.opensearch.knn.index.codec.KNNDocFormatFacade;
import org.opensearch.knn.index.codec.KNNDocFormatFactory;
import org.opensearch.knn.index.codec.KNNFormatFacade;
import org.opensearch.knn.index.codec.KNNFormatFactory;

import static org.opensearch.knn.index.codec.KNNCodecFactory.CodecDelegateFactory.createKNN91DefaultDelegate;

/**
* Extends the Codec to support a new file format for KNN index
Expand All @@ -27,13 +21,13 @@
public final class KNN91Codec extends FilterCodec {

public static final String KNN_91 = "KNN91Codec";
private KNNDocFormatFacade docFormatFacade;
private KNNFormatFacade knnFormatFacade;

/**
* No arg constructor that uses Lucene91 as the delegate
*/
public KNN91Codec() {
this(new Lucene91Codec());
this(createKNN91DefaultDelegate());
}

/**
Expand All @@ -43,16 +37,16 @@ public KNN91Codec() {
*/
public KNN91Codec(Codec delegate) {
super(KNN_91, delegate);
docFormatFacade = KNNDocFormatFactory.createKNN91DocFormat(delegate);
knnFormatFacade = KNNFormatFactory.createKNN91Format(delegate);
}

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

@Override
public CompoundFormat compoundFormat() {
return docFormatFacade.compoundFormat();
return knnFormatFacade.compoundFormat();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
/*
* Copyright OpenSearch Contributors
* 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.docformat;

import org.apache.lucene.index.BinaryDocValues;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
/*
* Copyright OpenSearch Contributors
* 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.docformat;

import org.apache.lucene.codecs.CompoundDirectory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
/*
* Copyright OpenSearch Contributors
* 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.docformat;

import com.google.common.collect.ImmutableMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
/*
* Copyright OpenSearch Contributors
* 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.docformat;

import org.apache.lucene.codecs.DocValuesConsumer;
import org.apache.lucene.codecs.DocValuesFormat;
import org.apache.lucene.codecs.DocValuesProducer;
import org.apache.lucene.codecs.lucene90.Lucene90DocValuesFormat;
import org.apache.lucene.index.SegmentReadState;
import org.apache.lucene.index.SegmentWriteState;

Expand All @@ -26,10 +18,6 @@
public class KNN91DocValuesFormat extends DocValuesFormat {
private final DocValuesFormat delegate;

public KNN91DocValuesFormat() {
this(new Lucene90DocValuesFormat());
}

/**
* Constructor that takes delegate in order to handle non-overridden methods
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
/*
* Copyright OpenSearch Contributors
* 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.docformat;

import org.apache.lucene.codecs.DocValuesProducer;
Expand Down
47 changes: 37 additions & 10 deletions src/main/java/org/opensearch/knn/index/codec/KNNCodecFactory.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,52 @@
/*
* Copyright OpenSearch Contributors
* 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;

import com.google.common.collect.ImmutableMap;
import org.apache.lucene.codecs.Codec;
import org.apache.lucene.codecs.lucene91.Lucene91Codec;
import org.opensearch.knn.index.codec.KNN91Codec.KNN91Codec;

import java.lang.reflect.Constructor;
import java.util.Map;

/**
* Factory abstraction for KNN codes
*/
public class KNNCodecFactory {

public static Codec createKNN91Codec(Codec userCodec) {
return new KNN91Codec(userCodec);
private static Map<KNNCodecVersion, Class> CODEC_BY_VERSION = ImmutableMap.of(KNNCodecVersion.KNN91, KNN91Codec.class);

private static KNNCodecVersion LATEST_KNN_CODEC_VERSION = KNNCodecVersion.KNN91;

public static Codec createKNNCodec(final Codec userCodec) {
return getCodec(LATEST_KNN_CODEC_VERSION, userCodec);
}

public static Codec createKNNCodec(final KNNCodecVersion knnCodecVersion, final Codec userCodec) {
return getCodec(knnCodecVersion, userCodec);
}

private static Codec getCodec(final KNNCodecVersion knnCodecVersion, final Codec userCodec) {
try {
Constructor<?> constructor = CODEC_BY_VERSION.getOrDefault(knnCodecVersion, CODEC_BY_VERSION.get(LATEST_KNN_CODEC_VERSION))
.getConstructor(Codec.class);
return (Codec) constructor.newInstance(userCodec);
} catch (Exception ex) {
throw new RuntimeException("Cannot create instance of KNN codec", ex);
}
}

public static class CodecDelegateFactory {

public static Codec createKNN91DefaultDelegate() {
return new Lucene91Codec();
}
}

enum KNNCodecVersion {
KNN91
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public KNNCodecService(CodecServiceConfig codecServiceConfig) {
*/
@Override
public Codec codec(String name) {
return KNNCodecFactory.createKNN91Codec(super.codec(name));
return KNNCodecFactory.createKNNCodec(super.codec(name));
}
}

This file was deleted.

This file was deleted.

30 changes: 30 additions & 0 deletions src/main/java/org/opensearch/knn/index/codec/KNNFormatFacade.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.knn.index.codec;

import org.apache.lucene.codecs.CompoundFormat;
import org.apache.lucene.codecs.DocValuesFormat;

/**
* Class abstracts facade for plugin formats.
*/
public class KNNFormatFacade {

private final DocValuesFormat docValuesFormat;
private final CompoundFormat compoundFormat;

public KNNFormatFacade(final DocValuesFormat docValuesFormat, final CompoundFormat compoundFormat) {
this.docValuesFormat = docValuesFormat;
this.compoundFormat = compoundFormat;
}

public DocValuesFormat docValuesFormat() {
return docValuesFormat;
}

public CompoundFormat compoundFormat() {
return compoundFormat;
}
}
23 changes: 23 additions & 0 deletions src/main/java/org/opensearch/knn/index/codec/KNNFormatFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.knn.index.codec;

import org.apache.lucene.codecs.Codec;
import org.opensearch.knn.index.codec.KNN91Codec.docformat.KNN91CompoundFormat;
import org.opensearch.knn.index.codec.KNN91Codec.docformat.KNN91DocValuesFormat;

/**
* Factory abstraction for KNN document format facades
*/
public class KNNFormatFactory {

public static KNNFormatFacade createKNN91Format(final Codec delegate) {
final KNNFormatFacade knnFormatFacade = new KNNFormatFacade(
new KNN91DocValuesFormat(delegate.docValuesFormat()),
new KNN91CompoundFormat(delegate.compoundFormat())
);
return knnFormatFacade;
}
}

0 comments on commit af79c68

Please sign in to comment.