|
| 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.indices; |
| 20 | + |
| 21 | +import org.elasticsearch.ElasticsearchException; |
| 22 | +import org.elasticsearch.action.support.DefaultShardOperationFailedException; |
| 23 | +import org.elasticsearch.action.support.master.ShardsAcknowledgedResponse; |
| 24 | +import org.elasticsearch.common.Nullable; |
| 25 | +import org.elasticsearch.common.ParseField; |
| 26 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 27 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 28 | +import org.elasticsearch.common.xcontent.XContentParserUtils; |
| 29 | + |
| 30 | +import java.util.List; |
| 31 | +import java.util.Objects; |
| 32 | + |
| 33 | +import static java.util.Collections.emptyList; |
| 34 | +import static java.util.Collections.unmodifiableList; |
| 35 | +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg; |
| 36 | +import static org.elasticsearch.common.xcontent.ObjectParser.ValueType; |
| 37 | + |
| 38 | +public class CloseIndexResponse extends ShardsAcknowledgedResponse { |
| 39 | + |
| 40 | + @SuppressWarnings("unchecked") |
| 41 | + private static final ConstructingObjectParser<CloseIndexResponse, Void> PARSER = new ConstructingObjectParser<>("close_index_response", |
| 42 | + true, args -> { |
| 43 | + boolean acknowledged = (boolean) args[0]; |
| 44 | + boolean shardsAcknowledged = args[1] != null ? (boolean) args[1] : acknowledged; |
| 45 | + List<CloseIndexResponse.IndexResult> indices = args[2] != null ? (List<CloseIndexResponse.IndexResult>) args[2] : emptyList(); |
| 46 | + return new CloseIndexResponse(acknowledged, shardsAcknowledged, indices); |
| 47 | + }); |
| 48 | + |
| 49 | + static { |
| 50 | + declareAcknowledgedField(PARSER); |
| 51 | + PARSER.declareField(optionalConstructorArg(), (parser, context) -> parser.booleanValue(), SHARDS_ACKNOWLEDGED, ValueType.BOOLEAN); |
| 52 | + PARSER.declareNamedObjects(optionalConstructorArg(), (p, c, name) -> IndexResult.fromXContent(p, name), new ParseField("indices")); |
| 53 | + } |
| 54 | + |
| 55 | + private final List<CloseIndexResponse.IndexResult> indices; |
| 56 | + |
| 57 | + public CloseIndexResponse(final boolean acknowledged, final boolean shardsAcknowledged, final List<IndexResult> indices) { |
| 58 | + super(acknowledged, shardsAcknowledged); |
| 59 | + this.indices = unmodifiableList(Objects.requireNonNull(indices)); |
| 60 | + } |
| 61 | + |
| 62 | + public List<IndexResult> getIndices() { |
| 63 | + return indices; |
| 64 | + } |
| 65 | + |
| 66 | + public static CloseIndexResponse fromXContent(final XContentParser parser) { |
| 67 | + return PARSER.apply(parser, null); |
| 68 | + } |
| 69 | + |
| 70 | + public static class IndexResult { |
| 71 | + |
| 72 | + @SuppressWarnings("unchecked") |
| 73 | + private static final ConstructingObjectParser<IndexResult, String> PARSER = new ConstructingObjectParser<>("index_result", true, |
| 74 | + (args, index) -> { |
| 75 | + Exception exception = (Exception) args[1]; |
| 76 | + if (exception != null) { |
| 77 | + assert (boolean) args[0] == false; |
| 78 | + return new IndexResult(index, exception); |
| 79 | + } |
| 80 | + ShardResult[] shardResults = args[2] != null ? ((List<ShardResult>) args[2]).toArray(new ShardResult[0]) : null; |
| 81 | + if (shardResults != null) { |
| 82 | + assert (boolean) args[0] == false; |
| 83 | + return new IndexResult(index, shardResults); |
| 84 | + } |
| 85 | + assert (boolean) args[0]; |
| 86 | + return new IndexResult(index); |
| 87 | + }); |
| 88 | + static { |
| 89 | + PARSER.declareBoolean(optionalConstructorArg(), new ParseField("closed")); |
| 90 | + PARSER.declareObject(optionalConstructorArg(), (p, c) -> { |
| 91 | + XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, p.currentToken(), p::getTokenLocation); |
| 92 | + XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, p.nextToken(), p::getTokenLocation); |
| 93 | + Exception e = ElasticsearchException.failureFromXContent(p); |
| 94 | + XContentParserUtils.ensureExpectedToken(XContentParser.Token.END_OBJECT, p.nextToken(), p::getTokenLocation); |
| 95 | + return e; |
| 96 | + }, new ParseField("exception")); |
| 97 | + PARSER.declareNamedObjects(optionalConstructorArg(), |
| 98 | + (p, c, id) -> ShardResult.fromXContent(p, id), new ParseField("failedShards")); |
| 99 | + } |
| 100 | + |
| 101 | + private final String index; |
| 102 | + private final @Nullable Exception exception; |
| 103 | + private final @Nullable ShardResult[] shards; |
| 104 | + |
| 105 | + IndexResult(final String index) { |
| 106 | + this(index, null, null); |
| 107 | + } |
| 108 | + |
| 109 | + IndexResult(final String index, final Exception failure) { |
| 110 | + this(index, Objects.requireNonNull(failure), null); |
| 111 | + } |
| 112 | + |
| 113 | + IndexResult(final String index, final ShardResult[] shards) { |
| 114 | + this(index, null, Objects.requireNonNull(shards)); |
| 115 | + } |
| 116 | + |
| 117 | + private IndexResult(final String index, @Nullable final Exception exception, @Nullable final ShardResult[] shards) { |
| 118 | + this.index = Objects.requireNonNull(index); |
| 119 | + this.exception = exception; |
| 120 | + this.shards = shards; |
| 121 | + } |
| 122 | + |
| 123 | + public String getIndex() { |
| 124 | + return index; |
| 125 | + } |
| 126 | + |
| 127 | + public @Nullable Exception getException() { |
| 128 | + return exception; |
| 129 | + } |
| 130 | + |
| 131 | + public @Nullable ShardResult[] getShards() { |
| 132 | + return shards; |
| 133 | + } |
| 134 | + |
| 135 | + public boolean hasFailures() { |
| 136 | + if (exception != null) { |
| 137 | + return true; |
| 138 | + } |
| 139 | + if (shards != null) { |
| 140 | + for (ShardResult shard : shards) { |
| 141 | + if (shard.hasFailures()) { |
| 142 | + return true; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + return false; |
| 147 | + } |
| 148 | + |
| 149 | + static IndexResult fromXContent(final XContentParser parser, final String name) { |
| 150 | + return PARSER.apply(parser, name); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + public static class ShardResult { |
| 155 | + |
| 156 | + @SuppressWarnings("unchecked") |
| 157 | + private static final ConstructingObjectParser<ShardResult, String> PARSER = new ConstructingObjectParser<>("shard_result", true, |
| 158 | + (arg, id) -> { |
| 159 | + Failure[] failures = arg[0] != null ? ((List<Failure>) arg[0]).toArray(new Failure[0]) : new Failure[0]; |
| 160 | + return new ShardResult(Integer.parseInt(id), failures); |
| 161 | + }); |
| 162 | + |
| 163 | + static { |
| 164 | + PARSER.declareObjectArray(optionalConstructorArg(), (p, c) -> Failure.PARSER.apply(p, null), new ParseField("failures")); |
| 165 | + } |
| 166 | + |
| 167 | + private final int id; |
| 168 | + private final Failure[] failures; |
| 169 | + |
| 170 | + ShardResult(final int id, final Failure[] failures) { |
| 171 | + this.id = id; |
| 172 | + this.failures = failures; |
| 173 | + } |
| 174 | + |
| 175 | + public boolean hasFailures() { |
| 176 | + return failures != null && failures.length > 0; |
| 177 | + } |
| 178 | + |
| 179 | + public int getId() { |
| 180 | + return id; |
| 181 | + } |
| 182 | + |
| 183 | + public Failure[] getFailures() { |
| 184 | + return failures; |
| 185 | + } |
| 186 | + |
| 187 | + static ShardResult fromXContent(final XContentParser parser, final String id) { |
| 188 | + return PARSER.apply(parser, id); |
| 189 | + } |
| 190 | + |
| 191 | + public static class Failure extends DefaultShardOperationFailedException { |
| 192 | + |
| 193 | + static final ConstructingObjectParser<Failure, Void> PARSER = new ConstructingObjectParser<>("failure", true, |
| 194 | + arg -> new Failure((String) arg[0], (int) arg[1], (Throwable) arg[2], (String) arg[3])); |
| 195 | + |
| 196 | + static { |
| 197 | + declareFields(PARSER); |
| 198 | + PARSER.declareStringOrNull(optionalConstructorArg(), new ParseField("node")); |
| 199 | + } |
| 200 | + |
| 201 | + private @Nullable String nodeId; |
| 202 | + |
| 203 | + Failure(final String index, final int shardId, final Throwable reason, final String nodeId) { |
| 204 | + super(index, shardId, reason); |
| 205 | + this.nodeId = nodeId; |
| 206 | + } |
| 207 | + |
| 208 | + public String getNodeId() { |
| 209 | + return nodeId; |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | +} |
0 commit comments