|
33 | 33 | import java.nio.file.attribute.BasicFileAttributes; |
34 | 34 | import java.nio.file.attribute.FileTime; |
35 | 35 | import java.util.ArrayDeque; |
| 36 | +import java.util.Collections; |
36 | 37 | import java.util.Deque; |
37 | 38 | import java.util.HashSet; |
38 | 39 | import java.util.Map; |
39 | 40 | import java.util.Set; |
40 | 41 | import java.util.concurrent.ConcurrentHashMap; |
41 | 42 | import java.util.concurrent.Executor; |
| 43 | +import java.util.function.BiFunction; |
42 | 44 |
|
43 | 45 | import org.apache.logging.log4j.LogManager; |
44 | 46 | import org.apache.logging.log4j.Logger; |
| 47 | +import org.checkerframework.checker.nullness.qual.Nullable; |
45 | 48 |
|
46 | 49 | import engineering.swat.watch.WatchEvent; |
47 | 50 | import engineering.swat.watch.WatchScope; |
48 | 51 | import engineering.swat.watch.impl.EventHandlingWatch; |
49 | 52 |
|
50 | 53 | public class IndexingRescanner extends MemorylessRescanner { |
51 | 54 | private final Logger logger = LogManager.getLogger(); |
52 | | - private final Map<Path, FileTime> index = new ConcurrentHashMap<>(); |
| 55 | + private final PathMap<FileTime> index = new PathMap<>(); |
53 | 56 |
|
54 | 57 | public IndexingRescanner(Executor exec, Path path, WatchScope scope) { |
55 | 58 | super(exec); |
56 | 59 | new Indexer(path, scope).walkFileTree(); // Make an initial scan to populate the index |
57 | 60 | } |
58 | 61 |
|
| 62 | + private static class PathMap<V> { |
| 63 | + private final Map<Path, Map<Path, V>> values = new ConcurrentHashMap<>(); |
| 64 | + // ^^^^ ^^^^ |
| 65 | + // Parent File name (regular file or directory) |
| 66 | + |
| 67 | + public @Nullable V put(Path p, V value) { |
| 68 | + return apply(put(value), p); |
| 69 | + } |
| 70 | + |
| 71 | + public @Nullable V get(Path p) { |
| 72 | + return apply(this::get, p); |
| 73 | + } |
| 74 | + |
| 75 | + public Set<Path> getParents() { |
| 76 | + return (Set<Path>) values.keySet(); // Cast for Checker Framework |
| 77 | + } |
| 78 | + |
| 79 | + public Set<Path> getFileNames(Path parent) { |
| 80 | + var inner = values.get(parent); |
| 81 | + return inner == null ? Collections.emptySet() : (Set<Path>) inner.keySet(); // Cast for Checker Framework |
| 82 | + } |
| 83 | + |
| 84 | + public @Nullable V remove(Path p) { |
| 85 | + return apply(this::remove, p); |
| 86 | + } |
| 87 | + |
| 88 | + private static <V> @Nullable V apply(BiFunction<Path, Path, @Nullable V> action, Path p) { |
| 89 | + var parent = p.getParent(); |
| 90 | + var fileName = p.getFileName(); |
| 91 | + if (parent != null && fileName != null) { |
| 92 | + return action.apply(parent, fileName); |
| 93 | + } else { |
| 94 | + throw new IllegalArgumentException("The path should have both a parent and a file name"); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private BiFunction<Path, Path, @Nullable V> put(V value) { |
| 99 | + return (parent, fileName) -> put(parent, fileName, value); |
| 100 | + } |
| 101 | + |
| 102 | + private @Nullable V put(Path parent, Path fileName, V value) { |
| 103 | + var inner = values.computeIfAbsent(parent, x -> new ConcurrentHashMap<>()); |
| 104 | + |
| 105 | + // This thread (henceforth: "here") optimistically puts a new entry |
| 106 | + // in `inner`. However, another thread (henceforth: "there") may |
| 107 | + // concurrently remove `inner` from `values`. Thus, the new entry |
| 108 | + // may be lost. The comments below explain the countermeasures. |
| 109 | + var previous = inner.put(fileName, value); |
| 110 | + |
| 111 | + // <-- At this point "here", if `values.remove(parent)` happens |
| 112 | + // "there", then `values.get(parent) != inner` becomes true |
| 113 | + // "here", so the new entry will be re-put "here". |
| 114 | + if (values.get(parent) != inner) { |
| 115 | + previous = put(parent, fileName, value); |
| 116 | + } |
| 117 | + // <-- At this point "here", `!inner.isEmpty()` has become true |
| 118 | + // "there", so if `values.remove(parent)` happens "there", then |
| 119 | + // the new entry will be re-put "there". |
| 120 | + return previous; |
| 121 | + } |
| 122 | + |
| 123 | + private @Nullable V get(Path parent, Path fileName) { |
| 124 | + var inner = values.get(parent); |
| 125 | + return inner == null ? null : inner.get(fileName); |
| 126 | + } |
| 127 | + |
| 128 | + private @Nullable V remove(Path parent, Path fileName) { |
| 129 | + var inner = values.get(parent); |
| 130 | + if (inner != null) { |
| 131 | + var removed = inner.remove(fileName); |
| 132 | + |
| 133 | + // This thread (henceforth: "here") optimistically removes |
| 134 | + // `inner` from `values` when it has become empty. However, |
| 135 | + // another thread (henceforth: "there") may concurrently put a |
| 136 | + // new entry in `inner`. Thus, the new entry may be lost. The |
| 137 | + // comments below explain the countermeasures. |
| 138 | + if (inner.isEmpty() && values.remove(parent, inner)) { |
| 139 | + |
| 140 | + // <-- At this point "here", if `inner.put(...)` happens |
| 141 | + // "there", then `!inner.isEmpty()` becomes true "here", |
| 142 | + // so the new entry is re-put "here". |
| 143 | + if (!inner.isEmpty()) { |
| 144 | + for (var e : inner.entrySet()) { |
| 145 | + put(parent, e.getKey(), e.getValue()); |
| 146 | + } |
| 147 | + } |
| 148 | + // <-- At this point "here", `values.get(parent) != inner` |
| 149 | + // has become true "there", so if `inner.put(...)` |
| 150 | + // happens "there", then the new entry will be re-put |
| 151 | + // "there". |
| 152 | + } |
| 153 | + return removed; |
| 154 | + } else { |
| 155 | + return null; |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
59 | 160 | private class Indexer extends BaseFileVisitor { |
60 | 161 | public Indexer(Path path, WatchScope scope) { |
61 | 162 | super(path, scope); |
@@ -96,10 +197,11 @@ public Generator(Path path, WatchScope scope) { |
96 | 197 | this.visited.push(new HashSet<>()); // Initial set for content of `path` |
97 | 198 | } |
98 | 199 |
|
99 | | - private <T> void addToPeeked(Deque<Set<T>> deque, T t) { |
| 200 | + private void addToPeeked(Deque<Set<Path>> deque, Path p) { |
100 | 201 | var peeked = deque.peek(); |
101 | | - if (peeked != null) { |
102 | | - peeked.add(t); |
| 202 | + var fileName = p.getFileName(); |
| 203 | + if (peeked != null && fileName != null) { |
| 204 | + peeked.add(fileName); |
103 | 205 | } |
104 | 206 | } |
105 | 207 |
|
@@ -140,9 +242,15 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOEx |
140 | 242 | // Issue `DELETED` events based on the set of paths visited in `dir` |
141 | 243 | var visitedInDir = visited.pop(); |
142 | 244 | if (visitedInDir != null) { |
143 | | - for (var p : index.keySet()) { |
144 | | - if (dir.equals(p.getParent()) && !visitedInDir.contains(p)) { |
145 | | - events.add(new WatchEvent(WatchEvent.Kind.DELETED, p)); |
| 245 | + for (var p : index.getFileNames(dir)) { |
| 246 | + if (!visitedInDir.contains(p)) { |
| 247 | + var fullPath = dir.resolve(p); |
| 248 | + // The index may have been updated during the visit, so |
| 249 | + // even if `p` isn't contained in `visitedInDir`, by |
| 250 | + // now, it may have come into existence. |
| 251 | + if (!Files.exists(fullPath)) { |
| 252 | + events.add(new WatchEvent(WatchEvent.Kind.DELETED, fullPath)); |
| 253 | + } |
146 | 254 | } |
147 | 255 | } |
148 | 256 | } |
@@ -176,7 +284,16 @@ public void accept(EventHandlingWatch watch, WatchEvent event) { |
176 | 284 | watch.handleEvent(watch.relativize(created)); |
177 | 285 | } |
178 | 286 | } catch (IOException e) { |
179 | | - logger.error("Could not get modification time of: {} ({})", fullPath, e); |
| 287 | + // It can happen that, by the time a `CREATED`/`MODIFIED` |
| 288 | + // event is handled above, getting the last-modified-time |
| 289 | + // fails because the file has already been deleted. That's |
| 290 | + // fine: we can just ignore the event. (The corresponding |
| 291 | + // `DELETED` event will later be handled and remove the file |
| 292 | + // from the index.) If the file exists, though, something |
| 293 | + // went legitimately wrong, so it needs to be reported. |
| 294 | + if (Files.exists(fullPath)) { |
| 295 | + logger.error("Could not get modification time of: {} ({})", fullPath, e); |
| 296 | + } |
180 | 297 | } |
181 | 298 | break; |
182 | 299 | case DELETED: |
|
0 commit comments