Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify map copying #88432

Merged
merged 1 commit into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ private RoutingNode(RoutingNode original) {
this.shards = new LinkedHashMap<>(original.shards);
this.relocatingShards = new LinkedHashSet<>(original.relocatingShards);
this.initializingShards = new LinkedHashSet<>(original.initializingShards);
this.shardsByIndex = Maps.newMapWithExpectedSize(original.shardsByIndex.size());
for (Map.Entry<Index, Set<ShardRouting>> entry : original.shardsByIndex.entrySet()) {
shardsByIndex.put(entry.getKey(), new HashSet<>(entry.getValue()));
}
this.shardsByIndex = Maps.copyOf(original.shardsByIndex, HashSet::new);
assert invariant();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,27 +152,14 @@ private RoutingNodes(RoutingNodes routingNodes) {
// instance
assert routingNodes.readOnly : "tried to create a mutable copy from a mutable instance";
this.readOnly = false;
this.nodesToShards = Maps.newMapWithExpectedSize(routingNodes.nodesToShards.size());
for (Map.Entry<String, RoutingNode> entry : routingNodes.nodesToShards.entrySet()) {
this.nodesToShards.put(entry.getKey(), entry.getValue().copy());
}
this.assignedShards = Maps.newMapWithExpectedSize(routingNodes.assignedShards.size());
for (Map.Entry<ShardId, List<ShardRouting>> entry : routingNodes.assignedShards.entrySet()) {
this.assignedShards.put(entry.getKey(), new ArrayList<>(entry.getValue()));
}
this.nodesToShards = Maps.copyOf(routingNodes.nodesToShards, RoutingNode::copy);
this.assignedShards = Maps.copyOf(routingNodes.assignedShards, ArrayList::new);
this.unassignedShards = routingNodes.unassignedShards.copyFor(this);

this.inactivePrimaryCount = routingNodes.inactivePrimaryCount;
this.inactiveShardCount = routingNodes.inactiveShardCount;
this.relocatingShards = routingNodes.relocatingShards;
this.attributeValuesByAttribute = Maps.newMapWithExpectedSize(routingNodes.attributeValuesByAttribute.size());
for (Map.Entry<String, Set<String>> entry : routingNodes.attributeValuesByAttribute.entrySet()) {
this.attributeValuesByAttribute.put(entry.getKey(), new HashSet<>(entry.getValue()));
}
this.recoveriesPerNode = Maps.newMapWithExpectedSize(routingNodes.recoveriesPerNode.size());
for (Map.Entry<String, Recoveries> entry : routingNodes.recoveriesPerNode.entrySet()) {
this.recoveriesPerNode.put(entry.getKey(), entry.getValue().copy());
}
this.attributeValuesByAttribute = Maps.copyOf(routingNodes.attributeValuesByAttribute, HashSet::new);
this.recoveriesPerNode = Maps.copyOf(routingNodes.recoveriesPerNode, Recoveries::copy);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions server/src/main/java/org/elasticsearch/common/util/Maps.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,14 @@ static int capacity(int expectedSize) {
throw new IllegalStateException("Unsupported input format");
}

/**
* This method creates a copy of the {@code source} map using {@code copyValueFunction} to create a defensive copy of each value.
*/
public static <K, V> Map<K, V> copyOf(Map<K, V> source, Function<V, V> copyValueFunction) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe better call if deepCopy to make it clearer from the name what this is for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep it consistent with java.util.Map#copyOf especially as it could also be used for stream>map>collect type of operations as well

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was kinda my point actually. I'd rather not have the same name as the JDK method here since this does something quite different :) But either way, this was more of a NIT, go with it if you disagree, it was more of a NIT :)

var copy = Maps.<K, V>newHashMapWithExpectedSize(source.size());
for (var entry : source.entrySet()) {
copy.put(entry.getKey(), copyValueFunction.apply(entry.getValue()));
}
return copy;
}
}