Skip to content

Commit

Permalink
adding more tests
Browse files Browse the repository at this point in the history
Signed-off-by: Sarthak Aggarwal <[email protected]>
  • Loading branch information
sarthakaggarwal97 committed Aug 25, 2024
1 parent 2a08288 commit 911a4c0
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ public class FixedLengthStarTreeNode implements StarTreeNode {
* NUM_LONG_SERIALIZABLE_FIELDS) + (NUM_BYTE_SERIALIZABLE_FIELDS * Byte.BYTES);

// Byte offsets for each field in the serialized data
private static final int DIMENSION_ID_OFFSET = 0;
private static final int DIMENSION_VALUE_OFFSET = DIMENSION_ID_OFFSET + Integer.BYTES;
private static final int START_DOC_ID_OFFSET = DIMENSION_VALUE_OFFSET + Long.BYTES;
private static final int END_DOC_ID_OFFSET = START_DOC_ID_OFFSET + Integer.BYTES;
private static final int AGGREGATE_DOC_ID_OFFSET = END_DOC_ID_OFFSET + Integer.BYTES;
private static final int STAR_NODE_TYPE_OFFSET = AGGREGATE_DOC_ID_OFFSET + Integer.BYTES;
private static final int FIRST_CHILD_ID_OFFSET = STAR_NODE_TYPE_OFFSET + Byte.BYTES;
private static final int LAST_CHILD_ID_OFFSET = FIRST_CHILD_ID_OFFSET + Integer.BYTES;
static final int DIMENSION_ID_OFFSET = 0;
static final int DIMENSION_VALUE_OFFSET = DIMENSION_ID_OFFSET + Integer.BYTES;
static final int START_DOC_ID_OFFSET = DIMENSION_VALUE_OFFSET + Long.BYTES;
static final int END_DOC_ID_OFFSET = START_DOC_ID_OFFSET + Integer.BYTES;
static final int AGGREGATE_DOC_ID_OFFSET = END_DOC_ID_OFFSET + Integer.BYTES;
static final int STAR_NODE_TYPE_OFFSET = AGGREGATE_DOC_ID_OFFSET + Integer.BYTES;
static final int FIRST_CHILD_ID_OFFSET = STAR_NODE_TYPE_OFFSET + Byte.BYTES;
static final int LAST_CHILD_ID_OFFSET = FIRST_CHILD_ID_OFFSET + Integer.BYTES;

