From cc103ce92614fa95298732356d910fc1b73d9d9a Mon Sep 17 00:00:00 2001 From: Tobias Lindaaker Date: Tue, 23 Jan 2018 14:42:34 +0100 Subject: [PATCH] Improve SystemNanoClock Avoid indirection for millis() Support withZone(...) --- .../main/java/org/neo4j/time/FakeClock.java | 36 ++++++++++++++++++- .../java/org/neo4j/time/SystemNanoClock.java | 10 ++++-- .../TransactionStatusResultTest.java | 4 +-- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/community/common/src/main/java/org/neo4j/time/FakeClock.java b/community/common/src/main/java/org/neo4j/time/FakeClock.java index 1deb602d133bd..a4090ad437cad 100644 --- a/community/common/src/main/java/org/neo4j/time/FakeClock.java +++ b/community/common/src/main/java/org/neo4j/time/FakeClock.java @@ -50,7 +50,7 @@ public ZoneId getZone() @Override public Clock withZone( ZoneId zone ) { - throw new UnsupportedOperationException(); + return new WithZone( zone ); } @Override @@ -76,4 +76,38 @@ public FakeClock forward( long delta, TimeUnit unit ) nanoTime += unit.toNanos( delta ); return this; } + + private class WithZone extends Clock + { + private final ZoneId zone; + + WithZone( ZoneId zone ) + { + this.zone = zone; + } + + @Override + public ZoneId getZone() + { + return zone; + } + + @Override + public Clock withZone( ZoneId zone ) + { + return new WithZone( zone ); + } + + @Override + public long millis() + { + return FakeClock.this.millis(); + } + + @Override + public Instant instant() + { + return FakeClock.this.instant(); + } + } } diff --git a/community/common/src/main/java/org/neo4j/time/SystemNanoClock.java b/community/common/src/main/java/org/neo4j/time/SystemNanoClock.java index 288fb1008698f..8c835b57439ec 100644 --- a/community/common/src/main/java/org/neo4j/time/SystemNanoClock.java +++ b/community/common/src/main/java/org/neo4j/time/SystemNanoClock.java @@ -46,13 +46,19 @@ public ZoneId getZone() @Override public Clock withZone( ZoneId zone ) { - throw new UnsupportedOperationException( "Zone update is not supported." ); + return Clock.system( zone ); // the users of this method do not need a NanoClock } @Override public Instant instant() { - return Instant.now(); + return Instant.ofEpochMilli( millis() ); + } + + @Override + public long millis() + { + return System.currentTimeMillis(); } public long nanos() diff --git a/enterprise/kernel/src/test/java/org/neo4j/kernel/enterprise/builtinprocs/TransactionStatusResultTest.java b/enterprise/kernel/src/test/java/org/neo4j/kernel/enterprise/builtinprocs/TransactionStatusResultTest.java index 329a63056c104..bba9f5b90ff9c 100644 --- a/enterprise/kernel/src/test/java/org/neo4j/kernel/enterprise/builtinprocs/TransactionStatusResultTest.java +++ b/enterprise/kernel/src/test/java/org/neo4j/kernel/enterprise/builtinprocs/TransactionStatusResultTest.java @@ -162,7 +162,7 @@ private ExecutingQuery createExecutingQuery( long queryId ) return new ExecutingQuery( queryId, getTestConnectionInfo(), "testUser", "testQuery", VirtualValues.EMPTY_MAP, Collections.emptyMap(), () -> 1L, PageCursorTracer.NULL, Thread.currentThread().getId(), Thread.currentThread().getName(), - new CountingSystemNanoClock(), new CountingCpuClock(), new CountingHeapAllocation() ); + new CountingNanoClock(), new CountingCpuClock(), new CountingHeapAllocation() ); } private HttpConnectionInfo getTestConnectionInfo() @@ -215,7 +215,7 @@ protected void init( long threadId, PageCursorTracer pageCursorTracer ) } } - private static class CountingSystemNanoClock extends SystemNanoClock + private static class CountingNanoClock extends SystemNanoClock { private long time;