Skip to content

Commit

Permalink
Add Flush Flow and Metric Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Shailesh Singh committed Nov 27, 2024
1 parent 7011249 commit 1716e7f
Show file tree
Hide file tree
Showing 26 changed files with 888 additions and 138 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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;

import org.opensearch.common.annotation.ExperimentalApi;

/**
* Represents the type of comparison to be performed on a dimension.
*
* @opensearch.experimental
*/
@ExperimentalApi
public enum ComparatorType {
LONG {
@Override
int compare(Long a, Long b) {
if (a == null && b == null) {
return 0;
}
if (b == null) {
return -1;
}
if (a == null) {
return 1;
}
return Long.compare(a, b);
}
},
UNSIGNED_LONG {
@Override
int compare(Long a, Long b) {
if (a == null && b == null) {
return 0;
}
if (b == null) {
return -1;
}
if (a == null) {
return 1;
}
return Long.compareUnsigned(a, b);
}
};

abstract int compare(Long a, Long b);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.opensearch.common.annotation.ExperimentalApi;
import org.opensearch.core.xcontent.ToXContent;

import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;

Expand All @@ -34,8 +35,8 @@ public interface Dimension extends ToXContent {
/**
* Sets the dimension values with the consumer
*
* @param value The value to be set
* @param dimSetter Consumer which sets the dimensions
* @param value The value to be set
* @param dimSetter Consumer which sets the dimensions
*/
void setDimensionValues(final Long value, final Consumer<Long> dimSetter);

Expand All @@ -45,4 +46,19 @@ public interface Dimension extends ToXContent {
List<String> getSubDimensionNames();

DocValuesType getDocValuesType();

/**
* Returns the comparator type used for comparing dimension values. <br>
* This determines how numeric values are compared: <br>
* - ComparatorType.UNSIGNED_LONG for unsigned long values <br>
* - ComparatorType.LONG for all other numeric types (DEFAULT)
*/
default ComparatorType getComparatorType() {
return ComparatorType.LONG;
}

default Comparator<Long> comparator() {
return (a, b) -> getComparatorType().compare(a, b);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.stream.Collectors;

import static org.opensearch.index.compositeindex.datacube.DateDimension.CALENDAR_INTERVALS;
import static org.opensearch.index.compositeindex.datacube.KeywordDimension.KEYWORD;

/**
* Dimension factory class mainly used to parse and create dimension from the mappings
Expand All @@ -36,16 +35,17 @@ public class DimensionFactory {
public static Dimension parseAndCreateDimension(
String name,
String type,
Boolean isUnsignedLong,
Map<String, Object> dimensionMap,
Mapper.TypeParser.ParserContext c
) {
switch (type) {
case DateDimension.DATE:
return parseAndCreateDateDimension(name, dimensionMap, c);
case NumericDimension.NUMERIC:
return new NumericDimension(name, isUnsignedLong);
case KEYWORD:
return new NumericDimension(name);
case UnsignedLongDimension.UNSIGNED_LONG:
return new UnsignedLongDimension(name);
case KeywordDimension.KEYWORD:
return new KeywordDimension(name);
default:
throw new IllegalArgumentException(
Expand All @@ -54,15 +54,6 @@ public static Dimension parseAndCreateDimension(
}
}

public static Dimension parseAndCreateDimension(
String name,
String type,
Map<String, Object> dimensionMap,
Mapper.TypeParser.ParserContext c
) {
return parseAndCreateDimension(name, type, false, dimensionMap, c);
}

public static Dimension parseAndCreateDimension(
String name,
Mapper.Builder builder,
Expand All @@ -78,7 +69,9 @@ public static Dimension parseAndCreateDimension(
case DATE:
return parseAndCreateDateDimension(name, dimensionMap, c);
case NUMERIC:
return new NumericDimension(name, builder.isUnsignedLong());
return new NumericDimension(name);
case UNSIGNED_LONG:
return new UnsignedLongDimension(name);
case KEYWORD:
return new KeywordDimension(name);
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public enum DimensionType {
*/
NUMERIC,

/**
* Represents an unsigned long dimension type.
* This is used for dimensions that contain numerical values of type unsigned long.
*/
UNSIGNED_LONG,

/**
* Represents a date dimension type.
* This is used for dimensions that contain date or timestamp values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,11 @@
@ExperimentalApi
public class NumericDimension implements Dimension {
public static final String NUMERIC = "numeric";
public static final String IS_UNSIGNED_LONG_FIELD = "isUnsignedLong";

private final String field;
private final Boolean isUnsignedLong;
protected final String field;

public NumericDimension(String field) {
this.field = field;
isUnsignedLong = false;
}

public NumericDimension(String field, Boolean isUnsignedLong) {
this.field = field;
this.isUnsignedLong = isUnsignedLong;
}

public String getField() {
Expand Down Expand Up @@ -70,7 +62,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.startObject();
builder.field(CompositeDataCubeFieldType.NAME, field);
builder.field(CompositeDataCubeFieldType.TYPE, NUMERIC);
builder.field(IS_UNSIGNED_LONG_FIELD, isUnsignedLong);
builder.endObject();
return builder;
}
Expand All @@ -88,7 +79,4 @@ public int hashCode() {
return Objects.hash(field);
}

public Boolean isUnsignedLong() {
return isUnsignedLong;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,24 @@ public class ReadDimension implements Dimension {
public static final String READ = "read";
private final String field;
private final DocValuesType docValuesType;
private final ComparatorType comparatorType;

public ReadDimension(String field) {
this.field = field;
this.docValuesType = DocValuesType.SORTED_NUMERIC;
this.comparatorType = ComparatorType.LONG;
}

public ReadDimension(String field, DocValuesType docValuesType) {
this.field = field;
this.docValuesType = docValuesType;
this.comparatorType = ComparatorType.LONG;
}

public ReadDimension(String field, DocValuesType docValuesType, ComparatorType comparatorType) {
this.field = field;
this.docValuesType = docValuesType;
this.comparatorType = comparatorType;
}

public String getField() {
Expand Down Expand Up @@ -82,4 +91,10 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(field);
}

@Override
public ComparatorType getComparatorType() {
return comparatorType;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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;

import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.index.mapper.CompositeDataCubeFieldType;

import java.io.IOException;

public class UnsignedLongDimension extends NumericDimension {

public static final String UNSIGNED_LONG = "unsigned_long";

public UnsignedLongDimension(String field) {
super(field);
}

@Override
public ComparatorType getComparatorType() {
return ComparatorType.UNSIGNED_LONG;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(CompositeDataCubeFieldType.NAME, field);
builder.field(CompositeDataCubeFieldType.TYPE, UNSIGNED_LONG);
builder.endObject();
return builder;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.util.LongValues;
import org.opensearch.common.annotation.ExperimentalApi;
import org.opensearch.index.compositeindex.datacube.Dimension;
import org.opensearch.index.compositeindex.datacube.startree.StarTreeDocument;
import org.opensearch.index.compositeindex.datacube.startree.StarTreeField;
import org.opensearch.index.compositeindex.datacube.startree.index.StarTreeValues;
Expand All @@ -22,6 +23,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -271,18 +273,12 @@ public StarTreeDocument next() {
*/
private void sortStarTreeDocumentsFromDimensionId(StarTreeDocument[] starTreeDocuments, int dimensionId) {
Arrays.sort(starTreeDocuments, (o1, o2) -> {
List<Dimension> dimensionsOrder = starTreeField.getDimensionsOrder();
for (int i = dimensionId; i < numDimensions; i++) {
if (!Objects.equals(o1.dimensions[i], o2.dimensions[i])) {
if (o1.dimensions[i] == null && o2.dimensions[i] == null) {
return 0;
}
if (o1.dimensions[i] == null) {
return 1;
}
if (o2.dimensions[i] == null) {
return -1;
}
return Long.compare(o1.dimensions[i], o2.dimensions[i]);
Dimension dimension = dimensionsOrder.get(i);
Comparator<Long> comparator = dimension.comparator();
return comparator.compare(o1.dimensions[i], o2.dimensions[i]);
}
}
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
package org.opensearch.index.compositeindex.datacube.startree.fileformats.node;

import org.apache.lucene.store.RandomAccessInput;
import org.opensearch.index.compositeindex.datacube.Dimension;
import org.opensearch.index.compositeindex.datacube.startree.node.StarTreeNode;
import org.opensearch.index.compositeindex.datacube.startree.node.StarTreeNodeType;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Comparator;
import java.util.Iterator;

/**
Expand Down Expand Up @@ -192,15 +194,15 @@ public StarTreeNode getChildStarNode() throws IOException {
}

@Override
public StarTreeNode getChildForDimensionValue(Long dimensionValue) throws IOException {
public StarTreeNode getChildForDimensionValue(Long dimensionValue, Dimension dimension) throws IOException {
// there will be no children for leaf nodes
if (isLeaf()) {
return null;
}

StarTreeNode resultStarTreeNode = null;
if (null != dimensionValue) {
resultStarTreeNode = binarySearchChild(dimensionValue);
resultStarTreeNode = binarySearchChild(dimensionValue, dimension);
}
return resultStarTreeNode;
}
Expand Down Expand Up @@ -240,7 +242,7 @@ private static FixedLengthStarTreeNode matchStarTreeNodeTypeOrNull(FixedLengthSt
* @return The child node if found, null otherwise
* @throws IOException If there's an error reading from the input
*/
private FixedLengthStarTreeNode binarySearchChild(long dimensionValue) throws IOException {
private FixedLengthStarTreeNode binarySearchChild(long dimensionValue, Dimension dimension) throws IOException {

int low = firstChildId;

Expand All @@ -255,14 +257,15 @@ private FixedLengthStarTreeNode binarySearchChild(long dimensionValue) throws IO
high--;
}

Comparator<Long> comparator = dimension.comparator();
while (low <= high) {
int mid = low + (high - low) / 2;
FixedLengthStarTreeNode midNode = new FixedLengthStarTreeNode(in, mid);
long midDimensionValue = midNode.getDimensionValue();

if (midDimensionValue == dimensionValue) {
int compare = comparator.compare(midDimensionValue, dimensionValue);
if (compare == 0) {
return midNode;
} else if (midDimensionValue < dimensionValue) {
} else if (compare < 0) {
low = mid + 1;
} else {
high = mid - 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.opensearch.index.compositeindex.datacube.startree.node;

import org.opensearch.common.annotation.ExperimentalApi;
import org.opensearch.index.compositeindex.datacube.Dimension;

import java.io.IOException;
import java.util.Iterator;
Expand Down Expand Up @@ -103,11 +104,11 @@ public interface StarTreeNode {
/**
* Returns the child node for the given dimension value in the star-tree.
*
* @param dimensionValue the dimension value
* @param dimensionValue the dimension value
* @return the child node for the given dimension value or null if child is not present
* @throws IOException if an I/O error occurs while retrieving the child node
*/
StarTreeNode getChildForDimensionValue(Long dimensionValue) throws IOException;
StarTreeNode getChildForDimensionValue(Long dimensionValue, Dimension dimension) throws IOException;

/**
* Returns the child star node for a node in the star-tree.
Expand Down
Loading

0 comments on commit 1716e7f

Please sign in to comment.