|
| 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.client.ccr; |
| 21 | + |
| 22 | +import org.elasticsearch.common.unit.ByteSizeValue; |
| 23 | +import org.elasticsearch.common.unit.TimeValue; |
| 24 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 25 | +import org.elasticsearch.common.xcontent.ObjectParser; |
| 26 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 27 | +import org.elasticsearch.common.xcontent.XContentParser.Token; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.HashMap; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.Objects; |
| 35 | + |
| 36 | +public final class GetAutoFollowPatternResponse { |
| 37 | + |
| 38 | + public static GetAutoFollowPatternResponse fromXContent(final XContentParser parser) throws IOException { |
| 39 | + final Map<String, Pattern> patterns = new HashMap<>(); |
| 40 | + for (Token token = parser.nextToken(); token != Token.END_OBJECT; token = parser.nextToken()) { |
| 41 | + if (token == Token.FIELD_NAME) { |
| 42 | + final String name = parser.currentName(); |
| 43 | + final Pattern pattern = Pattern.PARSER.parse(parser, null); |
| 44 | + patterns.put(name, pattern); |
| 45 | + } |
| 46 | + } |
| 47 | + return new GetAutoFollowPatternResponse(patterns); |
| 48 | + } |
| 49 | + |
| 50 | + private final Map<String, Pattern> patterns; |
| 51 | + |
| 52 | + GetAutoFollowPatternResponse(Map<String, Pattern> patterns) { |
| 53 | + this.patterns = Collections.unmodifiableMap(patterns); |
| 54 | + } |
| 55 | + |
| 56 | + public Map<String, Pattern> getPatterns() { |
| 57 | + return patterns; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public boolean equals(Object o) { |
| 62 | + if (this == o) return true; |
| 63 | + if (o == null || getClass() != o.getClass()) return false; |
| 64 | + GetAutoFollowPatternResponse that = (GetAutoFollowPatternResponse) o; |
| 65 | + return Objects.equals(patterns, that.patterns); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public int hashCode() { |
| 70 | + return Objects.hash(patterns); |
| 71 | + } |
| 72 | + |
| 73 | + public static class Pattern extends FollowConfig { |
| 74 | + |
| 75 | + @SuppressWarnings("unchecked") |
| 76 | + private static final ConstructingObjectParser<Pattern, Void> PARSER = new ConstructingObjectParser<>( |
| 77 | + "pattern", args -> new Pattern((String) args[0], (List<String>) args[1], (String) args[2])); |
| 78 | + |
| 79 | + static { |
| 80 | + PARSER.declareString(ConstructingObjectParser.constructorArg(), PutFollowRequest.REMOTE_CLUSTER_FIELD); |
| 81 | + PARSER.declareStringArray(ConstructingObjectParser.constructorArg(), PutAutoFollowPatternRequest.LEADER_PATTERNS_FIELD); |
| 82 | + PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), PutAutoFollowPatternRequest.FOLLOW_PATTERN_FIELD); |
| 83 | + PARSER.declareInt(Pattern::setMaxReadRequestOperationCount, FollowConfig.MAX_READ_REQUEST_OPERATION_COUNT); |
| 84 | + PARSER.declareField( |
| 85 | + Pattern::setMaxReadRequestSize, |
| 86 | + (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), FollowConfig.MAX_READ_REQUEST_SIZE.getPreferredName()), |
| 87 | + PutFollowRequest.MAX_READ_REQUEST_SIZE, |
| 88 | + ObjectParser.ValueType.STRING); |
| 89 | + PARSER.declareInt(Pattern::setMaxOutstandingReadRequests, FollowConfig.MAX_OUTSTANDING_READ_REQUESTS); |
| 90 | + PARSER.declareInt(Pattern::setMaxWriteRequestOperationCount, FollowConfig.MAX_WRITE_REQUEST_OPERATION_COUNT); |
| 91 | + PARSER.declareField( |
| 92 | + Pattern::setMaxWriteRequestSize, |
| 93 | + (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), FollowConfig.MAX_WRITE_REQUEST_SIZE.getPreferredName()), |
| 94 | + PutFollowRequest.MAX_WRITE_REQUEST_SIZE, |
| 95 | + ObjectParser.ValueType.STRING); |
| 96 | + PARSER.declareInt(Pattern::setMaxOutstandingWriteRequests, FollowConfig.MAX_OUTSTANDING_WRITE_REQUESTS); |
| 97 | + PARSER.declareInt(Pattern::setMaxWriteBufferCount, FollowConfig.MAX_WRITE_BUFFER_COUNT); |
| 98 | + PARSER.declareField( |
| 99 | + Pattern::setMaxWriteBufferSize, |
| 100 | + (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), FollowConfig.MAX_WRITE_BUFFER_SIZE.getPreferredName()), |
| 101 | + PutFollowRequest.MAX_WRITE_BUFFER_SIZE, |
| 102 | + ObjectParser.ValueType.STRING); |
| 103 | + PARSER.declareField( |
| 104 | + Pattern::setMaxRetryDelay, |
| 105 | + (p, c) -> TimeValue.parseTimeValue(p.text(), FollowConfig.MAX_RETRY_DELAY_FIELD.getPreferredName()), |
| 106 | + PutFollowRequest.MAX_RETRY_DELAY_FIELD, |
| 107 | + ObjectParser.ValueType.STRING); |
| 108 | + PARSER.declareField( |
| 109 | + Pattern::setReadPollTimeout, |
| 110 | + (p, c) -> TimeValue.parseTimeValue(p.text(), FollowConfig.READ_POLL_TIMEOUT.getPreferredName()), |
| 111 | + PutFollowRequest.READ_POLL_TIMEOUT, |
| 112 | + ObjectParser.ValueType.STRING); |
| 113 | + } |
| 114 | + |
| 115 | + private final String remoteCluster; |
| 116 | + private final List<String> leaderIndexPatterns; |
| 117 | + private final String followIndexNamePattern; |
| 118 | + |
| 119 | + Pattern(String remoteCluster, List<String> leaderIndexPatterns, String followIndexNamePattern) { |
| 120 | + this.remoteCluster = remoteCluster; |
| 121 | + this.leaderIndexPatterns = leaderIndexPatterns; |
| 122 | + this.followIndexNamePattern = followIndexNamePattern; |
| 123 | + } |
| 124 | + |
| 125 | + public String getRemoteCluster() { |
| 126 | + return remoteCluster; |
| 127 | + } |
| 128 | + |
| 129 | + public List<String> getLeaderIndexPatterns() { |
| 130 | + return leaderIndexPatterns; |
| 131 | + } |
| 132 | + |
| 133 | + public String getFollowIndexNamePattern() { |
| 134 | + return followIndexNamePattern; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public boolean equals(Object o) { |
| 139 | + if (this == o) return true; |
| 140 | + if (o == null || getClass() != o.getClass()) return false; |
| 141 | + if (!super.equals(o)) return false; |
| 142 | + Pattern pattern = (Pattern) o; |
| 143 | + return Objects.equals(remoteCluster, pattern.remoteCluster) && |
| 144 | + Objects.equals(leaderIndexPatterns, pattern.leaderIndexPatterns) && |
| 145 | + Objects.equals(followIndexNamePattern, pattern.followIndexNamePattern); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public int hashCode() { |
| 150 | + return Objects.hash( |
| 151 | + super.hashCode(), |
| 152 | + remoteCluster, |
| 153 | + leaderIndexPatterns, |
| 154 | + followIndexNamePattern |
| 155 | + ); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | +} |
0 commit comments