diff options
author | Ola Aunrønning <olaa@verizonmedia.com> | 2019-08-22 16:57:30 +0200 |
---|---|---|
committer | Ola Aunrønning <olaa@verizonmedia.com> | 2019-08-22 16:57:30 +0200 |
commit | e3fd6222a80fbd55e10db5676d107221db0e6f6f (patch) | |
tree | caf3ff8cc9cf82a77006fe19f7927850c0661612 /node-admin | |
parent | db73d97f1a3229f6d89808da35f42a9125396d7e (diff) |
Add last modified time to StoredInteger
Diffstat (limited to 'node-admin')
-rw-r--r-- | node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/StoredInteger.java | 36 |
1 files changed, 25 insertions, 11 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/StoredInteger.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/StoredInteger.java index 9151dde19a6..79283983303 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/StoredInteger.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/StoredInteger.java @@ -8,6 +8,8 @@ import java.io.UncheckedIOException; import java.nio.file.Files; import java.nio.file.NoSuchFileException; import java.nio.file.Path; +import java.time.Instant; +import java.util.Optional; import java.util.OptionalInt; import java.util.function.Supplier; import java.util.logging.Logger; @@ -24,6 +26,7 @@ public class StoredInteger implements Supplier<OptionalInt> { private final Path path; private OptionalInt value; private boolean hasBeenRead = false; + private Optional<Instant> lastModifiedTime; public StoredInteger(Path path) { this.path = path; @@ -31,17 +34,7 @@ public class StoredInteger implements Supplier<OptionalInt> { @Override public OptionalInt get() { - if (!hasBeenRead) { - try { - String value = new String(Files.readAllBytes(path)); - this.value = OptionalInt.of(Integer.parseInt(value)); - } catch (NoSuchFileException e) { - this.value = OptionalInt.empty(); - } catch (IOException e) { - throw new UncheckedIOException("Failed to read integer in " + path, e); - } - hasBeenRead = true; - } + if (!hasBeenRead) readValue(); return value; } @@ -50,9 +43,30 @@ public class StoredInteger implements Supplier<OptionalInt> { Files.write(path, Integer.toString(value).getBytes()); this.value = OptionalInt.of(value); this.hasBeenRead = true; + this.lastModifiedTime = Optional.of(Instant.now()); taskContext.log(logger, "Stored new integer in %s: %d", path, value); } catch (IOException e) { throw new UncheckedIOException("Failed to store integer in " + path, e); } } + + public Optional<Instant> getLastModifiedTime() { + if (!hasBeenRead) readValue(); + return lastModifiedTime; + } + + private void readValue() { + try { + String value = new String(Files.readAllBytes(path)); + this.value = OptionalInt.of(Integer.parseInt(value)); + this.lastModifiedTime = Optional.of(Files.getLastModifiedTime(path).toInstant()); + } catch (NoSuchFileException e) { + this.value = OptionalInt.empty(); + this.lastModifiedTime = Optional.empty(); + } catch (IOException e) { + throw new UncheckedIOException("Failed to read integer in " + path, e); + } + hasBeenRead = true; + } + } |