Skip to content

Commit

Permalink
refactor: move Snowflake class
Browse files Browse the repository at this point in the history
  • Loading branch information
gtn1024 committed Sep 27, 2024
1 parent ca0b20b commit fdcfa87
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 77 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ snowflake4cj = { git = "https://github.com/gtn1024/snowflake4cj.git", tag = "v1.
## 使用 / Usage

```cj
import snowflake4cj.snowflake.Snowflake
import snowflake4cj.Snowflake
main(): Int64 {
let snowflake = Snowflake(0, 0)
Expand Down
74 changes: 74 additions & 0 deletions src/snowflake.cj
Original file line number Diff line number Diff line change
@@ -1 +1,75 @@
package snowflake4cj

import std.time.{DateTime}
import std.sync.{ReentrantMutex}

public open class Snowflake {
internal let twepoch: Int64 = 1725148800000
internal let workerIdBits: Int64 = 5
internal let datacenterIdBits: Int64 = 5
internal let maxWorkerId: Int64 = -1 ^ (-1 << workerIdBits)
internal let maxDatacenterId: Int64 = -1 ^ (-1 << datacenterIdBits)
internal let sequenceBits: Int64 = 12
internal let workerIdShift: Int64 = sequenceBits
internal let datacenterIdShift: Int64 = sequenceBits + workerIdBits
internal let timestampLeftShift: Int64 = sequenceBits + workerIdBits + datacenterIdBits
internal let sequenceMask: Int64 = -1 ^ (-1 << sequenceBits)

internal let workerId: Int64
internal let datacenterId: Int64
internal var sequence: Int64 = 0
internal var lastTimestamp: Int64 = -1

public init(workerId: Int64, datacenterId: Int64) {
if (workerId > maxWorkerId || workerId < 0) {
throw IllegalArgumentException("workerId can't be greater than ${maxWorkerId} or less than 0")
}

if (datacenterId > maxDatacenterId || datacenterId < 0) {
throw IllegalArgumentException("datacenterId can't be greater than ${maxWorkerId} or less than 0")
}

this.workerId = workerId
this.datacenterId = datacenterId
}

let mtx = ReentrantMutex()

public func nextId() {
synchronized (mtx) {
var timestamp = timeGen()

if (timestamp < lastTimestamp) {
throw IllegalStateException("Clock moved backwards. Refusing to generate id for ${lastTimestamp - timestamp} milliseconds")
}

if (lastTimestamp == timestamp) {
sequence = (sequence + 1) & sequenceMask

if (sequence == 0) {
timestamp = tilNextMillis(lastTimestamp)
}
} else {
sequence = 0
}

lastTimestamp = timestamp

((timestamp - twepoch) << timestampLeftShift) | (datacenterId << datacenterIdShift) | (workerId << workerIdShift) | sequence
}
}

internal func tilNextMillis(lastTimestamp: Int64) {
var timestamp = timeGen()

while (timestamp <= lastTimestamp) {
timestamp = timeGen()
}

timestamp
}

internal func timeGen() {
DateTime.nowUTC().toUnixTimeStamp().toMilliseconds()
}
}
75 changes: 0 additions & 75 deletions src/snowflake/snowflake.cj

This file was deleted.

2 changes: 1 addition & 1 deletion src/snowflake/snowflake_test.cj → src/snowflake_test.cj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package snowflake4cj.snowflake
package snowflake4cj

import std.unittest.*
import std.unittest.testmacro.*
Expand Down

0 comments on commit fdcfa87

Please sign in to comment.