Skip to content

Commit f3fb106

Browse files
author
Shailesh Singh
committed
Add Flush Flow and Metric Tests
1 parent 7011249 commit f3fb106

27 files changed

+840
-202
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.index.compositeindex.datacube;
10+
11+
import org.opensearch.common.annotation.ExperimentalApi;
12+
13+
/**
14+
* Represents the type of comparison to be performed on a dimension.
15+
*
16+
* @opensearch.experimental
17+
*/
18+
@ExperimentalApi
19+
public enum ComparisonType {
20+
SIGNED,
21+
UNSIGNED
22+
}

server/src/main/java/org/opensearch/index/compositeindex/datacube/Dimension.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.opensearch.common.annotation.ExperimentalApi;
1313
import org.opensearch.core.xcontent.ToXContent;
1414

15+
import java.util.Comparator;
1516
import java.util.List;
1617
import java.util.function.Consumer;
1718

@@ -45,4 +46,26 @@ public interface Dimension extends ToXContent {
4546
List<String> getSubDimensionNames();
4647

4748
DocValuesType getDocValuesType();
49+
50+
default ComparisonType getComparisonType() {
51+
return ComparisonType.SIGNED;
52+
}
53+
54+
default Comparator<Long> comparator() {
55+
return (a, b) -> {
56+
if (a == null && b == null) {
57+
return 0;
58+
}
59+
if (b == null) {
60+
return -1;
61+
}
62+
if (a == null) {
63+
return 1;
64+
}
65+
return getComparisonType() == ComparisonType.UNSIGNED
66+
? Long.compareUnsigned(a, b)
67+
: Long.compare(a, b);
68+
};
69+
}
70+
4871
}

server/src/main/java/org/opensearch/index/compositeindex/datacube/DimensionFactory.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.stream.Collectors;
2525

2626
import static org.opensearch.index.compositeindex.datacube.DateDimension.CALENDAR_INTERVALS;
27-
import static org.opensearch.index.compositeindex.datacube.KeywordDimension.KEYWORD;
2827

2928
/**
3029
* Dimension factory class mainly used to parse and create dimension from the mappings
@@ -36,16 +35,17 @@ public class DimensionFactory {
3635
public static Dimension parseAndCreateDimension(
3736
String name,
3837
String type,
39-
Boolean isUnsignedLong,
4038
Map<String, Object> dimensionMap,
4139
Mapper.TypeParser.ParserContext c
4240
) {
4341
switch (type) {
4442
case DateDimension.DATE:
4543
return parseAndCreateDateDimension(name, dimensionMap, c);
4644
case NumericDimension.NUMERIC:
47-
return new NumericDimension(name, isUnsignedLong);
48-
case KEYWORD:
45+
return new NumericDimension(name);
46+
case UnsignedLongDimension.UNSIGNED_LONG:
47+
return new UnsignedLongDimension(name);
48+
case KeywordDimension.KEYWORD:
4949
return new KeywordDimension(name);
5050
default:
5151
throw new IllegalArgumentException(
@@ -54,15 +54,6 @@ public static Dimension parseAndCreateDimension(
5454
}
5555
}
5656

57-
public static Dimension parseAndCreateDimension(
58-
String name,
59-
String type,
60-
Map<String, Object> dimensionMap,
61-
Mapper.TypeParser.ParserContext c
62-
) {
63-
return parseAndCreateDimension(name, type, false, dimensionMap, c);
64-
}
65-
6657
public static Dimension parseAndCreateDimension(
6758
String name,
6859
Mapper.Builder builder,
@@ -78,7 +69,9 @@ public static Dimension parseAndCreateDimension(
7869
case DATE:
7970
return parseAndCreateDateDimension(name, dimensionMap, c);
8071
case NUMERIC:
81-
return new NumericDimension(name, builder.isUnsignedLong());
72+
return new NumericDimension(name);
73+
case UNSIGNED_LONG:
74+
return new UnsignedLongDimension(name);
8275
case KEYWORD:
8376
return new KeywordDimension(name);
8477
default:

server/src/main/java/org/opensearch/index/compositeindex/datacube/DimensionType.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ public enum DimensionType {
2323
*/
2424
NUMERIC,
2525

