Skip to content

Commit b19cdaf

Browse files
committed
Improve event handler of IndexingRescanner (avoid double map lookup)
1 parent 25d5eb7 commit b19cdaf

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/main/java/engineering/swat/watch/impl/overflows/IndexingRescanner.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,22 @@ public void accept(EventHandlingWatch watch, WatchEvent event) {
136136

137137
// Additional processing is needed to update the index when `CREATED`,
138138
// `MODIFIED`, and `DELETED` events happen.
139+
var kind = event.getKind();
139140
var fullPath = event.calculateFullPath();
140-
switch (event.getKind()) {
141-
case MODIFIED:
142-
// If a `MODIFIED` event happens for a path that's not in the
143-
// index yet, then a `CREATED` event has somehow been missed.
144-
// Just in case, it's issued synthetically here.
145-
if (!index.containsKey(fullPath)) {
146-
var created = new WatchEvent(WatchEvent.Kind.CREATED, fullPath);
147-
watch.handleEvent(created);
148-
}
149-
// Fallthrough intended
141+
switch (kind) {
150142
case CREATED:
143+
case MODIFIED:
151144
try {
152-
index.put(fullPath, Files.getLastModifiedTime(fullPath));
145+
var lastModifiedTimeNew = Files.getLastModifiedTime(fullPath);
146+
var lastModifiedTimeOld = index.put(fullPath, lastModifiedTimeNew);
147+
148+
// If a `MODIFIED` event happens for a path that wasn't in
149+
// the index yet, then a `CREATED` event has somehow been
150+
// missed. Just in case, it's issued synthetically here.
151+
if (lastModifiedTimeOld == null && kind == WatchEvent.Kind.MODIFIED) {
152+
var created = new WatchEvent(WatchEvent.Kind.CREATED, fullPath);
153+
watch.handleEvent(created);
154+
}
153155
} catch (IOException e) {
154156
logger.error("Could not get modification time of: {} ({})", fullPath, e);
155157
}

0 commit comments

Comments
 (0)