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;
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.
* <p>
@ -39,17 +41,17 @@ public class SimilarityLookupService extends AbstractIndexComponent {
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) {
this(index, indexSettings, ImmutableMap.<String, SimilarityProvider.Factory>of());
this(index, indexSettings, emptyMap());
}
@Inject
public SimilarityLookupService(Index index, @IndexSettings Settings indexSettings, Map<String, SimilarityProvider.Factory> similarities) {
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);
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;
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<BytesReference, Stream> streams = ImmutableMap.of();
private static Map<BytesReference, Stream> 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<BytesReference, Stream> uStreams = MapBuilder.newMapBuilder(streams);
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;
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<InternalAggregation> aggregations = Collections.emptyList();
private Map<String, InternalAggregation> aggregationsAsMap;
private Map<String, Aggregation> aggregationsAsMap;
private InternalAggregations() {
}
@ -88,13 +90,13 @@ public class InternalAggregations implements Aggregations, ToXContent, Streamabl
@Override
public Map<String, Aggregation> getAsMap() {
if (aggregationsAsMap == null) {
Map<String, InternalAggregation> aggregationsAsMap = new HashMap<>();
Map<String, InternalAggregation> 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++) {

View File

@ -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<BytesReference, Stream> STREAMS = ImmutableMap.of();
private static final Map<BytesReference, Stream> 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<BytesReference, Stream> 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);
}
}

View File

@ -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<BytesReference, Stream> streams = emptyMap();
private static Map<BytesReference, Stream> 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<BytesReference, Stream> newStreams = new HashMap<>(streams);
for (BytesReference type : types) {
newStreams.put(type, stream);
streams.put(type, stream);
}
streams = unmodifiableMap(newStreams);
}
/**