|
| 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.cluster.metadata; |
| 21 | + |
| 22 | +import org.elasticsearch.cluster.AbstractDiffable; |
| 23 | +import org.elasticsearch.cluster.Diff; |
| 24 | +import org.elasticsearch.common.Nullable; |
| 25 | +import org.elasticsearch.common.ParseField; |
| 26 | +import org.elasticsearch.common.Strings; |
| 27 | +import org.elasticsearch.common.bytes.BytesArray; |
| 28 | +import org.elasticsearch.common.compress.CompressedXContent; |
| 29 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 30 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 31 | +import org.elasticsearch.common.settings.Settings; |
| 32 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 33 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 34 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 35 | +import org.elasticsearch.common.xcontent.XContentFactory; |
| 36 | +import org.elasticsearch.common.xcontent.XContentHelper; |
| 37 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 38 | +import org.elasticsearch.common.xcontent.XContentType; |
| 39 | + |
| 40 | +import java.io.IOException; |
| 41 | +import java.util.HashMap; |
| 42 | +import java.util.Map; |
| 43 | +import java.util.Objects; |
| 44 | + |
| 45 | +/** |
| 46 | + * A component template is a re-usable template as well as metadata about the template. Each |
| 47 | + * component template is expected to be valid on its own. For example, if a component template |
| 48 | + * contains a field "foo", it's expected to contain all the necessary settings/mappings/etc for the |
| 49 | + * "foo" field. These component templates make up the individual pieces composing an index template. |
| 50 | + */ |
| 51 | +public class ComponentTemplate extends AbstractDiffable<ComponentTemplate> implements ToXContentObject { |
| 52 | + private static final ParseField TEMPLATE = new ParseField("template"); |
| 53 | + private static final ParseField VERSION = new ParseField("version"); |
| 54 | + private static final ParseField METADATA = new ParseField("_meta"); |
| 55 | + |
| 56 | + @SuppressWarnings("unchecked") |
| 57 | + private static final ConstructingObjectParser<ComponentTemplate, Void> PARSER = |
| 58 | + new ConstructingObjectParser<>("component_template", false, |
| 59 | + a -> new ComponentTemplate((Template) a[0], (Long) a[1], (Map<String, Object>) a[2])); |
| 60 | + |
| 61 | + static { |
| 62 | + PARSER.declareObject(ConstructingObjectParser.constructorArg(), Template.PARSER, TEMPLATE); |
| 63 | + PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), VERSION); |
| 64 | + PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> p.map(), METADATA); |
| 65 | + } |
| 66 | + |
| 67 | + private final Template template; |
| 68 | + @Nullable |
| 69 | + private final Long version; |
| 70 | + @Nullable |
| 71 | + private final Map<String, Object> metadata; |
| 72 | + |
| 73 | + static Diff<ComponentTemplate> readComponentTemplateDiffFrom(StreamInput in) throws IOException { |
| 74 | + return AbstractDiffable.readDiffFrom(ComponentTemplate::new, in); |
| 75 | + } |
| 76 | + |
| 77 | + public static ComponentTemplate parse(XContentParser parser) { |
| 78 | + return PARSER.apply(parser, null); |
| 79 | + } |
| 80 | + |
| 81 | + public ComponentTemplate(Template template, @Nullable Long version, @Nullable Map<String, Object> metadata) { |
| 82 | + this.template = template; |
| 83 | + this.version = version; |
| 84 | + this.metadata = metadata; |
| 85 | + } |
| 86 | + |
| 87 | + public ComponentTemplate(StreamInput in) throws IOException { |
| 88 | + this.template = new Template(in); |
| 89 | + this.version = in.readOptionalVLong(); |
| 90 | + if (in.readBoolean()) { |
| 91 | + this.metadata = in.readMap(); |
| 92 | + } else { |
| 93 | + this.metadata = null; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public Template template() { |
| 98 | + return template; |
| 99 | + } |
| 100 | + |
| 101 | + @Nullable |
| 102 | + public Long version() { |
| 103 | + return version; |
| 104 | + } |
| 105 | + |
| 106 | + @Nullable |
| 107 | + public Map<String, Object> metadata() { |
| 108 | + return metadata; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void writeTo(StreamOutput out) throws IOException { |
| 113 | + this.template.writeTo(out); |
| 114 | + out.writeOptionalVLong(this.version); |
| 115 | + if (this.metadata == null) { |
| 116 | + out.writeBoolean(false); |
| 117 | + } else { |
| 118 | + out.writeBoolean(true); |
| 119 | + out.writeMap(this.metadata); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public int hashCode() { |
| 125 | + return Objects.hash(template, version, metadata); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public boolean equals(Object obj) { |
| 130 | + if (obj == null) { |
| 131 | + return false; |
| 132 | + } |
| 133 | + if (obj.getClass() != getClass()) { |
| 134 | + return false; |
| 135 | + } |
| 136 | + ComponentTemplate other = (ComponentTemplate) obj; |
| 137 | + return Objects.equals(template, other.template) && |
| 138 | + Objects.equals(version, other.version) && |
| 139 | + Objects.equals(metadata, other.metadata); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public String toString() { |
| 144 | + return Strings.toString(this); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 149 | + builder.startObject(); |
| 150 | + builder.field(TEMPLATE.getPreferredName(), this.template); |
| 151 | + if (this.version != null) { |
| 152 | + builder.field(VERSION.getPreferredName(), this.version); |
| 153 | + } |
| 154 | + if (this.metadata != null) { |
| 155 | + builder.field(METADATA.getPreferredName(), this.metadata); |
| 156 | + } |
| 157 | + builder.endObject(); |
| 158 | + return builder; |
| 159 | + } |
| 160 | + |
| 161 | + static class Template extends AbstractDiffable<Template> implements ToXContentObject { |
| 162 | + private static final ParseField SETTINGS = new ParseField("settings"); |
| 163 | + private static final ParseField MAPPINGS = new ParseField("mappings"); |
| 164 | + private static final ParseField ALIASES = new ParseField("aliases"); |
| 165 | + |
| 166 | + @SuppressWarnings("unchecked") |
| 167 | + private static final ConstructingObjectParser<Template, Void> PARSER = new ConstructingObjectParser<>("template", false, |
| 168 | + a -> new Template((Settings) a[0], (CompressedXContent) a[1], (Map<String, AliasMetaData>) a[2])); |
| 169 | + |
| 170 | + static { |
| 171 | + PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> Settings.fromXContent(p), SETTINGS); |
| 172 | + PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> |
| 173 | + new CompressedXContent(Strings.toString(XContentFactory.jsonBuilder().map(p.mapOrdered()))), MAPPINGS); |
| 174 | + PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> { |
| 175 | + Map<String, AliasMetaData> aliasMap = new HashMap<>(); |
| 176 | + while ((p.nextToken()) != XContentParser.Token.END_OBJECT) { |
| 177 | + AliasMetaData alias = AliasMetaData.Builder.fromXContent(p); |
| 178 | + aliasMap.put(alias.alias(), alias); |
| 179 | + } |
| 180 | + return aliasMap; |
| 181 | + }, ALIASES); |
| 182 | + } |
| 183 | + |
| 184 | + @Nullable |
| 185 | + private final Settings settings; |
| 186 | + @Nullable |
| 187 | + private final CompressedXContent mappings; |
| 188 | + @Nullable |
| 189 | + private final Map<String, AliasMetaData> aliases; |
| 190 | + |
| 191 | + Template(@Nullable Settings settings, @Nullable CompressedXContent mappings, @Nullable Map<String, AliasMetaData> aliases) { |
| 192 | + this.settings = settings; |
| 193 | + this.mappings = mappings; |
| 194 | + this.aliases = aliases; |
| 195 | + } |
| 196 | + |
| 197 | + Template(StreamInput in) throws IOException { |
| 198 | + if (in.readBoolean()) { |
| 199 | + this.settings = Settings.readSettingsFromStream(in); |
| 200 | + } else { |
| 201 | + this.settings = null; |
| 202 | + } |
| 203 | + if (in.readBoolean()) { |
| 204 | + this.mappings = CompressedXContent.readCompressedString(in); |
| 205 | + } else { |
| 206 | + this.mappings = null; |
| 207 | + } |
| 208 | + if (in.readBoolean()) { |
| 209 | + this.aliases = in.readMap(StreamInput::readString, AliasMetaData::new); |
| 210 | + } else { |
| 211 | + this.aliases = null; |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + public Settings settings() { |
| 216 | + return settings; |
| 217 | + } |
| 218 | + |
| 219 | + public CompressedXContent mappings() { |
| 220 | + return mappings; |
| 221 | + } |
| 222 | + |
| 223 | + public Map<String, AliasMetaData> aliases() { |
| 224 | + return aliases; |
| 225 | + } |
| 226 | + |
| 227 | + @Override |
| 228 | + public void writeTo(StreamOutput out) throws IOException { |
| 229 | + if (this.settings == null) { |
| 230 | + out.writeBoolean(false); |
| 231 | + } else { |
| 232 | + out.writeBoolean(true); |
| 233 | + Settings.writeSettingsToStream(this.settings, out); |
| 234 | + } |
| 235 | + if (this.mappings == null) { |
| 236 | + out.writeBoolean(false); |
| 237 | + } else { |
| 238 | + out.writeBoolean(true); |
| 239 | + this.mappings.writeTo(out); |
| 240 | + } |
| 241 | + if (this.aliases == null) { |
| 242 | + out.writeBoolean(false); |
| 243 | + } else { |
| 244 | + out.writeBoolean(true); |
| 245 | + out.writeMap(this.aliases, StreamOutput::writeString, (stream, aliasMetaData) -> aliasMetaData.writeTo(stream)); |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + @Override |
| 250 | + public int hashCode() { |
| 251 | + return Objects.hash(settings, mappings, aliases); |
| 252 | + } |
| 253 | + |
| 254 | + @Override |
| 255 | + public boolean equals(Object obj) { |
| 256 | + if (obj == null) { |
| 257 | + return false; |
| 258 | + } |
| 259 | + if (obj.getClass() != getClass()) { |
| 260 | + return false; |
| 261 | + } |
| 262 | + Template other = (Template) obj; |
| 263 | + return Objects.equals(settings, other.settings) && |
| 264 | + Objects.equals(mappings, other.mappings) && |
| 265 | + Objects.equals(aliases, other.aliases); |
| 266 | + } |
| 267 | + |
| 268 | + @Override |
| 269 | + public String toString() { |
| 270 | + return Strings.toString(this); |
| 271 | + } |
| 272 | + |
| 273 | + @Override |
| 274 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 275 | + builder.startObject(); |
| 276 | + if (this.settings != null) { |
| 277 | + builder.startObject(SETTINGS.getPreferredName()); |
| 278 | + this.settings.toXContent(builder, params); |
| 279 | + builder.endObject(); |
| 280 | + } |
| 281 | + if (this.mappings != null) { |
| 282 | + Map<String, Object> uncompressedMapping = |
| 283 | + XContentHelper.convertToMap(new BytesArray(this.mappings.uncompressed()), true, XContentType.JSON).v2(); |
| 284 | + if (uncompressedMapping.size() > 0) { |
| 285 | + builder.field(MAPPINGS.getPreferredName()); |
| 286 | + builder.map(uncompressedMapping); |
| 287 | + } |
| 288 | + } |
| 289 | + if (this.aliases != null) { |
| 290 | + builder.startObject(ALIASES.getPreferredName()); |
| 291 | + for (AliasMetaData alias : this.aliases.values()) { |
| 292 | + AliasMetaData.Builder.toXContent(alias, builder, params); |
| 293 | + } |
| 294 | + builder.endObject(); |
| 295 | + } |
| 296 | + builder.endObject(); |
| 297 | + return builder; |
| 298 | + } |
| 299 | + } |
| 300 | +} |
0 commit comments