Skip to content

Commit

Permalink
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 11, 2024
1 parent feab228 commit 3fc2c79
Show file tree
Hide file tree
Showing 14 changed files with 334 additions and 145 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public class MetricTypeFieldPair implements Comparable<MetricTypeFieldPair> {
public static final String STAR = "*";
public static final MetricTypeFieldPair COUNT_STAR = new MetricTypeFieldPair(MetricType.COUNT, STAR);

private final MetricType functionType;
private final MetricType metricType;
private final String field;

/**
* Constructor for MetricTypeFieldPair
*/
public MetricTypeFieldPair(MetricType functionType, String field) {
this.functionType = functionType;
if (functionType == MetricType.COUNT) {
public MetricTypeFieldPair(MetricType metricType, String field) {
this.metricType = metricType;
if (metricType == MetricType.COUNT) {
this.field = STAR;
} else {
this.field = field;
Expand All @@ -39,8 +39,8 @@ public MetricTypeFieldPair(MetricType functionType, String field) {
/**
* @return Metric Type
*/
public MetricType getFunctionType() {
return functionType;
public MetricType getMetricType() {
return metricType;
}

/**
Expand All @@ -51,42 +51,42 @@ public String getField() {
}

/**
* @return field name with function type and field
* @return field name with metric type and field
*/
public String toFieldName() {
return toFieldName(functionType, field);
return toFieldName(metricType, field);
}

/**
* Builds field name with function type and field
* Builds field name with metric type and field
*/
public static String toFieldName(MetricType functionType, String field) {
return functionType.getTypeName() + DELIMITER + field;
public static String toFieldName(MetricType metricType, String field) {
return metricType.getTypeName() + DELIMITER + field;
}

/**
* Builds MetricTypeFieldPair from field name
*/
public static MetricTypeFieldPair fromFieldName(String fieldName) {
String[] parts = fieldName.split(DELIMITER, 2);
return fromFunctionAndFieldName(parts[0], parts[1]);
return fromMetricAndFieldName(parts[0], parts[1]);
}

/**
* Builds MetricTypeFieldPair from function and field name
* Builds MetricTypeFieldPair from metric and field name
*/
private static MetricTypeFieldPair fromFunctionAndFieldName(String functionName, String fieldName) {
MetricType functionType = MetricType.fromTypeName(functionName);
if (functionType == MetricType.COUNT) {
private static MetricTypeFieldPair fromMetricAndFieldName(String metricName, String fieldName) {
MetricType metricType = MetricType.fromTypeName(metricName);
if (metricType == MetricType.COUNT) {
return COUNT_STAR;
} else {
return new MetricTypeFieldPair(functionType, fieldName);
return new MetricTypeFieldPair(metricType, fieldName);
}
}

@Override
public int hashCode() {
return 31 * functionType.hashCode() + field.hashCode();
return 31 * metricType.hashCode() + field.hashCode();
}

@Override
Expand All @@ -96,7 +96,7 @@ public boolean equals(Object obj) {
}
if (obj instanceof MetricTypeFieldPair) {
MetricTypeFieldPair anotherPair = (MetricTypeFieldPair) obj;
return functionType == anotherPair.functionType && field.equals(anotherPair.field);
return metricType == anotherPair.metricType && field.equals(anotherPair.field);
}
return false;
}
Expand All @@ -109,7 +109,7 @@ public String toString() {
@Override
public int compareTo(MetricTypeFieldPair other) {
return Comparator.comparing((MetricTypeFieldPair o) -> o.field)
.thenComparing((MetricTypeFieldPair o) -> o.functionType)
.thenComparing((MetricTypeFieldPair o) -> o.metricType)
.compare(this, other);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package org.opensearch.index.compositeindex.startree.aggregators;

import org.opensearch.index.compositeindex.MetricType;
import org.opensearch.index.compositeindex.startree.data.DataType;

/**
* Sum value aggregator for star tree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package org.opensearch.index.compositeindex.startree.aggregators;

import org.opensearch.index.compositeindex.MetricType;
import org.opensearch.index.compositeindex.startree.data.DataType;

/**
* A value aggregator that pre-aggregates on the input values for a specific type of aggregation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package org.opensearch.index.compositeindex.startree.aggregators;

import org.opensearch.index.compositeindex.MetricType;
import org.opensearch.index.compositeindex.startree.data.DataType;

/**
* Value aggregator factory for a given aggregation type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

package org.opensearch.index.compositeindex.startree.builder;

import org.opensearch.index.compositeindex.startree.data.StarTreeDocValues;

import java.io.Closeable;
import java.io.IOException;
import java.util.List;
Expand All @@ -16,7 +18,7 @@
* A star-tree builder that builds a single star-tree.
* @opensearch.experimental
*/
public interface CompositeFieldWriter extends Closeable {
public interface SingleTreeBuilder extends Closeable {

/**
* Builds the data structure for the given composite index config.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.index.compositeindex.startree.aggregators;
package org.opensearch.index.compositeindex.startree.data;

/**
* Data type of doc values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.index.compositeindex.startree.builder;
package org.opensearch.index.compositeindex.startree.data;

import org.apache.lucene.index.SortedNumericDocValues;
import org.opensearch.index.compositeindex.startree.node.StarTree;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.startree.data;

import java.util.Arrays;

/**
* Star tree document
*/
public class StarTreeDocument {
public final long[] dimensions;
public final Object[] metrics;

public StarTreeDocument(long[] dimensions, Object[] metrics) {
this.dimensions = dimensions;
this.metrics = metrics;
}

@Override
public String toString() {
return Arrays.toString(dimensions) + " | " + Arrays.toString(metrics);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

/**
* Class representing each node in star tree
* The interface is implemented by build-mode based nodes
* @opensearch.experimental
*/
public interface StarTreeNode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ public class StarTreeBuilderUtils {

private StarTreeBuilderUtils() {}

public static final int INVALID_ID = -1;
public static final int ALL = -1;
public static final long MAGIC_MARKER = 0xBADDA55B00DAD00DL;

/** Tree node representation */
public static class TreeNode {
public int dimensionId = INVALID_ID;
public long dimensionValue = INVALID_ID;
public int startDocId = INVALID_ID;
public int endDocId = INVALID_ID;
public int aggregatedDocId = INVALID_ID;
public int childDimensionId = INVALID_ID;
public int dimensionId = ALL;
public long dimensionValue = ALL;
public int startDocId = ALL;
public int endDocId = ALL;
public int aggregatedDocId = ALL;
public int childDimensionId = ALL;
public Map<Long, TreeNode> children;
}

Expand Down Expand Up @@ -100,7 +100,7 @@ static void writeNodes(IndexOutput output, TreeNode rootNode) throws IOException
TreeNode node = queue.remove();

if (node.children == null) {
writeNode(output, node, INVALID_ID, INVALID_ID);
writeNode(output, node, ALL, ALL);
} else {
// Sort all children nodes based on dimension value
List<TreeNode> sortedChildren = new ArrayList<>(node.children.values());
Expand Down
Loading

0 comments on commit 3fc2c79

Please sign in to comment.