Skip to content

Commit

Permalink
Update tests for backwards codecs
Browse files Browse the repository at this point in the history
Updates tests for backwards codecs to prevent backwards codecs from
writing with a read only codec.

Signed-off-by: John Mazanec <[email protected]>
  • Loading branch information
jmazanec15 committed Jan 5, 2023
1 parent 6bbf086 commit ed0a642
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@
package org.opensearch.knn.index.codec.KNN940Codec;

import org.apache.lucene.codecs.Codec;
import org.apache.lucene.codecs.perfield.PerFieldKnnVectorsFormat;
import org.opensearch.index.mapper.MapperService;
import org.opensearch.knn.index.codec.KNNCodecTestCase;
import java.io.IOException;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;

import static org.opensearch.knn.index.codec.KNNCodecVersion.V_9_4_0;

Expand All @@ -26,15 +22,13 @@ public void testBuildFromModelTemplate() throws InterruptedException, ExecutionE
testBuildFromModelTemplate((KNN940Codec.builder().delegate(V_9_4_0.getDefaultCodecDelegate()).build()));
}

public void testKnnVectorIndex() throws Exception {
Function<MapperService, PerFieldKnnVectorsFormat> perFieldKnnVectorsFormatProvider = (
mapperService) -> new KNN940PerFieldKnnVectorsFormat(Optional.of(mapperService));

Function<PerFieldKnnVectorsFormat, Codec> knnCodecProvider = (knnVectorFormat) -> KNN940Codec.builder()
// Ensure that the codec is able to return the correct per field knn vectors format for codec
public void testCodecSetsCustomPerFieldKnnVectorsFormat() {
final Codec codec = KNN940Codec.builder()
.delegate(V_9_4_0.getDefaultCodecDelegate())
.knnVectorsFormat(knnVectorFormat)
.knnVectorsFormat(V_9_4_0.getPerFieldKnnVectorsFormat())
.build();

testKnnVectorIndex(knnCodecProvider, perFieldKnnVectorsFormatProvider);
assertTrue(codec.knnVectorsFormat() instanceof KNN940PerFieldKnnVectorsFormat);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
import org.apache.lucene.codecs.Codec;
import org.apache.lucene.codecs.perfield.PerFieldKnnVectorsFormat;
import org.opensearch.index.mapper.MapperService;
import org.opensearch.knn.index.codec.KNN940Codec.KNN940Codec;
import org.opensearch.knn.index.codec.KNN940Codec.KNN940PerFieldKnnVectorsFormat;
import org.opensearch.knn.index.codec.KNNCodecTestCase;

import java.util.Optional;
import java.util.function.Function;

import static org.opensearch.knn.index.codec.KNNCodecVersion.V_9_4_0;
import static org.opensearch.knn.index.codec.KNNCodecVersion.V_9_5_0;

public class KNN950CodecTests extends KNNCodecTestCase {
Expand All @@ -28,6 +31,18 @@ public void testBuildFromModelTemplate() {
testBuildFromModelTemplate((KNN950Codec.builder().delegate(V_9_5_0.getDefaultCodecDelegate()).build()));
}

// Ensure that the codec is able to return the correct per field knn vectors format for codec
public void testCodecSetsCustomPerFieldKnnVectorsFormat() {
final Codec codec = KNN940Codec.builder()
.delegate(V_9_4_0.getDefaultCodecDelegate())
.knnVectorsFormat(V_9_4_0.getPerFieldKnnVectorsFormat())
.build();

assertTrue(codec.knnVectorsFormat() instanceof KNN940PerFieldKnnVectorsFormat);
}

// IMPORTANT: When this Codec is moved to a backwards Codec, this test needs to be removed, because it attempts to
// write with a read only codec, which will fail
@SneakyThrows
public void testKnnVectorIndex() {
Function<MapperService, PerFieldKnnVectorsFormat> perFieldKnnVectorsFormatProvider = (
Expand Down

0 comments on commit ed0a642

Please sign in to comment.