-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add native compiled pattern state API #116
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
Open
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
reggie-runtime/src/main/java/com/datadoghq/reggie/runtime/ReggieCompilationRejection.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * Copyright 2026-Present Datadog, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.datadoghq.reggie.runtime; | ||
|
|
||
| /** Reason the native named linear-token-sequence profile did not admit a request. */ | ||
| public enum ReggieCompilationRejection { | ||
| UNSUPPORTED_FLAGS, | ||
| SOURCE_INLINE_MODIFIER, | ||
| PARSE_FAILURE, | ||
| PLAN_UNAVAILABLE, | ||
| MISSING_NAMED_CAPTURE, | ||
| PROFILE_INELIGIBLE | ||
| } |
59 changes: 59 additions & 0 deletions
59
reggie-runtime/src/main/java/com/datadoghq/reggie/runtime/ReggieCompilationResult.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* | ||
| * Copyright 2026-Present Datadog, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.datadoghq.reggie.runtime; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| /** Immutable outcome of a native named linear-token-sequence compilation request. */ | ||
| public final class ReggieCompilationResult { | ||
| private final ReggieCompiledPattern pattern; | ||
| private final ReggieCompilationRejection rejection; | ||
|
|
||
| private ReggieCompilationResult( | ||
| ReggieCompiledPattern pattern, ReggieCompilationRejection rejection) { | ||
| if ((pattern == null) == (rejection == null)) { | ||
| throw new IllegalArgumentException("exactly one of pattern or rejection is required"); | ||
| } | ||
| this.pattern = pattern; | ||
| this.rejection = rejection; | ||
| } | ||
|
|
||
| static ReggieCompilationResult admitted(ReggieCompiledPattern pattern) { | ||
| return new ReggieCompilationResult(Objects.requireNonNull(pattern, "pattern"), null); | ||
| } | ||
|
|
||
| static ReggieCompilationResult rejected(ReggieCompilationRejection rejection) { | ||
| return new ReggieCompilationResult(null, Objects.requireNonNull(rejection, "rejection")); | ||
| } | ||
|
|
||
| public boolean isAdmitted() { | ||
| return pattern != null; | ||
| } | ||
|
|
||
| public ReggieCompiledPattern pattern() { | ||
| if (pattern == null) { | ||
| throw new IllegalStateException("compilation was rejected: " + rejection); | ||
| } | ||
| return pattern; | ||
| } | ||
|
|
||
| public ReggieCompilationRejection rejection() { | ||
| if (rejection == null) { | ||
| throw new IllegalStateException("compilation was admitted"); | ||
| } | ||
| return rejection; | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
reggie-runtime/src/main/java/com/datadoghq/reggie/runtime/ReggieCompileFlag.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * Copyright 2026-Present Datadog, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.datadoghq.reggie.runtime; | ||
|
|
||
| import com.datadoghq.reggie.ReggieFlags; | ||
|
|
||
| /** Flags supported by the native named linear-token-sequence compilation profile. */ | ||
| public enum ReggieCompileFlag { | ||
| NONE(ReggieFlags.NONE), | ||
| DOTALL(ReggieFlags.DOTALL); | ||
|
|
||
| private final int reggieFlags; | ||
|
|
||
| ReggieCompileFlag(int reggieFlags) { | ||
| this.reggieFlags = reggieFlags; | ||
| } | ||
|
|
||
| int reggieFlags() { | ||
| return reggieFlags; | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
reggie-runtime/src/main/java/com/datadoghq/reggie/runtime/ReggieCompileRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * Copyright 2026-Present Datadog, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.datadoghq.reggie.runtime; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| /** Immutable request for the native named linear-token-sequence compilation profile. */ | ||
| public record ReggieCompileRequest(String source, ReggieCompileFlag flag) { | ||
| public ReggieCompileRequest { | ||
| Objects.requireNonNull(source, "source"); | ||
| Objects.requireNonNull(flag, "flag"); | ||
| } | ||
| } |
62 changes: 62 additions & 0 deletions
62
reggie-runtime/src/main/java/com/datadoghq/reggie/runtime/ReggieCompiledPattern.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * Copyright 2026-Present Datadog, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.datadoghq.reggie.runtime; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Immutable native compiled pattern for the named linear-token-sequence profile. | ||
| * | ||
| * <p>This API never selects another Reggie strategy and never delegates to the JDK. | ||
| */ | ||
| public final class ReggieCompiledPattern { | ||
| private final LinearTokenSequenceMatcher matcher; | ||
|
|
||
| private ReggieCompiledPattern(LinearTokenSequenceMatcher matcher) { | ||
| this.matcher = Objects.requireNonNull(matcher, "matcher"); | ||
| } | ||
|
|
||
| /** | ||
| * Attempts native compilation without consulting the general compiler or either compiler cache. | ||
| */ | ||
| public static ReggieCompilationResult tryCompile(ReggieCompileRequest request) { | ||
| Objects.requireNonNull(request, "request"); | ||
| RuntimeCompiler.NamedOnlyLtsCompilation compilation = | ||
| RuntimeCompiler.tryCompileNamedOnlyLinearTokenSequence( | ||
| request.source(), request.flag().reggieFlags()); | ||
| if (compilation.matcher() != null) { | ||
| return ReggieCompilationResult.admitted(new ReggieCompiledPattern(compilation.matcher())); | ||
| } | ||
| return ReggieCompilationResult.rejected(mapRejection(compilation.rejection())); | ||
| } | ||
|
|
||
| /** Creates a new single-thread-confined state object for matching this immutable pattern. */ | ||
| public ReggieMatchState newState() { | ||
| return new ReggieMatchState(matcher); | ||
| } | ||
|
|
||
| private static ReggieCompilationRejection mapRejection( | ||
| RuntimeCompiler.NamedOnlyLtsRejection rejection) { | ||
| return switch (rejection) { | ||
| case UNSUPPORTED_FLAGS -> ReggieCompilationRejection.UNSUPPORTED_FLAGS; | ||
| case SOURCE_INLINE_MODIFIER -> ReggieCompilationRejection.SOURCE_INLINE_MODIFIER; | ||
| case PARSE_FAILURE -> ReggieCompilationRejection.PARSE_FAILURE; | ||
| case PLAN_UNAVAILABLE -> ReggieCompilationRejection.PLAN_UNAVAILABLE; | ||
| case MISSING_NAMED_CAPTURE -> ReggieCompilationRejection.MISSING_NAMED_CAPTURE; | ||
| case PROFILE_INELIGIBLE -> ReggieCompilationRejection.PROFILE_INELIGIBLE; | ||
| }; | ||
| } | ||
| } | ||
82 changes: 82 additions & 0 deletions
82
reggie-runtime/src/main/java/com/datadoghq/reggie/runtime/ReggieMatchState.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * Copyright 2026-Present Datadog, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.datadoghq.reggie.runtime; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Single-thread-confined mutable matching state for a {@link ReggieCompiledPattern}. | ||
| * | ||
| * <p>The state retains spans only; it never retains the input sequence. | ||
| */ | ||
| public final class ReggieMatchState { | ||
| private final LinearTokenSequenceMatcher matcher; | ||
| private final LinearTokenSequenceMatcher.MatchWorkspace workspace; | ||
| private final int[] starts; | ||
| private final int[] ends; | ||
| private boolean matched; | ||
|
|
||
| ReggieMatchState(LinearTokenSequenceMatcher matcher) { | ||
| this.matcher = Objects.requireNonNull(matcher, "matcher"); | ||
| this.workspace = matcher.newMatchWorkspace(); | ||
| this.starts = new int[matcher.groupCount() + 1]; | ||
| this.ends = new int[matcher.groupCount() + 1]; | ||
| clear(); | ||
| } | ||
|
|
||
| /** | ||
| * Attempts a full match inside {@code [start, end)}. | ||
| * | ||
| * <p>Prior state is cleared before validating the input or attempting the match. | ||
| */ | ||
| public boolean matches(CharSequence input, int start, int end) { | ||
| clear(); | ||
| return matched = matcher.matchIntoBounded(input, start, end, starts, ends, workspace); | ||
| } | ||
|
|
||
| public int start() { | ||
| requireMatched(); | ||
| return starts[0]; | ||
| } | ||
|
|
||
| public int end() { | ||
| requireMatched(); | ||
| return ends[0]; | ||
| } | ||
|
|
||
| public int start(String name) { | ||
| requireMatched(); | ||
| return starts[matcher.groupIndex(name)]; | ||
| } | ||
|
|
||
| public int end(String name) { | ||
| requireMatched(); | ||
| return ends[matcher.groupIndex(name)]; | ||
| } | ||
|
|
||
| private void clear() { | ||
| Arrays.fill(starts, -1); | ||
| Arrays.fill(ends, -1); | ||
| matched = false; | ||
| } | ||
|
|
||
| private void requireMatched() { | ||
| if (!matched) { | ||
| throw new IllegalStateException("there is no current match"); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.