From ae08ea7010b47a4210d30ae9e6e192c5d8a01a5e Mon Sep 17 00:00:00 2001 From: George Fraser Date: Sun, 6 Jan 2019 12:53:17 -0800 Subject: Cache modified in FileStore --- src/main/java/org/javacs/Cache.java | 9 +------ src/main/java/org/javacs/FileStore.java | 34 ++++++++++++++++++++++++ src/main/java/org/javacs/JavaLanguageServer.java | 3 +++ 3 files changed, 38 insertions(+), 8 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/javacs/Cache.java b/src/main/java/org/javacs/Cache.java index 139d1e1..f08270f 100644 --- a/src/main/java/org/javacs/Cache.java +++ b/src/main/java/org/javacs/Cache.java @@ -1,7 +1,5 @@ package org.javacs; -import java.io.IOException; -import java.nio.file.Files; import java.nio.file.Path; import java.time.Instant; import java.util.HashMap; @@ -50,12 +48,7 @@ class Cache { // If key was loaded before file was last modified, it needs to be reloaded var value = map.get(key); - Instant modified; - try { - modified = Files.getLastModifiedTime(file).toInstant(); - } catch (IOException e) { - throw new RuntimeException(e); - } + var modified = FileStore.modified(file); return value.created.isBefore(modified); } diff --git a/src/main/java/org/javacs/FileStore.java b/src/main/java/org/javacs/FileStore.java index 8f60e25..f3648d6 100644 --- a/src/main/java/org/javacs/FileStore.java +++ b/src/main/java/org/javacs/FileStore.java @@ -6,7 +6,9 @@ import java.io.StringReader; import java.io.StringWriter; import java.net.URI; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; +import java.time.Instant; import java.util.HashMap; import java.util.Map; import java.util.Set; @@ -20,6 +22,38 @@ import org.javacs.lsp.TextDocumentContentChangeEvent; class FileStore { private static final Map activeDocuments = new HashMap<>(); + private static final Map modified = new HashMap<>(); + + static Instant modified(Path file) { + // If we've never checked before, look up modified time on disk + if (!modified.containsKey(file)) { + readModifiedFromDisk(file); + } + + // Look up modified time from cache + return modified.get(file); + } + + static void externalCreate(Path file) { + readModifiedFromDisk(file); + } + + static void externalChange(Path file) { + readModifiedFromDisk(file); + } + + static void externalDelete(Path file) { + modified.remove(file); + } + + private static void readModifiedFromDisk(Path file) { + try { + var time = Files.getLastModifiedTime(file).toInstant(); + modified.put(file, time); + } catch (IOException e) { + throw new RuntimeException(e); + } + } static boolean isJavaFile(URI uri) { return uri.getScheme().equals("file") && uri.getPath().endsWith(".java"); diff --git a/src/main/java/org/javacs/JavaLanguageServer.java b/src/main/java/org/javacs/JavaLanguageServer.java index 32d4bdf..2a6ee1b 100644 --- a/src/main/java/org/javacs/JavaLanguageServer.java +++ b/src/main/java/org/javacs/JavaLanguageServer.java @@ -268,12 +268,15 @@ class JavaLanguageServer extends LanguageServer { var file = Paths.get(c.uri); switch (c.type) { case FileChangeType.Created: + FileStore.externalCreate(file); created.add(file); break; case FileChangeType.Changed: + FileStore.externalChange(file); if (sourcePath.update(file)) changed = true; break; case FileChangeType.Deleted: + FileStore.externalDelete(file); deleted.add(file); break; } -- cgit v1.2.3