From 04968d72a17bd45d6901c353eff65d9878ebcee8 Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Wed, 24 Jun 2015 11:10:30 +0200 Subject: [PATCH] Remove XContentParser.map[Ordered]AndClose(). It is a bit weird to have an API to read a map then close the parser. --- .../put/PutRepositoryRequest.java | 14 ++--- .../create/CreateSnapshotRequest.java | 13 ++--- .../restore/RestoreSnapshotRequest.java | 13 ++--- .../indices/create/CreateIndexRequest.java | 4 +- .../template/put/PutIndexTemplateRequest.java | 12 ++--- .../cluster/metadata/AliasValidator.java | 5 +- .../metadata/IndexTemplateMetaData.java | 7 +-- .../cluster/metadata/MappingMetaData.java | 5 +- .../metadata/MetaDataCreateIndexService.java | 6 ++- .../common/xcontent/XContentHelper.java | 16 +++--- .../common/xcontent/XContentParser.java | 4 -- .../support/AbstractXContentParser.java | 19 ------- .../zen/ping/multicast/MulticastZenPing.java | 7 +-- .../index/mapper/DocumentMapperParser.java | 5 +- .../index/mapper/DocumentParser.java | 5 +- .../RestClusterUpdateSettingsAction.java | 6 ++- .../search/warmer/IndexWarmersMetaData.java | 5 +- .../support/XContentMapValuesTests.java | 52 ++++++++++++++----- .../CompletionFieldMapperTests.java | 11 +++- .../mapper/copyto/CopyToMapperTests.java | 6 ++- .../mapper/date/SimpleDateMappingTests.java | 6 ++- .../routing/RoutingTypeMapperTests.java | 6 ++- .../source/DefaultSourceMappingTests.java | 11 +++- .../string/SimpleStringMappingTests.java | 6 ++- .../timestamp/TimestampMappingTests.java | 6 ++- .../builder/SearchSourceBuilderTest.java | 6 ++- .../test/rest/json/JsonPath.java | 6 ++- .../test/rest/test/DoSectionParserTests.java | 6 ++- .../threadpool/SimpleThreadPoolTests.java | 6 ++- .../ThreadPoolSerializationTests.java | 6 ++- 30 files changed, 179 insertions(+), 101 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/action/admin/cluster/repositories/put/PutRepositoryRequest.java b/core/src/main/java/org/elasticsearch/action/admin/cluster/repositories/put/PutRepositoryRequest.java index 0046037c87d..fa5ed48d3d6 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/cluster/repositories/put/PutRepositoryRequest.java +++ b/core/src/main/java/org/elasticsearch/action/admin/cluster/repositories/put/PutRepositoryRequest.java @@ -20,7 +20,6 @@ package org.elasticsearch.action.admin.cluster.repositories.put; import org.elasticsearch.ElasticsearchGenerationException; - import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.support.master.AcknowledgedRequest; import org.elasticsearch.common.bytes.BytesReference; @@ -29,6 +28,7 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import java.io.IOException; @@ -231,8 +231,8 @@ public class PutRepositoryRequest extends AcknowledgedRequest 0) { - try { - return source(XContentFactory.xContent(source, offset, length).createParser(source, offset, length).mapOrderedAndClose()); + try (XContentParser parser = XContentFactory.xContent(source, offset, length).createParser(source, offset, length)) { + return source(parser.mapOrdered()); } catch (IOException e) { throw new IllegalArgumentException("failed to parse repository source", e); } @@ -447,8 +448,8 @@ public class CreateSnapshotRequest extends MasterNodeRequest 0) { - try { - return source(XContentFactory.xContent(source, offset, length).createParser(source, offset, length).mapOrderedAndClose()); + try (XContentParser parser = XContentFactory.xContent(source, offset, length).createParser(source, offset, length)) { + return source(parser.mapOrdered()); } catch (IOException e) { throw new IllegalArgumentException("failed to parse repository source", e); } @@ -604,8 +605,8 @@ public class RestoreSnapshotRequest extends MasterNodeRequest public CreateIndexRequest source(BytesReference source) { XContentType xContentType = XContentFactory.xContentType(source); if (xContentType != null) { - try { - source(XContentFactory.xContent(xContentType).createParser(source).mapAndClose()); + try (XContentParser parser = XContentFactory.xContent(xContentType).createParser(source)) { + source(parser.map()); } catch (IOException e) { throw new ElasticsearchParseException("failed to parse source for create index", e); } diff --git a/core/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java b/core/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java index a4b10cb7783..5477c681849 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java +++ b/core/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java @@ -308,8 +308,8 @@ public class PutIndexTemplateRequest extends MasterNodeRequest cursor : indexTemplateMetaData.mappings()) { byte[] data = cursor.value.uncompressed(); - XContentParser parser = XContentFactory.xContent(data).createParser(data); - Map mapping = parser.mapOrderedAndClose(); - builder.map(mapping); + try (XContentParser parser = XContentFactory.xContent(data).createParser(data)) { + Map mapping = parser.mapOrdered(); + builder.map(mapping); + } } builder.endArray(); } diff --git a/core/src/main/java/org/elasticsearch/cluster/metadata/MappingMetaData.java b/core/src/main/java/org/elasticsearch/cluster/metadata/MappingMetaData.java index f3cde99079f..468029a2b51 100644 --- a/core/src/main/java/org/elasticsearch/cluster/metadata/MappingMetaData.java +++ b/core/src/main/java/org/elasticsearch/cluster/metadata/MappingMetaData.java @@ -288,7 +288,10 @@ public class MappingMetaData extends AbstractDiffable { public MappingMetaData(CompressedXContent mapping) throws IOException { this.source = mapping; - Map mappingMap = XContentHelper.createParser(mapping.compressedReference()).mapOrderedAndClose(); + Map mappingMap; + try (XContentParser parser = XContentHelper.createParser(mapping.compressedReference())) { + mappingMap = parser.mapOrdered(); + } if (mappingMap.size() != 1) { throw new IllegalStateException("Can't derive type from mapping, no root type: " + mapping.string()); } diff --git a/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java b/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java index b53e218f216..83136b392bb 100644 --- a/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java +++ b/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java @@ -24,6 +24,7 @@ import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import com.google.common.base.Charsets; import com.google.common.collect.Lists; import com.google.common.collect.Maps; + import org.apache.lucene.util.CollectionUtil; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.Version; @@ -53,6 +54,7 @@ import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.index.Index; import org.elasticsearch.index.mapper.DocumentMapper; @@ -451,7 +453,9 @@ public class MetaDataCreateIndexService extends AbstractComponent { } private Map parseMapping(String mappingSource) throws Exception { - return XContentFactory.xContent(mappingSource).createParser(mappingSource).mapAndClose(); + try (XContentParser parser = XContentFactory.xContent(mappingSource).createParser(mappingSource)) { + return parser.map(); + } } private void addMappings(Map> mappings, Path mappingsDir) throws IOException { diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java b/core/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java index 4efd18e8fa9..3488231ac49 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java @@ -64,8 +64,8 @@ public class XContentHelper { public static Tuple> convertToMap(BytesReference bytes, boolean ordered) throws ElasticsearchParseException { try { - XContentParser parser; XContentType contentType; + InputStream input; Compressor compressor = CompressorFactory.compressor(bytes); if (compressor != null) { InputStream compressedStreamInput = compressor.streamInput(bytes.streamInput()); @@ -73,15 +73,17 @@ public class XContentHelper { compressedStreamInput = new BufferedInputStream(compressedStreamInput); } contentType = XContentFactory.xContentType(compressedStreamInput); - parser = XContentFactory.xContent(contentType).createParser(compressedStreamInput); + input = compressedStreamInput; } else { contentType = XContentFactory.xContentType(bytes); - parser = XContentFactory.xContent(contentType).createParser(bytes.streamInput()); + input = bytes.streamInput(); } - if (ordered) { - return Tuple.tuple(contentType, parser.mapOrderedAndClose()); - } else { - return Tuple.tuple(contentType, parser.mapAndClose()); + try (XContentParser parser = XContentFactory.xContent(contentType).createParser(input)) { + if (ordered) { + return Tuple.tuple(contentType, parser.mapOrdered()); + } else { + return Tuple.tuple(contentType, parser.map()); + } } } catch (IOException e) { throw new ElasticsearchParseException("Failed to parse content to map", e); diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/XContentParser.java b/core/src/main/java/org/elasticsearch/common/xcontent/XContentParser.java index 738fd9f6e72..b43804e1332 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/XContentParser.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/XContentParser.java @@ -130,10 +130,6 @@ public interface XContentParser extends Releasable { Map mapOrdered() throws IOException; - Map mapAndClose() throws IOException; - - Map mapOrderedAndClose() throws IOException; - String text() throws IOException; String textOrNull() throws IOException; diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/support/AbstractXContentParser.java b/core/src/main/java/org/elasticsearch/common/xcontent/support/AbstractXContentParser.java index 066504c60d7..77eae8b6b50 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/support/AbstractXContentParser.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/support/AbstractXContentParser.java @@ -213,25 +213,6 @@ public abstract class AbstractXContentParser implements XContentParser { return readOrderedMap(this); } - @Override - public Map mapAndClose() throws IOException { - try { - return map(); - } finally { - close(); - } - } - - @Override - public Map mapOrderedAndClose() throws IOException { - try { - return mapOrdered(); - } finally { - close(); - } - } - - static interface MapFactory { Map newMap(); } diff --git a/core/src/main/java/org/elasticsearch/discovery/zen/ping/multicast/MulticastZenPing.java b/core/src/main/java/org/elasticsearch/discovery/zen/ping/multicast/MulticastZenPing.java index 7cf14ad91e1..97f872c3108 100644 --- a/core/src/main/java/org/elasticsearch/discovery/zen/ping/multicast/MulticastZenPing.java +++ b/core/src/main/java/org/elasticsearch/discovery/zen/ping/multicast/MulticastZenPing.java @@ -39,6 +39,7 @@ import org.elasticsearch.common.util.concurrent.AbstractRunnable; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.discovery.zen.ping.PingContextProvider; import org.elasticsearch.discovery.zen.ping.ZenPing; @@ -408,9 +409,9 @@ public class MulticastZenPing extends AbstractLifecycleComponent implem xContentType = XContentFactory.xContentType(data); if (xContentType != null) { // an external ping - externalPingData = XContentFactory.xContent(xContentType) - .createParser(data) - .mapAndClose(); + try (XContentParser parser = XContentFactory.xContent(xContentType).createParser(data)) { + externalPingData = parser.map(); + } } else { throw new IllegalStateException("failed multicast message, probably message from previous version"); } diff --git a/core/src/main/java/org/elasticsearch/index/mapper/DocumentMapperParser.java b/core/src/main/java/org/elasticsearch/index/mapper/DocumentMapperParser.java index d5a3ff1f9ad..31c5d476ef8 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/DocumentMapperParser.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/DocumentMapperParser.java @@ -34,6 +34,7 @@ import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.AbstractIndexComponent; import org.elasticsearch.index.Index; import org.elasticsearch.index.analysis.AnalysisService; @@ -304,8 +305,8 @@ public class DocumentMapperParser extends AbstractIndexComponent { private Tuple> extractMapping(String type, String source) throws MapperParsingException { Map root; - try { - root = XContentFactory.xContent(source).createParser(source).mapOrderedAndClose(); + try (XContentParser parser = XContentFactory.xContent(source).createParser(source)) { + root = parser.mapOrdered(); } catch (Exception e) { throw new MapperParsingException("failed to parse mapping definition", e); } diff --git a/core/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java b/core/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java index 79ca3982442..478b7489a83 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java @@ -679,7 +679,10 @@ class DocumentParser implements Closeable { } private static XContentParser transform(Mapping mapping, XContentParser parser) throws IOException { - Map transformed = transformSourceAsMap(mapping, parser.mapOrderedAndClose()); + Map transformed; + try (XContentParser _ = parser) { + transformed = transformSourceAsMap(mapping, parser.mapOrdered()); + } XContentBuilder builder = XContentFactory.contentBuilder(parser.contentType()).value(transformed); return parser.contentType().xContent().createParser(builder.bytes()); } diff --git a/core/src/main/java/org/elasticsearch/rest/action/admin/cluster/settings/RestClusterUpdateSettingsAction.java b/core/src/main/java/org/elasticsearch/rest/action/admin/cluster/settings/RestClusterUpdateSettingsAction.java index 8536c037e89..b30f6848f30 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/admin/cluster/settings/RestClusterUpdateSettingsAction.java +++ b/core/src/main/java/org/elasticsearch/rest/action/admin/cluster/settings/RestClusterUpdateSettingsAction.java @@ -27,6 +27,7 @@ import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.rest.*; import org.elasticsearch.rest.action.support.AcknowledgedRestListener; @@ -48,7 +49,10 @@ public class RestClusterUpdateSettingsAction extends BaseRestHandler { final ClusterUpdateSettingsRequest clusterUpdateSettingsRequest = Requests.clusterUpdateSettingsRequest(); clusterUpdateSettingsRequest.timeout(request.paramAsTime("timeout", clusterUpdateSettingsRequest.timeout())); clusterUpdateSettingsRequest.masterNodeTimeout(request.paramAsTime("master_timeout", clusterUpdateSettingsRequest.masterNodeTimeout())); - Map source = XContentFactory.xContent(request.content()).createParser(request.content()).mapAndClose(); + Map source; + try (XContentParser parser = XContentFactory.xContent(request.content()).createParser(request.content())) { + source = parser.map(); + } if (source.containsKey("transient")) { clusterUpdateSettingsRequest.transientSettings((Map) source.get("transient")); } diff --git a/core/src/main/java/org/elasticsearch/search/warmer/IndexWarmersMetaData.java b/core/src/main/java/org/elasticsearch/search/warmer/IndexWarmersMetaData.java index ef1ef44ffb9..1a0731bc361 100644 --- a/core/src/main/java/org/elasticsearch/search/warmer/IndexWarmersMetaData.java +++ b/core/src/main/java/org/elasticsearch/search/warmer/IndexWarmersMetaData.java @@ -248,7 +248,10 @@ public class IndexWarmersMetaData extends AbstractDiffable if (binary) { builder.value(entry.source()); } else { - Map mapping = XContentFactory.xContent(entry.source()).createParser(entry.source()).mapOrderedAndClose(); + Map mapping; + try (XContentParser parser = XContentFactory.xContent(entry.source()).createParser(entry.source())) { + mapping = parser.mapOrdered(); + } builder.map(mapping); } builder.endObject(); diff --git a/core/src/test/java/org/elasticsearch/common/xcontent/support/XContentMapValuesTests.java b/core/src/test/java/org/elasticsearch/common/xcontent/support/XContentMapValuesTests.java index 056b9964a87..b449c10674c 100644 --- a/core/src/test/java/org/elasticsearch/common/xcontent/support/XContentMapValuesTests.java +++ b/core/src/test/java/org/elasticsearch/common/xcontent/support/XContentMapValuesTests.java @@ -24,6 +24,7 @@ import org.elasticsearch.common.collect.Tuple; 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.test.ElasticsearchTestCase; import org.hamcrest.Matchers; @@ -49,7 +50,10 @@ public class XContentMapValuesTests extends ElasticsearchTestCase { .field("something_else", "value3") .endObject(); - Map source = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); + Map source; + try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) { + source = parser.map(); + } Map filter = XContentMapValues.filter(source, new String[]{"test1"}, Strings.EMPTY_ARRAY); assertThat(filter.size(), equalTo(1)); assertThat(filter.get("test1").toString(), equalTo("value1")); @@ -75,7 +79,9 @@ public class XContentMapValuesTests extends ElasticsearchTestCase { .field("test1", "value1") .endObject(); - source = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); + try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) { + source = parser.map(); + } filter = XContentMapValues.filter(source, new String[]{"path1"}, Strings.EMPTY_ARRAY); assertThat(filter.size(), equalTo(1)); @@ -99,7 +105,10 @@ public class XContentMapValuesTests extends ElasticsearchTestCase { .field("test", "value") .endObject(); - Map map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); + Map map; + try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) { + map = parser.map(); + } assertThat(XContentMapValues.extractValue("test", map).toString(), equalTo("value")); assertThat(XContentMapValues.extractValue("test.me", map), nullValue()); assertThat(XContentMapValues.extractValue("something.else.2", map), nullValue()); @@ -108,7 +117,9 @@ public class XContentMapValuesTests extends ElasticsearchTestCase { .startObject("path1").startObject("path2").field("test", "value").endObject().endObject() .endObject(); - map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); + try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) { + map = parser.map(); + } assertThat(XContentMapValues.extractValue("path1.path2.test", map).toString(), equalTo("value")); assertThat(XContentMapValues.extractValue("path1.path2.test_me", map), nullValue()); assertThat(XContentMapValues.extractValue("path1.non_path2.test", map), nullValue()); @@ -128,7 +139,9 @@ public class XContentMapValuesTests extends ElasticsearchTestCase { .startObject("path1").field("test", "value1", "value2").endObject() .endObject(); - map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); + try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) { + map = parser.map(); + } extValue = XContentMapValues.extractValue("path1.test", map); assertThat(extValue, instanceOf(List.class)); @@ -145,7 +158,9 @@ public class XContentMapValuesTests extends ElasticsearchTestCase { .endObject() .endObject(); - map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); + try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) { + map = parser.map(); + } extValue = XContentMapValues.extractValue("path1.path2.test", map); assertThat(extValue, instanceOf(List.class)); @@ -159,14 +174,18 @@ public class XContentMapValuesTests extends ElasticsearchTestCase { builder = XContentFactory.jsonBuilder().startObject() .field("xxx.yyy", "value") .endObject(); - map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); + try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) { + map = parser.map(); + } assertThat(XContentMapValues.extractValue("xxx.yyy", map).toString(), equalTo("value")); builder = XContentFactory.jsonBuilder().startObject() .startObject("path1.xxx").startObject("path2.yyy").field("test", "value").endObject().endObject() .endObject(); - map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); + try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) { + map = parser.map(); + } assertThat(XContentMapValues.extractValue("path1.xxx.path2.yyy.test", map).toString(), equalTo("value")); } @@ -177,28 +196,37 @@ public class XContentMapValuesTests extends ElasticsearchTestCase { .field("test", "value") .endObject(); - Map map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); + Map map; + try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) { + map = parser.map(); + } assertThat(XContentMapValues.extractRawValues("test", map).get(0).toString(), equalTo("value")); builder = XContentFactory.jsonBuilder().startObject() .field("test.me", "value") .endObject(); - map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); + try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) { + map = parser.map(); + } assertThat(XContentMapValues.extractRawValues("test.me", map).get(0).toString(), equalTo("value")); builder = XContentFactory.jsonBuilder().startObject() .startObject("path1").startObject("path2").field("test", "value").endObject().endObject() .endObject(); - map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); + try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) { + map = parser.map(); + } assertThat(XContentMapValues.extractRawValues("path1.path2.test", map).get(0).toString(), equalTo("value")); builder = XContentFactory.jsonBuilder().startObject() .startObject("path1.xxx").startObject("path2.yyy").field("test", "value").endObject().endObject() .endObject(); - map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); + try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) { + map = parser.map(); + } assertThat(XContentMapValues.extractRawValues("path1.xxx.path2.yyy.test", map).get(0).toString(), equalTo("value")); } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/completion/CompletionFieldMapperTests.java b/core/src/test/java/org/elasticsearch/index/mapper/completion/CompletionFieldMapperTests.java index dfc96244389..e0031cf1d04 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/completion/CompletionFieldMapperTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/completion/CompletionFieldMapperTests.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.mapper.completion; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.FieldMapper; @@ -76,7 +77,10 @@ public class CompletionFieldMapperTests extends ElasticsearchSingleNodeTest { XContentBuilder builder = jsonBuilder().startObject(); completionFieldMapper.toXContent(builder, null).endObject(); builder.close(); - Map serializedMap = JsonXContent.jsonXContent.createParser(builder.bytes()).mapAndClose(); + Map serializedMap; + try (XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes())) { + serializedMap = parser.map(); + } Map configMap = (Map) serializedMap.get("completion"); assertThat(configMap.get("analyzer").toString(), is("simple")); assertThat(configMap.get("search_analyzer").toString(), is("standard")); @@ -105,7 +109,10 @@ public class CompletionFieldMapperTests extends ElasticsearchSingleNodeTest { XContentBuilder builder = jsonBuilder().startObject(); completionFieldMapper.toXContent(builder, null).endObject(); builder.close(); - Map serializedMap = JsonXContent.jsonXContent.createParser(builder.bytes()).mapAndClose(); + Map serializedMap; + try (XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes())) { + serializedMap = parser.map(); + } Map configMap = (Map) serializedMap.get("completion"); assertThat(configMap.get("analyzer").toString(), is("simple")); } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/copyto/CopyToMapperTests.java b/core/src/test/java/org/elasticsearch/index/mapper/copyto/CopyToMapperTests.java index 116bf9cfdfa..6832a6e9e85 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/copyto/CopyToMapperTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/copyto/CopyToMapperTests.java @@ -26,6 +26,7 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.mapper.DocumentMapper; @@ -91,7 +92,10 @@ public class CopyToMapperTests extends ElasticsearchSingleNodeTest { XContentBuilder builder = jsonBuilder().startObject(); stringFieldMapper.toXContent(builder, ToXContent.EMPTY_PARAMS).endObject(); builder.close(); - Map serializedMap = JsonXContent.jsonXContent.createParser(builder.bytes()).mapAndClose(); + Map serializedMap; + try (XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes())) { + serializedMap = parser.map(); + } Map copyTestMap = (Map) serializedMap.get("copy_test"); assertThat(copyTestMap.get("type").toString(), is("string")); List copyToList = (List) copyTestMap.get("copy_to"); diff --git a/core/src/test/java/org/elasticsearch/index/mapper/date/SimpleDateMappingTests.java b/core/src/test/java/org/elasticsearch/index/mapper/date/SimpleDateMappingTests.java index a9c075b0812..74a39addfe3 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/date/SimpleDateMappingTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/date/SimpleDateMappingTests.java @@ -32,6 +32,7 @@ import org.elasticsearch.common.util.LocaleUtils; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.mapper.*; @@ -380,7 +381,10 @@ public class SimpleDateMappingTests extends ElasticsearchSingleNodeTest { private Map getConfigurationViaXContent(DateFieldMapper dateFieldMapper) throws IOException { XContentBuilder builder = JsonXContent.contentBuilder().startObject(); dateFieldMapper.toXContent(builder, ToXContent.EMPTY_PARAMS).endObject(); - Map dateFieldMapperMap = JsonXContent.jsonXContent.createParser(builder.string()).mapAndClose(); + Map dateFieldMapperMap; + try (XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes())) { + dateFieldMapperMap = parser.map(); + } assertThat(dateFieldMapperMap, hasKey("field")); assertThat(dateFieldMapperMap.get("field"), is(instanceOf(Map.class))); return (Map) dateFieldMapperMap.get("field"); diff --git a/core/src/test/java/org/elasticsearch/index/mapper/routing/RoutingTypeMapperTests.java b/core/src/test/java/org/elasticsearch/index/mapper/routing/RoutingTypeMapperTests.java index d4acc74dbb6..4076842616f 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/routing/RoutingTypeMapperTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/routing/RoutingTypeMapperTests.java @@ -29,6 +29,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.ParsedDocument; @@ -79,7 +80,10 @@ public class RoutingTypeMapperTests extends ElasticsearchSingleNodeTest { XContentBuilder builder = JsonXContent.contentBuilder().startObject(); enabledMapper.routingFieldMapper().toXContent(builder, ToXContent.EMPTY_PARAMS).endObject(); builder.close(); - Map serializedMap = JsonXContent.jsonXContent.createParser(builder.bytes()).mapAndClose(); + Map serializedMap; + try (XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes())) { + serializedMap = parser.map(); + } assertThat(serializedMap, hasKey("_routing")); assertThat(serializedMap.get("_routing"), instanceOf(Map.class)); Map routingConfiguration = (Map) serializedMap.get("_routing"); diff --git a/core/src/test/java/org/elasticsearch/index/mapper/source/DefaultSourceMappingTests.java b/core/src/test/java/org/elasticsearch/index/mapper/source/DefaultSourceMappingTests.java index 10f33c9025d..2a3a30a8f77 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/source/DefaultSourceMappingTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/source/DefaultSourceMappingTests.java @@ -27,6 +27,7 @@ import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.compress.CompressorFactory; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.mapper.*; import org.elasticsearch.test.ElasticsearchSingleNodeTest; @@ -122,7 +123,10 @@ public class DefaultSourceMappingTests extends ElasticsearchSingleNodeTest { .endObject().bytes()); IndexableField sourceField = doc.rootDoc().getField("_source"); - Map sourceAsMap = XContentFactory.xContent(XContentType.JSON).createParser(new BytesArray(sourceField.binaryValue())).mapAndClose(); + Map sourceAsMap; + try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(new BytesArray(sourceField.binaryValue()))) { + sourceAsMap = parser.map(); + } assertThat(sourceAsMap.containsKey("path1"), equalTo(true)); assertThat(sourceAsMap.containsKey("path2"), equalTo(false)); } @@ -140,7 +144,10 @@ public class DefaultSourceMappingTests extends ElasticsearchSingleNodeTest { .endObject().bytes()); IndexableField sourceField = doc.rootDoc().getField("_source"); - Map sourceAsMap = XContentFactory.xContent(XContentType.JSON).createParser(new BytesArray(sourceField.binaryValue())).mapAndClose(); + Map sourceAsMap; + try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(new BytesArray(sourceField.binaryValue()))) { + sourceAsMap = parser.map(); + } assertThat(sourceAsMap.containsKey("path1"), equalTo(false)); assertThat(sourceAsMap.containsKey("path2"), equalTo(true)); } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/string/SimpleStringMappingTests.java b/core/src/test/java/org/elasticsearch/index/mapper/string/SimpleStringMappingTests.java index d0e758b81a6..3074c4a3228 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/string/SimpleStringMappingTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/string/SimpleStringMappingTests.java @@ -34,6 +34,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.fielddata.FieldDataType; @@ -286,7 +287,10 @@ public class SimpleStringMappingTests extends ElasticsearchSingleNodeTest { fieldMapper.toXContent(builder, ToXContent.EMPTY_PARAMS).endObject(); builder.close(); - Map fieldMap = JsonXContent.jsonXContent.createParser(builder.bytes()).mapAndClose(); + Map fieldMap; + try (XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes())) { + fieldMap = parser.map(); + } @SuppressWarnings("unchecked") Map result = (Map) fieldMap.get(fieldName); return result; diff --git a/core/src/test/java/org/elasticsearch/index/mapper/timestamp/TimestampMappingTests.java b/core/src/test/java/org/elasticsearch/index/mapper/timestamp/TimestampMappingTests.java index e8b0ce2db7b..6b3b3a8fa81 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/timestamp/TimestampMappingTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/timestamp/TimestampMappingTests.java @@ -35,6 +35,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.Index; import org.elasticsearch.index.mapper.DocumentMapper; @@ -168,7 +169,10 @@ public class TimestampMappingTests extends ElasticsearchSingleNodeTest { XContentBuilder builder = JsonXContent.contentBuilder().startObject(); enabledMapper.timestampFieldMapper().toXContent(builder, ToXContent.EMPTY_PARAMS).endObject(); builder.close(); - Map serializedMap = JsonXContent.jsonXContent.createParser(builder.bytes()).mapAndClose(); + Map serializedMap; + try (XContentParser parser = JsonXContent.jsonXContent.createParser(builder.bytes())) { + serializedMap = parser.map(); + } assertThat(serializedMap, hasKey("_timestamp")); assertThat(serializedMap.get("_timestamp"), instanceOf(Map.class)); Map timestampConfiguration = (Map) serializedMap.get("_timestamp"); diff --git a/core/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTest.java b/core/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTest.java index 42ecd47ca0c..378e45058c6 100644 --- a/core/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTest.java +++ b/core/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTest.java @@ -18,6 +18,7 @@ */ package org.elasticsearch.search.builder; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.test.ElasticsearchTestCase; import org.junit.Test; @@ -78,7 +79,10 @@ public class SearchSourceBuilderTest extends ElasticsearchTestCase { } private Map getSourceMap(SearchSourceBuilder builder) throws IOException { - Map data = JsonXContent.jsonXContent.createParser(builder.toString()).mapAndClose(); + Map data; + try (XContentParser parser = JsonXContent.jsonXContent.createParser(builder.toString())) { + data = parser.map(); + } assertThat(data, hasKey("_source")); return (Map) data.get("_source"); } diff --git a/core/src/test/java/org/elasticsearch/test/rest/json/JsonPath.java b/core/src/test/java/org/elasticsearch/test/rest/json/JsonPath.java index e2f1b7a3b28..567e849a391 100644 --- a/core/src/test/java/org/elasticsearch/test/rest/json/JsonPath.java +++ b/core/src/test/java/org/elasticsearch/test/rest/json/JsonPath.java @@ -19,6 +19,8 @@ package org.elasticsearch.test.rest.json; import com.google.common.collect.Lists; + +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.test.rest.Stash; @@ -40,7 +42,9 @@ public class JsonPath { } private static Map convertToMap(String json) throws IOException { - return JsonXContent.jsonXContent.createParser(json).mapOrderedAndClose(); + try (XContentParser parser = JsonXContent.jsonXContent.createParser(json)) { + return parser.mapOrdered(); + } } /** diff --git a/core/src/test/java/org/elasticsearch/test/rest/test/DoSectionParserTests.java b/core/src/test/java/org/elasticsearch/test/rest/test/DoSectionParserTests.java index 911bb8999ba..e48dd5e908e 100644 --- a/core/src/test/java/org/elasticsearch/test/rest/test/DoSectionParserTests.java +++ b/core/src/test/java/org/elasticsearch/test/rest/test/DoSectionParserTests.java @@ -18,6 +18,7 @@ */ package org.elasticsearch.test.rest.test; +import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.yaml.YamlXContent; import org.elasticsearch.test.rest.parser.DoSectionParser; @@ -387,7 +388,10 @@ public class DoSectionParserTests extends AbstractParserTests { } private static void assertJsonEquals(Map actual, String expected) throws IOException { - Map expectedMap = JsonXContent.jsonXContent.createParser(expected).mapOrderedAndClose(); + Map expectedMap; + try (XContentParser parser = JsonXContent.jsonXContent.createParser(expected)) { + expectedMap = parser.mapOrdered(); + } MatcherAssert.assertThat(actual, equalTo(expectedMap)); } } diff --git a/core/src/test/java/org/elasticsearch/threadpool/SimpleThreadPoolTests.java b/core/src/test/java/org/elasticsearch/threadpool/SimpleThreadPoolTests.java index 2e4ef0fe209..dc223deba0f 100644 --- a/core/src/test/java/org/elasticsearch/threadpool/SimpleThreadPoolTests.java +++ b/core/src/test/java/org/elasticsearch/threadpool/SimpleThreadPoolTests.java @@ -215,8 +215,10 @@ public class SimpleThreadPoolTests extends ElasticsearchIntegrationTest { info.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); builder.close(); - XContentParser parser = JsonXContent.jsonXContent.createParser(builder.string()); - Map poolsMap = parser.mapAndClose(); + Map poolsMap; + try (XContentParser parser = JsonXContent.jsonXContent.createParser(builder.string())) { + poolsMap = parser.map(); + } return (Map) ((Map) poolsMap.get("thread_pool")).get(poolName); } diff --git a/core/src/test/java/org/elasticsearch/threadpool/ThreadPoolSerializationTests.java b/core/src/test/java/org/elasticsearch/threadpool/ThreadPoolSerializationTests.java index 1984406306f..632f37eff65 100644 --- a/core/src/test/java/org/elasticsearch/threadpool/ThreadPoolSerializationTests.java +++ b/core/src/test/java/org/elasticsearch/threadpool/ThreadPoolSerializationTests.java @@ -82,8 +82,10 @@ public class ThreadPoolSerializationTests extends ElasticsearchTestCase { builder.endObject(); BytesReference bytesReference = builder.bytes(); - XContentParser parser = XContentFactory.xContent(bytesReference).createParser(bytesReference); - Map map = parser.mapAndClose(); + Map map; + try (XContentParser parser = XContentFactory.xContent(bytesReference).createParser(bytesReference)) { + map = parser.map(); + } assertThat(map, hasKey("foo")); map = (Map) map.get("foo"); assertThat(map, hasKey("queue_size"));