Skip to content

Commit

Permalink
Added new convenience class Nowish to always give a unique timestamp,…
Browse files Browse the repository at this point in the history
… even if it's not 100% accurate
  • Loading branch information
darkfrog26 committed Jul 25, 2024
1 parent 0b2da28 commit 35840c2
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions core/src/main/scala/lightdb/util/Nowish.scala
Original file line number Diff line number Diff line change
@@ -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
}
})
}

0 comments on commit 35840c2

Please sign in to comment.