1
0
mirror of synced 2025-11-06 03:20:55 +08:00
Files
SnowFlake-IdGenerator/ZeOthers/Vlang/source/gen/DefaultIdGenerator.v
2021-04-01 13:29:57 +08:00

67 lines
1.6 KiB
V
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

module gen
import contract
import core
import time
pub struct DefaultIdGenerator {
mut:
options &contract.IdGeneratorOptions
snow_worker &contract.ISnowWorker
}
pub fn make_generator(options &contract.IdGeneratorOptions) &DefaultIdGenerator {
min_time := i64(631123200000)
if options.base_time < min_time || options.base_time > time.now().unix_time_milli() {
panic("base_time error.")
}
if options.seq_bitlength+options.workerid_bitlength > 22 {
panic("errorworkerid_bitlength + seq_bitlength <= 22")
}
max_workerid_number := 1<<options.workerid_bitlength - 1
if options.worker_id > max_workerid_number {
panic("WorkerId error. (range:[1, " + max_workerid_number.str() + "]")
}
if options.seq_bitlength < 2 || options.seq_bitlength > 21 {
panic("seq_bitlength error. (range:[2, 21])")
}
max_seqnumber := 1<<options.seq_bitlength - 1
if options.max_seqnumber > max_seqnumber {
panic("MaxSeqNumber error. (range:[1, " + max_seqnumber.str() + "]")
}
if options.min_seqnumber > max_seqnumber {
panic("MinSeqNumber error. (range:[1, " + max_seqnumber.str() + "]")
}
match options.method {
1 {
return &DefaultIdGenerator{
options: options,
snow_worker: core.make_sf_m1(options),
}
}
2 {
return &DefaultIdGenerator{
options: options,
snow_worker: core.make_sf_m2(options),
}
}
else {
return &DefaultIdGenerator{
options: options,
snow_worker: core.make_sf_m1(options),
}
}
}
}
pub fn (mut dig DefaultIdGenerator) new_long() u64 {
return dig.snow_worker.next_id()
}