|
| 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.rollup; |
| 20 | + |
| 21 | +import org.elasticsearch.common.ParseField; |
| 22 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 23 | +import org.elasticsearch.common.xcontent.ToXContentFragment; |
| 24 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.Comparator; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Objects; |
| 31 | +import java.util.function.Function; |
| 32 | +import java.util.stream.Collectors; |
| 33 | + |
| 34 | +/** |
| 35 | + * Represents the rollup capabilities of a non-rollup index. E.g. what values/aggregations |
| 36 | + * were rolled up for this index, in what rollup jobs that data is stored and where those |
| 37 | + * concrete rollup indices exist |
| 38 | + * |
| 39 | + * The index name can either be a single index, or an index pattern (logstash-*) |
| 40 | + */ |
| 41 | +public class RollableIndexCaps implements ToXContentFragment { |
| 42 | + private static final ParseField ROLLUP_JOBS = new ParseField("rollup_jobs"); |
| 43 | + |
| 44 | + public static final Function<String, ConstructingObjectParser<RollableIndexCaps, Void>> PARSER = indexName -> { |
| 45 | + @SuppressWarnings("unchecked") |
| 46 | + ConstructingObjectParser<RollableIndexCaps, Void> p |
| 47 | + = new ConstructingObjectParser<>(indexName, |
| 48 | + a -> new RollableIndexCaps(indexName, (List<RollupJobCaps>) a[0])); |
| 49 | + |
| 50 | + p.declareObjectArray(ConstructingObjectParser.constructorArg(), RollupJobCaps.PARSER::apply, |
| 51 | + ROLLUP_JOBS); |
| 52 | + return p; |
| 53 | + }; |
| 54 | + |
| 55 | + private final String indexName; |
| 56 | + private final List<RollupJobCaps> jobCaps; |
| 57 | + |
| 58 | + RollableIndexCaps(final String indexName, final List<RollupJobCaps> caps) { |
| 59 | + this.indexName = indexName; |
| 60 | + this.jobCaps = Collections.unmodifiableList(Objects.requireNonNull(caps) |
| 61 | + .stream() |
| 62 | + .sorted(Comparator.comparing(RollupJobCaps::getJobID)) |
| 63 | + .collect(Collectors.toList())); |
| 64 | + } |
| 65 | + |
| 66 | + public String getIndexName() { |
| 67 | + return indexName; |
| 68 | + } |
| 69 | + |
| 70 | + public List<RollupJobCaps> getJobCaps() { |
| 71 | + return jobCaps; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 76 | + builder.startObject(indexName); |
| 77 | + { |
| 78 | + builder.field(ROLLUP_JOBS.getPreferredName(), jobCaps); |
| 79 | + } |
| 80 | + builder.endObject(); |
| 81 | + return builder; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public boolean equals(Object other) { |
| 86 | + if (this == other) { |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | + if (other == null || getClass() != other.getClass()) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + RollableIndexCaps that = (RollableIndexCaps) other; |
| 95 | + return Objects.equals(this.jobCaps, that.jobCaps) |
| 96 | + && Objects.equals(this.indexName, that.indexName); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public int hashCode() { |
| 101 | + return Objects.hash(jobCaps, indexName); |
| 102 | + } |
| 103 | +} |
0 commit comments