Skip to content

Commit f1e0d34

Browse files
katrecopybara-github
authored andcommitted
Clean up RuleContext to use a Table instead of a Map of Maps.
Closes #13164. PiperOrigin-RevId: 361216667
1 parent d067669 commit f1e0d34

File tree

1 file changed

+25
-41
lines changed

1 file changed

+25
-41
lines changed

src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.google.common.collect.ImmutableMap;
2929
import com.google.common.collect.ImmutableSet;
3030
import com.google.common.collect.ImmutableSortedSet;
31+
import com.google.common.collect.ImmutableTable;
3132
import com.google.common.collect.Iterables;
3233
import com.google.common.collect.ListMultimap;
3334
import com.google.common.collect.Lists;
@@ -217,7 +218,7 @@ public ImmutableList<TransitiveInfoCollection> getFiles() {
217218
private final ConstraintSemantics<RuleContext> constraintSemantics;
218219
private final ImmutableSet<String> requiredConfigFragments;
219220
private final List<Expander> makeVariableExpanders = new ArrayList<>();
220-
private final ImmutableMap<String, ImmutableMap<String, String>> execProperties;
221+
private final ImmutableTable<String, String, String> execProperties;
221222

222223
/** Map of exec group names to ActionOwners. */
223224
private final Map<String, ActionOwner> actionOwners = new HashMap<>();
@@ -635,17 +636,16 @@ private static ImmutableMap<String, String> computeExecProperties(
635636
Map<String, String> execProperties = new HashMap<>();
636637

637638
if (executionPlatform != null) {
638-
Map<String, Map<String, String>> execPropertiesPerGroup =
639+
ImmutableTable<String, String, String> execPropertiesPerGroup =
639640
parseExecGroups(executionPlatform.execProperties());
640641

641-
if (execPropertiesPerGroup.containsKey(DEFAULT_EXEC_GROUP_NAME)) {
642-
execProperties.putAll(execPropertiesPerGroup.get(DEFAULT_EXEC_GROUP_NAME));
643-
execPropertiesPerGroup.remove(DEFAULT_EXEC_GROUP_NAME);
642+
if (execPropertiesPerGroup.containsRow(DEFAULT_EXEC_GROUP_NAME)) {
643+
execProperties.putAll(execPropertiesPerGroup.row(DEFAULT_EXEC_GROUP_NAME));
644644
}
645645

646-
for (Map.Entry<String, Map<String, String>> execGroup : execPropertiesPerGroup.entrySet()) {
647-
if (execGroups.contains(execGroup.getKey())) {
648-
execProperties.putAll(execGroup.getValue());
646+
for (String execGroup : execPropertiesPerGroup.rowKeySet()) {
647+
if (execGroups.contains(execGroup)) {
648+
execProperties.putAll(execPropertiesPerGroup.row(execGroup));
649649
}
650650
}
651651
}
@@ -1404,10 +1404,10 @@ public ImmutableSortedSet<String> getRequiredConfigFragments() {
14041404
return ans.build();
14051405
}
14061406

1407-
private ImmutableMap<String, ImmutableMap<String, String>> parseExecProperties(
1407+
private ImmutableTable<String, String, String> parseExecProperties(
14081408
Map<String, String> execProperties) throws InvalidExecGroupException {
14091409
if (execProperties.isEmpty()) {
1410-
return ImmutableMap.of(DEFAULT_EXEC_GROUP_NAME, ImmutableMap.of());
1410+
return ImmutableTable.of();
14111411
} else {
14121412
return parseExecProperties(
14131413
execProperties, toolchainContexts == null ? null : toolchainContexts.getExecGroups());
@@ -1420,39 +1420,35 @@ private ImmutableMap<String, ImmutableMap<String, String>> parseExecProperties(
14201420
* former get parsed into the default exec group, the latter get parsed into their relevant exec
14211421
* groups.
14221422
*/
1423-
private static Map<String, Map<String, String>> parseExecGroups(
1423+
private static ImmutableTable<String, String, String> parseExecGroups(
14241424
Map<String, String> rawExecProperties) {
1425-
Map<String, Map<String, String>> consolidatedProperties = new HashMap<>();
1426-
consolidatedProperties.put(DEFAULT_EXEC_GROUP_NAME, new HashMap<>());
1425+
ImmutableTable.Builder<String, String, String> execProperties = ImmutableTable.builder();
14271426
for (Map.Entry<String, String> execProperty : rawExecProperties.entrySet()) {
14281427
String rawProperty = execProperty.getKey();
14291428
int delimiterIndex = rawProperty.indexOf('.');
14301429
if (delimiterIndex == -1) {
1431-
consolidatedProperties
1432-
.get(DEFAULT_EXEC_GROUP_NAME)
1433-
.put(rawProperty, execProperty.getValue());
1430+
execProperties.put(DEFAULT_EXEC_GROUP_NAME, rawProperty, execProperty.getValue());
14341431
} else {
14351432
String execGroup = rawProperty.substring(0, delimiterIndex);
14361433
String property = rawProperty.substring(delimiterIndex + 1);
1437-
consolidatedProperties.putIfAbsent(execGroup, new HashMap<>());
1438-
consolidatedProperties.get(execGroup).put(property, execProperty.getValue());
1434+
execProperties.put(execGroup, property, execProperty.getValue());
14391435
}
14401436
}
1441-
return consolidatedProperties;
1437+
return execProperties.build();
14421438
}
14431439

14441440
/**
14451441
* Parse raw exec properties attribute value into a map of exec group names to their properties.
14461442
* If given a set of exec groups, validates all the exec groups in the map are applicable to the
14471443
* action.
14481444
*/
1449-
private static ImmutableMap<String, ImmutableMap<String, String>> parseExecProperties(
1445+
private static ImmutableTable<String, String, String> parseExecProperties(
14501446
Map<String, String> rawExecProperties, @Nullable Set<String> execGroups)
14511447
throws InvalidExecGroupException {
1452-
Map<String, Map<String, String>> consolidatedProperties = parseExecGroups(rawExecProperties);
1448+
ImmutableTable<String, String, String> consolidatedProperties =
1449+
parseExecGroups(rawExecProperties);
14531450
if (execGroups != null) {
1454-
for (Map.Entry<String, Map<String, String>> execGroup : consolidatedProperties.entrySet()) {
1455-
String execGroupName = execGroup.getKey();
1451+
for (String execGroupName : consolidatedProperties.rowKeySet()) {
14561452
if (!execGroupName.equals(DEFAULT_EXEC_GROUP_NAME) && !execGroups.contains(execGroupName)) {
14571453
throw new InvalidExecGroupException(
14581454
String.format(
@@ -1461,14 +1457,7 @@ private static ImmutableMap<String, ImmutableMap<String, String>> parseExecPrope
14611457
}
14621458
}
14631459

1464-
// Copy everything to immutable maps.
1465-
ImmutableMap.Builder<String, ImmutableMap<String, String>> execProperties =
1466-
new ImmutableMap.Builder<>();
1467-
for (Map.Entry<String, Map<String, String>> execGroupMap : consolidatedProperties.entrySet()) {
1468-
execProperties.put(execGroupMap.getKey(), ImmutableMap.copyOf(execGroupMap.getValue()));
1469-
}
1470-
1471-
return execProperties.build();
1460+
return consolidatedProperties;
14721461
}
14731462

14741463
/**
@@ -1480,16 +1469,16 @@ private static ImmutableMap<String, ImmutableMap<String, String>> parseExecPrope
14801469
* @param execProperties Map of exec group name to map of properties and values
14811470
*/
14821471
private static ImmutableMap<String, String> getExecProperties(
1483-
String execGroup, Map<String, ImmutableMap<String, String>> execProperties) {
1484-
if (!execProperties.containsKey(execGroup) || execGroup.equals(DEFAULT_EXEC_GROUP_NAME)) {
1485-
return execProperties.get(DEFAULT_EXEC_GROUP_NAME);
1472+
String execGroup, ImmutableTable<String, String, String> execProperties) {
1473+
if (!execProperties.containsRow(execGroup) || execGroup.equals(DEFAULT_EXEC_GROUP_NAME)) {
1474+
return execProperties.row(DEFAULT_EXEC_GROUP_NAME);
14861475
}
14871476

14881477
// Use a HashMap to build here because we expect duplicate keys to happen
14891478
// (and rewrite previous entries).
14901479
Map<String, String> targetAndGroupProperties =
1491-
new HashMap<>(execProperties.get(DEFAULT_EXEC_GROUP_NAME));
1492-
targetAndGroupProperties.putAll(execProperties.get(execGroup));
1480+
new HashMap<>(execProperties.row(DEFAULT_EXEC_GROUP_NAME));
1481+
targetAndGroupProperties.putAll(execProperties.row(execGroup));
14931482
return ImmutableMap.copyOf(targetAndGroupProperties);
14941483
}
14951484

@@ -1510,11 +1499,6 @@ public DetailedExitCode getDetailedExitCode() {
15101499
}
15111500
}
15121501

1513-
@VisibleForTesting
1514-
public ImmutableMap<String, ImmutableMap<String, String>> getExecPropertiesForTesting() {
1515-
return execProperties;
1516-
}
1517-
15181502
private void checkAttributeIsDependency(String attributeName) {
15191503
Attribute attributeDefinition = attributes.getAttributeDefinition(attributeName);
15201504
if (attributeDefinition == null) {

0 commit comments

Comments
 (0)