Skip to content

Commit

Permalink
Less copying
Browse files Browse the repository at this point in the history
  • Loading branch information
n1v0lg committed Sep 30, 2024
1 parent f6fb4fc commit c8842fe
Showing 1 changed file with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

import org.elasticsearch.xpack.core.security.authc.support.mapper.ExpressionRoleMapping;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class ReservedRoleMappings {
private final ClusterStateRoleMapper clusterStateRoleMapper;
Expand All @@ -26,14 +26,17 @@ public List<ExpressionRoleMapping> combineWithReserved(List<ExpressionRoleMappin
if (reservedRoleMappings.isEmpty()) {
return roleMappings;
}
final Set<String> reservedNames = reservedRoleMappings.stream().map(ExpressionRoleMapping::getName).collect(Collectors.toSet());
final List<ExpressionRoleMapping> filteredNativeRoleMappings = roleMappings.stream()
.filter(roleMapping -> false == reservedNames.contains(roleMapping.getName()))
.toList();
// TODO optimize
final List<ExpressionRoleMapping> combined = new ArrayList<>(reservedRoleMappings);
combined.addAll(filteredNativeRoleMappings);
return List.copyOf(combined);
if (roleMappings.isEmpty()) {
return List.copyOf(reservedRoleMappings);
}
final Map<String, ExpressionRoleMapping> combinedMappings = new LinkedHashMap<>();
for (ExpressionRoleMapping mapping : reservedRoleMappings) {
combinedMappings.put(mapping.getName(), mapping);
}
for (ExpressionRoleMapping mapping : roleMappings) {
combinedMappings.putIfAbsent(mapping.getName(), mapping);
}
return List.copyOf(combinedMappings.values());
}

public boolean isReserved(String roleMappingName) {
Expand Down

0 comments on commit c8842fe

Please sign in to comment.