forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Sarthak Aggarwal <[email protected]>
- Loading branch information
1 parent
2a08288
commit 911a4c0
Showing
6 changed files
with
306 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
.../opensearch/index/compositeindex/datacube/startree/node/FixedLengthStarTreeNodeTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...ava/org/opensearch/index/compositeindex/datacube/startree/node/StarTreeNodeTypeTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters