diff --git a/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/KNN91Codec.java b/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/KNN91Codec.java index 4499796642..5894ee5a11 100644 --- a/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/KNN91Codec.java +++ b/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/KNN91Codec.java @@ -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 @@ -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()); } /** @@ -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(); } } diff --git a/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/KNN91DocFormat.java b/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/KNN91DocFormat.java deleted file mode 100644 index 72b2d082f4..0000000000 --- a/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/KNN91DocFormat.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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.CompoundFormat; -import org.apache.lucene.codecs.DocValuesFormat; -import org.opensearch.knn.index.codec.KNNDocFormatFacade; - -public class KNN91DocFormat implements KNNDocFormatFacade { - - private final DocValuesFormat docValuesFormat; - private final CompoundFormat compoundFormat; - - public KNN91DocFormat(final DocValuesFormat docValuesFormat, final CompoundFormat compoundFormat) { - this.docValuesFormat = docValuesFormat; - this.compoundFormat = compoundFormat; - } - - @Override - public DocValuesFormat docValuesFormat() { - return docValuesFormat; - } - - @Override - public CompoundFormat compoundFormat() { - return compoundFormat; - } -} diff --git a/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/docformat/KNN91BinaryDocValues.java b/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/docformat/KNN91BinaryDocValues.java index 7c3baf5c5c..b0d015e64a 100644 --- a/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/docformat/KNN91BinaryDocValues.java +++ b/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/docformat/KNN91BinaryDocValues.java @@ -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; diff --git a/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/docformat/KNN91CompoundFormat.java b/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/docformat/KNN91CompoundFormat.java index 360806c5c3..ac3e59db20 100644 --- a/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/docformat/KNN91CompoundFormat.java +++ b/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/docformat/KNN91CompoundFormat.java @@ -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; diff --git a/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/docformat/KNN91DocValuesConsumer.java b/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/docformat/KNN91DocValuesConsumer.java index a02af15e3d..5dc1873466 100644 --- a/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/docformat/KNN91DocValuesConsumer.java +++ b/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/docformat/KNN91DocValuesConsumer.java @@ -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; diff --git a/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/docformat/KNN91DocValuesFormat.java b/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/docformat/KNN91DocValuesFormat.java index be8f0a6721..3e24af87be 100644 --- a/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/docformat/KNN91DocValuesFormat.java +++ b/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/docformat/KNN91DocValuesFormat.java @@ -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; @@ -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 * diff --git a/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/docformat/KNN91DocValuesReader.java b/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/docformat/KNN91DocValuesReader.java index c63bed0f9e..d870710dfa 100644 --- a/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/docformat/KNN91DocValuesReader.java +++ b/src/main/java/org/opensearch/knn/index/codec/KNN91Codec/docformat/KNN91DocValuesReader.java @@ -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; diff --git a/src/main/java/org/opensearch/knn/index/codec/KNNCodecFactory.java b/src/main/java/org/opensearch/knn/index/codec/KNNCodecFactory.java index 378e856727..68c4b879dd 100644 --- a/src/main/java/org/opensearch/knn/index/codec/KNNCodecFactory.java +++ b/src/main/java/org/opensearch/knn/index/codec/KNNCodecFactory.java @@ -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 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 } } diff --git a/src/main/java/org/opensearch/knn/index/codec/KNNCodecService.java b/src/main/java/org/opensearch/knn/index/codec/KNNCodecService.java index 622f6ef17b..cae6f7fb82 100644 --- a/src/main/java/org/opensearch/knn/index/codec/KNNCodecService.java +++ b/src/main/java/org/opensearch/knn/index/codec/KNNCodecService.java @@ -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)); } } diff --git a/src/main/java/org/opensearch/knn/index/codec/KNNDocFormatFacade.java b/src/main/java/org/opensearch/knn/index/codec/KNNDocFormatFacade.java deleted file mode 100644 index d5b6b5ff1b..0000000000 --- a/src/main/java/org/opensearch/knn/index/codec/KNNDocFormatFacade.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * 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 org.apache.lucene.codecs.CompoundFormat; -import org.apache.lucene.codecs.DocValuesFormat; - -public interface KNNDocFormatFacade { - - DocValuesFormat docValuesFormat(); - - CompoundFormat compoundFormat(); -} diff --git a/src/main/java/org/opensearch/knn/index/codec/KNNDocFormatFactory.java b/src/main/java/org/opensearch/knn/index/codec/KNNDocFormatFactory.java deleted file mode 100644 index e3a3d50181..0000000000 --- a/src/main/java/org/opensearch/knn/index/codec/KNNDocFormatFactory.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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 org.apache.lucene.codecs.Codec; -import org.opensearch.knn.index.codec.KNN91Codec.docformat.KNN91CompoundFormat; -import org.opensearch.knn.index.codec.KNN91Codec.KNN91DocFormat; -import org.opensearch.knn.index.codec.KNN91Codec.docformat.KNN91DocValuesFormat; - -/** - * Factory abstraction for KNN document format facades - */ -public class KNNDocFormatFactory { - - public static KNNDocFormatFacade createKNN91DocFormat(Codec delegate) { - final KNNDocFormatFacade knnDocFormatFacade = new KNN91DocFormat( - new KNN91DocValuesFormat(delegate.docValuesFormat()), - new KNN91CompoundFormat(delegate.compoundFormat()) - ); - return knnDocFormatFacade; - } -} diff --git a/src/main/java/org/opensearch/knn/index/codec/KNNFormatFacade.java b/src/main/java/org/opensearch/knn/index/codec/KNNFormatFacade.java new file mode 100644 index 0000000000..bf9b1ad7f1 --- /dev/null +++ b/src/main/java/org/opensearch/knn/index/codec/KNNFormatFacade.java @@ -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; + } +} diff --git a/src/main/java/org/opensearch/knn/index/codec/KNNFormatFactory.java b/src/main/java/org/opensearch/knn/index/codec/KNNFormatFactory.java new file mode 100644 index 0000000000..5f5a447c6c --- /dev/null +++ b/src/main/java/org/opensearch/knn/index/codec/KNNFormatFactory.java @@ -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; + } +}