|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.client.indexlifecycle; |
| 20 | + |
| 21 | +import org.elasticsearch.common.Nullable; |
| 22 | +import org.elasticsearch.common.ParseField; |
| 23 | +import org.elasticsearch.common.Strings; |
| 24 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 25 | +import org.elasticsearch.common.xcontent.ObjectParser; |
| 26 | +import org.elasticsearch.common.xcontent.ToXContent; |
| 27 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 28 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 29 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | + |
| 33 | +/** |
| 34 | + * A {@link LifecycleAction} which sets the index's priority. The higher the priority, the faster the recovery. |
| 35 | + */ |
| 36 | +public class SetPriorityAction implements LifecycleAction, ToXContentObject { |
| 37 | + public static final String NAME = "set_priority"; |
| 38 | + private static final ParseField RECOVERY_PRIORITY_FIELD = new ParseField("priority"); |
| 39 | + |
| 40 | + @SuppressWarnings("unchecked") |
| 41 | + private static final ConstructingObjectParser<SetPriorityAction, Void> PARSER = new ConstructingObjectParser<>(NAME, true, |
| 42 | + a -> new SetPriorityAction((Integer) a[0])); |
| 43 | + |
| 44 | + //package private for testing |
| 45 | + final Integer recoveryPriority; |
| 46 | + |
| 47 | + static { |
| 48 | + PARSER.declareField(ConstructingObjectParser.constructorArg(), |
| 49 | + (p) -> p.currentToken() == XContentParser.Token.VALUE_NULL ? null : p.intValue() |
| 50 | + , RECOVERY_PRIORITY_FIELD, ObjectParser.ValueType.INT_OR_NULL); |
| 51 | + } |
| 52 | + |
| 53 | + public static SetPriorityAction parse(XContentParser parser) { |
| 54 | + return PARSER.apply(parser, null); |
| 55 | + } |
| 56 | + |
| 57 | + public SetPriorityAction(@Nullable Integer recoveryPriority) { |
| 58 | + if (recoveryPriority != null && recoveryPriority <= 0) { |
| 59 | + throw new IllegalArgumentException("[" + RECOVERY_PRIORITY_FIELD.getPreferredName() + "] must be 0 or greater"); |
| 60 | + } |
| 61 | + this.recoveryPriority = recoveryPriority; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { |
| 66 | + builder.startObject(); |
| 67 | + builder.field(RECOVERY_PRIORITY_FIELD.getPreferredName(), recoveryPriority); |
| 68 | + builder.endObject(); |
| 69 | + return builder; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public boolean equals(Object o) { |
| 74 | + if (this == o) return true; |
| 75 | + if (o == null || getClass() != o.getClass()) return false; |
| 76 | + |
| 77 | + SetPriorityAction that = (SetPriorityAction) o; |
| 78 | + |
| 79 | + return recoveryPriority != null ? recoveryPriority.equals(that.recoveryPriority) : that.recoveryPriority == null; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public int hashCode() { |
| 84 | + return recoveryPriority != null ? recoveryPriority.hashCode() : 0; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public String toString() { |
| 89 | + return Strings.toString(this); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public String getName() { |
| 94 | + return NAME; |
| 95 | + } |
| 96 | +} |
0 commit comments