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.
This commit is contained in:
Nik Everett 2015-10-02 00:18:05 +02:00
parent a9d59024b9
commit 9430e17f70
16 changed files with 210 additions and 105 deletions

View File

@ -24,6 +24,7 @@ import com.google.common.collect.ImmutableMap;
import org.elasticsearch.cluster.AbstractDiffable; import org.elasticsearch.cluster.AbstractDiffable;
import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MetaDataIndexStateService; 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.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.RestStatus;
@ -68,7 +69,7 @@ public class ClusterBlocks extends AbstractDiffable<ClusterBlocks> {
.filter(containsLevel) .filter(containsLevel)
.collect(toSet())); .collect(toSet()));
ImmutableMap.Builder<String, Set<ClusterBlock>> indicesBuilder = ImmutableMap.builder(); ImmutableOpenMap.Builder<String, Set<ClusterBlock>> indicesBuilder = ImmutableOpenMap.builder();
for (Map.Entry<String, Set<ClusterBlock>> entry : indicesBlocks.entrySet()) { for (Map.Entry<String, Set<ClusterBlock>> entry : indicesBlocks.entrySet()) {
indicesBuilder.put(entry.getKey(), unmodifiableSet(entry.getValue().stream() indicesBuilder.put(entry.getKey(), unmodifiableSet(entry.getValue().stream()
.filter(containsLevel) .filter(containsLevel)
@ -91,7 +92,7 @@ public class ClusterBlocks extends AbstractDiffable<ClusterBlocks> {
return levelHolders[level.id()].global(); return levelHolders[level.id()].global();
} }
public Map<String, Set<ClusterBlock>> indices(ClusterBlockLevel level) { public ImmutableOpenMap<String, Set<ClusterBlock>> indices(ClusterBlockLevel level) {
return levelHolders[level.id()].indices(); return levelHolders[level.id()].indices();
} }
@ -238,12 +239,12 @@ public class ClusterBlocks extends AbstractDiffable<ClusterBlocks> {
static class ImmutableLevelHolder { static class ImmutableLevelHolder {
static final ImmutableLevelHolder EMPTY = new ImmutableLevelHolder(emptySet(), ImmutableMap.of()); static final ImmutableLevelHolder EMPTY = new ImmutableLevelHolder(emptySet(), ImmutableOpenMap.of());
private final Set<ClusterBlock> global; private final Set<ClusterBlock> global;
private final ImmutableMap<String, Set<ClusterBlock>> indices; private final ImmutableOpenMap<String, Set<ClusterBlock>> indices;
ImmutableLevelHolder(Set<ClusterBlock> global, ImmutableMap<String, Set<ClusterBlock>> indices) { ImmutableLevelHolder(Set<ClusterBlock> global, ImmutableOpenMap<String, Set<ClusterBlock>> indices) {
this.global = global; this.global = global;
this.indices = indices; this.indices = indices;
} }
@ -252,7 +253,7 @@ public class ClusterBlocks extends AbstractDiffable<ClusterBlocks> {
return global; return global;
} }
public ImmutableMap<String, Set<ClusterBlock>> indices() { public ImmutableOpenMap<String, Set<ClusterBlock>> indices() {
return indices; return indices;
} }
} }

View File

@ -19,7 +19,6 @@
package org.elasticsearch.cluster.routing; package org.elasticsearch.cluster.routing;
import com.google.common.collect.ImmutableMap;
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;
import org.elasticsearch.common.io.stream.Streamable; import org.elasticsearch.common.io.stream.Streamable;
@ -31,6 +30,8 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static java.util.Collections.emptyMap;
/** /**
* Encapsulates the result of a routing table validation and provides access to * Encapsulates the result of a routing table validation and provides access to
* validation failures. * validation failures.
@ -72,7 +73,7 @@ public class RoutingTableValidation implements Streamable {
public Map<String, List<String>> indicesFailures() { public Map<String, List<String>> indicesFailures() {
if (indicesFailures == null) { if (indicesFailures == null) {
return ImmutableMap.of(); return emptyMap();
} }
return indicesFailures; return indicesFailures;
} }
@ -128,7 +129,7 @@ public class RoutingTableValidation implements Streamable {
} }
size = in.readVInt(); size = in.readVInt();
if (size == 0) { if (size == 0) {
indicesFailures = ImmutableMap.of(); indicesFailures = emptyMap();
} else { } else {
indicesFailures = new HashMap<>(); indicesFailures = new HashMap<>();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {

View File

@ -19,13 +19,13 @@
package org.elasticsearch.common; package org.elasticsearch.common;
import com.google.common.collect.ImmutableMap;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static java.util.Collections.emptyMap;
/** /**
*/ */
public class Table { public class Table {
@ -115,7 +115,7 @@ public class Table {
Map<String, String> mAttr; Map<String, String> mAttr;
if (attributes.length() == 0) { if (attributes.length() == 0) {
if (inHeaders) { if (inHeaders) {
mAttr = ImmutableMap.of(); mAttr = emptyMap();
} else { } else {
// get the attributes of the header cell we are going to add to // get the attributes of the header cell we are going to add to
mAttr = headers.get(currentCells.size()).attr; mAttr = headers.get(currentCells.size()).attr;

View File

@ -19,14 +19,15 @@
package org.elasticsearch.common.path; package org.elasticsearch.common.path;
import com.google.common.collect.ImmutableMap;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; 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<T> {
}; };
private final Decoder decoder; private final Decoder decoder;
private final TrieNode<T> root; private final TrieNode root;
private final char separator; private final char separator;
private T rootValue; private T rootValue;
@ -60,10 +61,10 @@ public class PathTrie<T> {
public PathTrie(char separator, String wildcard, Decoder decoder) { public PathTrie(char separator, String wildcard, Decoder decoder) {
this.decoder = decoder; this.decoder = decoder;
this.separator = separator; 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<T> { public class TrieNode {
private transient String key; private transient String key;
private transient T value; private transient T value;
private boolean isWildcard; private boolean isWildcard;
@ -71,17 +72,14 @@ public class PathTrie<T> {
private transient String namedWildcard; private transient String namedWildcard;
private ImmutableMap<String, TrieNode<T>> children; private Map<String, TrieNode> children;
private final TrieNode parent; public TrieNode(String key, T value, String wildcard) {
public TrieNode(String key, T value, TrieNode parent, String wildcard) {
this.key = key; this.key = key;
this.wildcard = wildcard; this.wildcard = wildcard;
this.isWildcard = (key.equals(wildcard)); this.isWildcard = (key.equals(wildcard));
this.parent = parent;
this.value = value; this.value = value;
this.children = ImmutableMap.of(); this.children = emptyMap();
if (isNamedWildcard(key)) { if (isNamedWildcard(key)) {
namedWildcard = key.substring(key.indexOf('{') + 1, key.indexOf('}')); namedWildcard = key.substring(key.indexOf('{') + 1, key.indexOf('}'));
} else { } else {
@ -98,8 +96,14 @@ public class PathTrie<T> {
return isWildcard; return isWildcard;
} }
public synchronized void addChild(TrieNode<T> child) { public synchronized void addChild(TrieNode child) {
children = newMapBuilder(children).put(child.key, child).immutableMap(); addInnerChild(child.key, child);
}
private void addInnerChild(String key, TrieNode child) {
Map<String, TrieNode> newChildren = new HashMap<>(children);
newChildren.put(key, child);
children = unmodifiableMap(newChildren);
} }
public TrieNode getChild(String key) { public TrieNode getChild(String key) {
@ -115,14 +119,11 @@ public class PathTrie<T> {
if (isNamedWildcard(token)) { if (isNamedWildcard(token)) {
key = wildcard; key = wildcard;
} }
TrieNode<T> node = children.get(key); TrieNode node = children.get(key);
if (node == null) { if (node == null) {
if (index == (path.length - 1)) { T nodeValue = index == path.length - 1 ? value : null;
node = new TrieNode<>(token, value, this, wildcard); node = new TrieNode(token, nodeValue, wildcard);
} else { addInnerChild(key, node);
node = new TrieNode<>(token, null, this, wildcard);
}
children = newMapBuilder(children).put(key, node).immutableMap();
} else { } else {
if (isNamedWildcard(token)) { if (isNamedWildcard(token)) {
node.updateKeyWithNamedWildcard(token); node.updateKeyWithNamedWildcard(token);
@ -158,7 +159,7 @@ public class PathTrie<T> {
return null; return null;
String token = path[index]; String token = path[index];
TrieNode<T> node = children.get(token); TrieNode node = children.get(token);
boolean usedWildcard; boolean usedWildcard;
if (node == null) { if (node == null) {
node = children.get(wildcard); node = children.get(wildcard);
@ -195,11 +196,16 @@ public class PathTrie<T> {
return res; return res;
} }
private void put(Map<String, String> params, TrieNode<T> node, String value) { private void put(Map<String, String> params, TrieNode node, String value) {
if (params != null && node.isNamedWildcard()) { if (params != null && node.isNamedWildcard()) {
params.put(node.namedWildcard(), value); params.put(node.namedWildcard(), value);
} }
} }
@Override
public String toString() {
return key;
}
} }
public void insert(String path, T value) { public void insert(String path, T value) {

View File

@ -20,6 +20,7 @@
package org.elasticsearch.gateway; package org.elasticsearch.gateway;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.common.component.AbstractComponent;
@ -34,6 +35,8 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import static java.util.Collections.emptyMap;
/** /**
* The dangling indices state is responsible for finding new dangling indices (indices that have * 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 * 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(); indices = nodeEnv.findAllIndices();
} catch (Throwable e) { } catch (Throwable e) {
logger.warn("failed to list dangling indices", e); logger.warn("failed to list dangling indices", e);
return ImmutableMap.of(); return emptyMap();
} }
Map<String, IndexMetaData> newIndices = new HashMap<>(); Map<String, IndexMetaData> newIndices = new HashMap<>();

View File

@ -20,11 +20,11 @@
package org.elasticsearch.index; package org.elasticsearch.index;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import org.apache.lucene.util.Accountable; import org.apache.lucene.util.Accountable;
import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.IOUtils;
import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
@ -74,6 +74,7 @@ import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import static java.util.Collections.emptyMap;
import static org.elasticsearch.common.collect.MapBuilder.newMapBuilder; 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 NodeEnvironment nodeEnv;
private final IndicesService indicesServices; private final IndicesService indicesServices;
private volatile ImmutableMap<Integer, IndexShardInjectorPair> shards = ImmutableMap.of(); private volatile Map<Integer, IndexShardInjectorPair> shards = emptyMap();
private static class IndexShardInjectorPair { private static class IndexShardInjectorPair {
private final IndexShard indexShard; private final IndexShard indexShard;

View File

@ -19,7 +19,6 @@
package org.elasticsearch.index.get; package org.elasticsearch.index.get;
import com.google.common.collect.ImmutableMap;
import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.compress.CompressorFactory; import org.elasticsearch.common.compress.CompressorFactory;
@ -40,6 +39,7 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static java.util.Collections.emptyMap;
import static org.elasticsearch.index.get.GetField.readGetField; import static org.elasticsearch.index.get.GetField.readGetField;
/** /**
@ -68,7 +68,7 @@ public class GetResult implements Streamable, Iterable<GetField>, ToXContent {
this.source = source; this.source = source;
this.fields = fields; this.fields = fields;
if (this.fields == null) { if (this.fields == null) {
this.fields = ImmutableMap.of(); this.fields = emptyMap();
} }
} }
@ -286,7 +286,7 @@ public class GetResult implements Streamable, Iterable<GetField>, ToXContent {
} }
int size = in.readVInt(); int size = in.readVInt();
if (size == 0) { if (size == 0) {
fields = ImmutableMap.of(); fields = emptyMap();
} else { } else {
fields = new HashMap<>(size); fields = new HashMap<>(size);
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {

View File

@ -20,7 +20,6 @@
package org.elasticsearch.index.mapper; package org.elasticsearch.index.mapper;
import com.carrotsearch.hppc.ObjectHashSet; import com.carrotsearch.hppc.ObjectHashSet;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterators; import com.google.common.collect.Iterators;
import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.Analyzer;
@ -64,6 +63,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -73,7 +73,9 @@ import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Function; import java.util.function.Function;
import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet; import static java.util.Collections.emptySet;
import static java.util.Collections.unmodifiableMap;
import static java.util.Collections.unmodifiableSet; import static java.util.Collections.unmodifiableSet;
import static org.elasticsearch.common.collect.MapBuilder.newMapBuilder; 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 defaultMappingSource;
private volatile String defaultPercolatorMappingSource; private volatile String defaultPercolatorMappingSource;
private volatile Map<String, DocumentMapper> mappers = ImmutableMap.of(); private volatile Map<String, DocumentMapper> mappers = emptyMap();
// A lock for mappings: modifications (put mapping) need to be performed // A lock for mappings: modifications (put mapping) need to be performed
// under the write lock and read operations (document parsing) need to be // 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<DocumentTypeListener> typeListeners = new CopyOnWriteArrayList<>(); private final List<DocumentTypeListener> typeListeners = new CopyOnWriteArrayList<>();
private volatile ImmutableMap<String, MappedFieldType> unmappedFieldTypes = ImmutableMap.of(); private volatile Map<String, MappedFieldType> unmappedFieldTypes = emptyMap();
private volatile Set<String> parentTypes = emptySet(); private volatile Set<String> 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. * Given a type (eg. long, string, ...), return an anonymous field mapper that can be used for search operations.
*/ */
public MappedFieldType unmappedFieldType(String type) { public MappedFieldType unmappedFieldType(String type) {
final ImmutableMap<String, MappedFieldType> unmappedFieldMappers = this.unmappedFieldTypes; MappedFieldType fieldType = unmappedFieldTypes.get(type);
MappedFieldType fieldType = unmappedFieldMappers.get(type);
if (fieldType == null) { if (fieldType == null) {
final Mapper.TypeParser.ParserContext parserContext = documentMapperParser().parserContext(type); final Mapper.TypeParser.ParserContext parserContext = documentMapperParser().parserContext(type);
Mapper.TypeParser typeParser = parserContext.typeParser(type); Mapper.TypeParser typeParser = parserContext.typeParser(type);
if (typeParser == null) { if (typeParser == null) {
throw new IllegalArgumentException("No mapper found for type [" + type + "]"); throw new IllegalArgumentException("No mapper found for type [" + type + "]");
} }
final Mapper.Builder<?, ?> builder = typeParser.parse("__anonymous_" + type, ImmutableMap.<String, Object>of(), parserContext); final Mapper.Builder<?, ?> builder = typeParser.parse("__anonymous_" + type, emptyMap(), parserContext);
final BuilderContext builderContext = new BuilderContext(indexSettings, new ContentPath(1)); final BuilderContext builderContext = new BuilderContext(indexSettings, new ContentPath(1));
fieldType = ((FieldMapper)builder.build(builderContext)).fieldType(); fieldType = ((FieldMapper)builder.build(builderContext)).fieldType();
// There is no need to synchronize writes here. In the case of concurrent access, we could just // 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 // compute some mappers several times, which is not a big deal
this.unmappedFieldTypes = ImmutableMap.<String, MappedFieldType>builder() Map<String, MappedFieldType> newUnmappedFieldTypes = new HashMap<>();
.putAll(unmappedFieldMappers) newUnmappedFieldTypes.putAll(unmappedFieldTypes);
.put(type, fieldType) newUnmappedFieldTypes.put(type, fieldType);
.build(); unmappedFieldTypes = unmodifiableMap(newUnmappedFieldTypes);
} }
return fieldType; return fieldType;
} }

View File

@ -22,7 +22,6 @@ package org.elasticsearch.index.mapper;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
@ -34,6 +33,8 @@ import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static java.util.Collections.emptyMap;
/** /**
* Wrapper around everything that defines a mapping, without references to * Wrapper around everything that defines a mapping, without references to
* utility classes like MapperService, ... * utility classes like MapperService, ...
@ -150,22 +151,11 @@ public final class Mapping implements ToXContent {
return builder; return builder;
} }
/** Serialize to a {@link BytesReference}. */
public BytesReference toBytes() {
try {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
toXContent(builder, new ToXContent.MapParams(ImmutableMap.<String, String>of()));
return builder.endObject().bytes();
} catch (IOException bogus) {
throw new AssertionError(bogus);
}
}
@Override @Override
public String toString() { public String toString() {
try { try {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject(); XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
toXContent(builder, new ToXContent.MapParams(ImmutableMap.<String, String>of())); toXContent(builder, new ToXContent.MapParams(emptyMap()));
return builder.endObject().string(); return builder.endObject().string();
} catch (IOException bogus) { } catch (IOException bogus) {
throw new AssertionError(bogus); throw new AssertionError(bogus);

View File

@ -19,7 +19,11 @@
package org.elasticsearch.index.snapshots.blobstore; 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.IOContext;
import org.apache.lucene.store.IndexInput; import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput; 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.common.util.iterable.Iterables;
import org.elasticsearch.index.IndexService; import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.shard.ShardId; 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.snapshots.blobstore.BlobStoreIndexShardSnapshot.FileInfo;
import org.elasticsearch.index.store.Store; import org.elasticsearch.index.store.Store;
import org.elasticsearch.index.store.StoreFileMetaData; import org.elasticsearch.index.store.StoreFileMetaData;
@ -63,8 +71,15 @@ import org.elasticsearch.repositories.blobstore.LegacyBlobStoreFormat;
import java.io.FilterInputStream; import java.io.FilterInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; 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; 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()); snapshotMetaData.put(fileInfo.metadata().name(), fileInfo.metadata());
fileInfos.put(fileInfo.metadata().name(), fileInfo); 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); final Store.RecoveryDiff diff = sourceMetaData.recoveryDiff(recoveryTargetMetadata);
for (StoreFileMetaData md : diff.identical) { for (StoreFileMetaData md : diff.identical) {
FileInfo fileInfo = fileInfos.get(md.name()); FileInfo fileInfo = fileInfos.get(md.name());

View File

@ -20,13 +20,35 @@
package org.elasticsearch.index.store; package org.elasticsearch.index.store;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import org.apache.lucene.codecs.CodecUtil; import org.apache.lucene.codecs.CodecUtil;
import org.apache.lucene.index.*; import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.store.*; import org.apache.lucene.index.IndexCommit;
import org.apache.lucene.util.*; 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.ElasticsearchException;
import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.ExceptionsHelper;
import org.apache.lucene.util.Version;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.Tuple; 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.settings.Settings;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.util.Callback; import org.elasticsearch.common.util.Callback;
import org.elasticsearch.common.util.iterable.Iterables;
import org.elasticsearch.common.util.SingleObjectCache; import org.elasticsearch.common.util.SingleObjectCache;
import org.elasticsearch.common.util.concurrent.AbstractRefCounted; import org.elasticsearch.common.util.concurrent.AbstractRefCounted;
import org.elasticsearch.common.util.concurrent.RefCounted; import org.elasticsearch.common.util.concurrent.RefCounted;
import org.elasticsearch.common.util.iterable.Iterables;
import org.elasticsearch.env.ShardLock; import org.elasticsearch.env.ShardLock;
import org.elasticsearch.index.engine.Engine; import org.elasticsearch.index.engine.Engine;
import org.elasticsearch.index.settings.IndexSettings; import org.elasticsearch.index.settings.IndexSettings;
import org.elasticsearch.index.shard.AbstractIndexShardComponent; import org.elasticsearch.index.shard.AbstractIndexShardComponent;
import org.elasticsearch.index.shard.ShardId; 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.NoSuchFileException;
import java.nio.file.Path; 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.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.zip.Adler32; import java.util.zip.Adler32;
import java.util.zip.CRC32; import java.util.zip.CRC32;
import java.util.zip.Checksum; 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 * 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 * 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 ESLogger logger = Loggers.getLogger(MetadataSnapshot.class);
private static final Version FIRST_LUCENE_CHECKSUM_VERSION = Version.LUCENE_4_8; private static final Version FIRST_LUCENE_CHECKSUM_VERSION = Version.LUCENE_4_8;
private final ImmutableMap<String, StoreFileMetaData> metadata; private final Map<String, StoreFileMetaData> metadata;
public static final MetadataSnapshot EMPTY = new MetadataSnapshot(); public static final MetadataSnapshot EMPTY = new MetadataSnapshot();
@ -743,16 +777,14 @@ public class Store extends AbstractIndexShardComponent implements Closeable, Ref
private final long numDocs; private final long numDocs;
public MetadataSnapshot(Map<String, StoreFileMetaData> metadata, Map<String, String> commitUserData, long numDocs) { public MetadataSnapshot(Map<String, StoreFileMetaData> metadata, Map<String, String> commitUserData, long numDocs) {
ImmutableMap.Builder<String, StoreFileMetaData> metaDataBuilder = ImmutableMap.builder(); this.metadata = metadata;
this.metadata = metaDataBuilder.putAll(metadata).build(); this.commitUserData = commitUserData;
ImmutableMap.Builder<String, String> commitUserDataBuilder = ImmutableMap.builder();
this.commitUserData = commitUserDataBuilder.putAll(commitUserData).build();
this.numDocs = numDocs; this.numDocs = numDocs;
} }
MetadataSnapshot() { MetadataSnapshot() {
metadata = ImmutableMap.of(); metadata = emptyMap();
commitUserData = ImmutableMap.of(); commitUserData = emptyMap();
numDocs = 0; numDocs = 0;
} }

View File

@ -18,21 +18,22 @@
*/ */
package org.elasticsearch.search.aggregations.pipeline; package org.elasticsearch.search.aggregations.pipeline;
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.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 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 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.
@ -48,11 +49,11 @@ 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) {
MapBuilder<BytesReference, Stream> uStreams = MapBuilder.newMapBuilder(streams); Map<BytesReference, Stream> newStreams = new HashMap<>(streams);
for (BytesReference type : types) { for (BytesReference type : types) {
uStreams.put(type, stream); newStreams.put(type, stream);
} }
streams = uStreams.immutableMap(); streams = unmodifiableMap(newStreams);
} }
/** /**

View File

@ -19,10 +19,10 @@
package org.elasticsearch.search.dfs; package org.elasticsearch.search.dfs;
import com.carrotsearch.hppc.ObjectObjectHashMap;
import com.carrotsearch.hppc.ObjectHashSet; import com.carrotsearch.hppc.ObjectHashSet;
import com.carrotsearch.hppc.ObjectObjectHashMap;
import com.carrotsearch.hppc.cursors.ObjectCursor; import com.carrotsearch.hppc.cursors.ObjectCursor;
import com.google.common.collect.ImmutableMap;
import org.apache.lucene.index.IndexReaderContext; import org.apache.lucene.index.IndexReaderContext;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermContext; import org.apache.lucene.index.TermContext;
@ -39,6 +39,8 @@ import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import static java.util.Collections.emptyMap;
/** /**
* *
*/ */
@ -46,7 +48,7 @@ public class DfsPhase implements SearchPhase {
@Override @Override
public Map<String, ? extends SearchParseElement> parseElements() { public Map<String, ? extends SearchParseElement> parseElements() {
return ImmutableMap.of(); return emptyMap();
} }
@Override @Override

View File

@ -20,7 +20,6 @@
package org.elasticsearch.common.path; package org.elasticsearch.common.path;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.junit.Test;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;

View File

@ -20,6 +20,7 @@
package org.elasticsearch.gateway; package org.elasticsearch.gateway;
import com.carrotsearch.randomizedtesting.generators.RandomPicks; import com.carrotsearch.randomizedtesting.generators.RandomPicks;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.cluster.ClusterInfo; import org.elasticsearch.cluster.ClusterInfo;
import org.elasticsearch.cluster.ClusterState; 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.metadata.MetaData;
import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodes; 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.RoutingAllocation;
import org.elasticsearch.cluster.routing.allocation.decider.AllocationDecider; import org.elasticsearch.cluster.routing.allocation.decider.AllocationDecider;
import org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders; import org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders;
@ -49,6 +58,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import static java.util.Collections.unmodifiableMap;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
/** /**
@ -358,7 +368,8 @@ public class ReplicaShardAllocatorTests extends ESAllocationTestCase {
if (syncId != null) { if (syncId != null) {
commitData.put(Engine.SYNC_COMMIT_ID, syncId); 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; return this;
} }

View File

@ -24,9 +24,35 @@ import org.apache.lucene.codecs.FilterCodec;
import org.apache.lucene.codecs.SegmentInfoFormat; import org.apache.lucene.codecs.SegmentInfoFormat;
import org.apache.lucene.codecs.lucene50.Lucene50SegmentInfoFormat; import org.apache.lucene.codecs.lucene50.Lucene50SegmentInfoFormat;
import org.apache.lucene.codecs.lucene53.Lucene53Codec; import org.apache.lucene.codecs.lucene53.Lucene53Codec;
import org.apache.lucene.document.*; import org.apache.lucene.document.Document;
import org.apache.lucene.index.*; import org.apache.lucene.document.Field;
import org.apache.lucene.store.*; 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.BytesRef;
import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.TestUtil; import org.apache.lucene.util.TestUtil;
@ -55,14 +81,30 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.NoSuchFileException; import java.nio.file.NoSuchFileException;
import java.nio.file.Path; 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.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.zip.Adler32; 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.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 { public class StoreTests extends ESTestCase {
@ -800,9 +842,9 @@ public class StoreTests extends ESTestCase {
Map<String, StoreFileMetaData> metaDataMap = new HashMap<>(); Map<String, StoreFileMetaData> metaDataMap = new HashMap<>();
metaDataMap.put("segments_1", new StoreFileMetaData("segments_1", 50, null, null, new BytesRef(new byte[]{1}))); 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())); 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); Store.RecoveryDiff recoveryDiff = first.recoveryDiff(second);
assertEquals(recoveryDiff.toString(), recoveryDiff.different.size(), 2); assertEquals(recoveryDiff.toString(), recoveryDiff.different.size(), 2);
} }
@ -1068,7 +1110,7 @@ public class StoreTests extends ESTestCase {
Map<String, StoreFileMetaData> metaDataMap = new HashMap<>(); Map<String, StoreFileMetaData> metaDataMap = new HashMap<>();
metaDataMap.put("segments_1", new StoreFileMetaData("segments_1", 50, null, null, new BytesRef(new byte[]{1}))); 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())); 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); final ShardId shardId = new ShardId(new Index("index"), 1);
DirectoryService directoryService = new LuceneManagedDirectoryService(random()); DirectoryService directoryService = new LuceneManagedDirectoryService(random());
@ -1202,7 +1244,7 @@ public class StoreTests extends ESTestCase {
Map<String, String> commitUserData = new HashMap<>(); Map<String, String> commitUserData = new HashMap<>();
commitUserData.put("userdata_1", "test"); commitUserData.put("userdata_1", "test");
commitUserData.put("userdata_2", "test"); commitUserData.put("userdata_2", "test");
return new Store.MetadataSnapshot(storeFileMetaDataMap, commitUserData, 0); return new Store.MetadataSnapshot(unmodifiableMap(storeFileMetaDataMap), unmodifiableMap(commitUserData), 0);
} }
@Test @Test