Skip to content

Commit

Permalink
Improve SystemNanoClock
Browse files Browse the repository at this point in the history
Avoid indirection for millis()

Support withZone(...)
  • Loading branch information
thobe committed Jan 30, 2018
1 parent 2cdf76a commit cc103ce
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
36 changes: 35 additions & 1 deletion community/common/src/main/java/org/neo4j/time/FakeClock.java
Expand Up @@ -50,7 +50,7 @@ public ZoneId getZone()
@Override
public Clock withZone( ZoneId zone )
{
throw new UnsupportedOperationException();
return new WithZone( zone );
}

@Override
Expand All @@ -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();
}
}
}
Expand Up @@ -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()
Expand Down
Expand Up @@ -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()
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit cc103ce

Please sign in to comment.