Skip to content

Commit

Permalink
star tree file formats meta and data writer
Browse files Browse the repository at this point in the history
Signed-off-by: Sarthak Aggarwal <[email protected]>
  • Loading branch information
sarthakaggarwal97 committed Aug 19, 2024
1 parent 9661e8d commit 613f583
Show file tree
Hide file tree
Showing 22 changed files with 1,897 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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;

/**
* This class contains constants used in the Composite Index implementation.
*/
public class CompositeIndexConstants {

Check warning on line 14 in server/src/main/java/org/opensearch/index/compositeindex/CompositeIndexConstants.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/index/compositeindex/CompositeIndexConstants.java#L14

Added line #L14 was not covered by tests

/**
* The magic marker value used for sanity checks in the Composite Index implementation.
*/
public static final long COMPOSITE_FIELD_MARKER = 0xC0950513F1E1DL; // Composite Field

/**
* Represents the key to fetch number of documents in a segment.
*/
public static final String SEGMENT_DOCS_COUNT = "segmentDocsCount";

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

import org.opensearch.index.mapper.CompositeMappedFieldType;

/**
* This class represents the metadata of a Composite Index, which includes information about
* the composite field name, type, and the specific metadata for the type of composite field
* (e.g., Tree metadata).
*
* @opensearch.experimental
*/
public class CompositeIndexMetadata {

private final String compositeFieldName;
private final CompositeMappedFieldType.CompositeFieldType compositeFieldType;

/**
* Constructs a CompositeIndexMetadata object with the provided composite field name and type.
*
* @param compositeFieldName the name of the composite field
* @param compositeFieldType the type of the composite field
*/
public CompositeIndexMetadata(String compositeFieldName, CompositeMappedFieldType.CompositeFieldType compositeFieldType) {
this.compositeFieldName = compositeFieldName;
this.compositeFieldType = compositeFieldType;
}

/**
* Returns the name of the composite field.
*
* @return the composite field name
*/
public String getCompositeFieldName() {
return compositeFieldName;
}

/**
* Returns the type of the composite field.
*
* @return the composite field type
*/
public CompositeMappedFieldType.CompositeFieldType getCompositeFieldType() {
return compositeFieldType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,28 @@
*/
@ExperimentalApi
public enum MetricStat {
COUNT("count"),
AVG("avg"),
SUM("sum"),
MIN("min"),
MAX("max");
COUNT("count", 0),
AVG("avg", 1),
SUM("sum", 2),
MIN("min", 3),
MAX("max", 4);

private final String typeName;
private final int metricOrdinal;

MetricStat(String typeName) {
MetricStat(String typeName, int metricOrdinal) {
this.typeName = typeName;
this.metricOrdinal = metricOrdinal;
}

public String getTypeName() {
return typeName;
}

public int getMetricOrdinal() {
return metricOrdinal;
}

public static MetricStat fromTypeName(String typeName) {
for (MetricStat metric : MetricStat.values()) {
if (metric.getTypeName().equalsIgnoreCase(typeName)) {
Expand All @@ -41,4 +47,13 @@ public static MetricStat fromTypeName(String typeName) {
}
throw new IllegalArgumentException("Invalid metric stat: " + typeName);
}

public static MetricStat fromMetricOrdinal(int metricOrdinal) {
for (MetricStat metric : MetricStat.values()) {
if (metric.getMetricOrdinal() == metricOrdinal) {
return metric;
}
}
throw new IllegalArgumentException("Invalid metric stat: " + metricOrdinal);

Check warning on line 57 in server/src/main/java/org/opensearch/index/compositeindex/datacube/MetricStat.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/index/compositeindex/datacube/MetricStat.java#L57

Added line #L57 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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;
import java.util.Objects;

/**
* Composite index merge dimension class
*
* @opensearch.experimental
*/
public class ReadDimension implements Dimension {
public static final String READ = "read";
private final String field;

public ReadDimension(String field) {
this.field = field;
}

Check warning on line 28 in server/src/main/java/org/opensearch/index/compositeindex/datacube/ReadDimension.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/index/compositeindex/datacube/ReadDimension.java#L26-L28

Added lines #L26 - L28 were not covered by tests

public String getField() {
return field;

Check warning on line 31 in server/src/main/java/org/opensearch/index/compositeindex/datacube/ReadDimension.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L31 was not covered by tests
}

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

Check warning on line 40 in server/src/main/java/org/opensearch/index/compositeindex/datacube/ReadDimension.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/index/compositeindex/datacube/ReadDimension.java#L36-L40

Added lines #L36 - L40 were not covered by tests
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ReadDimension dimension = (ReadDimension) o;
return Objects.equals(field, dimension.getField());

Check warning on line 48 in server/src/main/java/org/opensearch/index/compositeindex/datacube/ReadDimension.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/index/compositeindex/datacube/ReadDimension.java#L47-L48

Added lines #L47 - L48 were not covered by tests
}

@Override
public int hashCode() {
return Objects.hash(field);

Check warning on line 53 in server/src/main/java/org/opensearch/index/compositeindex/datacube/ReadDimension.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L53 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,25 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
@ExperimentalApi
public enum StarTreeBuildMode {
// TODO : remove onheap support unless this proves useful
ON_HEAP("onheap"),
OFF_HEAP("offheap");
ON_HEAP("onheap", (byte) 0),
OFF_HEAP("offheap", (byte) 1);

private final String typeName;
private final byte buildModeOrdinal;

StarTreeBuildMode(String typeName) {
StarTreeBuildMode(String typeName, byte buildModeOrdinal) {
this.typeName = typeName;
this.buildModeOrdinal = buildModeOrdinal;
}

public String getTypeName() {
return typeName;
}

public byte getBuildModeOrdinal() {
return buildModeOrdinal;
}

public static StarTreeBuildMode fromTypeName(String typeName) {
for (StarTreeBuildMode starTreeBuildMode : StarTreeBuildMode.values()) {
if (starTreeBuildMode.getTypeName().equalsIgnoreCase(typeName)) {
Expand All @@ -77,6 +83,16 @@ public static StarTreeBuildMode fromTypeName(String typeName) {
}
throw new IllegalArgumentException(String.format(Locale.ROOT, "Invalid star tree build mode: [%s] ", typeName));
}

public static StarTreeBuildMode fromBuildModeOrdinal(byte buildModeOrdinal) {
for (StarTreeBuildMode starTreeBuildMode : StarTreeBuildMode.values()) {
if (starTreeBuildMode.getBuildModeOrdinal() == buildModeOrdinal) {
return starTreeBuildMode;
}
}
throw new IllegalArgumentException(String.format(Locale.ROOT, "Invalid star tree build mode: [%s] ", buildModeOrdinal));

Check warning on line 93 in server/src/main/java/org/opensearch/index/compositeindex/datacube/startree/StarTreeFieldConfiguration.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/index/compositeindex/datacube/startree/StarTreeFieldConfiguration.java#L93

Added line #L93 was not covered by tests
}

}

public int maxLeafDocs() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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.fileformats;

import org.apache.lucene.store.IndexOutput;
import org.opensearch.index.compositeindex.datacube.startree.StarTreeField;
import org.opensearch.index.compositeindex.datacube.startree.aggregators.MetricAggregatorInfo;
import org.opensearch.index.compositeindex.datacube.startree.fileformats.data.StarTreeDataWriter;
import org.opensearch.index.compositeindex.datacube.startree.fileformats.meta.StarTreeMetaWriter;
import org.opensearch.index.compositeindex.datacube.startree.node.InMemoryTreeNode;

import java.io.IOException;
import java.util.List;

/**
* Util class for building star tree
*
* @opensearch.experimental
*/
public class StarTreeWriter {

/** Initial version for the star tree writer */
public static final int VERSION_START = 0;

/** Current version for the star tree writer */
public static final int VERSION_CURRENT = VERSION_START;

private StarTreeWriter() {}

/**
* Write star tree to index output stream
*
* @param dataOut data index output
* @param rootNode root star-tree node
* @param numNodes number of nodes in the tree
* @param name name of the star-tree field
* @return total size of the three
* @throws IOException when star-tree data serialization fails
*/
public static long writeStarTree(IndexOutput dataOut, InMemoryTreeNode rootNode, int numNodes, String name) throws IOException {
return StarTreeDataWriter.writeStarTree(dataOut, rootNode, numNodes, name);
}

/**
* Write star tree metadata to index output stream
*
* @param metaOut meta index output
* @param starTreeField star tree field
* @param metricAggregatorInfos metric aggregator infos
* @param segmentAggregatedCount segment aggregated count
* @param dataFilePointer data file pointer
* @param dataFileLength data file length
* @throws IOException when star-tree data serialization fails
*/
public static void writeStarTreeMetadata(
IndexOutput metaOut,
StarTreeField starTreeField,
List<MetricAggregatorInfo> metricAggregatorInfos,
Integer segmentAggregatedCount,
long dataFilePointer,
long dataFileLength
) throws IOException {
StarTreeMetaWriter.writeStarTreeMetadata(
metaOut,
starTreeField,
metricAggregatorInfos,
segmentAggregatedCount,
dataFilePointer,
dataFileLength
);
}

}
Loading

0 comments on commit 613f583

Please sign in to comment.