-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
62 additions
and
25 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
stream-loader-core/src/main/scala/com/adform/streamloader/util/UuidExtensions.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.adform.streamloader.util | ||
|
||
import java.nio.ByteBuffer | ||
import java.security.SecureRandom | ||
import java.util.UUID | ||
|
||
object UuidExtensions { | ||
implicit class RichUuid(uuid: UUID) { | ||
def toBytes: Array[Byte] = { | ||
val bb = ByteBuffer.allocate(16) | ||
bb.putLong(uuid.getMostSignificantBits) | ||
bb.putLong(uuid.getLeastSignificantBits) | ||
bb.array | ||
} | ||
} | ||
|
||
private val random = new SecureRandom | ||
|
||
def randomUUIDv7(): UUID = { | ||
val value = new Array[Byte](16) | ||
random.nextBytes(value) | ||
|
||
val timestamp = ByteBuffer.allocate(8) | ||
timestamp.putLong(System.currentTimeMillis) | ||
System.arraycopy(timestamp.array, 2, value, 0, 6) | ||
|
||
value(6) = ((value(6) & 0x0f) | 0x70).toByte | ||
value(8) = ((value(8) & 0x3f) | 0x80).toByte | ||
|
||
val buf = ByteBuffer.wrap(value) | ||
val high = buf.getLong | ||
val low = buf.getLong | ||
|
||
new UUID(high, low) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
stream-loader-core/src/test/scala/com/adform/streamloader/util/UuidExtensionsTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.adform.streamloader.util | ||
|
||
import com.adform.streamloader.util.UuidExtensions.randomUUIDv7 | ||
import org.scalatest.funspec.AnyFunSpec | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
class UuidExtensionsTest extends AnyFunSpec with Matchers { | ||
|
||
it("should generate unique UUIDv7 values") { | ||
val uuids = (0 until 100).map(_ => randomUUIDv7()) | ||
uuids should contain theSameElementsInOrderAs uuids.distinct | ||
} | ||
|
||
it("should generate alphabetically sorted UUIDv7 values") { | ||
val uuids = (0 until 10) | ||
.map(_ => { | ||
randomUUIDv7() | ||
Thread.sleep(5) // the v7 spec guarantees ordering in a 1ms scope | ||
}) | ||
.map(_.toString) | ||
|
||
uuids should contain theSameElementsInOrderAs uuids.sorted | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 0 additions & 23 deletions
23
stream-loader-tests/src/main/scala/com/adform/streamloader/util/UuidExtensions.scala
This file was deleted.
Oops, something went wrong.