|
| 1 | +/* |
| 2 | + * BSD 2-Clause License |
| 3 | + * |
| 4 | + * Copyright (c) 2023, Swat.engineering |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are met: |
| 8 | + * |
| 9 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 10 | + * list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 20 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 22 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + */ |
| 27 | +package engineering.swat.watch.impl.overflows; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.nio.file.FileVisitResult; |
| 31 | +import java.nio.file.Files; |
| 32 | +import java.nio.file.Path; |
| 33 | +import java.nio.file.attribute.BasicFileAttributes; |
| 34 | +import java.nio.file.attribute.FileTime; |
| 35 | +import java.util.HashSet; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.Set; |
| 38 | +import java.util.concurrent.ConcurrentHashMap; |
| 39 | +import java.util.concurrent.Executor; |
| 40 | + |
| 41 | +import org.apache.logging.log4j.LogManager; |
| 42 | +import org.apache.logging.log4j.Logger; |
| 43 | + |
| 44 | +import engineering.swat.watch.WatchEvent; |
| 45 | +import engineering.swat.watch.WatchScope; |
| 46 | +import engineering.swat.watch.impl.EventHandlingWatch; |
| 47 | + |
| 48 | +public class IndexingRescanner extends MemorylessRescanner { |
| 49 | + private final Logger logger = LogManager.getLogger(); |
| 50 | + private final Map<Path, FileTime> index = new ConcurrentHashMap<>(); |
| 51 | + |
| 52 | + public IndexingRescanner(Executor exec, Path path, WatchScope scope) { |
| 53 | + super(exec); |
| 54 | + new Indexer(path, scope).walkFileTree(); // Make an initial scan to populate the index |
| 55 | + } |
| 56 | + |
| 57 | + private class Indexer extends BaseFileVisitor { |
| 58 | + public Indexer(Path path, WatchScope scope) { |
| 59 | + super(path, scope); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { |
| 64 | + if (!path.equals(dir)) { |
| 65 | + index.put(dir, attrs.lastModifiedTime()); |
| 66 | + } |
| 67 | + return FileVisitResult.CONTINUE; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 72 | + index.put(file, attrs.lastModifiedTime()); |
| 73 | + return FileVisitResult.CONTINUE; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // -- MemorylessRescanner -- |
| 78 | + |
| 79 | + @Override |
| 80 | + protected MemorylessRescanner.Generator newGenerator(Path path, WatchScope scope) { |
| 81 | + return new Generator(path, scope); |
| 82 | + } |
| 83 | + |
| 84 | + protected class Generator extends MemorylessRescanner.Generator { |
| 85 | + // Field to keep track of the paths that are visited during the current |
| 86 | + // rescan. After the visit, the `DELETED` events that happened since the |
| 87 | + // previous rescan can be approximated. |
| 88 | + private Set<Path> visited = new HashSet<>(); |
| 89 | + |
| 90 | + public Generator(Path path, WatchScope scope) { |
| 91 | + super(path, scope); |
| 92 | + } |
| 93 | + |
| 94 | + // -- MemorylessRescanner.Generator -- |
| 95 | + |
| 96 | + @Override |
| 97 | + protected void generateEvents(Path path, BasicFileAttributes attrs) { |
| 98 | + visited.add(path); |
| 99 | + var lastModifiedTimeOld = index.get(path); |
| 100 | + var lastModifiedTimeNew = attrs.lastModifiedTime(); |
| 101 | + |
| 102 | + // The path isn't indexed yet |
| 103 | + if (lastModifiedTimeOld == null) { |
| 104 | + super.generateEvents(path, attrs); |
| 105 | + } |
| 106 | + |
| 107 | + // The path is already indexed, and the previous last-modified-time |
| 108 | + // is older than the current last-modified-time |
| 109 | + else if (lastModifiedTimeOld.compareTo(lastModifiedTimeNew) < 0) { |
| 110 | + events.add(new WatchEvent(WatchEvent.Kind.MODIFIED, path)); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { |
| 116 | + // If the visitor is back at the root of the rescan, then the time |
| 117 | + // is right to issue `DELETED` events based on the set of `visited` |
| 118 | + // paths. |
| 119 | + if (dir.equals(path)) { |
| 120 | + for (var p : index.keySet()) { |
| 121 | + if (p.startsWith(path) && !visited.contains(p)) { |
| 122 | + events.add(new WatchEvent(WatchEvent.Kind.DELETED, p)); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + return super.postVisitDirectory(dir, exc); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // -- MemorylessRescanner -- |
| 131 | + |
| 132 | + @Override |
| 133 | + public void accept(EventHandlingWatch watch, WatchEvent event) { |
| 134 | + // Auto-handle `OVERFLOW` events |
| 135 | + super.accept(watch, event); |
| 136 | + |
| 137 | + // Additional processing is needed to update the index when `CREATED`, |
| 138 | + // `MODIFIED`, and `DELETED` events happen. |
| 139 | + var kind = event.getKind(); |
| 140 | + var fullPath = event.calculateFullPath(); |
| 141 | + switch (kind) { |
| 142 | + case CREATED: |
| 143 | + case MODIFIED: |
| 144 | + try { |
| 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 | + } |
| 155 | + } catch (IOException e) { |
| 156 | + logger.error("Could not get modification time of: {} ({})", fullPath, e); |
| 157 | + } |
| 158 | + break; |
| 159 | + case DELETED: |
| 160 | + index.remove(fullPath); |
| 161 | + break; |
| 162 | + case OVERFLOW: // Already auto-handled above |
| 163 | + break; |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments