From 57c343bc212561c9b5b05c691dc4dff81430155e Mon Sep 17 00:00:00 2001 From: Colin Goodheart-Smithe Date: Mon, 8 Jan 2018 14:14:43 +0000 Subject: [PATCH] Use the XContentRegistry provided by the parser --- .../xpack/indexlifecycle/LifecyclePolicy.java | 15 +++++------ .../xpack/indexlifecycle/Phase.java | 12 ++++----- .../action/PutLifecycleAction.java | 10 +++----- .../IndexLifecycleMetadata.java | 6 ++--- .../action/RestPutLifecycleAction.java | 3 +-- .../IndexLifecycleMetadataTests.java | 25 +++++++++---------- .../indexlifecycle/LifecyclePolicyTests.java | 20 +++++++++------ .../xpack/indexlifecycle/PhaseTests.java | 17 +++++++------ .../action/PutLifecycleRequestTests.java | 19 ++++++++------ 9 files changed, 63 insertions(+), 64 deletions(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/indexlifecycle/LifecyclePolicy.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/indexlifecycle/LifecyclePolicy.java index 6493b318fdd..62b1659b735 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/indexlifecycle/LifecyclePolicy.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/indexlifecycle/LifecyclePolicy.java @@ -10,12 +10,10 @@ import org.elasticsearch.cluster.AbstractDiffable; import org.elasticsearch.cluster.Diffable; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.Strings; -import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.logging.ESLoggerFactory; import org.elasticsearch.common.xcontent.ConstructingObjectParser; -import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.ObjectParser.ValueType; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -48,18 +46,17 @@ public class LifecyclePolicy extends AbstractDiffable public static final ParseField TYPE_FIELD = new ParseField("type"); @SuppressWarnings("unchecked") - public static ConstructingObjectParser> PARSER = new ConstructingObjectParser<>( - "lifecycle_policy", false, (a, c) -> { - String name = c.v1(); + public static ConstructingObjectParser PARSER = new ConstructingObjectParser<>("lifecycle_policy", false, + (a, name) -> { LifecycleType type = (LifecycleType) a[0]; List phases = (List) a[1]; Map phaseMap = phases.stream().collect(Collectors.toMap(Phase::getName, Function.identity())); return new LifecyclePolicy(type, name, phaseMap); }); static { - PARSER.declareField(constructorArg(), (p, c) -> c.v2().parseNamedObject(LifecycleType.class, p.text(), p, null), TYPE_FIELD, + PARSER.declareField(constructorArg(), (p, c) -> p.namedObject(LifecycleType.class, p.text(), null), TYPE_FIELD, ValueType.STRING); - PARSER.declareNamedObjects(constructorArg(), (p, c, n) -> Phase.parse(p, new Tuple<>(n, c.v2())), v -> { + PARSER.declareNamedObjects(constructorArg(), (p, c, n) -> Phase.parse(p, n), v -> { throw new IllegalArgumentException("ordered " + PHASES_FIELD.getPreferredName() + " are not supported"); }, PHASES_FIELD); } @@ -91,8 +88,8 @@ public class LifecyclePolicy extends AbstractDiffable phases = Collections.unmodifiableMap(in.readMap(StreamInput::readString, Phase::new)); } - public static LifecyclePolicy parse(XContentParser parser, Tuple context) { - return PARSER.apply(parser, context); + public static LifecyclePolicy parse(XContentParser parser, String name) { + return PARSER.apply(parser, name); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/indexlifecycle/Phase.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/indexlifecycle/Phase.java index c020fd2a613..0de43c7194d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/indexlifecycle/Phase.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/indexlifecycle/Phase.java @@ -8,14 +8,12 @@ package org.elasticsearch.xpack.indexlifecycle; import org.apache.logging.log4j.Logger; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.Strings; -import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.logging.ESLoggerFactory; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.ConstructingObjectParser; -import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.ObjectParser.ValueType; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -43,20 +41,20 @@ public class Phase implements ToXContentObject, Writeable { public static final ParseField ACTIONS_FIELD = new ParseField("actions"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser> PARSER = new ConstructingObjectParser<>( - "phase", false, (a, c) -> new Phase(c.v1(), (TimeValue) a[0], + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("phase", false, + (a, name) -> new Phase(name, (TimeValue) a[0], convertListToMapValues(LifecycleAction::getWriteableName, (List) a[1]))); static { PARSER.declareField(ConstructingObjectParser.constructorArg(), (p, c) -> TimeValue.parseTimeValue(p.text(), AFTER_FIELD.getPreferredName()), AFTER_FIELD, ValueType.VALUE); PARSER.declareNamedObjects(ConstructingObjectParser.constructorArg(), - (p, c, n) -> c.v2().parseNamedObject(LifecycleAction.class, n, p, c.v2()), v -> { + (p, c, n) -> p.namedObject(LifecycleAction.class, n, null), v -> { throw new IllegalArgumentException("ordered " + ACTIONS_FIELD.getPreferredName() + " are not supported"); }, ACTIONS_FIELD); } - public static Phase parse(XContentParser parser, Tuple context) { - return PARSER.apply(parser, context); + public static Phase parse(XContentParser parser, String name) { + return PARSER.apply(parser, name); } private String name; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/PutLifecycleAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/PutLifecycleAction.java index 4385650e03e..1877eda12d7 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/PutLifecycleAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/PutLifecycleAction.java @@ -13,11 +13,9 @@ import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.ElasticsearchClient; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.Strings; -import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.ConstructingObjectParser; -import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; @@ -106,10 +104,10 @@ public class PutLifecycleAction extends Action implements ToXContentObject { public static final ParseField POLICY_FIELD = new ParseField("policy"); - private static final ConstructingObjectParser> PARSER = + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("put_lifecycle_request", a -> new Request((LifecyclePolicy) a[0])); static { - PARSER.declareObject(ConstructingObjectParser.constructorArg(), (p, c) -> LifecyclePolicy.parse(p, c), POLICY_FIELD); + PARSER.declareObject(ConstructingObjectParser.constructorArg(), (p, name) -> LifecyclePolicy.parse(p, name), POLICY_FIELD); } private LifecyclePolicy policy; @@ -130,8 +128,8 @@ public class PutLifecycleAction extends Action(name, namedXContentRegistry)); + public static Request parseRequest(String name, XContentParser parser) { + return PARSER.apply(parser, name); } @Override diff --git a/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleMetadata.java b/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleMetadata.java index 384e2695b02..a944b08ee54 100644 --- a/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleMetadata.java +++ b/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleMetadata.java @@ -14,11 +14,9 @@ import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.metadata.MetaData.Custom; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.Strings; -import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.ConstructingObjectParser; -import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.XContentBuilder; import java.io.IOException; @@ -36,11 +34,11 @@ public class IndexLifecycleMetadata implements MetaData.Custom { public static final ParseField POLICIES_FIELD = new ParseField("policies"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( TYPE, a -> new IndexLifecycleMetadata( ObjectParserUtils.convertListToMapValues(LifecyclePolicy::getName, (List) a[0]))); static { - PARSER.declareNamedObjects(ConstructingObjectParser.constructorArg(), (p, c, n) -> LifecyclePolicy.parse(p, new Tuple<>(n, c)), + PARSER.declareNamedObjects(ConstructingObjectParser.constructorArg(), (p, c, n) -> LifecyclePolicy.parse(p, n), v -> { throw new IllegalArgumentException("ordered " + POLICIES_FIELD.getPreferredName() + " are not supported"); }, POLICIES_FIELD); diff --git a/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/RestPutLifecycleAction.java b/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/RestPutLifecycleAction.java index 10a414ad3e4..815881b3851 100644 --- a/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/RestPutLifecycleAction.java +++ b/x-pack/plugin/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/RestPutLifecycleAction.java @@ -33,8 +33,7 @@ public class RestPutLifecycleAction extends BaseRestHandler { protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException { String lifecycleName = restRequest.param("name"); XContentParser parser = restRequest.contentParser(); - PutLifecycleAction.Request putLifecycleRequest = PutLifecycleAction.Request.parseRequest(lifecycleName, parser, - restRequest.getXContentRegistry()); + PutLifecycleAction.Request putLifecycleRequest = PutLifecycleAction.Request.parseRequest(lifecycleName, parser); putLifecycleRequest.timeout(restRequest.paramAsTime("timeout", putLifecycleRequest.timeout())); putLifecycleRequest.masterNodeTimeout(restRequest.paramAsTime("master_timeout", putLifecycleRequest.masterNodeTimeout())); diff --git a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleMetadataTests.java b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleMetadataTests.java index 9adbabec905..545d275bb99 100644 --- a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleMetadataTests.java +++ b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleMetadataTests.java @@ -6,6 +6,7 @@ package org.elasticsearch.xpack.indexlifecycle; import org.elasticsearch.Version; +import org.elasticsearch.cluster.ClusterModule; import org.elasticsearch.cluster.Diff; import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.metadata.MetaData.Custom; @@ -17,9 +18,9 @@ import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.test.AbstractDiffableSerializationTestCase; import org.elasticsearch.xpack.indexlifecycle.IndexLifecycleMetadata.IndexLifecycleMetadataDiff; -import org.junit.Before; import java.io.IOException; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -30,17 +31,6 @@ import java.util.TreeMap; public class IndexLifecycleMetadataTests extends AbstractDiffableSerializationTestCase { - private NamedXContentRegistry registry; - - @Before - public void setup() { - List entries = Arrays - .asList(new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse), - new NamedXContentRegistry.Entry(LifecycleType.class, new ParseField(TestLifecycleType.TYPE), - (p) -> TestLifecycleType.INSTANCE)); - registry = new NamedXContentRegistry(entries); - } - @Override protected IndexLifecycleMetadata createTestInstance() { int numPolicies = randomInt(5); @@ -65,7 +55,7 @@ public class IndexLifecycleMetadataTests extends AbstractDiffableSerializationTe @Override protected IndexLifecycleMetadata doParseInstance(XContentParser parser) throws IOException { - return IndexLifecycleMetadata.PARSER.apply(parser, registry); + return IndexLifecycleMetadata.PARSER.apply(parser, null); } @Override @@ -80,6 +70,15 @@ public class IndexLifecycleMetadataTests extends AbstractDiffableSerializationTe new NamedWriteableRegistry.Entry(LifecycleType.class, TestLifecycleType.TYPE, (in) -> TestLifecycleType.INSTANCE))); } + @Override + protected NamedXContentRegistry xContentRegistry() { + List entries = new ArrayList<>(ClusterModule.getNamedXWriteables()); + entries.add(new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse)); + entries.add(new NamedXContentRegistry.Entry(LifecycleType.class, new ParseField(TestLifecycleType.TYPE), + (p) -> TestLifecycleType.INSTANCE)); + return new NamedXContentRegistry(entries); + } + @Override protected MetaData.Custom mutateInstance(MetaData.Custom instance) { IndexLifecycleMetadata metadata = (IndexLifecycleMetadata) instance; diff --git a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/LifecyclePolicyTests.java b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/LifecyclePolicyTests.java index c3872c7b594..40d7f3993fc 100644 --- a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/LifecyclePolicyTests.java +++ b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/LifecyclePolicyTests.java @@ -5,8 +5,8 @@ */ package org.elasticsearch.xpack.indexlifecycle; +import org.elasticsearch.cluster.ClusterModule; import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable.Reader; import org.elasticsearch.common.unit.TimeValue; @@ -16,6 +16,7 @@ import org.elasticsearch.test.AbstractSerializingTestCase; import org.junit.Before; import java.io.IOException; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -34,11 +35,10 @@ public class LifecyclePolicyTests extends AbstractSerializingTestCase(lifecycleName, registry)); + return LifecyclePolicy.parse(parser, lifecycleName); } @Override @@ -48,6 +48,15 @@ public class LifecyclePolicyTests extends AbstractSerializingTestCase TestLifecycleType.INSTANCE))); } + @Override + protected NamedXContentRegistry xContentRegistry() { + List entries = new ArrayList<>(ClusterModule.getNamedXWriteables()); + entries.add(new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse)); + entries.add(new NamedXContentRegistry.Entry(LifecycleType.class, new ParseField(TestLifecycleType.TYPE), + (p) -> TestLifecycleType.INSTANCE)); + return new NamedXContentRegistry(entries); + } + @Override protected LifecyclePolicy createTestInstance() { int numberPhases = randomInt(5); @@ -91,11 +100,6 @@ public class LifecyclePolicyTests extends AbstractSerializingTestCase entries = Arrays - .asList(new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse), - new NamedXContentRegistry.Entry(LifecycleType.class, new ParseField(TestLifecycleType.TYPE), - (p) -> TestLifecycleType.INSTANCE)); - registry = new NamedXContentRegistry(entries); indexName = randomAlphaOfLengthBetween(1, 20); lifecycleName = randomAlphaOfLengthBetween(1, 20); Map phases = new LinkedHashMap<>(); diff --git a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/PhaseTests.java b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/PhaseTests.java index af527acd22a..0552eaa289d 100644 --- a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/PhaseTests.java +++ b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/PhaseTests.java @@ -5,8 +5,8 @@ */ package org.elasticsearch.xpack.indexlifecycle; +import org.elasticsearch.cluster.ClusterModule; import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable.Reader; import org.elasticsearch.common.unit.TimeValue; @@ -24,15 +24,10 @@ import java.util.List; import java.util.Map; public class PhaseTests extends AbstractSerializingTestCase { - - private NamedXContentRegistry registry; private String phaseName; @Before public void setup() { - List entries = Arrays - .asList(new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse)); - registry = new NamedXContentRegistry(entries); phaseName = randomAlphaOfLength(20); // NORELEASE we need to randomise the phase name rather // than use the same name for all instances } @@ -49,8 +44,7 @@ public class PhaseTests extends AbstractSerializingTestCase { @Override protected Phase doParseInstance(XContentParser parser) throws IOException { - - return Phase.parse(parser, new Tuple<>(phaseName, registry)); + return Phase.parse(parser, phaseName); } @Override @@ -63,6 +57,13 @@ public class PhaseTests extends AbstractSerializingTestCase { .asList(new NamedWriteableRegistry.Entry(LifecycleAction.class, DeleteAction.NAME, DeleteAction::new))); } + @Override + protected NamedXContentRegistry xContentRegistry() { + List entries = new ArrayList<>(ClusterModule.getNamedXWriteables()); + entries.add(new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse)); + return new NamedXContentRegistry(entries); + } + @Override protected Phase mutateInstance(Phase instance) throws IOException { String name = instance.getName(); diff --git a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/action/PutLifecycleRequestTests.java b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/action/PutLifecycleRequestTests.java index c07f3623e22..40dd1e8e14e 100644 --- a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/action/PutLifecycleRequestTests.java +++ b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/action/PutLifecycleRequestTests.java @@ -5,6 +5,7 @@ */ package org.elasticsearch.xpack.indexlifecycle.action; +import org.elasticsearch.cluster.ClusterModule; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.unit.TimeValue; @@ -21,6 +22,7 @@ import org.elasticsearch.xpack.indexlifecycle.TestLifecycleType; import org.elasticsearch.xpack.indexlifecycle.action.PutLifecycleAction.Request; import org.junit.Before; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -29,16 +31,10 @@ import java.util.Map; public class PutLifecycleRequestTests extends AbstractStreamableXContentTestCase { - private NamedXContentRegistry registry; private String lifecycleName; @Before public void setup() { - List entries = Arrays.asList( - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse), - new NamedXContentRegistry.Entry(LifecycleType.class, new ParseField(TestLifecycleType.TYPE), - (p) -> TestLifecycleType.INSTANCE)); - registry = new NamedXContentRegistry(entries); lifecycleName = randomAlphaOfLength(20); // NORELEASE we need to randomise the lifecycle name rather // than use the same name for all instances } @@ -55,7 +51,7 @@ public class PutLifecycleRequestTests extends AbstractStreamableXContentTestCase @Override protected Request doParseInstance(XContentParser parser) { - return PutLifecycleAction.Request.parseRequest(lifecycleName, parser, registry); + return PutLifecycleAction.Request.parseRequest(lifecycleName, parser); } protected NamedWriteableRegistry getNamedWriteableRegistry() { @@ -64,6 +60,15 @@ public class PutLifecycleRequestTests extends AbstractStreamableXContentTestCase new NamedWriteableRegistry.Entry(LifecycleType.class, TestLifecycleType.TYPE, in -> TestLifecycleType.INSTANCE))); } + @Override + protected NamedXContentRegistry xContentRegistry() { + List entries = new ArrayList<>(ClusterModule.getNamedXWriteables()); + entries.add(new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse)); + entries.add(new NamedXContentRegistry.Entry(LifecycleType.class, new ParseField(TestLifecycleType.TYPE), + (p) -> TestLifecycleType.INSTANCE)); + return new NamedXContentRegistry(entries); + } + protected boolean supportsUnknownFields() { return false; }