Skip to content

Commit

Permalink
Simplify Uuidv7 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
hfhbd committed Aug 17, 2024
1 parent f20d298 commit 50c573a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 32 deletions.
17 changes: 11 additions & 6 deletions detekt-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,19 @@
<ID>MagicNumber:Uuid.kt$60</ID>
<ID>MagicNumber:Uuid.kt$61</ID>
<ID>MagicNumber:Uuid.kt$7</ID>
<ID>MagicNumber:Uuidv7.kt$0x0fffL</ID>
<ID>MagicNumber:Uuidv7.kt$0xffff0000L</ID>
<ID>MagicNumber:Uuidv7.kt$0x3F</ID>
<ID>MagicNumber:Uuidv7.kt$0x70</ID>
<ID>MagicNumber:Uuidv7.kt$0x80</ID>
<ID>MagicNumber:Uuidv7.kt$0xFF</ID>
<ID>MagicNumber:Uuidv7.kt$16</ID>
<ID>MagicNumber:Uuidv7.kt$28672</ID>
<ID>MagicNumber:Uuidv7.kt$24</ID>
<ID>MagicNumber:Uuidv7.kt$3</ID>
<ID>MagicNumber:Uuidv7.kt$32</ID>
<ID>MagicNumber:Uuidv7.kt$4095</ID>
<ID>MagicNumber:Uuidv7.kt$48</ID>
<ID>MagicNumber:Uuidv7.kt$62</ID>
<ID>MagicNumber:Uuidv7.kt$4</ID>
<ID>MagicNumber:Uuidv7.kt$40</ID>
<ID>MagicNumber:Uuidv7.kt$5</ID>
<ID>MagicNumber:Uuidv7.kt$6</ID>
<ID>MagicNumber:Uuidv7.kt$8</ID>
<ID>TooManyFunctions:SHA1.kt$SHA1</ID>
</CurrentIssues>
</SmellBaseline>
30 changes: 12 additions & 18 deletions kotlinx-uuid-core/src/commonMain/kotlin/app/softwork/uuid/Uuidv7.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,19 @@ public fun Uuidv7(timeStamp: Long, random: Random): Uuid {
require(timeStamp <= UNIX_48_TIMESTAMP) {
"timeStamp $timeStamp must be <= 48 bits, was $timeStamp."
}
val (
helperTimeStampAndVersionRaw,
helperClockSequenceVariantAndNodeRaw
) = random.nextUuid().toLongs { mostSignificantBits, leastSignificantBits ->
mostSignificantBits to leastSignificantBits
}
val leftTimeStamp = timeStamp shl 16
// set version to 0b0111
val leftTimeStampAndVersion = leftTimeStamp or 28672
val randA = helperTimeStampAndVersionRaw.let { timeStampAndVersionRaw ->
(timeStampAndVersionRaw ushr 32) or
(timeStampAndVersionRaw and 0xffff0000L shl 16) or
(timeStampAndVersionRaw and 0x0fffL shl 48)
} and 4095
val timeStampAndVersionRaw = leftTimeStampAndVersion or randA
// set variant to 0b10
val clockSequenceVariantAndNodeRaw = (2L shl 62) or (helperClockSequenceVariantAndNodeRaw ushr 2)
val value = random.nextBytes(Uuid.SIZE_BYTES)

value[0] = ((timeStamp shr 40) and 0xFF).toByte()
value[1] = ((timeStamp shr 32) and 0xFF).toByte()
value[2] = ((timeStamp shr 24) and 0xFF).toByte()
value[3] = ((timeStamp shr 16) and 0xFF).toByte()
value[4] = ((timeStamp shr 8) and 0xFF).toByte()
value[5] = (timeStamp and 0xFF).toByte()

value[6] = (value[6].toInt() and 0x0F or 0x70).toByte()
value[8] = (value[8].toInt() and 0x3F or 0x80).toByte()

return Uuid.fromLongs(timeStampAndVersionRaw, clockSequenceVariantAndNodeRaw)
return Uuid.fromByteArray(value)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import kotlin.test.*
class Uuidv7Test {
@Test
fun test() {
val one = Uuidv7(0x17F22E279B0, random = Random(4242))
val two = Uuidv7(1645557742000, random = Random(4242))
assertEquals(one, two)
val uuid = Uuidv7(1645557742000, random = Random(4242))

assertEquals(1645557742000, one.unixTimeStamp)
assertEquals(1645557742000, two.unixTimeStamp)
assertEquals("017f22e2-79b0-7b35-ab5c-c2334bd875e2", one.toString())
assertEquals(7, one.versionNumber)
assertEquals(1645557742000, uuid.unixTimeStamp)
assertEquals("017f22e2-79b0-7a1e-ad73-08cd2f61d78a", uuid.toString())
assertEquals(7, uuid.versionNumber)
assertEquals(5, uuid.variant)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class InstantTest {
val timestamp = Instant.parse("2020-02-20T10:21:42Z")
val uuid = Uuidv7(timeStamp = timestamp, random = Random(4242))
assertEquals(timestamp, uuid.instant)
assertEquals("0170621e-0ef0-7b35-ab5c-c2334bd875e2", uuid.toString())
assertEquals("0170621e-0ef0-7a1e-ad73-08cd2f61d78a", uuid.toString())
assertEquals(7, uuid.versionNumber)
}
}

0 comments on commit 50c573a

Please sign in to comment.