Skip to content

Commit 0768557

Browse files
authored
Simplify map copying (elastic#88432)
This commit introduces a method that simplifies creating a map deep copy
1 parent 5d23480 commit 0768557

File tree

3 files changed

+15
-21
lines changed

3 files changed

+15
-21
lines changed

server/src/main/java/org/elasticsearch/cluster/routing/RoutingNode.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,7 @@ private RoutingNode(RoutingNode original) {
6969
this.shards = new LinkedHashMap<>(original.shards);
7070
this.relocatingShards = new LinkedHashSet<>(original.relocatingShards);
7171
this.initializingShards = new LinkedHashSet<>(original.initializingShards);
72-
this.shardsByIndex = Maps.newMapWithExpectedSize(original.shardsByIndex.size());
73-
for (Map.Entry<Index, Set<ShardRouting>> entry : original.shardsByIndex.entrySet()) {
74-
shardsByIndex.put(entry.getKey(), new HashSet<>(entry.getValue()));
75-
}
72+
this.shardsByIndex = Maps.copyOf(original.shardsByIndex, HashSet::new);
7673
assert invariant();
7774
}
7875

server/src/main/java/org/elasticsearch/cluster/routing/RoutingNodes.java

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -152,27 +152,14 @@ private RoutingNodes(RoutingNodes routingNodes) {
152152
// instance
153153
assert routingNodes.readOnly : "tried to create a mutable copy from a mutable instance";
154154
this.readOnly = false;
155-
this.nodesToShards = Maps.newMapWithExpectedSize(routingNodes.nodesToShards.size());
156-
for (Map.Entry<String, RoutingNode> entry : routingNodes.nodesToShards.entrySet()) {
157-
this.nodesToShards.put(entry.getKey(), entry.getValue().copy());
158-
}
159-
this.assignedShards = Maps.newMapWithExpectedSize(routingNodes.assignedShards.size());
160-
for (Map.Entry<ShardId, List<ShardRouting>> entry : routingNodes.assignedShards.entrySet()) {
161-
this.assignedShards.put(entry.getKey(), new ArrayList<>(entry.getValue()));
162-
}
155+
this.nodesToShards = Maps.copyOf(routingNodes.nodesToShards, RoutingNode::copy);
156+
this.assignedShards = Maps.copyOf(routingNodes.assignedShards, ArrayList::new);
163157
this.unassignedShards = routingNodes.unassignedShards.copyFor(this);
164-
165158
this.inactivePrimaryCount = routingNodes.inactivePrimaryCount;
166159
this.inactiveShardCount = routingNodes.inactiveShardCount;
167160
this.relocatingShards = routingNodes.relocatingShards;
168-
this.attributeValuesByAttribute = Maps.newMapWithExpectedSize(routingNodes.attributeValuesByAttribute.size());
169-
for (Map.Entry<String, Set<String>> entry : routingNodes.attributeValuesByAttribute.entrySet()) {
170-
this.attributeValuesByAttribute.put(entry.getKey(), new HashSet<>(entry.getValue()));
171-
}
172-
this.recoveriesPerNode = Maps.newMapWithExpectedSize(routingNodes.recoveriesPerNode.size());
173-
for (Map.Entry<String, Recoveries> entry : routingNodes.recoveriesPerNode.entrySet()) {
174-
this.recoveriesPerNode.put(entry.getKey(), entry.getValue().copy());
175-
}
161+
this.attributeValuesByAttribute = Maps.copyOf(routingNodes.attributeValuesByAttribute, HashSet::new);
162+
this.recoveriesPerNode = Maps.copyOf(routingNodes.recoveriesPerNode, Recoveries::copy);
176163
}
177164

178165
/**

server/src/main/java/org/elasticsearch/common/util/Maps.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,14 @@ static int capacity(int expectedSize) {
298298
throw new IllegalStateException("Unsupported input format");
299299
}
300300

301+
/**
302+
* This method creates a copy of the {@code source} map using {@code copyValueFunction} to create a defensive copy of each value.
303+
*/
304+
public static <K, V> Map<K, V> copyOf(Map<K, V> source, Function<V, V> copyValueFunction) {
305+
var copy = Maps.<K, V>newHashMapWithExpectedSize(source.size());
306+
for (var entry : source.entrySet()) {
307+
copy.put(entry.getKey(), copyValueFunction.apply(entry.getValue()));
308+
}
309+
return copy;
310+
}
301311
}

0 commit comments

Comments
 (0)