refactor(resource): Refactor StaticSelector.match to avoid throwing exceptions#27159
refactor(resource): Refactor StaticSelector.match to avoid throwing exceptions#27159ceekay47 wants to merge 1 commit intoprestodb:masterfrom
Conversation
Reviewer's GuideStaticSelector now precomputes named capture groups per regex pattern at construction time and reuses them in addVariableValues, eliminating per-variable IllegalArgumentException-based control flow and reducing repeated regex work. Sequence diagram for updated addVariableValues flowsequenceDiagram
participant SS as StaticSelector
participant P as Pattern
participant C as CandidateString
participant M as MappingMap
participant G as PatternNamedGroups
participant Ma as Matcher
SS->>G: getOrDefault(P, emptySet)
G-->>SS: groups
SS->>SS: check groups isEmpty
alt groups empty
SS-->>M: return without changes
else groups not empty
SS->>P: matcher(C)
P-->>Ma: new Matcher
SS->>Ma: find()
alt match found
loop for each key in groups
SS->>Ma: group(key)
Ma-->>SS: value
SS->>M: put(key, value) if value not null
end
else no match
SS-->>M: return without changes
end
end
Class diagram for updated StaticSelector precomputed named groupsclassDiagram
class StaticSelector {
- Optional userRegex
- Optional sourceRegex
- Optional principalRegex
- ResourceGroupIdTemplate group
- Set variableNames
- ImmutableMap patternNamedGroups
+ StaticSelector(Optional userRegex, Optional sourceRegex, Optional principalRegex, ResourceGroupIdTemplate group)
- static void addNamedGroups(Pattern pattern, HashSet variables)
- static ImmutableSet extractNamedGroups(Pattern pattern)
- void addVariableValues(Pattern pattern, String candidate, Map mapping)
}
StaticSelector --> Pattern : uses
StaticSelector --> ImmutableMap : stores
StaticSelector --> ImmutableSet : stores
StaticSelector --> Matcher : uses
StaticSelector --> ResourceGroupIdTemplate : uses
StaticSelector --> Set : uses
StaticSelector --> Map : uses
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `presto-resource-group-managers/src/main/java/com/facebook/presto/resourceGroups/StaticSelector.java:178-185` </location>
<code_context>
}
}
+ private static ImmutableSet<String> extractNamedGroups(Pattern pattern)
+ {
+ ImmutableSet.Builder<String> groups = ImmutableSet.builder();
+ Matcher matcher = NAMED_GROUPS_PATTERN.matcher(pattern.pattern());
+ while (matcher.find()) {
+ groups.add(matcher.group(1));
+ }
+ return groups.build();
+ }
+
</code_context>
<issue_to_address>
**suggestion:** Named-group extraction logic is duplicated and could be consolidated.
This duplicates the existing `NAMED_GROUPS_PATTERN` scan in `addNamedGroups`, differing only in how the result is stored. To avoid maintaining the parsing logic in two places, you could extract a single helper that returns a `Set<String>` of group names, then have `addNamedGroups` and `extractNamedGroups` both delegate to it (e.g., `addAll` on the returned set or wrap it in an `ImmutableSet`).
Suggested implementation:
```java
private static ImmutableSet<String> extractNamedGroups(Pattern pattern)
{
return ImmutableSet.copyOf(parseNamedGroups(pattern));
}
private static Set<String> parseNamedGroups(Pattern pattern)
{
Set<String> groups = new HashSet<>();
Matcher matcher = NAMED_GROUPS_PATTERN.matcher(pattern.pattern());
while (matcher.find()) {
groups.add(matcher.group(1));
}
return groups;
}
```
```java
private static void addNamedGroups(Pattern pattern, Set<String> variableNames)
{
variableNames.addAll(parseNamedGroups(pattern));
}
```
1. If `HashSet` is not yet imported in this file, add `import java.util.HashSet;` near the other `java.util` imports.
2. If the existing `addNamedGroups` method signature differs (e.g., different parameter names or modifiers), adjust the `SEARCH` and `REPLACE` blocks to match the exact current signature and style.
3. If there is an existing `parseNamedGroups`-like helper, merge with it instead of introducing a duplicate.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| private static ImmutableSet<String> extractNamedGroups(Pattern pattern) | ||
| { | ||
| ImmutableSet.Builder<String> groups = ImmutableSet.builder(); | ||
| Matcher matcher = NAMED_GROUPS_PATTERN.matcher(pattern.pattern()); | ||
| while (matcher.find()) { | ||
| groups.add(matcher.group(1)); | ||
| } | ||
| return groups.build(); |
There was a problem hiding this comment.
suggestion: Named-group extraction logic is duplicated and could be consolidated.
This duplicates the existing NAMED_GROUPS_PATTERN scan in addNamedGroups, differing only in how the result is stored. To avoid maintaining the parsing logic in two places, you could extract a single helper that returns a Set<String> of group names, then have addNamedGroups and extractNamedGroups both delegate to it (e.g., addAll on the returned set or wrap it in an ImmutableSet).
Suggested implementation:
private static ImmutableSet<String> extractNamedGroups(Pattern pattern)
{
return ImmutableSet.copyOf(parseNamedGroups(pattern));
}
private static Set<String> parseNamedGroups(Pattern pattern)
{
Set<String> groups = new HashSet<>();
Matcher matcher = NAMED_GROUPS_PATTERN.matcher(pattern.pattern());
while (matcher.find()) {
groups.add(matcher.group(1));
}
return groups;
} private static void addNamedGroups(Pattern pattern, Set<String> variableNames)
{
variableNames.addAll(parseNamedGroups(pattern));
}- If
HashSetis not yet imported in this file, addimport java.util.HashSet;near the otherjava.utilimports. - If the existing
addNamedGroupsmethod signature differs (e.g., different parameter names or modifiers), adjust theSEARCHandREPLACEblocks to match the exact current signature and style. - If there is an existing
parseNamedGroups-like helper, merge with it instead of introducing a duplicate.
2819621 to
b764af5
Compare
…27159) Summary: The `addVariableValues` method in `StaticSelector` previously iterated over all `variableNames` and caught `IllegalArgumentException` for each name not present as a named group in the pattern. This is inefficient and relies on exception handling for control flow. This change pre-computes the named groups for each regex pattern at construction time into an `ImmutableMap<Pattern, ImmutableSet<String>>`. The `addVariableValues` method now looks up the pre-computed groups for the given pattern and only iterates over matching group names, avoiding the need to catch exceptions entirely. Differential Revision: D93437091
…27159) Summary: Pull Request resolved: prestodb#27159 The `addVariableValues` method in `StaticSelector` previously iterated over all `variableNames` and caught `IllegalArgumentException` for each name not present as a named group in the pattern. This is inefficient and relies on exception handling for control flow. This change pre-computes the named groups for each regex pattern at construction time into an `ImmutableMap<Pattern, ImmutableSet<String>>`. The `addVariableValues` method now looks up the pre-computed groups for the given pattern and only iterates over matching group names, avoiding the need to catch exceptions entirely. Differential Revision: D93437091
50a101f to
81b3a26
Compare
…27159) Summary: The `addVariableValues` method in `StaticSelector` previously iterated over all `variableNames` and caught `IllegalArgumentException` for each name not present as a named group in the pattern. This is inefficient and relies on exception handling for control flow. This change pre-computes the named groups for each regex pattern at construction time into an `ImmutableMap<Pattern, ImmutableSet<String>>`. The `addVariableValues` method now looks up the pre-computed groups for the given pattern and only iterates over matching group names, avoiding the need to catch exceptions entirely. Differential Revision: D93437091
…27159) Summary: Pull Request resolved: prestodb#27159 The `addVariableValues` method in `StaticSelector` previously iterated over all `variableNames` and caught `IllegalArgumentException` for each name not present as a named group in the pattern. This is inefficient and relies on exception handling for control flow. This change pre-computes the named groups for each regex pattern at construction time into an `ImmutableMap<Pattern, ImmutableSet<String>>`. The `addVariableValues` method now looks up the pre-computed groups for the given pattern and only iterates over matching group names, avoiding the need to catch exceptions entirely. Differential Revision: D93437091
81b3a26 to
3ed6a17
Compare
…27159) Summary: The `addVariableValues` method in `StaticSelector` previously iterated over all `variableNames` and caught `IllegalArgumentException` for each name not present as a named group in the pattern. This is inefficient and relies on exception handling for control flow. This change pre-computes the named groups for each regex pattern at construction time into an `ImmutableMap<Pattern, ImmutableSet<String>>`. The `addVariableValues` method now looks up the pre-computed groups for the given pattern and only iterates over matching group names, avoiding the need to catch exceptions entirely. Differential Revision: D93437091
3ed6a17 to
d8306a3
Compare
…27159) Summary: Pull Request resolved: prestodb#27159 The `addVariableValues` method in `StaticSelector` previously iterated over all `variableNames` and caught `IllegalArgumentException` for each name not present as a named group in the pattern. This is inefficient and relies on exception handling for control flow. This change pre-computes the named groups for each regex pattern at construction time into an `ImmutableMap<Pattern, ImmutableSet<String>>`. The `addVariableValues` method now looks up the pre-computed groups for the given pattern and only iterates over matching group names, avoiding the need to catch exceptions entirely. Differential Revision: D93437091
e840536 to
9f74fcc
Compare
…27159) Summary: The `addVariableValues` method in `StaticSelector` previously iterated over all `variableNames` and caught `IllegalArgumentException` for each name not present as a named group in the pattern. This is inefficient and relies on exception handling for control flow. This change pre-computes the named groups for each regex pattern at construction time into an `ImmutableMap<Pattern, ImmutableSet<String>>`. The `addVariableValues` method now looks up the pre-computed groups for the given pattern and only iterates over matching group names, avoiding the need to catch exceptions entirely. Differential Revision: D93437091
…27159) Summary: Pull Request resolved: prestodb#27159 The `addVariableValues` method in `StaticSelector` previously iterated over all `variableNames` and caught `IllegalArgumentException` for each name not present as a named group in the pattern. This is inefficient and relies on exception handling for control flow. This change pre-computes the named groups for each regex pattern at construction time into an `ImmutableMap<Pattern, ImmutableSet<String>>`. The `addVariableValues` method now looks up the pre-computed groups for the given pattern and only iterates over matching group names, avoiding the need to catch exceptions entirely. Differential Revision: D93437091
9f74fcc to
4fd127a
Compare
Summary: The `addVariableValues` method in `StaticSelector` previously iterated over all `variableNames` and caught `IllegalArgumentException` for each name not present as a named group in the pattern. This is inefficient and relies on exception handling for control flow. This change pre-computes the named groups for each regex pattern at construction time into an `ImmutableMap<Pattern, ImmutableSet<String>>`. The `addVariableValues` method now looks up the pre-computed groups for the given pattern and only iterates over matching group names, avoiding the need to catch exceptions entirely. Added unit tests covering named group extraction from userRegex, sourceRegex, multiple named groups, combined user+source named groups, patterns without named groups, and unresolved variable validation. Reviewed By: swapsmagic Differential Revision: D93437091
4fd127a to
a11cb87
Compare
Summary:
The
addVariableValuesmethod inStaticSelectorpreviously iterated over allvariableNamesand caughtIllegalArgumentExceptionfor each name not present as a named group in the pattern. This is inefficient and relies on exception handling for control flow.This change pre-computes the named groups for each regex pattern at construction time into an
ImmutableMap<Pattern, ImmutableSet<String>>. TheaddVariableValuesmethod now looks up the pre-computed groups for the given pattern and only iterates over matching group names, avoiding the need to catch exceptions entirely.Differential Revision: D93437091
Summary by Sourcery
Refactor StaticSelector variable extraction to precompute regex named groups and avoid exception-based control flow during matching.
Enhancements: