Skip to content

Commit

Permalink
Added tests and refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Sarthak Aggarwal <sarthagg@amazon.com>
  • Loading branch information
sarthakaggarwal97 committed Jun 5, 2023
1 parent f43f51c commit fb50270
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 19 deletions.
Empty file.
1 change: 0 additions & 1 deletion server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ dependencies {
// jna
api "net.java.dev.jna:jna:${versions.jna}"

api "com.github.luben:zstd-jni:1.5.5-1"
// protobuf
api "com.google.protobuf:protobuf-java:${versions.protobuf}"
api "jakarta.annotation:jakarta.annotation-api:${versions.jakarta_annotation}"
Expand Down
1 change: 0 additions & 1 deletion server/licenses/zstd-jni-1.5.5-1.jar.sha1

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.opensearch.index.codec.PerFieldMappingPostingFormatCodec;
import org.opensearch.index.mapper.MapperService;

abstract class Lucene95CustomCodec extends FilterCodec {
public abstract class Lucene95CustomCodec extends FilterCodec {
public static final int DEFAULT_COMPRESSION_LEVEL = 6;

/** Each mode represents a compression algorithm. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public StoredFieldsWriter fieldsWriter(Directory directory, SegmentInfo si, IOCo
return impl(mode).fieldsWriter(directory, si, context);
}

private StoredFieldsFormat impl(Lucene95CustomCodec.Mode mode) {
StoredFieldsFormat impl(Lucene95CustomCodec.Mode mode) {
switch (mode) {
case ZSTD:
return new Lucene90CompressingStoredFieldsFormat(
Expand All @@ -118,4 +118,8 @@ private StoredFieldsFormat impl(Lucene95CustomCodec.Mode mode) {
throw new AssertionError();
}
}

Lucene95CustomCodec.Mode getMode() {
return mode;
}
}
81 changes: 66 additions & 15 deletions server/src/test/java/org/opensearch/index/codec/CodecTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
import org.opensearch.env.Environment;
import org.opensearch.index.IndexSettings;
import org.opensearch.index.analysis.IndexAnalyzers;
import org.opensearch.index.codec.customcodecs.Lucene95CustomCodec;
import org.opensearch.index.codec.customcodecs.Lucene95CustomStoredFieldsFormat;
import org.opensearch.index.mapper.MapperService;
import org.opensearch.index.similarity.SimilarityService;
import org.opensearch.indices.mapper.MapperRegistry;
Expand All @@ -63,40 +65,75 @@
public class CodecTests extends OpenSearchTestCase {

public void testResolveDefaultCodecs() throws Exception {
CodecService codecService = createCodecService();
CodecService codecService = createCodecService(false);
assertThat(codecService.codec("default"), instanceOf(PerFieldMappingPostingFormatCodec.class));
assertThat(codecService.codec("default"), instanceOf(Lucene95Codec.class));
}

public void testDefault() throws Exception {
Codec codec = createCodecService().codec("default");
Codec codec = createCodecService(false).codec("default");
assertStoredFieldsCompressionEquals(Lucene95Codec.Mode.BEST_SPEED, codec);
}

public void testBestCompression() throws Exception {
Codec codec = createCodecService().codec("best_compression");
Codec codec = createCodecService(false).codec("best_compression");
assertStoredFieldsCompressionEquals(Lucene95Codec.Mode.BEST_COMPRESSION, codec);
}

public void testZstd() throws Exception {
Codec codec = createCodecService(false).codec("zstd");
assertStoredFieldsCompressionEquals(Lucene95CustomCodec.Mode.ZSTD, codec);
}

public void testZstdNoDict() throws Exception {
Codec codec = createCodecService(false).codec("zstd_no_dict");
assertStoredFieldsCompressionEquals(Lucene95CustomCodec.Mode.ZSTD_NO_DICT, codec);
}

public void testDefaultMapperServiceNull() throws Exception {
Codec codec = createCodecService(true).codec("default");
assertStoredFieldsCompressionEquals(Lucene95Codec.Mode.BEST_SPEED, codec);
}

public void testBestCompressionMapperServiceNull() throws Exception {
Codec codec = createCodecService(true).codec("best_compression");
assertStoredFieldsCompressionEquals(Lucene95Codec.Mode.BEST_COMPRESSION, codec);
}

public void testZstdMapperServiceNull() throws Exception {
Codec codec = createCodecService(true).codec("zstd");
assertStoredFieldsCompressionEquals(Lucene95CustomCodec.Mode.ZSTD, codec);
}

public void testZstdNoDictMapperServiceNull() throws Exception {
Codec codec = createCodecService(true).codec("zstd_no_dict");
assertStoredFieldsCompressionEquals(Lucene95CustomCodec.Mode.ZSTD_NO_DICT, codec);
}

public void testExceptionCodecNull() {
assertThrows(IllegalArgumentException.class, () -> createCodecService(true).codec(null));
}

// write some docs with it, inspect .si to see this was the used compression
private void assertStoredFieldsCompressionEquals(Lucene95Codec.Mode expected, Codec actual) throws Exception {
Directory dir = newDirectory();
IndexWriterConfig iwc = newIndexWriterConfig(null);
iwc.setCodec(actual);
IndexWriter iw = new IndexWriter(dir, iwc);
iw.addDocument(new Document());
iw.commit();
iw.close();
DirectoryReader ir = DirectoryReader.open(dir);
SegmentReader sr = (SegmentReader) ir.leaves().get(0).reader();
SegmentReader sr = getSegmentReader(actual);
String v = sr.getSegmentInfo().info.getAttribute(Lucene90StoredFieldsFormat.MODE_KEY);
assertNotNull(v);
assertEquals(expected, Lucene95Codec.Mode.valueOf(v));
ir.close();
dir.close();
}

private CodecService createCodecService() throws IOException {
private void assertStoredFieldsCompressionEquals(Lucene95CustomCodec.Mode expected, Codec actual) throws Exception {
SegmentReader sr = getSegmentReader(actual);
String v = sr.getSegmentInfo().info.getAttribute(Lucene95CustomStoredFieldsFormat.MODE_KEY);
assertNotNull(v);
assertEquals(expected, Lucene95CustomCodec.Mode.valueOf(v));
}

private CodecService createCodecService(boolean isMapperServiceNull) throws IOException {

if (isMapperServiceNull) {
return new CodecService(null, LogManager.getLogger("test"));
}
Settings nodeSettings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).build();
IndexSettings settings = IndexSettingsModule.newIndexSettings("_na", nodeSettings);
SimilarityService similarityService = new SimilarityService(settings, null, Collections.emptyMap());
Expand All @@ -115,4 +152,18 @@ private CodecService createCodecService() throws IOException {
return new CodecService(service, LogManager.getLogger("test"));
}

private SegmentReader getSegmentReader(Codec actual) throws IOException {
Directory dir = newDirectory();
IndexWriterConfig iwc = newIndexWriterConfig(null);
iwc.setCodec(actual);
IndexWriter iw = new IndexWriter(dir, iwc);
iw.addDocument(new Document());
iw.commit();
iw.close();
DirectoryReader ir = DirectoryReader.open(dir);
SegmentReader sr = (SegmentReader) ir.leaves().get(0).reader();
ir.close();
dir.close();
return sr;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.
*/

package org.opensearch.index.codec.customcodecs;

import org.opensearch.test.OpenSearchTestCase;

public class Lucene95CustomStoredFieldsFormatTests extends OpenSearchTestCase {

public void testDefaultLucene95CustomCodecMode() {
Lucene95CustomStoredFieldsFormat lucene95CustomStoredFieldsFormat = new Lucene95CustomStoredFieldsFormat();
assertEquals(Lucene95CustomCodec.Mode.ZSTD, lucene95CustomStoredFieldsFormat.getMode());
}

public void testZstdNoDictLucene95CustomCodecMode() {
Lucene95CustomStoredFieldsFormat lucene95CustomStoredFieldsFormat = new Lucene95CustomStoredFieldsFormat(Lucene95CustomCodec.Mode.ZSTD_NO_DICT);
assertEquals(Lucene95CustomCodec.Mode.ZSTD_NO_DICT, lucene95CustomStoredFieldsFormat.getMode());
}

}

0 comments on commit fb50270

Please sign in to comment.