Skip to content

Commit 13d7c9c

Browse files
committed
rename OnMatchEventHandler to MatchHandler, so it align with NativeMatchHandler
1 parent 8346a4d commit 13d7c9c

12 files changed

Lines changed: 48 additions & 48 deletions

File tree

README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ The scratch space, and by extension the scanner object, are modified during the
7070
A scanner's scan() method requires two arguments.
7171
1. First, an object holding the payload for what you want to scan over.
7272
The wrapper supports Strings, byte arrays, ByteBuffers (both on-heap and direct), and MemorySegments (both heap and native).
73-
2. Second, an [OnMatchEventHandler](src/main/java/com/dynatrace/vectorscan4j/OnMatchEventHandler.java), which is a user-provided callback that gets executed on every match.
73+
2. Second, a [MatchHandler](src/main/java/com/dynatrace/vectorscan4j/MatchHandler.java), which is a user-provided callback that gets executed on every match.
7474
Through that callback, the user can pass arbitrary logic to be executed on every match.
7575

7676
## Code example
@@ -92,7 +92,7 @@ we want to count how often each pattern exists inside the input:
9292

9393
try (Database database = new Database(exprs, ExecutionMode.BLOCK);
9494
BlockScanner scanner = new BlockScanner(database)) {
95-
OnMatchEventHandler countMatches = (id, from, to, flags) -> {
95+
MatchHandler countMatches = (id, from, to, flags) -> {
9696
countsById[id]++;
9797
return true;
9898
};
@@ -110,7 +110,7 @@ The list of Expression objects specify the set of patterns we want to match. Eve
110110
starting from 0, in the order in which they are added to the List. A Database object gets instantiated by passing it
111111
the list of expressions, and specifying an execution mode. A Scanner object then references the Database
112112
(BlockScanner objects for Databases with Block execution mode, and StreamScanner objects for Databases with Stream execution mode)
113-
For the scan method, you pass it an input, and an OnMatchEventHandler, which is a functional interface that will get executed on every match. It receives 4 arguments:
113+
For the scan method, you pass it an input, and a MatchHandler, which is a functional interface that will get executed on every match. It receives 4 arguments:
114114

115115
1. The id of the expression that matched.
116116
2. The byte position of where the match started. Note that by default, vectorscan does not keep track of this value, unless the pattern flag SOM_LEFTMOST is enabled.
@@ -119,7 +119,7 @@ For the scan method, you pass it an input, and an OnMatchEventHandler, which is
119119
The return value of the callback specifies whether you want the scanning to continue.
120120
Returning true will continue the scan, while returning false will stop the scanning early.
121121

122-
In this example, the provided OnMatchEventHandler increments the value inside an integer array, and then lets the scan continue.
122+
In this example, the provided MatchHandler increments the value inside an integer array, and then lets the scan continue.
123123

124124
### Database Serialization/Deserialization
125125

@@ -185,7 +185,7 @@ passing it a MemorySegment that points to a non-allocated region of memory might
185185
try (Database db = new Database(List.of(new Expression("p1")), BLOCK_MODE);
186186
BlockScanner scanner = new BlockScanner(db);
187187
Arena arena = Arena.ofConfined()) {
188-
OnMatchEventHandler doNothing = (_, _, _, _) -> true;
188+
MatchHandler doNothing = (_, _, _, _) -> true;
189189

190190
MemorySegment tiny = arena.allocate(10);
191191
MemorySegment larger = tiny.reinterpret(200000L);
@@ -197,15 +197,15 @@ passing it a MemorySegment that points to a non-allocated region of memory might
197197
}
198198
```
199199

200-
### 3. Throwing exceptions inside the OnMatchEventHandler upcall
200+
### 3. Throwing exceptions inside the MatchHandler upcall
201201

202-
Throwing exceptions inside the OnMatchEventHandler will always break the JVM, i.e. you can not catch
202+
Throwing exceptions inside the MatchHandler will always break the JVM, i.e. you can not catch
203203
that exception outside the scan call:
204204

205205
``` java
206206
try (try (Database db = new Database(List.of(new Expression("p1")), BLOCK_MODE);
207207
BlockScanner scanner = new BlockScanner(db)) {
208-
OnMatchEventHandler throwException = (_, _, _, _) -> {
208+
MatchHandler throwException = (_, _, _, _) -> {
209209
throw new RuntimeException("Hi, try and catch me!");
210210
};
211211
try {
@@ -263,4 +263,3 @@ The corresponding third-party notice and license reproduction is provided in:
263263
264264
This wrapper currently supports a core subset of vectorscan's features, but not everything.
265265
If additional features are required, do not hesitate to raise issues or submit pull requests directly.
266-

src/main/java/com/dynatrace/vectorscan4j/BlockScanner.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
/**
2626
* A {@link Scanner} that operates in block mode: each call to {@code scan(...)} processes a single,
2727
* self-contained input buffer in isolation. Each scan starts from the initial DFA/NFA state and ends when the entire input
28-
* * has been consumed (or the {@link OnMatchEventHandler} requests early termination by returning
28+
* * has been consumed (or the {@link MatchHandler} requests early termination by returning
2929
* * {@code false}).
3030
*
3131
* <p>Each scanner owns its own native scratch space and is therefore <strong>not</strong> safe to
@@ -69,7 +69,7 @@ public BlockScanner(Database db) {
6969
* @throws VectorscanException if the native scan call returns an error other than {@link
7070
* com.dynatrace.vectorscan4j.constants.ErrorCode#HS_SCAN_TERMINATED HS_SCAN_TERMINATED}
7171
*/
72-
public void scan(MemorySegment data, OnMatchEventHandler handler) {
72+
public void scan(MemorySegment data, MatchHandler handler) {
7373
if (data.byteSize() > Integer.MAX_VALUE) {
7474
throw new IllegalArgumentException("Input MemorySegment is too big.");
7575
}
@@ -87,7 +87,7 @@ public void scan(MemorySegment data, OnMatchEventHandler handler) {
8787
/**
8888
* Scans {@code data} using a <em>native</em> match-event callback supplied by the caller.
8989
*
90-
* <p>Unlike {@link #scan(MemorySegment, OnMatchEventHandler)}, this method passes the
90+
* <p>Unlike {@link #scan(MemorySegment, MatchHandler)}, this method passes the
9191
* caller-provided native function pointer directly to vectorscan, so matches do
9292
* <strong>not</strong> incur an upcall back into Java. This is intended for hot paths
9393
* where the per-match Java callback dominates cost.

src/main/java/com/dynatrace/vectorscan4j/OnMatchEventHandler.java renamed to src/main/java/com/dynatrace/vectorscan4j/MatchHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* should continue.
2525
*/
2626
@FunctionalInterface
27-
public interface OnMatchEventHandler {
27+
public interface MatchHandler {
2828

2929
/**
3030
* Called for every match found during a scan.

src/main/java/com/dynatrace/vectorscan4j/Scanner.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ abstract class Scanner implements AutoCloseable {
4444
private final Cleaner.Cleanable cleanable;
4545

4646
static class CallHandlerOnMatch implements VectorscanMatchEventHandler.Function {
47-
OnMatchEventHandler handler;
47+
MatchHandler handler;
4848

4949
@Override
5050
public int apply(int id, long from, long to, int flags, MemorySegment context) {
@@ -84,7 +84,7 @@ public void run() {
8484
}
8585
}
8686

87-
protected void setHandler(OnMatchEventHandler handler) {
87+
protected void setHandler(MatchHandler handler) {
8888
this.callHandler.handler = handler;
8989
}
9090

@@ -128,14 +128,14 @@ private void resizeBuffer(long newSize) {
128128
* Scans the given input string for the patterns that were compiled in the database, emitting a
129129
* callback for each match.
130130
*
131-
* <p>This is a convenience overload that delegates to {@link #scan(byte[], OnMatchEventHandler)}
131+
* <p>This is a convenience overload that delegates to {@link #scan(byte[], MatchHandler)}
132132
* using {@link StandardCharsets#UTF_8}.
133133
*
134134
* @param input text to scan
135135
* @param handler callback invoked for each match; return {@code true} to continue scanning,
136136
* {@code false} to stop early
137137
*/
138-
public void scan(String input, OnMatchEventHandler handler) {
138+
public void scan(String input, MatchHandler handler) {
139139
scan(input.getBytes(StandardCharsets.UTF_8), handler);
140140
}
141141

@@ -152,7 +152,7 @@ public void scan(String input, OnMatchEventHandler handler) {
152152
* @throws IndexOutOfBoundsException if {@code offset} or {@code length} are invalid for {@code
153153
* data}
154154
*/
155-
public void scan(byte[] data, int offset, int length, OnMatchEventHandler handler) {
155+
public void scan(byte[] data, int offset, int length, MatchHandler handler) {
156156
scan(ByteBuffer.wrap(data, offset, length), handler);
157157
}
158158

@@ -163,7 +163,7 @@ public void scan(byte[] data, int offset, int length, OnMatchEventHandler handle
163163
* @param handler callback invoked for each match; return {@code true} to continue scanning,
164164
* {@code false} to stop early
165165
*/
166-
public void scan(byte[] data, OnMatchEventHandler handler) {
166+
public void scan(byte[] data, MatchHandler handler) {
167167
scan(ByteBuffer.wrap(data), handler);
168168
}
169169

@@ -179,7 +179,7 @@ public void scan(byte[] data, OnMatchEventHandler handler) {
179179
* @param handler callback invoked for each match; return {@code true} to continue scanning,
180180
* {@code false} to stop early
181181
*/
182-
public void scan(ByteBuffer buf, OnMatchEventHandler handler) {
182+
public void scan(ByteBuffer buf, MatchHandler handler) {
183183
if (buf.isDirect()) {
184184
MemorySegment data = MemorySegment.ofBuffer(buf);
185185
scan(data, handler);
@@ -268,7 +268,7 @@ public void scan(ByteBuffer buf, NativeMatchHandler handler) {
268268
* @param handler callback invoked for each match; return {@code true} to continue scanning,
269269
* {@code false} to stop early
270270
*/
271-
protected abstract void scan(MemorySegment data, OnMatchEventHandler handler);
271+
protected abstract void scan(MemorySegment data, MatchHandler handler);
272272

273273
/**
274274
* Implementation hook for the no-upcall native-callback path used by concrete scanner types

src/main/java/com/dynatrace/vectorscan4j/StreamScanner.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*
3030
* <p>Stream scanning preserves matcher state across successive {@code
3131
* scan(...)} calls, which allows matches to span chunk boundaries. The stream state can be reset
32-
* with {@link #resetStream(OnMatchEventHandler handler)} or terminated with {@link #closeStream(OnMatchEventHandler)}. After
32+
* with {@link #resetStream(MatchHandler handler)} or terminated with {@link #closeStream(MatchHandler)}. After
3333
* closing, a new state can be created via {@link #openStream()}.
3434
*
3535
* <p>Always close this scanner to release native resources deterministically. Prefer
@@ -76,7 +76,7 @@ public StreamScanner(Database db) {
7676
* {@code false} to stop early
7777
* @throws VectorscanException if vectorscan reports an error other than early termination
7878
*/
79-
public void scan(MemorySegment data, OnMatchEventHandler handler) {
79+
public void scan(MemorySegment data, MatchHandler handler) {
8080
if (database.isClosed()) {
8181
throw new IllegalStateException("Database was already closed.");
8282
}
@@ -96,7 +96,7 @@ public void scan(MemorySegment data, OnMatchEventHandler handler) {
9696
/**
9797
* Opens a new native stream state for this scanner.
9898
*
99-
* <p>Use this after {@link #closeStream(OnMatchEventHandler)} when starting a fresh stream.
99+
* <p>Use this after {@link #closeStream(MatchHandler)} when starting a fresh stream.
100100
*
101101
* @throws VectorscanException if vectorscan cannot open the stream
102102
*/
@@ -120,7 +120,7 @@ public void openStream() {
120120
* @param handler callback used for any matches emitted during the reset
121121
* @throws VectorscanException if vectorscan reports an error during reset
122122
*/
123-
public void resetStream(OnMatchEventHandler handler) {
123+
public void resetStream(MatchHandler handler) {
124124
setHandler(handler);
125125
int ans = hs_reset_stream(stream, 0, scratch, funcPtr, MemorySegment.NULL);
126126
if (ans != HS_SUCCESS.getCode()) {
@@ -138,7 +138,7 @@ public void resetStream(OnMatchEventHandler handler) {
138138
* @param handler callback used for any close-time matches
139139
* @throws VectorscanException if vectorscan reports an error while closing the stream
140140
*/
141-
public void closeStream(OnMatchEventHandler handler) {
141+
public void closeStream(MatchHandler handler) {
142142
if (!streamOpen) return;
143143
setHandler(handler);
144144
int ans = hs_close_stream(stream, scratch, funcPtr, MemorySegment.NULL);

src/main/java/com/dynatrace/vectorscan4j/constants/Flags.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package com.dynatrace.vectorscan4j.constants;
1717

1818
import com.dynatrace.vectorscan4j.BlockScanner;
19-
import com.dynatrace.vectorscan4j.OnMatchEventHandler;
19+
import com.dynatrace.vectorscan4j.MatchHandler;
2020
import java.lang.foreign.MemorySegment;
2121

2222
/** Pattern compilation flags for expressions. */
@@ -55,7 +55,7 @@ public enum Flags {
5555
* <p>This flag sets the expression's match ID to match at most once. In streaming mode, this
5656
* means that the expression will return only a single match over the lifetime of the stream,
5757
* rather than reporting every match as per standard Vectorscan semantics. In block mode, only the
58-
* first match for each invocation of {@link BlockScanner#scan(MemorySegment, OnMatchEventHandler)
58+
* first match for each invocation of {@link BlockScanner#scan(MemorySegment, MatchHandler)
5959
* BlockScanner.scan(...)} will be returned.
6060
*
6161
* <p>Note: The use of this flag in combination with {@link #SOM_LEFTMOST} is not currently

src/main/java/com/dynatrace/vectorscan4j/internal/NativeLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static String normalizeOs(String osName) {
6666
if (os.contains("mac") || os.contains("darwin")) {
6767
return "macos";
6868
}
69-
if (os.contains("nux") || os.contains("nix") || os.contains("linux")) {
69+
if (os.contains("nux") || os.contains("nix")) {
7070
return "linux";
7171
}
7272
return os.replace(' ', '_');

src/test/java/com/dynatrace/vectorscan4j/BlockScannerTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class BlockScannerTests {
4040
new Expression("pattern3", EnumSet.of(SOM_LEFTMOST, CASELESS)));
4141
static int nMatches = 0;
4242

43-
final OnMatchEventHandler doNothing = (_, _, _, _) -> true;
43+
final MatchHandler doNothing = (_, _, _, _) -> true;
4444

4545
@Test
4646
void databaseWrongMode() {
@@ -68,8 +68,8 @@ void defaultScan() {
6868

6969
@Test
7070
void earlyStopScan() {
71-
// if an OnMatchEventHandler returns false, the execution of the engine stops early.
72-
OnMatchEventHandler incrementAndStop = (_, _, _, _) -> {
71+
// if a MatchHandler returns false, the execution of the engine stops early.
72+
MatchHandler incrementAndStop = (_, _, _, _) -> {
7373
nMatches += 1;
7474
return false;
7575
};
@@ -118,7 +118,7 @@ void emptyInput() {
118118
try (Database db = new Database(expressions, BLOCK_MODE);
119119
BlockScanner scanner = new BlockScanner(db)) {
120120
List<Integer> matchedIds = new ArrayList<>();
121-
OnMatchEventHandler collect = (id, _, _, _) -> {
121+
MatchHandler collect = (id, _, _, _) -> {
122122
matchedIds.add(id);
123123
return true;
124124
};
@@ -170,7 +170,7 @@ void byteBufferRangeRespected() {
170170
BlockScanner scanner = new BlockScanner(db)) {
171171
ByteBuffer buf = ByteBuffer.wrap("pattern1__pattern1__pattern1".getBytes(StandardCharsets.UTF_8));
172172
List<Integer> matchedIds = new ArrayList<>();
173-
OnMatchEventHandler collect = (id, _, _, _) -> {
173+
MatchHandler collect = (id, _, _, _) -> {
174174
matchedIds.add(id);
175175
return true;
176176
};
@@ -446,7 +446,7 @@ void usingScannerFromMultipleThreadsThrowsVectorscanException() throws Exception
446446
try {
447447
CountDownLatch stop = new CountDownLatch(1);
448448
CountDownLatch scannerInUse = new CountDownLatch(1);
449-
OnMatchEventHandler handler = (_, _, _, _) -> {
449+
MatchHandler handler = (_, _, _, _) -> {
450450
try {
451451
scannerInUse.countDown();
452452
stop.await();
@@ -477,7 +477,7 @@ void twoScannersWithSameDatabaseCanScanConcurrently() throws Exception {
477477
List<Integer> scanner1Matches = Collections.synchronizedList(new ArrayList<>());
478478
List<Integer> scanner2Matches = Collections.synchronizedList(new ArrayList<>());
479479

480-
OnMatchEventHandler handler1 = (id, _, _, _) -> {
480+
MatchHandler handler1 = (id, _, _, _) -> {
481481
scanner1Matches.add(id);
482482
handlersEntered.countDown();
483483
try {
@@ -488,7 +488,7 @@ void twoScannersWithSameDatabaseCanScanConcurrently() throws Exception {
488488
}
489489
return true;
490490
};
491-
OnMatchEventHandler handler2 = (id, _, _, _) -> {
491+
MatchHandler handler2 = (id, _, _, _) -> {
492492
scanner2Matches.add(id);
493493
handlersEntered.countDown();
494494
try {

src/test/java/com/dynatrace/vectorscan4j/DatabaseTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class DatabaseTests {
4141
new Expression("pattern3", EnumSet.of(SOM_LEFTMOST, CASELESS)));
4242
static int nMatches = 0;
4343

44-
private final OnMatchEventHandler countMatch = (_, _, _, _) -> {
44+
private final MatchHandler countMatch = (_, _, _, _) -> {
4545
nMatches += 1;
4646
return true;
4747
};

src/test/java/com/dynatrace/vectorscan4j/RegexFeaturesTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
public class RegexFeaturesTests {
2727
static int nMatches = 0;
28-
static final OnMatchEventHandler countMatch = (_, _, _, _) -> {
28+
static final MatchHandler countMatch = (_, _, _, _) -> {
2929
nMatches += 1;
3030
return true;
3131
};

0 commit comments

Comments
 (0)