From e6303302e689ae495144faa0c4cf2221fa15180e Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Fri, 2 Oct 2015 00:40:38 +0200 Subject: [PATCH] Remove more ImmutableMap#of TODO - look for places where CopyOnWriteHashMap will do. --- .../similarity/SimilarityLookupService.java | 14 ++++++++------ .../search/aggregations/AggregationStreams.java | 11 ++++------- .../aggregations/InternalAggregations.java | 16 +++++++++------- .../aggregations/bucket/BucketStreams.java | 13 +++++-------- .../pipeline/PipelineAggregatorStreams.java | 11 +++-------- 5 files changed, 29 insertions(+), 36 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/index/similarity/SimilarityLookupService.java b/core/src/main/java/org/elasticsearch/index/similarity/SimilarityLookupService.java index 8b8b68812cb..903f0ea4039 100644 --- a/core/src/main/java/org/elasticsearch/index/similarity/SimilarityLookupService.java +++ b/core/src/main/java/org/elasticsearch/index/similarity/SimilarityLookupService.java @@ -19,16 +19,18 @@ package org.elasticsearch.index.similarity; -import com.google.common.collect.ImmutableMap; -import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.AbstractIndexComponent; import org.elasticsearch.index.Index; import org.elasticsearch.index.settings.IndexSettings; +import java.util.HashMap; import java.util.Map; +import static java.util.Collections.emptyMap; +import static java.util.Collections.unmodifiableMap; + /** * Service for looking up configured {@link SimilarityProvider} implementations by name. *