/**
* Constant representing an invalid node ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.opensearch.index.compositeindex.datacube.startree.node.InMemoryTreeNode;
import org.opensearch.index.compositeindex.datacube.startree.node.StarTree;
import org.opensearch.index.compositeindex.datacube.startree.node.StarTreeNode;
import org.opensearch.index.compositeindex.datacube.startree.node.StarTreeNodeType;
import org.opensearch.test.OpenSearchTestCase;
import org.junit.Before;

Expand Down Expand Up @@ -79,26 +80,70 @@ public void test_StarTreeNode() throws IOException {
if (starTreeNode.getChildDimensionId() != -1) {
while (childrenIterator.hasNext()) {
StarTreeNode child = childrenIterator.next();
assertStarTreeNode(
starTreeNode.getChildForDimensionValue(child.getDimensionValue(), false),
inMemoryTreeNodeMap.get(child.getDimensionValue())
);
if (child.getStarTreeNodeType() == StarTreeNodeType.DEFAULT.getValue()) {
assertStarTreeNode(
starTreeNode.getChildForDimensionValue(child.getDimensionValue(), false),
inMemoryTreeNodeMap.get(child.getDimensionValue())
);
assertNull(starTreeNode.getChildForDimensionValue(child.getDimensionValue(), true));
}

queue.add(child);
}
} else {
assertTrue(starTreeNode.isLeaf());
}
}

dataIn.close();

}

public void test_starTreeSearch() throws IOException {

dataOut = directory.createOutput("star-tree-data", IOContext.DEFAULT);
Map<Long, InMemoryTreeNode> inMemoryTreeNodeMap = new LinkedHashMap<>();
InMemoryTreeNode root = generateSampleTree(inMemoryTreeNodeMap);
StarTreeWriter starTreeWriter = new StarTreeWriter();
long starTreeDataLength = starTreeWriter.writeStarTree(dataOut, root, inMemoryTreeNodeMap.size(), "star-tree");

// asserting on the actual length of the star tree data file
assertEquals(starTreeDataLength, (inMemoryTreeNodeMap.size() * 33L) + 16);
dataOut.close();

dataIn = directory.openInput("star-tree-data", IOContext.READONCE);

StarTreeMetadata starTreeMetadata = mock(StarTreeMetadata.class);
when(starTreeMetadata.getDataLength()).thenReturn(starTreeDataLength);
when(starTreeMetadata.getDataStartFilePointer()).thenReturn(0L);
StarTree starTree = new StarTree(dataIn, starTreeMetadata);

StarTreeNode starTreeNode = starTree.getRoot();
InMemoryTreeNode inMemoryTreeNode = inMemoryTreeNodeMap.get(starTreeNode.getDimensionValue());
assertNotNull(inMemoryTreeNode);

for (int i = 0; i < maxLevels - 1; i++) {
InMemoryTreeNode randomChildNode = randomFrom(inMemoryTreeNode.children.values());
StarTreeNode randomStarTreeChildNode = starTreeNode.getChildForDimensionValue(randomChildNode.dimensionValue, false);

assertNotNull(randomStarTreeChildNode);
assertStarTreeNode(randomStarTreeChildNode, randomChildNode);

starTreeNode = randomStarTreeChildNode;
inMemoryTreeNode = randomChildNode;

}
dataIn.close();
}

private void assertStarTreeNode(StarTreeNode starTreeNode, InMemoryTreeNode treeNode) throws IOException {
assertEquals(starTreeNode.getDimensionId(), treeNode.dimensionId);
assertEquals(starTreeNode.getDimensionValue(), treeNode.dimensionValue);
assertEquals(starTreeNode.getStartDocId(), treeNode.startDocId);
assertEquals(starTreeNode.getEndDocId(), treeNode.endDocId);
assertEquals(starTreeNode.getChildDimensionId(), treeNode.childDimensionId);
assertEquals(starTreeNode.getAggregatedDocId(), treeNode.aggregatedDocId);
assertEquals(starTreeNode.getStarTreeNodeType(), treeNode.nodeType);

if (starTreeNode.getChildDimensionId() != -1) {
assertFalse(starTreeNode.isLeaf());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import static org.opensearch.index.compositeindex.CompositeIndexConstants.COMPOSITE_FIELD_MARKER;
Expand Down Expand Up @@ -125,7 +125,7 @@ public void test_starTreeMetadata() throws IOException {
int maxLeafDocs = randomNonNegativeInt();
StarTreeFieldConfiguration starTreeFieldConfiguration = new StarTreeFieldConfiguration(
maxLeafDocs,
new HashSet<>(),
Set.of("field10"),
StarTreeFieldConfiguration.StarTreeBuildMode.ON_HEAP
);
starTreeField = new StarTreeField("star_tree", dimensionsOrder, metrics, starTreeFieldConfiguration);
Expand Down Expand Up @@ -168,8 +168,10 @@ public void test_starTreeMetadata() throws IOException {
);

StarTreeMetadata starTreeMetadata = new StarTreeMetadata(metaIn, compositeFieldName, compositeFieldType);
assertEquals(starTreeField.getName(), starTreeMetadata.getStarTreeFieldName());
assertEquals(starTreeField.getName(), starTreeMetadata.getCompositeFieldName());
assertEquals(STAR_TREE, starTreeMetadata.getCompositeFieldType());
assertEquals(STAR_TREE.getName(), starTreeMetadata.getStarTreeFieldType());

assertNotNull(starTreeMetadata);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/*
* 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.node;

import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
import org.opensearch.index.compositeindex.datacube.startree.fileformats.StarTreeWriter;
import org.opensearch.index.compositeindex.datacube.startree.fileformats.meta.StarTreeMetadata;
import org.opensearch.index.compositeindex.datacube.startree.utils.StarTreeUtils;
import org.opensearch.test.OpenSearchTestCase;
import org.junit.Before;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

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

public class FixedLengthStarTreeNodeTests extends OpenSearchTestCase {

private IndexOutput dataOut;
private IndexInput dataIn;
private Directory directory;
InMemoryTreeNode node;
InMemoryTreeNode starChild;
FixedLengthStarTreeNode starTreeNode;

@Before
public void setup() throws IOException {
directory = newFSDirectory(createTempDir());

dataOut = directory.createOutput("star-tree-data", IOContext.DEFAULT);
StarTreeWriter starTreeWriter = new StarTreeWriter();

node = new InMemoryTreeNode();
node.dimensionId = 0;
node.startDocId = randomInt();
node.endDocId = randomInt();
node.childDimensionId = 1;
node.aggregatedDocId = randomInt();
node.nodeType = randomFrom((byte) 0, (byte) -1, (byte) 2);
node.children = new HashMap<>();

starChild = new InMemoryTreeNode();
starChild.dimensionId = node.dimensionId + 1;
starChild.dimensionValue = -1;
starChild.startDocId = randomInt();
starChild.endDocId = randomInt();
starChild.childDimensionId = -1;
starChild.aggregatedDocId = randomInt();
starChild.nodeType = (byte) -2;
starChild.children = new HashMap<>();
node.children.put(-1L, starChild);

for (int i = 1; i < randomIntBetween(2, 5); i++) {
InMemoryTreeNode child = new InMemoryTreeNode();
child.dimensionId = node.dimensionId + 1;
child.dimensionValue = node.dimensionValue + i; // Assign a unique dimension value for each child
child.startDocId = randomInt();
child.endDocId = randomInt();
child.childDimensionId = -1;
child.aggregatedDocId = randomInt();
child.nodeType = (byte) 0;
child.children = new HashMap<>();
node.children.put(child.dimensionValue, child);
}

long starTreeDataLength = starTreeWriter.writeStarTree(dataOut, node, 1 + node.children.size(), "star-tree");

// asserting on the actual length of the star tree data file
assertEquals(starTreeDataLength, 33L * node.children.size() + 33 + 16);
dataOut.close();

dataIn = directory.openInput("star-tree-data", IOContext.READONCE);
StarTreeMetadata starTreeMetadata = mock(StarTreeMetadata.class);
when(starTreeMetadata.getDataLength()).thenReturn(starTreeDataLength);
when(starTreeMetadata.getDataStartFilePointer()).thenReturn(0L);
StarTree starTree = new StarTree(dataIn, starTreeMetadata);

starTreeNode = (FixedLengthStarTreeNode) starTree.getRoot();

}

public void testOffsets() {
assertEquals(0, FixedLengthStarTreeNode.DIMENSION_ID_OFFSET);
assertEquals(4, FixedLengthStarTreeNode.DIMENSION_VALUE_OFFSET);
assertEquals(12, FixedLengthStarTreeNode.START_DOC_ID_OFFSET);
assertEquals(16, FixedLengthStarTreeNode.END_DOC_ID_OFFSET);
assertEquals(20, FixedLengthStarTreeNode.AGGREGATE_DOC_ID_OFFSET);
assertEquals(24, FixedLengthStarTreeNode.STAR_NODE_TYPE_OFFSET);
assertEquals(25, FixedLengthStarTreeNode.FIRST_CHILD_ID_OFFSET);
assertEquals(29, FixedLengthStarTreeNode.LAST_CHILD_ID_OFFSET);
}

public void testSerializableDataSize() {
assertEquals(33, FixedLengthStarTreeNode.SERIALIZABLE_DATA_SIZE_IN_BYTES);
}

public void testGetDimensionId() throws IOException {
assertEquals(node.dimensionId, starTreeNode.getDimensionId());
}

public void testGetDimensionValue() throws IOException {
assertEquals(node.dimensionValue, starTreeNode.getDimensionValue());
}

public void testGetStartDocId() throws IOException {
assertEquals(node.startDocId, starTreeNode.getStartDocId());
}

public void testGetEndDocId() throws IOException {
assertEquals(node.endDocId, starTreeNode.getEndDocId());
}

public void testGetAggregatedDocId() throws IOException {
assertEquals(node.aggregatedDocId, starTreeNode.getAggregatedDocId());
}

public void testGetNumChildren() throws IOException {
assertEquals(node.children.size(), starTreeNode.getNumChildren());
}

public void testIsLeaf() {
assertFalse(starTreeNode.isLeaf());
}

public void testGetStarTreeNodeType() throws IOException {
assertEquals(node.getNodeType(), starTreeNode.getStarTreeNodeType());
}

public void testGetChildForDimensionValue() throws IOException {
long dimensionValue = randomIntBetween(0, node.children.size() - 2);
FixedLengthStarTreeNode childNode = (FixedLengthStarTreeNode) starTreeNode.getChildForDimensionValue(dimensionValue, false);
assertNotNull(childNode);
assertEquals(dimensionValue, childNode.getDimensionValue());
}

public void testGetChildrenIterator() throws IOException {
Iterator<FixedLengthStarTreeNode> iterator = starTreeNode.getChildrenIterator();
int count = 0;
while (iterator.hasNext()) {
FixedLengthStarTreeNode child = iterator.next();
assertNotNull(child);
count++;
}
assertEquals(starTreeNode.getNumChildren(), count);
}

public void testGetChildForStarNode() throws IOException {
// Assuming the first child is a star node in our test data
FixedLengthStarTreeNode starNode = (FixedLengthStarTreeNode) starTreeNode.getChildForDimensionValue((long) StarTreeUtils.ALL, true);
assertNotNull(starNode);
assertEquals(StarTreeUtils.ALL, starNode.getDimensionValue());
}

public void testGetChildForNullNode() throws IOException {
FixedLengthStarTreeNode nullNode = (FixedLengthStarTreeNode) starTreeNode.getChildForDimensionValue(null, false);
assertNull(nullNode);
}

public void testGetChildForInvalidDimensionValue() throws IOException {
long invalidDimensionValue = Long.MAX_VALUE;
assertThrows(AssertionError.class, () -> starTreeNode.getChildForDimensionValue(invalidDimensionValue, false));
}

public void tearDown() throws Exception {
super.tearDown();
dataIn.close();
dataOut.close();
directory.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.node;

import org.opensearch.test.OpenSearchTestCase;

public class StarTreeNodeTypeTests extends OpenSearchTestCase {

public void testStarNodeType() {
assertEquals("star", StarTreeNodeType.STAR.getName());
assertEquals((byte) -2, StarTreeNodeType.STAR.getValue());
}

public void testNullNodeType() {
assertEquals("null", StarTreeNodeType.NULL.getName());
assertEquals((byte) -1, StarTreeNodeType.NULL.getValue());
}

public void testDefaultNodeType() {
assertEquals("default", StarTreeNodeType.DEFAULT.getName());
assertEquals((byte) 0, StarTreeNodeType.DEFAULT.getValue());
}

public void testFromValue() {
assertEquals(StarTreeNodeType.STAR, StarTreeNodeType.fromValue((byte) -2));
assertEquals(StarTreeNodeType.NULL, StarTreeNodeType.fromValue((byte) -1));
assertEquals(StarTreeNodeType.DEFAULT, StarTreeNodeType.fromValue((byte) 0));
}

public void testFromValueInvalid() {
IllegalStateException exception = expectThrows(IllegalStateException.class, () -> StarTreeNodeType.fromValue((byte) 1));
assertEquals("Unrecognized value byte to determine star-tree node type: [1]", exception.getMessage());
}

public void testEnumValues() {
StarTreeNodeType[] values = StarTreeNodeType.values();
assertEquals(3, values.length);
assertArrayEquals(new StarTreeNodeType[] { StarTreeNodeType.STAR, StarTreeNodeType.NULL, StarTreeNodeType.DEFAULT }, values);
}

public void testEnumValueOf() {
assertEquals(StarTreeNodeType.STAR, StarTreeNodeType.valueOf("STAR"));
assertEquals(StarTreeNodeType.NULL, StarTreeNodeType.valueOf("NULL"));
assertEquals(StarTreeNodeType.DEFAULT, StarTreeNodeType.valueOf("DEFAULT"));
}

public void testEnumValueOfInvalid() {
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> StarTreeNodeType.valueOf("INVALID"));
assertTrue(exception.getMessage().contains("No enum constant"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.opensearch.index.compositeindex.datacube.Metric;
import org.opensearch.index.compositeindex.datacube.MetricStat;
import org.opensearch.index.compositeindex.datacube.NumericDimension;
import org.opensearch.index.compositeindex.datacube.ReadDimension;
import org.opensearch.index.compositeindex.datacube.startree.StarTreeField;
import org.opensearch.index.compositeindex.datacube.startree.StarTreeFieldConfiguration;
import org.junit.After;
Expand Down Expand Up @@ -309,6 +310,11 @@ public void testDimensions() {
assertEquals(n1, n2);
n2 = new NumericDimension("name1");
assertNotEquals(n1, n2);
ReadDimension r1 = new ReadDimension("name");
ReadDimension r2 = new ReadDimension("name");
assertEquals(r1, r2);
r2 = new ReadDimension("name1");
assertNotEquals(r1, r2);
}

public void testStarTreeField() {
Expand Down

0 comments on commit 911a4c0

Please sign in to comment.