Skip to content

Commit

Permalink
adding more tests
Browse files Browse the repository at this point in the history
Signed-off-by: Sarthak Aggarwal <sarthagg@amazon.com>
  • Loading branch information
sarthakaggarwal97 committed Jul 12, 2024
1 parent 40ca917 commit 8d969ca
Show file tree
Hide file tree
Showing 4 changed files with 316 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void close() throws IOException {

}

private static StarTreeBuilder getSingleTreeBuilder(
static StarTreeBuilder getSingleTreeBuilder(
StarTreeField starTreeField,
Map<String, DocValuesProducer> fieldProducerMap,
SegmentWriteState state,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* 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.compositeindex.datacube.startree.builder;

import org.apache.lucene.codecs.DocValuesProducer;
import org.apache.lucene.index.DocValuesType;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.index.SortedNumericDocValues;
import org.apache.lucene.index.VectorEncoding;
import org.apache.lucene.index.VectorSimilarityFunction;
import org.apache.lucene.search.DocIdSetIterator;
import org.opensearch.index.compositeindex.datacube.startree.utils.SequentialDocValuesIterator;
import org.opensearch.test.OpenSearchTestCase;

import java.io.IOException;
import java.util.Collections;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class StarTreeDocValuesIteratorAdapterTests extends OpenSearchTestCase {

private StarTreeDocValuesIteratorAdapter adapter;

@Override
public void setUp() throws Exception {
super.setUp();
adapter = new StarTreeDocValuesIteratorAdapter();
}

public void testGetDocValuesIterator() throws IOException {
DocValuesProducer mockProducer = mock(DocValuesProducer.class);
SortedNumericDocValues mockSortedNumericDocValues = mock(SortedNumericDocValues.class);

when(mockProducer.getSortedNumeric(any())).thenReturn(mockSortedNumericDocValues);

SequentialDocValuesIterator iterator = adapter.getDocValuesIterator(DocValuesType.SORTED_NUMERIC, any(), mockProducer);

assertNotNull(iterator);
assertEquals(mockSortedNumericDocValues, iterator.getDocIdSetIterator());
}

public void testGetDocValuesIteratorWithUnsupportedType() {
DocValuesProducer mockProducer = mock(DocValuesProducer.class);
FieldInfo fieldInfo = new FieldInfo(
"random_field",
0,
false,
false,
true,
IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS,
DocValuesType.SORTED_NUMERIC,
-1,
Collections.emptyMap(),
0,
0,
0,
0,
VectorEncoding.FLOAT32,
VectorSimilarityFunction.EUCLIDEAN,
false,
false
);
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> {
adapter.getDocValuesIterator(DocValuesType.BINARY, fieldInfo, mockProducer);
});

assertEquals("Unsupported DocValuesType: BINARY", exception.getMessage());
}

public void testGetNextValue() throws IOException {
SortedNumericDocValues mockSortedNumericDocValues = mock(SortedNumericDocValues.class);
SequentialDocValuesIterator iterator = new SequentialDocValuesIterator(mockSortedNumericDocValues);
iterator.setDocId(1);
when(mockSortedNumericDocValues.nextValue()).thenReturn(42L);

Long nextValue = adapter.getNextValue(iterator, 1);

assertEquals(Long.valueOf(42L), nextValue);
assertEquals(Long.valueOf(42L), iterator.getDocValue());
}

public void testGetNextValueWithInvalidDocId() {
SortedNumericDocValues mockSortedNumericDocValues = mock(SortedNumericDocValues.class);
SequentialDocValuesIterator iterator = new SequentialDocValuesIterator(mockSortedNumericDocValues);
iterator.setDocId(DocIdSetIterator.NO_MORE_DOCS);

IllegalStateException exception = expectThrows(IllegalStateException.class, () -> { adapter.getNextValue(iterator, 1); });

assertEquals("invalid doc id to fetch the next value", exception.getMessage());
}

public void testGetNextValueWithUnsupportedIterator() {
DocIdSetIterator mockIterator = mock(DocIdSetIterator.class);
SequentialDocValuesIterator iterator = new SequentialDocValuesIterator(mockIterator);

IllegalStateException exception = expectThrows(IllegalStateException.class, () -> { adapter.getNextValue(iterator, 1); });

assertEquals("Unsupported Iterator: " + mockIterator.toString(), exception.getMessage());
}

public void testNextDoc() throws IOException {
SortedNumericDocValues mockSortedNumericDocValues = mock(SortedNumericDocValues.class);
SequentialDocValuesIterator iterator = new SequentialDocValuesIterator(mockSortedNumericDocValues);
when(mockSortedNumericDocValues.nextDoc()).thenReturn(2, 3, DocIdSetIterator.NO_MORE_DOCS);
when(mockSortedNumericDocValues.nextValue()).thenReturn(42L, 32L);

int nextDocId = adapter.nextDoc(iterator, 1);
assertEquals(2, nextDocId);
assertEquals(Long.valueOf(42L), adapter.getNextValue(iterator, nextDocId));

nextDocId = adapter.nextDoc(iterator, 2);
assertEquals(3, nextDocId);
when(mockSortedNumericDocValues.nextValue()).thenReturn(42L, 32L);

}

public void testNextDoc_noMoreDocs() throws IOException {
SortedNumericDocValues mockSortedNumericDocValues = mock(SortedNumericDocValues.class);
SequentialDocValuesIterator iterator = new SequentialDocValuesIterator(mockSortedNumericDocValues);
when(mockSortedNumericDocValues.nextDoc()).thenReturn(2, DocIdSetIterator.NO_MORE_DOCS);
when(mockSortedNumericDocValues.nextValue()).thenReturn(42L, 32L);

int nextDocId = adapter.nextDoc(iterator, 1);
assertEquals(2, nextDocId);
assertEquals(Long.valueOf(42L), adapter.getNextValue(iterator, nextDocId));

assertThrows(IllegalStateException.class, () -> adapter.nextDoc(iterator, 2));

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* 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.compositeindex.datacube.startree.builder;

import org.apache.lucene.codecs.DocValuesProducer;
import org.apache.lucene.codecs.lucene99.Lucene99Codec;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.FieldInfos;
import org.apache.lucene.index.SegmentInfo;
import org.apache.lucene.index.SegmentWriteState;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.InfoStream;
import org.apache.lucene.util.Version;
import org.opensearch.index.compositeindex.datacube.startree.StarTreeField;
import org.opensearch.index.compositeindex.datacube.startree.StarTreeFieldConfiguration;
import org.opensearch.index.mapper.MapperService;
import org.opensearch.index.mapper.StarTreeMapper;
import org.opensearch.test.OpenSearchTestCase;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

public class StarTreesBuilderTests extends OpenSearchTestCase {

private MapperService mapperService;
private SegmentWriteState segmentWriteState;
private DocValuesProducer docValuesProducer;
private StarTreeMapper.StarTreeFieldType starTreeFieldType;
private StarTreeField starTreeField;
private Map<String, DocValuesProducer> fieldProducerMap;
private Directory directory;

public void setUp() throws Exception {
super.setUp();
mapperService = mock(MapperService.class);
directory = newFSDirectory(createTempDir());
SegmentInfo segmentInfo = new SegmentInfo(
directory,
Version.LATEST,
Version.LUCENE_9_11_0,
"test_segment",
5,
false,
false,
new Lucene99Codec(),
new HashMap<>(),
UUID.randomUUID().toString().substring(0, 16).getBytes(StandardCharsets.UTF_8),
new HashMap<>(),
null
);
FieldInfos fieldInfos = new FieldInfos(new FieldInfo[0]);
segmentWriteState = new SegmentWriteState(
InfoStream.getDefault(),
segmentInfo.dir,
segmentInfo,
fieldInfos,
null,
newIOContext(random())
);
docValuesProducer = mock(DocValuesProducer.class);
StarTreeFieldConfiguration starTreeFieldConfiguration = new StarTreeFieldConfiguration(
1,
new HashSet<>(),
StarTreeFieldConfiguration.StarTreeBuildMode.ON_HEAP
);
starTreeField = new StarTreeField("star_tree", new ArrayList<>(), new ArrayList<>(), starTreeFieldConfiguration);
starTreeFieldType = new StarTreeMapper.StarTreeFieldType("star_tree", starTreeField);
fieldProducerMap = new HashMap<>();
fieldProducerMap.put("field1", docValuesProducer);
}

public void test_buildWithNoStarTreeFields() throws IOException {
when(mapperService.getCompositeFieldTypes()).thenReturn(new HashSet<>());

StarTreesBuilder starTreesBuilder = new StarTreesBuilder(fieldProducerMap, segmentWriteState, mapperService);
starTreesBuilder.build();

verifyNoInteractions(docValuesProducer);
}

public void test_getSingleTreeBuilder() throws IOException {
when(mapperService.getCompositeFieldTypes()).thenReturn(Set.of(starTreeFieldType));
StarTreeBuilder starTreeBuilder = StarTreesBuilder.getSingleTreeBuilder(starTreeField, fieldProducerMap, segmentWriteState, mapperService);
assertTrue(starTreeBuilder instanceof OnHeapStarTreeBuilder);
}

public void test_getSingleTreeBuilder_illegalArgument() {
when(mapperService.getCompositeFieldTypes()).thenReturn(Set.of(starTreeFieldType));
StarTreeFieldConfiguration starTreeFieldConfiguration = new StarTreeFieldConfiguration(1, new HashSet<>(), StarTreeFieldConfiguration.StarTreeBuildMode.OFF_HEAP);
StarTreeField starTreeField = new StarTreeField("star_tree", new ArrayList<>(), new ArrayList<>(), starTreeFieldConfiguration);
assertThrows(IllegalArgumentException.class, () -> StarTreesBuilder.getSingleTreeBuilder(starTreeField, fieldProducerMap, segmentWriteState, mapperService));
}

public void test_closeWithNoStarTreeFields() throws IOException {
StarTreeFieldConfiguration starTreeFieldConfiguration = new StarTreeFieldConfiguration(
1,
new HashSet<>(),
StarTreeFieldConfiguration.StarTreeBuildMode.OFF_HEAP
);
StarTreeField starTreeField = new StarTreeField("star_tree", new ArrayList<>(), new ArrayList<>(), starTreeFieldConfiguration);
starTreeFieldType = new StarTreeMapper.StarTreeFieldType("star_tree", starTreeField);
when(mapperService.getCompositeFieldTypes()).thenReturn(Set.of(starTreeFieldType));
StarTreesBuilder starTreesBuilder = new StarTreesBuilder(fieldProducerMap, segmentWriteState, mapperService);
starTreesBuilder.close();

verifyNoInteractions(docValuesProducer);
}

@Override
public void tearDown() throws Exception {
super.tearDown();
directory.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.compositeindex.datacube.startree.utils;

import org.apache.lucene.index.SortedNumericDocValues;
import org.opensearch.index.fielddata.AbstractNumericDocValues;
import org.opensearch.test.OpenSearchTestCase;

import java.io.IOException;

public class SequentialDocValuesIteratorTests extends OpenSearchTestCase {

public void test_sequentialDocValuesIterator() {
SequentialDocValuesIterator sequentialDocValuesIterator = new SequentialDocValuesIterator(new AbstractNumericDocValues() {
@Override
public long longValue() throws IOException {
return 0;
}

@Override
public boolean advanceExact(int i) throws IOException {
return false;
}

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

assertTrue(sequentialDocValuesIterator.getDocIdSetIterator() instanceof AbstractNumericDocValues);
assertEquals(sequentialDocValuesIterator.getDocId(), 0);
}

public void test_sequentialDocValuesIterator_default() {
SequentialDocValuesIterator sequentialDocValuesIterator = new SequentialDocValuesIterator();
assertTrue(sequentialDocValuesIterator.getDocIdSetIterator() instanceof SortedNumericDocValues);
}

}

0 comments on commit 8d969ca

Please sign in to comment.