Skip to content

Commit

Permalink
nits
Browse files Browse the repository at this point in the history
Signed-off-by: Sarthak Aggarwal <[email protected]>
  • Loading branch information
sarthakaggarwal97 committed Sep 2, 2024
1 parent d2bba60 commit 4aec8d2
Showing 1 changed file with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package org.opensearch.index.compositeindex.datacube.startree.node;

import org.opensearch.common.SetOnce;
import org.opensearch.common.annotation.ExperimentalApi;

import java.util.LinkedHashMap;
Expand All @@ -24,6 +25,12 @@
public class InMemoryTreeNode {

public InMemoryTreeNode() {
this.dimensionId = ALL;
this.startDocId = ALL;
this.endDocId = ALL;
this.nodeType = (byte) 0;
this.dimensionValue = ALL;
this.childStarNode = new SetOnce<>();
this.children = new LinkedHashMap<>();
}

Expand All @@ -33,23 +40,24 @@ public InMemoryTreeNode(int dimensionId, int startDocId, int endDocId, byte node
this.endDocId = endDocId;
this.nodeType = nodeType;
this.dimensionValue = dimensionValue;
this.childStarNode = new SetOnce<>();
this.children = new LinkedHashMap<>();
}

/**
* The dimension id for the dimension (field) associated with this star-tree node.
*/
private int dimensionId = ALL;
private final int dimensionId;

/**
* The starting document id (inclusive) associated with this star-tree node.
*/
private int startDocId = ALL;
private final int startDocId;

/**
* The ending document id (exclusive) associated with this star-tree node.
*/
private int endDocId = ALL;
private final int endDocId;

/**
* The aggregated document id associated with this star-tree node.
Expand All @@ -64,12 +72,12 @@ public InMemoryTreeNode(int dimensionId, int startDocId, int endDocId, byte node
/**
* The value of the dimension associated with this star-tree node.
*/
private long dimensionValue = ALL;
private final long dimensionValue;

/**
* A byte indicating whether the node is star node, null node or default node (with dimension value present).
*/
private byte nodeType = 0;
private byte nodeType;

/**
* A map containing the child nodes of this star-tree node, keyed by their dimension id.
Expand All @@ -79,7 +87,7 @@ public InMemoryTreeNode(int dimensionId, int startDocId, int endDocId, byte node
/**
* A map containing the child star node of this star-tree node.
*/
private InMemoryTreeNode childStarNode;
private final SetOnce<InMemoryTreeNode> childStarNode;

public long getDimensionValue() {
return dimensionValue;
Expand All @@ -90,7 +98,7 @@ public byte getNodeType() {
}

public boolean hasChild() {
return !(this.children.isEmpty() && this.childStarNode == null);
return !(this.children.isEmpty() && this.childStarNode.get() == null);
}

public int getDimensionId() {
Expand All @@ -111,8 +119,10 @@ public void setNodeType(byte nodeType) {

public void addChildNode(InMemoryTreeNode childNode, Long dimensionValue) {
if (childNode.getNodeType() == StarTreeNodeType.STAR.getValue()) {
this.childStarNode = childNode;
assert this.childStarNode.get() == null;
this.childStarNode.set(childNode);
} else {
assert assertStarTreeChildOrder(childNode);
this.children.put(dimensionValue, childNode);
}
}
Expand All @@ -122,7 +132,7 @@ public Map<Long, InMemoryTreeNode> getChildren() {
}

public InMemoryTreeNode getChildStarNode() {
return childStarNode;
return childStarNode.get();
}

public int getChildDimensionId() {
Expand All @@ -140,4 +150,16 @@ public int getAggregatedDocId() {
public void setAggregatedDocId(int aggregatedDocId) {
this.aggregatedDocId = aggregatedDocId;
}

private boolean assertStarTreeChildOrder(InMemoryTreeNode childNode) {
if (childNode.nodeType != StarTreeNodeType.NULL.getValue() && !this.children.isEmpty()) {
InMemoryTreeNode lastNode = null;
for (Map.Entry<Long, InMemoryTreeNode> entry : this.children.entrySet()) {
lastNode = entry.getValue();
}
assert lastNode.dimensionValue <= childNode.dimensionValue;
}
return true;
}

}

0 comments on commit 4aec8d2

Please sign in to comment.