26+
/**
27+
* Represents an unsigned long dimension type.
28+
* This is used for dimensions that contain numerical values of type unsigned long.
29+
*/
30+
UNSIGNED_LONG,
31+
2632
/**
2733
* Represents a date dimension type.
2834
* This is used for dimensions that contain date or timestamp values.

server/src/main/java/org/opensearch/index/compositeindex/datacube/NumericDimension.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,11 @@
2626
@ExperimentalApi
2727
public class NumericDimension implements Dimension {
2828
public static final String NUMERIC = "numeric";
29-
public static final String IS_UNSIGNED_LONG_FIELD = "isUnsignedLong";
3029

31-
private final String field;
32-
private final Boolean isUnsignedLong;
30+
protected final String field;
3331

3432
public NumericDimension(String field) {
3533
this.field = field;
36-
isUnsignedLong = false;
37-
}
38-
39-
public NumericDimension(String field, Boolean isUnsignedLong) {
40-
this.field = field;
41-
this.isUnsignedLong = isUnsignedLong;
4234
}
4335

4436
public String getField() {
@@ -70,7 +62,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
7062
builder.startObject();
7163
builder.field(CompositeDataCubeFieldType.NAME, field);
7264
builder.field(CompositeDataCubeFieldType.TYPE, NUMERIC);
73-
builder.field(IS_UNSIGNED_LONG_FIELD, isUnsignedLong);
7465
builder.endObject();
7566
return builder;
7667
}
@@ -88,7 +79,4 @@ public int hashCode() {
8879
return Objects.hash(field);
8980
}
9081

91-
public Boolean isUnsignedLong() {
92-
return isUnsignedLong;
93-
}
9482
}

server/src/main/java/org/opensearch/index/compositeindex/datacube/ReadDimension.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,24 @@ public class ReadDimension implements Dimension {
2626
public static final String READ = "read";
2727
private final String field;
2828
private final DocValuesType docValuesType;
29+
private final ComparisonType comparisonType;
2930

3031
public ReadDimension(String field) {
3132
this.field = field;
3233
this.docValuesType = DocValuesType.SORTED_NUMERIC;
34+
this.comparisonType = ComparisonType.SIGNED;
3335
}
3436

3537
public ReadDimension(String field, DocValuesType docValuesType) {
3638
this.field = field;
3739
this.docValuesType = docValuesType;
40+
this.comparisonType = ComparisonType.SIGNED;
41+
}
42+
43+
public ReadDimension(String field, ComparisonType comparisonType){
44+
this.field = field;
45+
this.docValuesType = DocValuesType.SORTED_NUMERIC;
46+
this.comparisonType = comparisonType;
3847
}
3948

4049
public String getField() {
@@ -82,4 +91,10 @@ public boolean equals(Object o) {
8291
public int hashCode() {
8392
return Objects.hash(field);
8493
}
94+
95+
@Override
96+
public ComparisonType getComparisonType() {
97+
return comparisonType;
98+
}
99+
85100
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.index.compositeindex.datacube;
10+
11+
import org.opensearch.core.xcontent.XContentBuilder;
12+
import org.opensearch.index.mapper.CompositeDataCubeFieldType;
13+
14+
import java.io.IOException;
15+
import java.util.Comparator;
16+
17+
public class UnsignedLongDimension extends NumericDimension {
18+
public static final String UNSIGNED_LONG = "unsigned_long";
19+
20+
public UnsignedLongDimension(String field) {
21+
super(field);
22+
}
23+
24+
@Override
25+
public ComparisonType getComparisonType() {
26+
return ComparisonType.UNSIGNED;
27+
}
28+
29+
@Override
30+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
31+
builder.startObject();
32+
builder.field(CompositeDataCubeFieldType.NAME, field);
33+
builder.field(CompositeDataCubeFieldType.TYPE, UNSIGNED_LONG);
34+
builder.endObject();
35+
return builder;
36+
}
37+
38+
}

server/src/main/java/org/opensearch/index/compositeindex/datacube/startree/builder/OnHeapStarTreeBuilder.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,16 @@
1313
import org.apache.lucene.store.IndexOutput;
1414
import org.apache.lucene.util.LongValues;
1515
import org.opensearch.common.annotation.ExperimentalApi;
16+
import org.opensearch.index.compositeindex.datacube.Dimension;
17+
import org.opensearch.index.compositeindex.datacube.UnsignedLongDimension;
1618
import org.opensearch.index.compositeindex.datacube.startree.StarTreeDocument;
1719
import org.opensearch.index.compositeindex.datacube.startree.StarTreeField;
1820
import org.opensearch.index.compositeindex.datacube.startree.index.StarTreeValues;
1921
import org.opensearch.index.compositeindex.datacube.startree.utils.SequentialDocValuesIterator;
2022
import org.opensearch.index.mapper.MapperService;
2123

2224
import java.io.IOException;
23-
import java.util.ArrayList;
24-
import java.util.Arrays;
25-
import java.util.Iterator;
26-
import java.util.LinkedHashMap;
27-
import java.util.List;
28-
import java.util.Map;
29-
import java.util.Objects;
25+
import java.util.*;
3026
import java.util.concurrent.atomic.AtomicInteger;
3127

3228
/**
@@ -271,18 +267,16 @@ public StarTreeDocument next() {
271267
*/
272268
private void sortStarTreeDocumentsFromDimensionId(StarTreeDocument[] starTreeDocuments, int dimensionId) {
273269
Arrays.sort(starTreeDocuments, (o1, o2) -> {
270+
List<Dimension> dimensionsOrder = starTreeField.getDimensionsOrder();
274271
for (int i = dimensionId; i < numDimensions; i++) {
275272
if (!Objects.equals(o1.dimensions[i], o2.dimensions[i])) {
276-
if (o1.dimensions[i] == null && o2.dimensions[i] == null) {
277-
return 0;
273+
Dimension dimension = dimensionsOrder.get(i);
274+
if (dimension instanceof UnsignedLongDimension) {
275+
UnsignedLongDimension unsignedLongDimension = (UnsignedLongDimension) dimension;
276+
return unsignedLongDimension.comparator().compare(o1.dimensions[i], o2.dimensions[i]);
278277
}
279-
if (o1.dimensions[i] == null) {
280-
return 1;
281-
}
282-
if (o2.dimensions[i] == null) {
283-
return -1;
284-
}
285-
return Long.compare(o1.dimensions[i], o2.dimensions[i]);
278+
Comparator<Long> comparator = dimension.comparator();
279+
return comparator.compare(o1.dimensions[i], o2.dimensions[i]);
286280
}
287281
}
288282
return 0;

server/src/main/java/org/opensearch/index/compositeindex/datacube/startree/fileformats/meta/StarTreeMetadata.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.apache.lucene.store.IndexInput;
1515
import org.opensearch.common.annotation.ExperimentalApi;
1616
import org.opensearch.index.compositeindex.CompositeIndexMetadata;
17+
import org.opensearch.index.compositeindex.datacube.Dimension;
1718
import org.opensearch.index.compositeindex.datacube.Metric;
1819
import org.opensearch.index.compositeindex.datacube.MetricStat;
1920
import org.opensearch.index.compositeindex.datacube.startree.StarTreeFieldConfiguration;

server/src/main/java/org/opensearch/index/compositeindex/datacube/startree/fileformats/node/FixedLengthStarTreeNode.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
package org.opensearch.index.compositeindex.datacube.startree.fileformats.node;
99

1010
import org.apache.lucene.store.RandomAccessInput;
11+
import org.opensearch.index.compositeindex.datacube.Dimension;
1112
import org.opensearch.index.compositeindex.datacube.startree.node.StarTreeNode;
1213
import org.opensearch.index.compositeindex.datacube.startree.node.StarTreeNodeType;
13-
1414
import java.io.IOException;
1515
import java.io.UncheckedIOException;
16+
import java.util.Comparator;
1617
import java.util.Iterator;
1718

1819
/**
@@ -192,15 +193,15 @@ public StarTreeNode getChildStarNode() throws IOException {
192193
}
193194

194195
@Override
195-
public StarTreeNode getChildForDimensionValue(Long dimensionValue) throws IOException {
196+
public StarTreeNode getChildForDimensionValue(Long dimensionValue, Dimension dimension) throws IOException {
196197
// there will be no children for leaf nodes
197198
if (isLeaf()) {
198199
return null;
199200
}
200201

201202
StarTreeNode resultStarTreeNode = null;
202203
if (null != dimensionValue) {
203-
resultStarTreeNode = binarySearchChild(dimensionValue);
204+
resultStarTreeNode = binarySearchChild(dimensionValue, dimension);
204205
}
205206
return resultStarTreeNode;
206207
}
@@ -240,7 +241,7 @@ private static FixedLengthStarTreeNode matchStarTreeNodeTypeOrNull(FixedLengthSt
240241
* @return The child node if found, null otherwise
241242
* @throws IOException If there's an error reading from the input
242243
*/
243-
private FixedLengthStarTreeNode binarySearchChild(long dimensionValue) throws IOException {
244+
private FixedLengthStarTreeNode binarySearchChild(long dimensionValue, Dimension dimension) throws IOException {
244245

245246
int low = firstChildId;
246247

@@ -255,14 +256,15 @@ private FixedLengthStarTreeNode binarySearchChild(long dimensionValue) throws IO
255256
high--;
256257
}
257258

259+
Comparator<Long> comparator = dimension.comparator();
258260
while (low <= high) {
259261
int mid = low + (high - low) / 2;
260262
FixedLengthStarTreeNode midNode = new FixedLengthStarTreeNode(in, mid);
261263
long midDimensionValue = midNode.getDimensionValue();
262-
263-
if (midDimensionValue == dimensionValue) {
264+
int compare = comparator.compare(midDimensionValue, dimensionValue);
265+
if (compare == 0) {
264266
return midNode;
265-
} else if (midDimensionValue < dimensionValue) {
267+
} else if (compare < 0) {
266268
low = mid + 1;
267269
} else {
268270
high = mid - 1;

0 commit comments

Comments
 (0)