Skip to content

Commit 53f3038

Browse files
authored
Merge pull request #18 from SWAT-engineering/improved-overflow-support/scaffolding
Improved overflow support: Scaffolding
2 parents e23a194 + c365b4a commit 53f3038

File tree

5 files changed

+40
-39
lines changed

5 files changed

+40
-39
lines changed

.github/workflows/build.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ on:
88
pull_request:
99
branches:
1010
- main
11+
- improved-overflow-support-main
1112

1213
jobs:
1314
test:

src/main/java/engineering/swat/watch/WatchEvent.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ public enum Kind {
6868
private final Path rootPath;
6969
private final Path relativePath;
7070

71+
public WatchEvent(Kind kind, Path rootPath) {
72+
this(kind, rootPath, null);
73+
}
74+
7175
public WatchEvent(Kind kind, Path rootPath, @Nullable Path relativePath) {
7276
this.kind = kind;
7377
this.rootPath = rootPath;

src/main/java/engineering/swat/watch/impl/jdk/JDKBaseWatch.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,27 +90,29 @@ protected boolean startIfFirstTime() throws IOException {
9090
}
9191

9292
protected WatchEvent translate(java.nio.file.WatchEvent<?> jdkEvent) {
93-
WatchEvent.Kind kind;
94-
if (jdkEvent.kind() == StandardWatchEventKinds.ENTRY_CREATE) {
95-
kind = WatchEvent.Kind.CREATED;
96-
}
97-
else if (jdkEvent.kind() == StandardWatchEventKinds.ENTRY_MODIFY) {
98-
kind = WatchEvent.Kind.MODIFIED;
99-
}
100-
else if (jdkEvent.kind() == StandardWatchEventKinds.ENTRY_DELETE) {
101-
kind = WatchEvent.Kind.DELETED;
102-
}
103-
else if (jdkEvent.kind() == StandardWatchEventKinds.OVERFLOW) {
104-
kind = WatchEvent.Kind.OVERFLOW;
105-
}
106-
else {
107-
throw new IllegalArgumentException("Unexpected watch event: " + jdkEvent);
108-
}
93+
var kind = translate(jdkEvent.kind());
10994
var rootPath = path;
110-
var relativePath = kind == WatchEvent.Kind.OVERFLOW ? Path.of("") : (@Nullable Path)jdkEvent.context();
95+
var relativePath = kind == WatchEvent.Kind.OVERFLOW ? null : (@Nullable Path) jdkEvent.context();
11196

11297
var event = new WatchEvent(kind, rootPath, relativePath);
11398
logger.trace("Translated: {} to {}", jdkEvent, event);
11499
return event;
115100
}
101+
102+
private WatchEvent.Kind translate(java.nio.file.WatchEvent.Kind<?> jdkKind) {
103+
if (jdkKind == StandardWatchEventKinds.ENTRY_CREATE) {
104+
return WatchEvent.Kind.CREATED;
105+
}
106+
if (jdkKind == StandardWatchEventKinds.ENTRY_MODIFY) {
107+
return WatchEvent.Kind.MODIFIED;
108+
}
109+
if (jdkKind == StandardWatchEventKinds.ENTRY_DELETE) {
110+
return WatchEvent.Kind.DELETED;
111+
}
112+
if (jdkKind == StandardWatchEventKinds.OVERFLOW) {
113+
return WatchEvent.Kind.OVERFLOW;
114+
}
115+
116+
throw new IllegalArgumentException("Unexpected watch kind: " + jdkKind);
117+
}
116118
}

src/main/java/engineering/swat/watch/impl/jdk/JDKDirectoryWatch.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public JDKDirectoryWatch(Path directory, Executor exec, Consumer<WatchEvent> eve
5858
this.nativeRecursive = nativeRecursive;
5959
}
6060

61-
private void handleChanges(List<java.nio.file.WatchEvent<?>> events) {
61+
private void handleJDKEvents(List<java.nio.file.WatchEvent<?>> events) {
6262
exec.execute(() -> {
6363
for (var ev : events) {
6464
try {
@@ -85,6 +85,6 @@ public synchronized void close() throws IOException {
8585
protected synchronized void start() throws IOException {
8686
assert bundledJDKWatcher == null;
8787
var key = new SubscriptionKey(path, nativeRecursive);
88-
bundledJDKWatcher = BUNDLED_JDK_WATCHERS.subscribe(key, this::handleChanges);
88+
bundledJDKWatcher = BUNDLED_JDK_WATCHERS.subscribe(key, this::handleJDKEvents);
8989
}
9090
}

src/main/java/engineering/swat/watch/impl/jdk/JDKFileWatch.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333

3434
import org.apache.logging.log4j.LogManager;
3535
import org.apache.logging.log4j.Logger;
36-
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
3736
import org.checkerframework.checker.nullness.qual.Nullable;
3837

3938
import engineering.swat.watch.WatchEvent;
@@ -45,17 +44,23 @@
4544
*/
4645
public class JDKFileWatch extends JDKBaseWatch {
4746
private final Logger logger = LogManager.getLogger();
48-
private final Path parent;
49-
private final Path fileName;
50-
private volatile @MonotonicNonNull JDKDirectoryWatch parentWatch;
47+
private final JDKBaseWatch internal;
5148

5249
public JDKFileWatch(Path file, Executor exec, Consumer<WatchEvent> eventHandler) {
5350
super(file, exec, eventHandler);
5451

5552
var message = "The root path is not a valid path for a file watch";
56-
this.parent = requireNonNull(path.getParent(), message);
57-
this.fileName = requireNonNull(path.getFileName(), message);
58-
assert !parent.equals(path);
53+
var parent = requireNonNull(file.getParent(), message);
54+
var fileName = requireNonNull(file.getFileName(), message);
55+
assert !parent.equals(file);
56+
57+
this.internal = new JDKDirectoryWatch(parent, exec, e -> {
58+
if (fileName.equals(e.getRelativePath())) {
59+
eventHandler.accept(e);
60+
}
61+
});
62+
63+
logger.debug("File watch (for: {}) is in reality a directory watch (for: {}) with a filter (for: {})", file, parent, fileName);
5964
}
6065

6166
private static Path requireNonNull(@Nullable Path p, String message) {
@@ -65,26 +70,15 @@ private static Path requireNonNull(@Nullable Path p, String message) {
6570
return p;
6671
}
6772

68-
private void filter(WatchEvent event) {
69-
if (fileName.equals(event.getRelativePath())) {
70-
eventHandler.accept(event);
71-
}
72-
}
73-
7473
// -- JDKBaseWatch --
7574

7675
@Override
7776
public synchronized void close() throws IOException {
78-
if (parentWatch != null) {
79-
parentWatch.close();
80-
}
77+
internal.close();
8178
}
8279

8380
@Override
8481
protected synchronized void start() throws IOException {
85-
assert parentWatch == null;
86-
parentWatch = new JDKDirectoryWatch(parent, exec, this::filter);
87-
parentWatch.open();
88-
logger.debug("File watch (for: {}) is in reality a directory watch (for: {}) with a filter (for: {})", path, parent, fileName);
82+
internal.open();
8983
}
9084
}

0 commit comments

Comments
 (0)