diff --git a/C#.NET/source/Yitter.IdGenTest/Program.cs b/C#.NET/source/Yitter.IdGenTest/Program.cs index 4c614ae..0946be0 100644 --- a/C#.NET/source/Yitter.IdGenTest/Program.cs +++ b/C#.NET/source/Yitter.IdGenTest/Program.cs @@ -54,9 +54,9 @@ namespace Yitter.OrgSystem.TestA while (true) { - //RunSingle(); + RunSingle(); // Go(options); - CallDll(); + //CallDll(); Thread.Sleep(1000); // 每隔1秒执行一次Go } } diff --git a/ZeOthers/Vlang/source/contract/IIdGenerator.v b/ZeOthers/Vlang/source/contract/IIdGenerator.v new file mode 100644 index 0000000..c85a917 --- /dev/null +++ b/ZeOthers/Vlang/source/contract/IIdGenerator.v @@ -0,0 +1,5 @@ +module contract + +pub interface IIdGenerator { + new_long() u64 +} \ No newline at end of file diff --git a/ZeOthers/Vlang/source/contract/ISnowWorker.v b/ZeOthers/Vlang/source/contract/ISnowWorker.v new file mode 100644 index 0000000..0494115 --- /dev/null +++ b/ZeOthers/Vlang/source/contract/ISnowWorker.v @@ -0,0 +1,5 @@ +module contract + +pub interface ISnowWorker { + next_id() u64 +} \ No newline at end of file diff --git a/ZeOthers/Vlang/source/contract/IdGeneratorOptions.v b/ZeOthers/Vlang/source/contract/IdGeneratorOptions.v new file mode 100644 index 0000000..f31e5ee --- /dev/null +++ b/ZeOthers/Vlang/source/contract/IdGeneratorOptions.v @@ -0,0 +1,13 @@ +module contract + +pub struct IdGeneratorOptions { +pub mut: + method u16 =1// 雪花计算方法,(1-漂移算法|2-传统算法),默认1 + base_time i64// 基础时间,不能超过当前系统时间 + worker_id u16 =1// 机器码,与 workerid_bitlength 有关系 + workerid_bitlength byte=6// 机器码位长,范围:1-21(要求:序列数位长+机器码位长不超过22) + seq_bitlength byte=6// 序列数位长,范围:2-21(要求:序列数位长+机器码位长不超过22) + max_seqnumber u32// 最大序列数(含),(由seq_bitlength计算的最大值) + min_seqnumber u32// 最小序列数(含),默认5,不小于1,不大于max_seqnumber + top_over_cost_count u32 =2000// 最大漂移次数(含),默认2000,推荐范围500-10000(与计算能力有关) +} \ No newline at end of file diff --git a/ZeOthers/Vlang/source/core/SnowWorkerM1.v b/ZeOthers/Vlang/source/core/SnowWorkerM1.v new file mode 100644 index 0000000..af38d65 --- /dev/null +++ b/ZeOthers/Vlang/source/core/SnowWorkerM1.v @@ -0,0 +1,198 @@ +module core + +import contract +import time +import sync + +pub struct SnowWorkerM1{ +mut: + method u16 // 雪花计算方法,(1-漂移算法|2-传统算法),默认1 + base_time i64 // 基础时间,不能超过当前系统时间 + worker_id u16 // 机器码,与 workerid_bitlength 有关系 + workerid_bitlength byte// 机器码位长,范围:1-21(要求:序列数位长+机器码位长不超过22) + seq_bitlength byte// 序列数位长,范围:2-21(要求:序列数位长+机器码位长不超过22) + max_seqnumber u32 // 最大序列数(含),(由seq_bitlength计算的最大值) + min_seqnumber u32 // 最小序列数(含),默认5,不小于1,不大于max_seqnumber + top_over_cost_count u32 // 最大漂移次数(含),默认2000,推荐范围500-10000(与计算能力有关) + + timestamp_shift byte + current_seqnumber u32 + last_time_tick i64 + turn_back_timetick i64 + turnback_index byte + is_over_cost bool + overcostcount_inoneterm u32 + gencount_inoneterm u32 + term_index u32 + mu sync.Mutex +} + +pub fn make_sf_m1(options &contract.IdGeneratorOptions) &contract.ISnowWorker{ + worker_id := options.worker_id + + mut workerid_bitlength:=byte(6) + if options.workerid_bitlength != 0 { + workerid_bitlength = options.workerid_bitlength + } + mut seq_bitlength:=byte(6) + if options.seq_bitlength != 0 { + seq_bitlength = options.seq_bitlength + } + mut max_seqnumber:=u32(0) + if options.max_seqnumber > 0 { + max_seqnumber = options.max_seqnumber + } else { + max_seqnumber = (1< 10000 { + m1.term_index = 0 + } +} + +// fn (m1 &SnowWorkerM1) begin_turn_back_action(use_time_tick i64) { + +// } + +// fn (m1 &SnowWorkerM1) end_turn_back_action(use_time_tick i64) { + +// } + +fn (mut m1 SnowWorkerM1) next_over_cost_id() u64 { + current_time_tick := m1.get_current_time_tick() + if current_time_tick > m1.last_time_tick { + m1.end_over_cost_action() + m1.last_time_tick = current_time_tick + m1.current_seqnumber = m1.min_seqnumber + m1.is_over_cost = false + m1.overcostcount_inoneterm = 0 + m1.gencount_inoneterm = 0 + return m1.calc_id() + } + if m1.overcostcount_inoneterm >= m1.top_over_cost_count { + m1.end_over_cost_action() + m1.last_time_tick = m1.get_next_time_tick() + m1.current_seqnumber = m1.min_seqnumber + m1.is_over_cost = false + m1.overcostcount_inoneterm = 0 + m1.gencount_inoneterm = 0 + return m1.calc_id() + } + if m1.current_seqnumber > m1.max_seqnumber { + m1.last_time_tick++ + m1.current_seqnumber = m1.min_seqnumber + m1.is_over_cost = true + m1.overcostcount_inoneterm++ + m1.gencount_inoneterm++ + + return m1.calc_id() + } + m1.gencount_inoneterm++ + return m1.calc_id() +} + +fn (mut m1 SnowWorkerM1) next_normal_id() u64 { + current_time_tick := m1.get_current_time_tick() + if current_time_tick < m1.last_time_tick { + if m1.turn_back_timetick < 1 { + m1.turn_back_timetick = m1.last_time_tick - 1 + m1.turnback_index++ + // 每毫秒序列数的前5位是预留位,0用于手工新值,1-4是时间回拨次序 + // 最多4次回拨(防止回拨重叠) + if m1.turnback_index > 4 { + m1.turnback_index = 1 + } + // m1.begin_turn_back_action(m1.turn_back_timetick) + } + return m1.calc_turn_back_id() + } + // 时间追平时,turn_back_timetick清零 + if m1.turn_back_timetick > 0 { + // m1.end_turn_back_action(m1.turn_back_timetick) + m1.turn_back_timetick = 0 + } + if current_time_tick > m1.last_time_tick { + m1.last_time_tick = current_time_tick + m1.current_seqnumber = m1.min_seqnumber + return m1.calc_id() + } + if m1.current_seqnumber > m1.max_seqnumber { + // m1.begin_over_cost_action(current_time_tick) + m1.term_index++ + m1.last_time_tick++ + m1.current_seqnumber = m1.min_seqnumber + m1.is_over_cost = true + m1.overcostcount_inoneterm = 1 + m1.gencount_inoneterm = 1 + + return m1.calc_id() + } + return m1.calc_id() +} + +fn (mut m1 SnowWorkerM1) calc_id() u64 { + result := u64(m1.last_time_tick< time.now().unix_time_milli() { + panic("base_time error.") + } + + if options.seq_bitlength+options.workerid_bitlength > 22 { + panic("error:workerid_bitlength + seq_bitlength <= 22") + } + + max_workerid_number := 1< 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< 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() +} \ No newline at end of file diff --git a/ZeOthers/Vlang/source/gen/YitIdHelper.v b/ZeOthers/Vlang/source/gen/YitIdHelper.v new file mode 100644 index 0000000..9bbcc69 --- /dev/null +++ b/ZeOthers/Vlang/source/gen/YitIdHelper.v @@ -0,0 +1,12 @@ +module gen + +import contract + +pub struct YitIdHelper { +mut: + id_gen contract.IIdGenerator +} + +pub fn (yih &YitIdHelper) next_id() u64 { + return yih.id_gen.new_long() +} \ No newline at end of file diff --git a/ZeOthers/Vlang/source/test b/ZeOthers/Vlang/source/test new file mode 100644 index 0000000..24b2f2c Binary files /dev/null and b/ZeOthers/Vlang/source/test differ diff --git a/ZeOthers/Vlang/source/test.v b/ZeOthers/Vlang/source/test.v new file mode 100644 index 0000000..ee4fc91 --- /dev/null +++ b/ZeOthers/Vlang/source/test.v @@ -0,0 +1,31 @@ +module main +import time +import contract +import gen + +fn main(){ + // 方法一:直接采用默认方法生成一个Id + yid := gen.YitIdHelper{ + id_gen: gen.make_generator(&contract.IdGeneratorOptions{ + method: 1 + base_time: 1582136402000 + workerid_bitlength:6 + seq_bitlength:6 + }) + } + println(yid.next_id()) + + // 方法二:自定义参数 + + times := 50000 + + for { + begin := time.now().unix_time_milli() + for i := 0; i < times; i++ { + yid.next_id() + } + end := time.now().unix_time_milli() + println("漂移,总共:"+times.str()+","+(end-begin).str()+" ms") + time.sleep(1 * time.second) + } +} \ No newline at end of file