-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Store Template's mappings as bytes for disk serialization #78746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
a17e437
1bbccb6
d490007
0880079
25bb42d
256618f
f36868c
1473a23
f0457af
a257b20
0d29911
579e139
fda4aa9
1758f8d
e1bd326
58a691a
5b0408f
f45b195
77f744d
81265a8
d8f4f86
e4ccdaa
58016b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,24 +9,25 @@ | |
| package org.elasticsearch.cluster.metadata; | ||
|
|
||
| import org.elasticsearch.cluster.AbstractDiffable; | ||
| import org.elasticsearch.common.util.Maps; | ||
| import org.elasticsearch.core.Nullable; | ||
| import org.elasticsearch.common.xcontent.ParseField; | ||
| import org.elasticsearch.common.Strings; | ||
| import org.elasticsearch.common.compress.CompressedXContent; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.common.io.stream.StreamOutput; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.common.util.Maps; | ||
| import org.elasticsearch.common.xcontent.ConstructingObjectParser; | ||
| import org.elasticsearch.common.xcontent.ParseField; | ||
| import org.elasticsearch.common.xcontent.ToXContentObject; | ||
| import org.elasticsearch.common.xcontent.XContentBuilder; | ||
| import org.elasticsearch.common.xcontent.XContentFactory; | ||
| import org.elasticsearch.common.xcontent.XContentHelper; | ||
| import org.elasticsearch.common.xcontent.XContentParser; | ||
| import org.elasticsearch.common.xcontent.XContentType; | ||
| import org.elasticsearch.core.Nullable; | ||
| import org.elasticsearch.index.mapper.MapperService; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Base64; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
|
|
@@ -47,8 +48,17 @@ public class Template extends AbstractDiffable<Template> implements ToXContentOb | |
|
|
||
| static { | ||
| PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> Settings.fromXContent(p), SETTINGS); | ||
| PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> | ||
| new CompressedXContent(Strings.toString(XContentFactory.jsonBuilder().map(p.mapOrdered()))), MAPPINGS); | ||
| PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> { | ||
| Map<String, Object> values = p.mapOrdered(); | ||
| Object compressed = values.get("compressed"); | ||
| if (compressed == null) { | ||
| return new CompressedXContent(Strings.toString(XContentFactory.jsonBuilder().map(values))); | ||
| } else if (compressed instanceof String) { | ||
| return new CompressedXContent(Base64.getDecoder().decode((String) compressed)); | ||
| } else { | ||
| return new CompressedXContent((byte[]) compressed); | ||
| } | ||
| }, MAPPINGS); | ||
| PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> { | ||
| Map<String, AliasMetadata> aliasMap = new HashMap<>(); | ||
| while ((p.nextToken()) != XContentParser.Token.END_OBJECT) { | ||
|
|
@@ -160,11 +170,17 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws | |
| builder.endObject(); | ||
| } | ||
| if (this.mappings != null) { | ||
| Map<String, Object> uncompressedMapping = | ||
| XContentHelper.convertToMap(this.mappings.uncompressed(), true, XContentType.JSON).v2(); | ||
| if (uncompressedMapping.size() > 0) { | ||
| builder.field(MAPPINGS.getPreferredName()); | ||
| builder.map(reduceMapping(uncompressedMapping)); | ||
| if (Metadata.CONTEXT_MODE_GATEWAY.equals(params.param(Metadata.CONTEXT_MODE_PARAM, Metadata.CONTEXT_MODE_API))) { | ||
|
||
| builder.startObject(MAPPINGS.getPreferredName()); | ||
| builder.field("compressed", mappings.compressed()); | ||
| builder.endObject(); | ||
| } else { | ||
| Map<String, Object> uncompressedMapping = | ||
| XContentHelper.convertToMap(this.mappings.uncompressed(), true, XContentType.JSON).v2(); | ||
| if (uncompressedMapping.size() > 0) { | ||
| builder.field(MAPPINGS.getPreferredName()); | ||
| builder.map(reduceMapping(uncompressedMapping)); | ||
| } | ||
| } | ||
| } | ||
| if (this.aliases != null) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ | |
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Random; | ||
|
|
@@ -88,6 +89,15 @@ private static String differenceBetweenObjectsIgnoringArrayOrder(String path, Ob | |
| } else { | ||
| return path + ": first element is null, the second element is not null"; | ||
| } | ||
| } else if (first instanceof byte[]) { | ||
|
||
| if (second instanceof byte[]) { | ||
| byte[] bf = (byte[]) first; | ||
| byte[] bs = (byte[]) second; | ||
| return Arrays.compare(bf, bs) == 0 ? null : | ||
| path + ": the elements don't match: " + Arrays.toString(bf) + " != " + Arrays.toString(bs); | ||
| } else { | ||
| return path + ": second not a byte array (got" + second + ")"; | ||
| } | ||
| } else if (first instanceof List) { | ||
| if (second instanceof List) { | ||
| List<Object> secondList = new ArrayList<>((List<Object>) second); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for this if we do things the same way we do them for mappings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This handles situation when you use
binaryparameter withJsonXContent- it stores byte arrays as BASE64 encoded strings. Without itToAndFromJsonMetadataTests#testSimpleJsonFromAndTofails