diff --git a/core/src/main/scala/lightdb/util/Nowish.scala b/core/src/main/scala/lightdb/util/Nowish.scala new file mode 100644 index 00000000..57b767ba --- /dev/null +++ b/core/src/main/scala/lightdb/util/Nowish.scala @@ -0,0 +1,20 @@ +package lightdb.util + +import java.util.concurrent.atomic.AtomicLong + +/** + * Always returns an incremented timestamp. If called multiple times within the same millisecond, the returned value will + * be incremented to always be unique. + */ +object Nowish { + private val lastTime = new AtomicLong(-1L) + + def apply(): Long = lastTime.updateAndGet(last => { + val now = System.currentTimeMillis() + if (now > last) { + now + } else { + last + 1 + } + }) +} \ No newline at end of file