@@ -39,17 +41,17 @@ public class SimilarityLookupService extends AbstractIndexComponent { public final static String DEFAULT_SIMILARITY = "default"; - private final ImmutableMap similarities; + private final Map similarities; public SimilarityLookupService(Index index, Settings indexSettings) { - this(index, indexSettings, ImmutableMap.of()); + this(index, indexSettings, emptyMap()); } @Inject public SimilarityLookupService(Index index, @IndexSettings Settings indexSettings, Map similarities) { super(index, indexSettings); - MapBuilder providers = MapBuilder.newMapBuilder(); + Map providers = new HashMap<>(); Map similaritySettings = indexSettings.getGroups(SimilarityModule.SIMILARITY_SETTINGS_PREFIX); for (Map.Entry entry : similarities.entrySet()) { @@ -70,7 +72,7 @@ public class SimilarityLookupService extends AbstractIndexComponent { } } - this.similarities = providers.immutableMap(); + this.similarities = unmodifiableMap(providers); } /** diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/AggregationStreams.java b/core/src/main/java/org/elasticsearch/search/aggregations/AggregationStreams.java index 97985f3c06e..f4715495c0a 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/AggregationStreams.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/AggregationStreams.java @@ -18,20 +18,19 @@ */ package org.elasticsearch.search.aggregations; -import com.google.common.collect.ImmutableMap; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.common.collect.MapBuilder; +import org.elasticsearch.common.collect.CopyOnWriteHashMap; import org.elasticsearch.common.io.stream.StreamInput; import java.io.IOException; +import java.util.Map; /** * A registry for all the dedicated streams in the aggregation module. This is to support dynamic addAggregation that * know how to stream themselves. */ public class AggregationStreams { - - private static ImmutableMap streams = ImmutableMap.of(); + private static Map streams = new CopyOnWriteHashMap<>(); /** * A stream that knows how to read an aggregation from the input. @@ -47,11 +46,9 @@ public class AggregationStreams { * @param types The types associated with the streams */ public static synchronized void registerStream(Stream stream, BytesReference... types) { - MapBuilder uStreams = MapBuilder.newMapBuilder(streams); for (BytesReference type : types) { - uStreams.put(type, stream); + streams.put(type, stream); } - streams = uStreams.immutableMap(); } /** diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/InternalAggregations.java b/core/src/main/java/org/elasticsearch/search/aggregations/InternalAggregations.java index 429768055f0..3841030240e 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/InternalAggregations.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/InternalAggregations.java @@ -18,7 +18,6 @@ */ package org.elasticsearch.search.aggregations; -import com.google.common.collect.ImmutableMap; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -37,6 +36,9 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.stream.Collectors; + +import static java.util.Collections.emptyMap; +import static java.util.Collections.unmodifiableMap; /** * An internal implementation of {@link Aggregations}. */ @@ -46,7 +48,7 @@ public class InternalAggregations implements Aggregations, ToXContent, Streamabl private List aggregations = Collections.emptyList(); - private Map aggregationsAsMap; + private Map aggregationsAsMap; private InternalAggregations() { } @@ -88,13 +90,13 @@ public class InternalAggregations implements Aggregations, ToXContent, Streamabl @Override public Map getAsMap() { if (aggregationsAsMap == null) { - Map aggregationsAsMap = new HashMap<>(); + Map newAggregationsAsMap = new HashMap<>(); for (InternalAggregation aggregation : aggregations) { - aggregationsAsMap.put(aggregation.getName(), aggregation); + newAggregationsAsMap.put(aggregation.getName(), aggregation); } - this.aggregationsAsMap = aggregationsAsMap; + this.aggregationsAsMap = unmodifiableMap(newAggregationsAsMap); } - return new HashMap<>(aggregationsAsMap); + return aggregationsAsMap; } /** @@ -200,7 +202,7 @@ public class InternalAggregations implements Aggregations, ToXContent, Streamabl int size = in.readVInt(); if (size == 0) { aggregations = Collections.emptyList(); - aggregationsAsMap = ImmutableMap.of(); + aggregationsAsMap = emptyMap(); } else { aggregations = new ArrayList<>(size); for (int i = 0; i < size; i++) { diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/BucketStreams.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/BucketStreams.java index aa489e064ed..4567547dac8 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/BucketStreams.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/BucketStreams.java @@ -19,16 +19,15 @@ package org.elasticsearch.search.aggregations.bucket; -import com.google.common.collect.ImmutableMap; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.common.collect.MapBuilder; +import org.elasticsearch.common.collect.CopyOnWriteHashMap; import org.elasticsearch.common.io.stream.StreamInput; import java.io.IOException; +import java.util.Map; public class BucketStreams { - - private static ImmutableMap STREAMS = ImmutableMap.of(); + private static final Map streams = new CopyOnWriteHashMap<>(); /** * A stream that knows how to read a bucket from the input. @@ -45,11 +44,9 @@ public class BucketStreams { * @param types The types associated with the streams */ public static synchronized void registerStream(Stream stream, BytesReference... types) { - MapBuilder uStreams = MapBuilder.newMapBuilder(STREAMS); for (BytesReference type : types) { - uStreams.put(type, stream); + streams.put(type, stream); } - STREAMS = uStreams.immutableMap(); } /** @@ -59,7 +56,7 @@ public class BucketStreams { * @return The associated stream */ public static Stream stream(BytesReference type) { - return STREAMS.get(type); + return streams.get(type); } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/PipelineAggregatorStreams.java b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/PipelineAggregatorStreams.java index 71046092879..5b27569d8a7 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/PipelineAggregatorStreams.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/pipeline/PipelineAggregatorStreams.java @@ -19,21 +19,18 @@ package org.elasticsearch.search.aggregations.pipeline; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.collect.CopyOnWriteHashMap; import org.elasticsearch.common.io.stream.StreamInput; import java.io.IOException; -import java.util.HashMap; import java.util.Map; -import static java.util.Collections.emptyMap; -import static java.util.Collections.unmodifiableMap; - /** * A registry for all the dedicated streams in the aggregation module. This is to support dynamic addAggregation that * know how to stream themselves. */ public class PipelineAggregatorStreams { - private static Map streams = emptyMap(); + private static Map streams = new CopyOnWriteHashMap<>(); /** * A stream that knows how to read an aggregation from the input. @@ -49,11 +46,9 @@ public class PipelineAggregatorStreams { * @param types The types associated with the streams */ public static synchronized void registerStream(Stream stream, BytesReference... types) { - Map newStreams = new HashMap<>(streams); for (BytesReference type : types) { - newStreams.put(type, stream); + streams.put(type, stream); } - streams = unmodifiableMap(newStreams); } /**