From 9430e17f707bde61418f31d1b080ed087ed4cd4c Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Fri, 2 Oct 2015 00:18:05 +0200 Subject: [PATCH] Yet more ImmutableMap#of removal Did some refactoring of PathTrie along the way to remove warnings. Added a simple toString to it to make it easier to debug. --- .../cluster/block/ClusterBlocks.java | 13 ++-- .../routing/RoutingTableValidation.java | 7 ++- .../java/org/elasticsearch/common/Table.java | 6 +- .../elasticsearch/common/path/PathTrie.java | 50 ++++++++------- .../gateway/DanglingIndicesState.java | 5 +- .../org/elasticsearch/index/IndexService.java | 5 +- .../elasticsearch/index/get/GetResult.java | 6 +- .../index/mapper/MapperService.java | 21 ++++--- .../elasticsearch/index/mapper/Mapping.java | 16 +---- .../BlobStoreIndexShardRepository.java | 23 +++++-- .../org/elasticsearch/index/store/Store.java | 60 +++++++++++++----- .../pipeline/PipelineAggregatorStreams.java | 17 ++--- .../elasticsearch/search/dfs/DfsPhase.java | 8 ++- .../common/path/PathTrieTests.java | 1 - .../gateway/ReplicaShardAllocatorTests.java | 15 ++++- .../elasticsearch/index/store/StoreTests.java | 62 ++++++++++++++++--- 16 files changed, 210 insertions(+), 105 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/cluster/block/ClusterBlocks.java b/core/src/main/java/org/elasticsearch/cluster/block/ClusterBlocks.java index ab5609ce9ef..00b372acae1 100644 --- a/core/src/main/java/org/elasticsearch/cluster/block/ClusterBlocks.java +++ b/core/src/main/java/org/elasticsearch/cluster/block/ClusterBlocks.java @@ -24,6 +24,7 @@ import com.google.common.collect.ImmutableMap; import org.elasticsearch.cluster.AbstractDiffable; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.MetaDataIndexStateService; +import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.rest.RestStatus; @@ -68,7 +69,7 @@ public class ClusterBlocks extends AbstractDiffable { .filter(containsLevel) .collect(toSet())); - ImmutableMap.Builder> indicesBuilder = ImmutableMap.builder(); + ImmutableOpenMap.Builder> indicesBuilder = ImmutableOpenMap.builder(); for (Map.Entry> entry : indicesBlocks.entrySet()) { indicesBuilder.put(entry.getKey(), unmodifiableSet(entry.getValue().stream() .filter(containsLevel) @@ -91,7 +92,7 @@ public class ClusterBlocks extends AbstractDiffable { return levelHolders[level.id()].global(); } - public Map> indices(ClusterBlockLevel level) { + public ImmutableOpenMap> indices(ClusterBlockLevel level) { return levelHolders[level.id()].indices(); } @@ -238,12 +239,12 @@ public class ClusterBlocks extends AbstractDiffable { static class ImmutableLevelHolder { - static final ImmutableLevelHolder EMPTY = new ImmutableLevelHolder(emptySet(), ImmutableMap.of()); + static final ImmutableLevelHolder EMPTY = new ImmutableLevelHolder(emptySet(), ImmutableOpenMap.of()); private final Set global; - private final ImmutableMap> indices; + private final ImmutableOpenMap> indices; - ImmutableLevelHolder(Set global, ImmutableMap> indices) { + ImmutableLevelHolder(Set global, ImmutableOpenMap> indices) { this.global = global; this.indices = indices; } @@ -252,7 +253,7 @@ public class ClusterBlocks extends AbstractDiffable { return global; } - public ImmutableMap> indices() { + public ImmutableOpenMap> indices() { return indices; } } diff --git a/core/src/main/java/org/elasticsearch/cluster/routing/RoutingTableValidation.java b/core/src/main/java/org/elasticsearch/cluster/routing/RoutingTableValidation.java index aec8bef1d91..472e73b3fda 100644 --- a/core/src/main/java/org/elasticsearch/cluster/routing/RoutingTableValidation.java +++ b/core/src/main/java/org/elasticsearch/cluster/routing/RoutingTableValidation.java @@ -19,7 +19,6 @@ package org.elasticsearch.cluster.routing; -import com.google.common.collect.ImmutableMap; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Streamable; @@ -31,6 +30,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import static java.util.Collections.emptyMap; + /** * Encapsulates the result of a routing table validation and provides access to * validation failures. @@ -72,7 +73,7 @@ public class RoutingTableValidation implements Streamable { public Map> indicesFailures() { if (indicesFailures == null) { - return ImmutableMap.of(); + return emptyMap(); } return indicesFailures; } @@ -128,7 +129,7 @@ public class RoutingTableValidation implements Streamable { } size = in.readVInt(); if (size == 0) { - indicesFailures = ImmutableMap.of(); + indicesFailures = emptyMap(); } else { indicesFailures = new HashMap<>(); for (int i = 0; i < size; i++) { diff --git a/core/src/main/java/org/elasticsearch/common/Table.java b/core/src/main/java/org/elasticsearch/common/Table.java index fd979cfa8d1..6156cc2e79f 100644 --- a/core/src/main/java/org/elasticsearch/common/Table.java +++ b/core/src/main/java/org/elasticsearch/common/Table.java @@ -19,13 +19,13 @@ package org.elasticsearch.common; -import com.google.common.collect.ImmutableMap; - import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import static java.util.Collections.emptyMap; + /** */ public class Table { @@ -115,7 +115,7 @@ public class Table { Map mAttr; if (attributes.length() == 0) { if (inHeaders) { - mAttr = ImmutableMap.of(); + mAttr = emptyMap(); } else { // get the attributes of the header cell we are going to add to mAttr = headers.get(currentCells.size()).attr; diff --git a/core/src/main/java/org/elasticsearch/common/path/PathTrie.java b/core/src/main/java/org/elasticsearch/common/path/PathTrie.java index 3bf2a9b17ee..2bee8273667 100644 --- a/core/src/main/java/org/elasticsearch/common/path/PathTrie.java +++ b/core/src/main/java/org/elasticsearch/common/path/PathTrie.java @@ -19,14 +19,15 @@ package org.elasticsearch.common.path; -import com.google.common.collect.ImmutableMap; import org.elasticsearch.common.Strings; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; -import static org.elasticsearch.common.collect.MapBuilder.newMapBuilder; +import static java.util.Collections.emptyMap; +import static java.util.Collections.unmodifiableMap; /** * @@ -45,7 +46,7 @@ public class PathTrie { }; private final Decoder decoder; - private final TrieNode root; + private final TrieNode root; private final char separator; private T rootValue; @@ -60,10 +61,10 @@ public class PathTrie { public PathTrie(char separator, String wildcard, Decoder decoder) { this.decoder = decoder; this.separator = separator; - root = new TrieNode<>(new String(new char[]{separator}), null, null, wildcard); + root = new TrieNode(new String(new char[]{separator}), null, wildcard); } - public class TrieNode { + public class TrieNode { private transient String key; private transient T value; private boolean isWildcard; @@ -71,17 +72,14 @@ public class PathTrie { private transient String namedWildcard; - private ImmutableMap> children; + private Map children; - private final TrieNode parent; - - public TrieNode(String key, T value, TrieNode parent, String wildcard) { + public TrieNode(String key, T value, String wildcard) { this.key = key; this.wildcard = wildcard; this.isWildcard = (key.equals(wildcard)); - this.parent = parent; this.value = value; - this.children = ImmutableMap.of(); + this.children = emptyMap(); if (isNamedWildcard(key)) { namedWildcard = key.substring(key.indexOf('{') + 1, key.indexOf('}')); } else { @@ -98,8 +96,14 @@ public class PathTrie { return isWildcard; } - public synchronized void addChild(TrieNode child) { - children = newMapBuilder(children).put(child.key, child).immutableMap(); + public synchronized void addChild(TrieNode child) { + addInnerChild(child.key, child); + } + + private void addInnerChild(String key, TrieNode child) { + Map newChildren = new HashMap<>(children); + newChildren.put(key, child); + children = unmodifiableMap(newChildren); } public TrieNode getChild(String key) { @@ -115,14 +119,11 @@ public class PathTrie { if (isNamedWildcard(token)) { key = wildcard; } - TrieNode node = children.get(key); + TrieNode node = children.get(key); if (node == null) { - if (index == (path.length - 1)) { - node = new TrieNode<>(token, value, this, wildcard); - } else { - node = new TrieNode<>(token, null, this, wildcard); - } - children = newMapBuilder(children).put(key, node).immutableMap(); + T nodeValue = index == path.length - 1 ? value : null; + node = new TrieNode(token, nodeValue, wildcard); + addInnerChild(key, node); } else { if (isNamedWildcard(token)) { node.updateKeyWithNamedWildcard(token); @@ -158,7 +159,7 @@ public class PathTrie { return null; String token = path[index]; - TrieNode node = children.get(token); + TrieNode node = children.get(token); boolean usedWildcard; if (node == null) { node = children.get(wildcard); @@ -195,11 +196,16 @@ public class PathTrie { return res; } - private void put(Map params, TrieNode node, String value) { + private void put(Map params, TrieNode node, String value) { if (params != null && node.isNamedWildcard()) { params.put(node.namedWildcard(), value); } } + + @Override + public String toString() { + return key; + } } public void insert(String path, T value) { diff --git a/core/src/main/java/org/elasticsearch/gateway/DanglingIndicesState.java b/core/src/main/java/org/elasticsearch/gateway/DanglingIndicesState.java index e4285d20ca7..4d462f25441 100644 --- a/core/src/main/java/org/elasticsearch/gateway/DanglingIndicesState.java +++ b/core/src/main/java/org/elasticsearch/gateway/DanglingIndicesState.java @@ -20,6 +20,7 @@ package org.elasticsearch.gateway; import com.google.common.collect.ImmutableMap; + import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.common.component.AbstractComponent; @@ -34,6 +35,8 @@ import java.util.HashMap; import java.util.Map; import java.util.Set; +import static java.util.Collections.emptyMap; + /** * The dangling indices state is responsible for finding new dangling indices (indices that have * their state written on disk, but don't exists in the metadata of the cluster), and importing @@ -107,7 +110,7 @@ public class DanglingIndicesState extends AbstractComponent { indices = nodeEnv.findAllIndices(); } catch (Throwable e) { logger.warn("failed to list dangling indices", e); - return ImmutableMap.of(); + return emptyMap(); } Map newIndices = new HashMap<>(); diff --git a/core/src/main/java/org/elasticsearch/index/IndexService.java b/core/src/main/java/org/elasticsearch/index/IndexService.java index 3c40b02a4b9..0aac941f727 100644 --- a/core/src/main/java/org/elasticsearch/index/IndexService.java +++ b/core/src/main/java/org/elasticsearch/index/IndexService.java @@ -20,11 +20,11 @@ package org.elasticsearch.index; import com.google.common.collect.ImmutableMap; + import org.apache.lucene.util.Accountable; import org.apache.lucene.util.IOUtils; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.cluster.metadata.IndexMetaData; -import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Strings; @@ -74,6 +74,7 @@ import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; +import static java.util.Collections.emptyMap; import static org.elasticsearch.common.collect.MapBuilder.newMapBuilder; /** @@ -110,7 +111,7 @@ public class IndexService extends AbstractIndexComponent implements IndexCompone private final NodeEnvironment nodeEnv; private final IndicesService indicesServices; - private volatile ImmutableMap shards = ImmutableMap.of(); + private volatile Map shards = emptyMap(); private static class IndexShardInjectorPair { private final IndexShard indexShard; diff --git a/core/src/main/java/org/elasticsearch/index/get/GetResult.java b/core/src/main/java/org/elasticsearch/index/get/GetResult.java index c788fcf3892..d2436949503 100644 --- a/core/src/main/java/org/elasticsearch/index/get/GetResult.java +++ b/core/src/main/java/org/elasticsearch/index/get/GetResult.java @@ -19,7 +19,6 @@ package org.elasticsearch.index.get; -import com.google.common.collect.ImmutableMap; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.compress.CompressorFactory; @@ -40,6 +39,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import static java.util.Collections.emptyMap; import static org.elasticsearch.index.get.GetField.readGetField; /** @@ -68,7 +68,7 @@ public class GetResult implements Streamable, Iterable, ToXContent { this.source = source; this.fields = fields; if (this.fields == null) { - this.fields = ImmutableMap.of(); + this.fields = emptyMap(); } } @@ -286,7 +286,7 @@ public class GetResult implements Streamable, Iterable, ToXContent { } int size = in.readVInt(); if (size == 0) { - fields = ImmutableMap.of(); + fields = emptyMap(); } else { fields = new HashMap<>(size); for (int i = 0; i < size; i++) { diff --git a/core/src/main/java/org/elasticsearch/index/mapper/MapperService.java b/core/src/main/java/org/elasticsearch/index/mapper/MapperService.java index dd31c6e60be..0357ef24060 100755 --- a/core/src/main/java/org/elasticsearch/index/mapper/MapperService.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/MapperService.java @@ -20,7 +20,6 @@ package org.elasticsearch.index.mapper; import com.carrotsearch.hppc.ObjectHashSet; -import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterators; import org.apache.lucene.analysis.Analyzer; @@ -64,6 +63,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; @@ -73,7 +73,9 @@ import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.function.Function; +import static java.util.Collections.emptyMap; import static java.util.Collections.emptySet; +import static java.util.Collections.unmodifiableMap; import static java.util.Collections.unmodifiableSet; import static org.elasticsearch.common.collect.MapBuilder.newMapBuilder; @@ -98,7 +100,7 @@ public class MapperService extends AbstractIndexComponent implements Closeable { private volatile String defaultMappingSource; private volatile String defaultPercolatorMappingSource; - private volatile Map mappers = ImmutableMap.of(); + private volatile Map mappers = emptyMap(); // A lock for mappings: modifications (put mapping) need to be performed // under the write lock and read operations (document parsing) need to be @@ -118,7 +120,7 @@ public class MapperService extends AbstractIndexComponent implements Closeable { private final List typeListeners = new CopyOnWriteArrayList<>(); - private volatile ImmutableMap unmappedFieldTypes = ImmutableMap.of(); + private volatile Map unmappedFieldTypes = emptyMap(); private volatile Set parentTypes = emptySet(); @@ -538,24 +540,23 @@ public class MapperService extends AbstractIndexComponent implements Closeable { * Given a type (eg. long, string, ...), return an anonymous field mapper that can be used for search operations. */ public MappedFieldType unmappedFieldType(String type) { - final ImmutableMap unmappedFieldMappers = this.unmappedFieldTypes; - MappedFieldType fieldType = unmappedFieldMappers.get(type); + MappedFieldType fieldType = unmappedFieldTypes.get(type); if (fieldType == null) { final Mapper.TypeParser.ParserContext parserContext = documentMapperParser().parserContext(type); Mapper.TypeParser typeParser = parserContext.typeParser(type); if (typeParser == null) { throw new IllegalArgumentException("No mapper found for type [" + type + "]"); } - final Mapper.Builder builder = typeParser.parse("__anonymous_" + type, ImmutableMap.of(), parserContext); + final Mapper.Builder builder = typeParser.parse("__anonymous_" + type, emptyMap(), parserContext); final BuilderContext builderContext = new BuilderContext(indexSettings, new ContentPath(1)); fieldType = ((FieldMapper)builder.build(builderContext)).fieldType(); // There is no need to synchronize writes here. In the case of concurrent access, we could just // compute some mappers several times, which is not a big deal - this.unmappedFieldTypes = ImmutableMap.builder() - .putAll(unmappedFieldMappers) - .put(type, fieldType) - .build(); + Map newUnmappedFieldTypes = new HashMap<>(); + newUnmappedFieldTypes.putAll(unmappedFieldTypes); + newUnmappedFieldTypes.put(type, fieldType); + unmappedFieldTypes = unmodifiableMap(newUnmappedFieldTypes); } return fieldType; } diff --git a/core/src/main/java/org/elasticsearch/index/mapper/Mapping.java b/core/src/main/java/org/elasticsearch/index/mapper/Mapping.java index 412bb56a785..c247d55688e 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/Mapping.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/Mapping.java @@ -22,7 +22,6 @@ package org.elasticsearch.index.mapper; import com.google.common.collect.ImmutableMap; import org.elasticsearch.Version; -import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; @@ -34,6 +33,8 @@ import java.util.Comparator; import java.util.List; import java.util.Map; +import static java.util.Collections.emptyMap; + /** * Wrapper around everything that defines a mapping, without references to * utility classes like MapperService, ... @@ -150,22 +151,11 @@ public final class Mapping implements ToXContent { return builder; } - /** Serialize to a {@link BytesReference}. */ - public BytesReference toBytes() { - try { - XContentBuilder builder = XContentFactory.jsonBuilder().startObject(); - toXContent(builder, new ToXContent.MapParams(ImmutableMap.of())); - return builder.endObject().bytes(); - } catch (IOException bogus) { - throw new AssertionError(bogus); - } - } - @Override public String toString() { try { XContentBuilder builder = XContentFactory.jsonBuilder().startObject(); - toXContent(builder, new ToXContent.MapParams(ImmutableMap.of())); + toXContent(builder, new ToXContent.MapParams(emptyMap())); return builder.endObject().string(); } catch (IOException bogus) { throw new AssertionError(bogus); diff --git a/core/src/main/java/org/elasticsearch/index/snapshots/blobstore/BlobStoreIndexShardRepository.java b/core/src/main/java/org/elasticsearch/index/snapshots/blobstore/BlobStoreIndexShardRepository.java index 991af6d8f2c..cbbfaae0fc2 100644 --- a/core/src/main/java/org/elasticsearch/index/snapshots/blobstore/BlobStoreIndexShardRepository.java +++ b/core/src/main/java/org/elasticsearch/index/snapshots/blobstore/BlobStoreIndexShardRepository.java @@ -19,7 +19,11 @@ package org.elasticsearch.index.snapshots.blobstore; -import org.apache.lucene.index.*; +import org.apache.lucene.index.CorruptIndexException; +import org.apache.lucene.index.IndexCommit; +import org.apache.lucene.index.IndexFormatTooNewException; +import org.apache.lucene.index.IndexFormatTooOldException; +import org.apache.lucene.index.SegmentInfos; import org.apache.lucene.store.IOContext; import org.apache.lucene.store.IndexInput; import org.apache.lucene.store.IndexOutput; @@ -47,7 +51,11 @@ import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.util.iterable.Iterables; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.shard.ShardId; -import org.elasticsearch.index.snapshots.*; +import org.elasticsearch.index.snapshots.IndexShardRepository; +import org.elasticsearch.index.snapshots.IndexShardRestoreFailedException; +import org.elasticsearch.index.snapshots.IndexShardSnapshotException; +import org.elasticsearch.index.snapshots.IndexShardSnapshotFailedException; +import org.elasticsearch.index.snapshots.IndexShardSnapshotStatus; import org.elasticsearch.index.snapshots.blobstore.BlobStoreIndexShardSnapshot.FileInfo; import org.elasticsearch.index.store.Store; import org.elasticsearch.index.store.StoreFileMetaData; @@ -63,8 +71,15 @@ import org.elasticsearch.repositories.blobstore.LegacyBlobStoreFormat; import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import static java.util.Collections.emptyMap; +import static java.util.Collections.unmodifiableMap; import static org.elasticsearch.repositories.blobstore.BlobStoreRepository.testBlobPrefix; /** @@ -808,7 +823,7 @@ public class BlobStoreIndexShardRepository extends AbstractComponent implements snapshotMetaData.put(fileInfo.metadata().name(), fileInfo.metadata()); fileInfos.put(fileInfo.metadata().name(), fileInfo); } - final Store.MetadataSnapshot sourceMetaData = new Store.MetadataSnapshot(snapshotMetaData, Collections.EMPTY_MAP, 0); + final Store.MetadataSnapshot sourceMetaData = new Store.MetadataSnapshot(unmodifiableMap(snapshotMetaData), emptyMap(), 0); final Store.RecoveryDiff diff = sourceMetaData.recoveryDiff(recoveryTargetMetadata); for (StoreFileMetaData md : diff.identical) { FileInfo fileInfo = fileInfos.get(md.name()); diff --git a/core/src/main/java/org/elasticsearch/index/store/Store.java b/core/src/main/java/org/elasticsearch/index/store/Store.java index 3d158ff925e..39a0fc13e49 100644 --- a/core/src/main/java/org/elasticsearch/index/store/Store.java +++ b/core/src/main/java/org/elasticsearch/index/store/Store.java @@ -20,13 +20,35 @@ package org.elasticsearch.index.store; import com.google.common.collect.ImmutableMap; + import org.apache.lucene.codecs.CodecUtil; -import org.apache.lucene.index.*; -import org.apache.lucene.store.*; -import org.apache.lucene.util.*; +import org.apache.lucene.index.CorruptIndexException; +import org.apache.lucene.index.IndexCommit; +import org.apache.lucene.index.IndexFileNames; +import org.apache.lucene.index.IndexFormatTooNewException; +import org.apache.lucene.index.IndexFormatTooOldException; +import org.apache.lucene.index.IndexNotFoundException; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.SegmentCommitInfo; +import org.apache.lucene.index.SegmentInfos; +import org.apache.lucene.store.AlreadyClosedException; +import org.apache.lucene.store.BufferedChecksum; +import org.apache.lucene.store.ByteArrayDataInput; +import org.apache.lucene.store.ChecksumIndexInput; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.FilterDirectory; +import org.apache.lucene.store.IOContext; +import org.apache.lucene.store.IndexInput; +import org.apache.lucene.store.IndexOutput; +import org.apache.lucene.store.Lock; +import org.apache.lucene.store.SimpleFSDirectory; +import org.apache.lucene.util.ArrayUtil; +import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.BytesRefBuilder; +import org.apache.lucene.util.IOUtils; +import org.apache.lucene.util.Version; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ExceptionsHelper; -import org.apache.lucene.util.Version; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.Tuple; @@ -44,26 +66,38 @@ import org.elasticsearch.common.lucene.store.InputStreamIndexInput; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.util.Callback; -import org.elasticsearch.common.util.iterable.Iterables; import org.elasticsearch.common.util.SingleObjectCache; import org.elasticsearch.common.util.concurrent.AbstractRefCounted; import org.elasticsearch.common.util.concurrent.RefCounted; +import org.elasticsearch.common.util.iterable.Iterables; import org.elasticsearch.env.ShardLock; import org.elasticsearch.index.engine.Engine; import org.elasticsearch.index.settings.IndexSettings; import org.elasticsearch.index.shard.AbstractIndexShardComponent; import org.elasticsearch.index.shard.ShardId; -import java.io.*; +import java.io.Closeable; +import java.io.EOFException; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; import java.nio.file.NoSuchFileException; import java.nio.file.Path; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.zip.Adler32; import java.util.zip.CRC32; import java.util.zip.Checksum; +import static java.util.Collections.emptyMap; + /** * A Store provides plain access to files written by an elasticsearch index shard. Each shard * has a dedicated store that is uses to access Lucene's Directory which represents the lowest level @@ -734,7 +768,7 @@ public class Store extends AbstractIndexShardComponent implements Closeable, Ref private static final ESLogger logger = Loggers.getLogger(MetadataSnapshot.class); private static final Version FIRST_LUCENE_CHECKSUM_VERSION = Version.LUCENE_4_8; - private final ImmutableMap metadata; + private final Map metadata; public static final MetadataSnapshot EMPTY = new MetadataSnapshot(); @@ -743,16 +777,14 @@ public class Store extends AbstractIndexShardComponent implements Closeable, Ref private final long numDocs; public MetadataSnapshot(Map metadata, Map commitUserData, long numDocs) { - ImmutableMap.Builder metaDataBuilder = ImmutableMap.builder(); - this.metadata = metaDataBuilder.putAll(metadata).build(); - ImmutableMap.Builder commitUserDataBuilder = ImmutableMap.builder(); - this.commitUserData = commitUserDataBuilder.putAll(commitUserData).build(); + this.metadata = metadata; + this.commitUserData = commitUserData; this.numDocs = numDocs; } MetadataSnapshot() { - metadata = ImmutableMap.of(); - commitUserData = ImmutableMap.of(); + metadata = emptyMap(); + commitUserData = emptyMap(); numDocs = 0; } 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 a633a3c25ca..71046092879 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 @@ -18,21 +18,22 @@ */ package org.elasticsearch.search.aggregations.pipeline; -import com.google.common.collect.ImmutableMap; - import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.common.collect.MapBuilder; 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 ImmutableMap streams = ImmutableMap.of(); + private static Map streams = emptyMap(); /** * A stream that knows how to read an aggregation from the input. @@ -48,11 +49,11 @@ public class PipelineAggregatorStreams { * @param types The types associated with the streams */ public static synchronized void registerStream(Stream stream, BytesReference... types) { - MapBuilder uStreams = MapBuilder.newMapBuilder(streams); + Map newStreams = new HashMap<>(streams); for (BytesReference type : types) { - uStreams.put(type, stream); + newStreams.put(type, stream); } - streams = uStreams.immutableMap(); + streams = unmodifiableMap(newStreams); } /** diff --git a/core/src/main/java/org/elasticsearch/search/dfs/DfsPhase.java b/core/src/main/java/org/elasticsearch/search/dfs/DfsPhase.java index f5522921e7d..e1b98c413e1 100644 --- a/core/src/main/java/org/elasticsearch/search/dfs/DfsPhase.java +++ b/core/src/main/java/org/elasticsearch/search/dfs/DfsPhase.java @@ -19,10 +19,10 @@ package org.elasticsearch.search.dfs; -import com.carrotsearch.hppc.ObjectObjectHashMap; import com.carrotsearch.hppc.ObjectHashSet; +import com.carrotsearch.hppc.ObjectObjectHashMap; import com.carrotsearch.hppc.cursors.ObjectCursor; -import com.google.common.collect.ImmutableMap; + import org.apache.lucene.index.IndexReaderContext; import org.apache.lucene.index.Term; import org.apache.lucene.index.TermContext; @@ -39,6 +39,8 @@ import java.util.Collection; import java.util.Iterator; import java.util.Map; +import static java.util.Collections.emptyMap; + /** * */ @@ -46,7 +48,7 @@ public class DfsPhase implements SearchPhase { @Override public Map parseElements() { - return ImmutableMap.of(); + return emptyMap(); } @Override diff --git a/core/src/test/java/org/elasticsearch/common/path/PathTrieTests.java b/core/src/test/java/org/elasticsearch/common/path/PathTrieTests.java index aec4fb24888..1309b585300 100644 --- a/core/src/test/java/org/elasticsearch/common/path/PathTrieTests.java +++ b/core/src/test/java/org/elasticsearch/common/path/PathTrieTests.java @@ -20,7 +20,6 @@ package org.elasticsearch.common.path; import org.elasticsearch.test.ESTestCase; -import org.junit.Test; import java.util.HashMap; import java.util.Map; diff --git a/core/src/test/java/org/elasticsearch/gateway/ReplicaShardAllocatorTests.java b/core/src/test/java/org/elasticsearch/gateway/ReplicaShardAllocatorTests.java index 2b4ccf7d80f..e692b620d2f 100644 --- a/core/src/test/java/org/elasticsearch/gateway/ReplicaShardAllocatorTests.java +++ b/core/src/test/java/org/elasticsearch/gateway/ReplicaShardAllocatorTests.java @@ -20,6 +20,7 @@ package org.elasticsearch.gateway; import com.carrotsearch.randomizedtesting.generators.RandomPicks; + import org.elasticsearch.Version; import org.elasticsearch.cluster.ClusterInfo; import org.elasticsearch.cluster.ClusterState; @@ -27,7 +28,15 @@ import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNodes; -import org.elasticsearch.cluster.routing.*; +import org.elasticsearch.cluster.routing.IndexRoutingTable; +import org.elasticsearch.cluster.routing.IndexShardRoutingTable; +import org.elasticsearch.cluster.routing.RoutingNode; +import org.elasticsearch.cluster.routing.RoutingNodes; +import org.elasticsearch.cluster.routing.RoutingTable; +import org.elasticsearch.cluster.routing.ShardRouting; +import org.elasticsearch.cluster.routing.ShardRoutingState; +import org.elasticsearch.cluster.routing.TestShardRouting; +import org.elasticsearch.cluster.routing.UnassignedInfo; import org.elasticsearch.cluster.routing.allocation.RoutingAllocation; import org.elasticsearch.cluster.routing.allocation.decider.AllocationDecider; import org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders; @@ -49,6 +58,7 @@ import java.util.HashMap; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; +import static java.util.Collections.unmodifiableMap; import static org.hamcrest.Matchers.equalTo; /** @@ -358,7 +368,8 @@ public class ReplicaShardAllocatorTests extends ESAllocationTestCase { if (syncId != null) { commitData.put(Engine.SYNC_COMMIT_ID, syncId); } - data.put(node, new TransportNodesListShardStoreMetaData.StoreFilesMetaData(allocated, shardId, new Store.MetadataSnapshot(filesAsMap, commitData, randomInt()))); + data.put(node, new TransportNodesListShardStoreMetaData.StoreFilesMetaData(allocated, shardId, + new Store.MetadataSnapshot(unmodifiableMap(filesAsMap), unmodifiableMap(commitData), randomInt()))); return this; } diff --git a/core/src/test/java/org/elasticsearch/index/store/StoreTests.java b/core/src/test/java/org/elasticsearch/index/store/StoreTests.java index 9386b6a20c0..6717d1c904b 100644 --- a/core/src/test/java/org/elasticsearch/index/store/StoreTests.java +++ b/core/src/test/java/org/elasticsearch/index/store/StoreTests.java @@ -24,9 +24,35 @@ import org.apache.lucene.codecs.FilterCodec; import org.apache.lucene.codecs.SegmentInfoFormat; import org.apache.lucene.codecs.lucene50.Lucene50SegmentInfoFormat; import org.apache.lucene.codecs.lucene53.Lucene53Codec; -import org.apache.lucene.document.*; -import org.apache.lucene.index.*; -import org.apache.lucene.store.*; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.document.SortedDocValuesField; +import org.apache.lucene.document.StringField; +import org.apache.lucene.document.TextField; +import org.apache.lucene.index.CorruptIndexException; +import org.apache.lucene.index.DirectoryReader; +import org.apache.lucene.index.IndexFileNames; +import org.apache.lucene.index.IndexFormatTooNewException; +import org.apache.lucene.index.IndexFormatTooOldException; +import org.apache.lucene.index.IndexNotFoundException; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.index.KeepOnlyLastCommitDeletionPolicy; +import org.apache.lucene.index.NoDeletionPolicy; +import org.apache.lucene.index.NoMergePolicy; +import org.apache.lucene.index.SegmentInfo; +import org.apache.lucene.index.SegmentInfos; +import org.apache.lucene.index.SnapshotDeletionPolicy; +import org.apache.lucene.index.Term; +import org.apache.lucene.store.AlreadyClosedException; +import org.apache.lucene.store.BaseDirectoryWrapper; +import org.apache.lucene.store.ChecksumIndexInput; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.IOContext; +import org.apache.lucene.store.IndexInput; +import org.apache.lucene.store.IndexOutput; +import org.apache.lucene.store.MockDirectoryWrapper; +import org.apache.lucene.store.RAMDirectory; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.TestUtil; @@ -55,14 +81,30 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.NoSuchFileException; import java.nio.file.Path; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.zip.Adler32; -import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS; +import static java.util.Collections.emptyMap; +import static java.util.Collections.unmodifiableMap; import static org.elasticsearch.test.VersionUtils.randomVersion; -import static org.hamcrest.Matchers.*; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.endsWith; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; public class StoreTests extends ESTestCase { @@ -800,9 +842,9 @@ public class StoreTests extends ESTestCase { Map metaDataMap = new HashMap<>(); metaDataMap.put("segments_1", new StoreFileMetaData("segments_1", 50, null, null, new BytesRef(new byte[]{1}))); metaDataMap.put("_0_1.del", new StoreFileMetaData("_0_1.del", 42, "foobarbaz", null, new BytesRef())); - Store.MetadataSnapshot first = new Store.MetadataSnapshot(metaDataMap, Collections.EMPTY_MAP, 0); + Store.MetadataSnapshot first = new Store.MetadataSnapshot(unmodifiableMap(new HashMap<>(metaDataMap)), emptyMap(), 0); - Store.MetadataSnapshot second = new Store.MetadataSnapshot(metaDataMap, Collections.EMPTY_MAP, 0); + Store.MetadataSnapshot second = new Store.MetadataSnapshot(unmodifiableMap(new HashMap<>(metaDataMap)), emptyMap(), 0); Store.RecoveryDiff recoveryDiff = first.recoveryDiff(second); assertEquals(recoveryDiff.toString(), recoveryDiff.different.size(), 2); } @@ -1068,7 +1110,7 @@ public class StoreTests extends ESTestCase { Map metaDataMap = new HashMap<>(); metaDataMap.put("segments_1", new StoreFileMetaData("segments_1", 50, null, null, new BytesRef(new byte[]{1}))); metaDataMap.put("_0_1.del", new StoreFileMetaData("_0_1.del", 42, "foobarbaz", null, new BytesRef())); - Store.MetadataSnapshot snapshot = new Store.MetadataSnapshot(metaDataMap, Collections.EMPTY_MAP, 0); + Store.MetadataSnapshot snapshot = new Store.MetadataSnapshot(unmodifiableMap(metaDataMap), emptyMap(), 0); final ShardId shardId = new ShardId(new Index("index"), 1); DirectoryService directoryService = new LuceneManagedDirectoryService(random()); @@ -1202,7 +1244,7 @@ public class StoreTests extends ESTestCase { Map commitUserData = new HashMap<>(); commitUserData.put("userdata_1", "test"); commitUserData.put("userdata_2", "test"); - return new Store.MetadataSnapshot(storeFileMetaDataMap, commitUserData, 0); + return new Store.MetadataSnapshot(unmodifiableMap(storeFileMetaDataMap), unmodifiableMap(commitUserData), 0); } @Test