Finish removing ImmutableMap#of

We'll need to be more careful with CopyOnWriteHashMap than I was at first.
This commit is contained in:
Nik Everett 2015-10-02 01:35:32 +02:00
parent e6303302e6
commit f68dabe615
7 changed files with 49 additions and 22 deletions

View File

@ -19,7 +19,6 @@
package org.elasticsearch.action.search.type;
import com.google.common.collect.ImmutableMap;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.CharsRefBuilder;
import org.elasticsearch.action.search.SearchRequest;
@ -38,6 +37,8 @@ import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import static java.util.Collections.emptyMap;
/**
*
*/
@ -112,7 +113,7 @@ public abstract class TransportSearchHelper {
Map<String, String> attributes;
int attributesSize = Integer.parseInt(elements[index++]);
if (attributesSize == 0) {
attributes = ImmutableMap.of();
attributes = emptyMap();
} else {
attributes = new HashMap<>(attributesSize);
for (int i = 0; i < attributesSize; i++) {

View File

@ -19,12 +19,15 @@
package org.elasticsearch.indices.flush;
import com.google.common.collect.ImmutableMap;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.index.shard.ShardId;
import java.util.HashMap;
import java.util.Map;
import static java.util.Collections.emptyMap;
/**
* Result for all copies of a shard
*/
@ -49,7 +52,7 @@ public class ShardsSyncedFlushResult {
public ShardsSyncedFlushResult(ShardId shardId, int totalShards, String failureReason) {
this.syncId = null;
this.failureReason = failureReason;
this.shardResponses = ImmutableMap.of();
this.shardResponses = emptyMap();
this.shardId = shardId;
this.totalShards = totalShards;
}

View File

@ -19,9 +19,12 @@
package org.elasticsearch.repositories;
import com.google.common.collect.ImmutableMap;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.cluster.*;
import org.elasticsearch.cluster.AckedClusterStateUpdateTask;
import org.elasticsearch.cluster.ClusterChangedEvent;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ClusterStateListener;
import org.elasticsearch.cluster.ack.ClusterStateUpdateRequest;
import org.elasticsearch.cluster.ack.ClusterStateUpdateResponse;
import org.elasticsearch.cluster.metadata.MetaData;
@ -40,9 +43,15 @@ import org.elasticsearch.snapshots.SnapshotsService;
import org.elasticsearch.transport.TransportService;
import java.io.IOException;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
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;
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
/**
@ -58,7 +67,7 @@ public class RepositoriesService extends AbstractComponent implements ClusterSta
private final VerifyNodeRepositoryAction verifyAction;
private volatile Map<String, RepositoryHolder> repositories = ImmutableMap.of();
private volatile Map<String, RepositoryHolder> repositories = emptyMap();
@Inject
public RepositoriesService(Settings settings, ClusterService clusterService, TransportService transportService, RepositoryTypesRegistry typesRegistry, Injector injector) {
@ -272,7 +281,7 @@ public class RepositoriesService extends AbstractComponent implements ClusterSta
}
}
ImmutableMap.Builder<String, RepositoryHolder> builder = ImmutableMap.builder();
Map<String, RepositoryHolder> builder = new HashMap<>();
if (newMetaData != null) {
// Now go through all repositories and update existing or create missing
for (RepositoryMetaData repositoryMetaData : newMetaData.repositories()) {
@ -303,7 +312,7 @@ public class RepositoriesService extends AbstractComponent implements ClusterSta
}
}
}
repositories = builder.build();
repositories = unmodifiableMap(builder);
} catch (Throwable ex) {
logger.warn("failure updating cluster state ", ex);
}
@ -368,7 +377,6 @@ public class RepositoriesService extends AbstractComponent implements ClusterSta
}
Map<String, RepositoryHolder> newRepositories = new HashMap<>(repositories);
newRepositories.put(repositoryMetaData.name(), holder);
repositories = ImmutableMap.copyOf(newRepositories);
return true;
}

View File

@ -19,18 +19,21 @@
package org.elasticsearch.search.aggregations;
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 AggregationStreams {
private static Map<BytesReference, Stream> streams = new CopyOnWriteHashMap<>();
private static Map<BytesReference, Stream> streams = emptyMap();
/**
* A stream that knows how to read an aggregation from the input.
@ -46,9 +49,11 @@ public class AggregationStreams {
* @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) {
streams.put(type, stream);
newStreams.put(type, stream);
}
streams = unmodifiableMap(newStreams);
}
/**

View File

@ -20,14 +20,17 @@
package org.elasticsearch.search.aggregations.bucket;
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;
public class BucketStreams {
private static final Map<BytesReference, Stream> streams = new CopyOnWriteHashMap<>();
private static Map<BytesReference, Stream> streams = emptyMap();
/**
* A stream that knows how to read a bucket from the input.
@ -44,9 +47,11 @@ public class BucketStreams {
* @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) {
streams.put(type, stream);
newStreams.put(type, stream);
}
streams = unmodifiableMap(newStreams);
}
/**

View File

@ -19,18 +19,21 @@
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 = new CopyOnWriteHashMap<>();
private static Map<BytesReference, Stream> streams = emptyMap();
/**
* A stream that knows how to read an aggregation from the input.
@ -46,9 +49,11 @@ 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) {
streams.put(type, stream);
newStreams.put(type, stream);
}
streams = unmodifiableMap(newStreams);
}
/**

View File

@ -18,8 +18,6 @@
*/
package org.elasticsearch.search.lookup;
import com.google.common.collect.ImmutableMap;
import org.apache.lucene.index.LeafReader;
import org.apache.lucene.index.LeafReaderContext;
import org.elasticsearch.ElasticsearchParseException;
@ -35,6 +33,8 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import static java.util.Collections.emptyMap;
/**
*
*/
@ -71,7 +71,7 @@ public class SourceLookup implements Map {
reader.document(docId, sourceFieldVisitor);
BytesReference source = sourceFieldVisitor.source();
if (source == null) {
this.source = ImmutableMap.of();
this.source = emptyMap();
this.sourceContentType = null;
} else {
Tuple<XContentType, Map<String, Object>> tuple = sourceAsMapAndType(source);