Remove more ImmutableMap#of

TODO - look for places where CopyOnWriteHashMap will do.
This commit is contained in:
Nik Everett 2015-10-02 00:40:38 +02:00
parent 9430e17f70
commit e6303302e6
5 changed files with 29 additions and 36 deletions

View File

@ -19,16 +19,18 @@
package org.elasticsearch.index.similarity; 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.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.AbstractIndexComponent; import org.elasticsearch.index.AbstractIndexComponent;
import org.elasticsearch.index.Index; import org.elasticsearch.index.Index;
import org.elasticsearch.index.settings.IndexSettings; import org.elasticsearch.index.settings.IndexSettings;
import java.util.HashMap;
import java.util.Map; 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. * Service for looking up configured {@link SimilarityProvider} implementations by name.
* <p> * <p>
@ -39,17 +41,17 @@ public class SimilarityLookupService extends AbstractIndexComponent {
public final static String DEFAULT_SIMILARITY = "default"; public final static String DEFAULT_SIMILARITY = "default";
private final ImmutableMap<String, SimilarityProvider> similarities; private final Map<String, SimilarityProvider> similarities;
public SimilarityLookupService(Index index, Settings indexSettings) { public SimilarityLookupService(Index index, Settings indexSettings) {
this(index, indexSettings, ImmutableMap.<String, SimilarityProvider.Factory>of()); this(index, indexSettings, emptyMap());
} }
@Inject @Inject
public SimilarityLookupService(Index index, @IndexSettings Settings indexSettings, Map<String, SimilarityProvider.Factory> similarities) { public SimilarityLookupService(Index index, @IndexSettings Settings indexSettings, Map<String, SimilarityProvider.Factory> similarities) {
super(index, indexSettings); super(index, indexSettings);
MapBuilder<String, SimilarityProvider> providers = MapBuilder.newMapBuilder(); Map<String, SimilarityProvider> providers = new HashMap<>();
Map<String, Settings> similaritySettings = indexSettings.getGroups(SimilarityModule.SIMILARITY_SETTINGS_PREFIX); Map<String, Settings> similaritySettings = indexSettings.getGroups(SimilarityModule.SIMILARITY_SETTINGS_PREFIX);
for (Map.Entry<String, SimilarityProvider.Factory> entry : similarities.entrySet()) { for (Map.Entry<String, SimilarityProvider.Factory> entry : similarities.entrySet()) {
@ -70,7 +72,7 @@ public class SimilarityLookupService extends AbstractIndexComponent {
} }
} }
this.similarities = providers.immutableMap(); this.similarities = unmodifiableMap(providers);
} }
/** /**

View File

@ -18,20 +18,19 @@
*/ */
package org.elasticsearch.search.aggregations; package org.elasticsearch.search.aggregations;
import com.google.common.collect.ImmutableMap;
import org.elasticsearch.common.bytes.BytesReference; 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 org.elasticsearch.common.io.stream.StreamInput;
import java.io.IOException; 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 * A registry for all the dedicated streams in the aggregation module. This is to support dynamic addAggregation that
* know how to stream themselves. * know how to stream themselves.
*/ */
public class AggregationStreams { public class AggregationStreams {
private static Map<BytesReference, Stream> streams = new CopyOnWriteHashMap<>();
private static ImmutableMap<BytesReference, Stream> streams = ImmutableMap.of();
/** /**
* A stream that knows how to read an aggregation from the input. * 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 * @param types The types associated with the streams
*/ */
public static synchronized void registerStream(Stream stream, BytesReference... types) { public static synchronized void registerStream(Stream stream, BytesReference... types) {
MapBuilder<BytesReference, Stream> uStreams = MapBuilder.newMapBuilder(streams);
for (BytesReference type : types) { for (BytesReference type : types) {
uStreams.put(type, stream); streams.put(type, stream);
} }
streams = uStreams.immutableMap();
} }
/** /**

View File

@ -18,7 +18,6 @@
*/ */
package org.elasticsearch.search.aggregations; package org.elasticsearch.search.aggregations;
import com.google.common.collect.ImmutableMap;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
@ -37,6 +36,9 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static java.util.Collections.emptyMap;
import static java.util.Collections.unmodifiableMap;
/** /**
* An internal implementation of {@link Aggregations}. * An internal implementation of {@link Aggregations}.
*/ */
@ -46,7 +48,7 @@ public class InternalAggregations implements Aggregations, ToXContent, Streamabl
private List<InternalAggregation> aggregations = Collections.emptyList(); private List<InternalAggregation> aggregations = Collections.emptyList();
private Map<String, InternalAggregation> aggregationsAsMap; private Map<String, Aggregation> aggregationsAsMap;
private InternalAggregations() { private InternalAggregations() {
} }
@ -88,13 +90,13 @@ public class InternalAggregations implements Aggregations, ToXContent, Streamabl
@Override @Override
public Map<String, Aggregation> getAsMap() { public Map<String, Aggregation> getAsMap() {
if (aggregationsAsMap == null) { if (aggregationsAsMap == null) {
Map<String, InternalAggregation> aggregationsAsMap = new HashMap<>(); Map<String, InternalAggregation> newAggregationsAsMap = new HashMap<>();
for (InternalAggregation aggregation : aggregations) { 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(); int size = in.readVInt();
if (size == 0) { if (size == 0) {
aggregations = Collections.emptyList(); aggregations = Collections.emptyList();
aggregationsAsMap = ImmutableMap.of(); aggregationsAsMap = emptyMap();
} else { } else {
aggregations = new ArrayList<>(size); aggregations = new ArrayList<>(size);
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {

View File

@ -19,16 +19,15 @@
package org.elasticsearch.search.aggregations.bucket; package org.elasticsearch.search.aggregations.bucket;
import com.google.common.collect.ImmutableMap;
import org.elasticsearch.common.bytes.BytesReference; 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 org.elasticsearch.common.io.stream.StreamInput;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
public class BucketStreams { public class BucketStreams {
private static final Map<BytesReference, Stream> streams = new CopyOnWriteHashMap<>();
private static ImmutableMap<BytesReference, Stream> STREAMS = ImmutableMap.of();
/** /**
* A stream that knows how to read a bucket from the input. * 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 * @param types The types associated with the streams
*/ */
public static synchronized void registerStream(Stream stream, BytesReference... types) { public static synchronized void registerStream(Stream stream, BytesReference... types) {
MapBuilder<BytesReference, Stream> uStreams = MapBuilder.newMapBuilder(STREAMS);
for (BytesReference type : types) { 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 * @return The associated stream
*/ */
public static Stream stream(BytesReference type) { public static Stream stream(BytesReference type) {
return STREAMS.get(type); return streams.get(type);
} }
} }

View File

@ -19,21 +19,18 @@
package org.elasticsearch.search.aggregations.pipeline; package org.elasticsearch.search.aggregations.pipeline;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.CopyOnWriteHashMap;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Map; 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 * A registry for all the dedicated streams in the aggregation module. This is to support dynamic addAggregation that
* know how to stream themselves. * know how to stream themselves.
*/ */
public class PipelineAggregatorStreams { public class PipelineAggregatorStreams {
private static Map<BytesReference, Stream> streams = emptyMap(); private static Map<BytesReference, Stream> streams = new CopyOnWriteHashMap<>();
/** /**
* A stream that knows how to read an aggregation from the input. * 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 * @param types The types associated with the streams
*/ */
public static synchronized void registerStream(Stream stream, BytesReference... types) { public static synchronized void registerStream(Stream stream, BytesReference... types) {
Map<BytesReference, Stream> newStreams = new HashMap<>(streams);
for (BytesReference type : types) { for (BytesReference type : types) {
newStreams.put(type, stream); streams.put(type, stream);
} }
streams = unmodifiableMap(newStreams);
} }
/** /**