|
| 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.cluster.metadata; |
| 10 | + |
| 11 | +import org.opensearch.common.annotation.ExperimentalApi; |
| 12 | +import org.opensearch.core.common.io.stream.StreamInput; |
| 13 | +import org.opensearch.core.common.io.stream.StreamOutput; |
| 14 | +import org.opensearch.core.common.io.stream.Writeable; |
| 15 | +import org.opensearch.core.xcontent.ToXContentFragment; |
| 16 | +import org.opensearch.core.xcontent.XContentBuilder; |
| 17 | +import org.opensearch.core.xcontent.XContentParser; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | + |
| 21 | +/** |
| 22 | + * Represents the hash range assigned to a shard. |
| 23 | + * |
| 24 | + * @opensearch.experimental |
| 25 | + */ |
| 26 | +@ExperimentalApi |
| 27 | +public class ShardRange implements Comparable<ShardRange>, ToXContentFragment, Writeable { |
| 28 | + private final int shardId; |
| 29 | + private final int start; |
| 30 | + private final int end; |
| 31 | + |
| 32 | + public ShardRange(int shardId, int start, int end) { |
| 33 | + this.shardId = shardId; |
| 34 | + this.start = start; |
| 35 | + this.end = end; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Constructs a new shard range from a stream. |
| 40 | + * @param in the stream to read from |
| 41 | + * @throws IOException if an error occurs while reading from the stream |
| 42 | + * @see #writeTo(StreamOutput) |
| 43 | + */ |
| 44 | + public ShardRange(StreamInput in) throws IOException { |
| 45 | + shardId = in.readVInt(); |
| 46 | + start = in.readInt(); |
| 47 | + end = in.readInt(); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public boolean equals(Object o) { |
| 52 | + if (this == o) return true; |
| 53 | + if (!(o instanceof ShardRange)) return false; |
| 54 | + |
| 55 | + ShardRange that = (ShardRange) o; |
| 56 | + |
| 57 | + if (shardId != that.shardId) return false; |
| 58 | + if (start != that.start) return false; |
| 59 | + return end == that.end; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public int hashCode() { |
| 64 | + int result = shardId; |
| 65 | + result = 31 * result + start; |
| 66 | + result = 31 * result + end; |
| 67 | + return result; |
| 68 | + } |
| 69 | + |
| 70 | + public int getShardId() { |
| 71 | + return shardId; |
| 72 | + } |
| 73 | + |
| 74 | + public int getStart() { |
| 75 | + return start; |
| 76 | + } |
| 77 | + |
| 78 | + public int getEnd() { |
| 79 | + return end; |
| 80 | + } |
| 81 | + |
| 82 | + public ShardRange copy() { |
| 83 | + return new ShardRange(shardId, start, end); |
| 84 | + } |
| 85 | + |
| 86 | + public boolean contains(long hash) { |
| 87 | + return hash >= start && hash <= end; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public int compareTo(ShardRange o) { |
| 92 | + return Integer.compare(start, o.start); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public String toString() { |
| 97 | + return "ShardRange{" + "shardId=" + shardId + ", start=" + start + ", end=" + end + '}'; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void writeTo(StreamOutput out) throws IOException { |
| 102 | + out.writeVInt(shardId); |
| 103 | + out.writeInt(start); |
| 104 | + out.writeInt(end); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 109 | + builder.startObject().field("shard_id", shardId).field("start", start).field("end", end); |
| 110 | + builder.endObject(); |
| 111 | + return builder; |
| 112 | + } |
| 113 | + |
| 114 | + public static ShardRange parse(XContentParser parser) throws IOException { |
| 115 | + int shardId = -1, start = -1, end = -1; |
| 116 | + XContentParser.Token token; |
| 117 | + String fieldName = null; |
| 118 | + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { |
| 119 | + if (token == XContentParser.Token.FIELD_NAME) { |
| 120 | + fieldName = parser.currentName(); |
| 121 | + } else if (token == XContentParser.Token.VALUE_NUMBER) { |
| 122 | + if ("shard_id".equals(fieldName)) { |
| 123 | + shardId = parser.intValue(); |
| 124 | + } else if ("start".equals(fieldName)) { |
| 125 | + start = parser.intValue(); |
| 126 | + } else if ("end".equals(fieldName)) { |
| 127 | + end = parser.intValue(); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return new ShardRange(shardId, start, end); |
| 133 | + } |
| 134 | +} |
0 commit comments