Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* 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.cluster.metadata;

import org.opensearch.common.annotation.ExperimentalApi;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.common.io.stream.Writeable;
import org.opensearch.core.xcontent.ToXContentFragment;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;

import java.io.IOException;

/**
* Represents the hash range assigned to a shard.
*
* @opensearch.experimental
*/
@ExperimentalApi
public record ShardRange(int shardId, int start, int end) implements Comparable<ShardRange>, ToXContentFragment, Writeable {

/**
* Constructs a new shard range from a stream.
* @param in the stream to read from
* @throws IOException if an error occurs while reading from the stream
* @see #writeTo(StreamOutput)
*/
public ShardRange(StreamInput in) throws IOException {
this(in.readVInt(), in.readInt(), in.readInt());
}

public boolean contains(int hash) {
return hash >= start && hash <= end;
}

@Override
public int compareTo(ShardRange o) {
return Integer.compare(start, o.start);
}

@Override
public String toString() {
return "ShardRange{" + "shardId=" + shardId + ", start=" + start + ", end=" + end + '}';
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(shardId);
out.writeInt(start);
out.writeInt(end);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject().field("shard_id", shardId).field("start", start).field("end", end);
builder.endObject();
return builder;
}

public static ShardRange parse(XContentParser parser) throws IOException {
int shardId = -1, start = -1, end = -1;
XContentParser.Token token;
String fieldName = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
fieldName = parser.currentName();
} else if (token == XContentParser.Token.VALUE_NUMBER) {
if ("shard_id".equals(fieldName)) {
shardId = parser.intValue();
} else if ("start".equals(fieldName)) {
start = parser.intValue();
} else if ("end".equals(fieldName)) {
end = parser.intValue();
}
}
}

return new ShardRange(shardId, start, end);
}
}
Loading
Loading