Skip to content

Commit

Permalink
added converting hybrid timestamp to date time
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamhead committed Oct 13, 2021
1 parent 6f50c44 commit f7f40f2
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions content/hybrid-clock.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,21 @@ class HybridClockMVCCStore…
getLogger().info("Available version keys " + versionKeys + ". Reading@" + versionKeys);
return (versionKeys == null)? Optional.empty(): Optional.of(versionKeys.getValue());
}
```

### 将混合时间戳转换为日期时间

通过将系统时间戳和逻辑计数合并在一起,混合时钟可以转换成实际的时间戳。正如在[混合时钟(hybrid-clock)](https://cse.buffalo.edu/tech-reports/2014-04.pdf)这篇论文中所讨论的,保留系统时间的前 48 位,而低 16 位有逻辑计数器所取代。

```java
class HybridTimestamp…

public LocalDateTime toDateTime() {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMillis()), ZoneOffset.UTC);
}

public long epochMillis() {
//Compact timestamp as discussed in https://cse.buffalo.edu/tech-reports/2014-04.pdf.
return (wallClockTime >> 16 << 16) | (ticks << 48 >> 48);
}
```

0 comments on commit f7f40f2

Please sign in to comment.