|
| 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 | + |
| 20 | +package org.elasticsearch.action.admin.indices.rollover; |
| 21 | + |
| 22 | +import org.elasticsearch.cluster.AbstractDiffable; |
| 23 | +import org.elasticsearch.cluster.Diff; |
| 24 | +import org.elasticsearch.common.ParseField; |
| 25 | +import org.elasticsearch.common.Strings; |
| 26 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 27 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 28 | +import org.elasticsearch.common.io.stream.Writeable; |
| 29 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 30 | +import org.elasticsearch.common.xcontent.ToXContentFragment; |
| 31 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 32 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 33 | + |
| 34 | +import java.io.IOException; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Objects; |
| 37 | + |
| 38 | +/** |
| 39 | + * Class for holding Rollover related information within an index |
| 40 | + */ |
| 41 | +public class RolloverInfo extends AbstractDiffable<RolloverInfo> implements Writeable, ToXContentFragment { |
| 42 | + |
| 43 | + public static final ParseField CONDITION_FIELD = new ParseField("met_conditions"); |
| 44 | + public static final ParseField TIME_FIELD = new ParseField("time"); |
| 45 | + |
| 46 | + @SuppressWarnings("unchecked") |
| 47 | + public static ConstructingObjectParser<RolloverInfo, String> PARSER = new ConstructingObjectParser<>("rollover_info", false, |
| 48 | + (a, alias) -> new RolloverInfo(alias, (List<Condition>) a[0], (Long) a[1])); |
| 49 | + static { |
| 50 | + PARSER.declareNamedObjects(ConstructingObjectParser.constructorArg(), |
| 51 | + (p, c, n) -> p.namedObject(Condition.class, n, c), CONDITION_FIELD); |
| 52 | + PARSER.declareLong(ConstructingObjectParser.constructorArg(), TIME_FIELD); |
| 53 | + } |
| 54 | + |
| 55 | + private final String alias; |
| 56 | + private final List<Condition> metConditions; |
| 57 | + private final long time; |
| 58 | + |
| 59 | + public RolloverInfo(String alias, List<Condition> metConditions, long time) { |
| 60 | + this.alias = alias; |
| 61 | + this.metConditions = metConditions; |
| 62 | + this.time = time; |
| 63 | + } |
| 64 | + |
| 65 | + public RolloverInfo(StreamInput in) throws IOException { |
| 66 | + this.alias = in.readString(); |
| 67 | + this.time = in.readVLong(); |
| 68 | + this.metConditions = in.readNamedWriteableList(Condition.class); |
| 69 | + } |
| 70 | + |
| 71 | + public static RolloverInfo parse(XContentParser parser, String alias) { |
| 72 | + return PARSER.apply(parser, alias); |
| 73 | + } |
| 74 | + |
| 75 | + public String getAlias() { |
| 76 | + return alias; |
| 77 | + } |
| 78 | + |
| 79 | + public List<Condition> getMetConditions() { |
| 80 | + return metConditions; |
| 81 | + } |
| 82 | + |
| 83 | + public long getTime() { |
| 84 | + return time; |
| 85 | + } |
| 86 | + |
| 87 | + public static Diff<RolloverInfo> readDiffFrom(StreamInput in) throws IOException { |
| 88 | + return readDiffFrom(RolloverInfo::new, in); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void writeTo(StreamOutput out) throws IOException { |
| 93 | + out.writeString(alias); |
| 94 | + out.writeVLong(time); |
| 95 | + out.writeNamedWriteableList(metConditions); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 100 | + builder.startObject(alias); |
| 101 | + builder.startObject(CONDITION_FIELD.getPreferredName()); |
| 102 | + for (Condition condition : metConditions) { |
| 103 | + condition.toXContent(builder, params); |
| 104 | + } |
| 105 | + builder.endObject(); |
| 106 | + builder.field(TIME_FIELD.getPreferredName(), time); |
| 107 | + builder.endObject(); |
| 108 | + return builder; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public int hashCode() { |
| 113 | + return Objects.hash(alias, metConditions, time); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public boolean equals(Object obj) { |
| 118 | + if (obj == null) { |
| 119 | + return false; |
| 120 | + } |
| 121 | + if (obj.getClass() != getClass()) { |
| 122 | + return false; |
| 123 | + } |
| 124 | + RolloverInfo other = (RolloverInfo) obj; |
| 125 | + return Objects.equals(alias, other.alias) && |
| 126 | + Objects.equals(metConditions, other.metConditions) && |
| 127 | + Objects.equals(time, other.time); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public String toString() { |
| 132 | + return Strings.toString(this); |
| 133 | + } |
| 134 | +} |
0 commit comments