| @@ -103,7 +103,7 @@ namespace Yitter.IdGenerator | |||
| if (MaxSeqNumber == 0) | |||
| { | |||
| MaxSeqNumber = (int)Math.Pow(2, SeqBitLength) - 1; | |||
| MaxSeqNumber = (1 << SeqBitLength) - 1; | |||
| } | |||
| _TimestampShift = (byte)(WorkerIdBitLength + SeqBitLength); | |||
| @@ -49,7 +49,7 @@ namespace Yitter.IdGenerator | |||
| throw new ApplicationException("error:WorkerIdBitLength + SeqBitLength <= 22"); | |||
| } | |||
| var maxWorkerIdNumber = Math.Pow(2, options.WorkerIdBitLength) - 1; | |||
| var maxWorkerIdNumber = (1 << options.WorkerIdBitLength) - 1; | |||
| if (options.WorkerId < 0 || options.WorkerId > maxWorkerIdNumber) | |||
| { | |||
| throw new ApplicationException("WorkerId error. (range:[0, " + (maxWorkerIdNumber > 0 ? maxWorkerIdNumber : 63) + "]"); | |||
| @@ -60,13 +60,13 @@ namespace Yitter.IdGenerator | |||
| throw new ApplicationException("SeqBitLength error. (range:[2, 21])"); | |||
| } | |||
| var maxSeqNumber = Math.Pow(2, options.SeqBitLength) - 1; | |||
| var maxSeqNumber = (1 << options.SeqBitLength) - 1; | |||
| if (options.MaxSeqNumber < 0 || options.MaxSeqNumber > maxSeqNumber) | |||
| { | |||
| throw new ApplicationException("MaxSeqNumber error. (range:[1, " + maxSeqNumber + "]"); | |||
| } | |||
| var maxValue = maxSeqNumber; // maxSeqNumber - 1; | |||
| var maxValue = maxSeqNumber; | |||
| if (options.MinSeqNumber < 1 || options.MinSeqNumber > maxValue) | |||
| { | |||
| throw new ApplicationException("MinSeqNumber error. (range:[1, " + maxValue + "]"); | |||
| @@ -7,7 +7,6 @@ | |||
| package gen | |||
| import ( | |||
| "math" | |||
| "time" | |||
| "yitidgen/contract" | |||
| "yitidgen/core" | |||
| @@ -33,7 +32,7 @@ func NewDefaultIdGenerator(options *contract.IdGeneratorOptions) *DefaultIdGener | |||
| panic("error:WorkerIdBitLength + SeqBitLength <= 22") | |||
| } | |||
| maxWorkerIdNumber := uint16(math.Pow(float64(2), float64(options.WorkerIdBitLength))) - 1 | |||
| maxWorkerIdNumber := uint16(1<<options.WorkerIdBitLength) - 1 | |||
| if options.WorkerId > maxWorkerIdNumber { | |||
| panic("WorkerId error. (range:[1, " + string(maxWorkerIdNumber) + "]") | |||
| } | |||
| @@ -42,7 +41,7 @@ func NewDefaultIdGenerator(options *contract.IdGeneratorOptions) *DefaultIdGener | |||
| panic("SeqBitLength error. (range:[2, 21])") | |||
| } | |||
| maxSeqNumber := uint32(math.Pow(2, float64(options.SeqBitLength))) - 1 | |||
| maxSeqNumber := uint32(1<<options.SeqBitLength) - 1 | |||
| if options.MaxSeqNumber > maxSeqNumber { | |||
| panic("MaxSeqNumber error. (range:[1, " + string(maxSeqNumber) + "]") | |||
| } | |||
| @@ -1,79 +1,79 @@ | |||
| /* | |||
| * 版权属于:yitter(yitter@126.com) | |||
| * 开源地址:https://gitee.com/yitter/idgenerator | |||
| */ | |||
| package com.yitter.idgen; | |||
| import com.yitter.contract.ISnowWorker; | |||
| import com.yitter.contract.IdGeneratorException; | |||
| import com.yitter.contract.IdGeneratorOptions; | |||
| import com.yitter.contract.IIdGenerator; | |||
| import com.yitter.core.SnowWorkerM1; | |||
| import com.yitter.core.SnowWorkerM2; | |||
| public class DefaultIdGenerator implements IIdGenerator { | |||
| private static ISnowWorker _SnowWorker = null; | |||
| public DefaultIdGenerator(IdGeneratorOptions options) throws IdGeneratorException { | |||
| if (options == null) { | |||
| throw new IdGeneratorException("options error."); | |||
| } | |||
| if (options.BaseTime < 315504000000L || options.BaseTime > System.currentTimeMillis()) { | |||
| throw new IdGeneratorException("BaseTime error."); | |||
| } | |||
| if (options.WorkerIdBitLength <= 0) { | |||
| throw new IdGeneratorException("WorkerIdBitLength error.(range:[1, 21])"); | |||
| } | |||
| if (options.SeqBitLength + options.WorkerIdBitLength > 22) { | |||
| throw new IdGeneratorException("error:WorkerIdBitLength + SeqBitLength <= 22"); | |||
| } | |||
| double maxWorkerIdNumber = Math.pow(2, options.WorkerIdBitLength) - 1; | |||
| if (options.WorkerId < 0 || options.WorkerId > maxWorkerIdNumber) { | |||
| throw new IdGeneratorException("WorkerId error. (range:[0, " + (maxWorkerIdNumber > 0 ? maxWorkerIdNumber : 63) + "]"); | |||
| } | |||
| if (options.SeqBitLength < 2 || options.SeqBitLength > 21) { | |||
| throw new IdGeneratorException("SeqBitLength error. (range:[2, 21])"); | |||
| } | |||
| double maxSeqNumber = Math.pow(2, options.SeqBitLength) - 1; | |||
| if (options.MaxSeqNumber < 0 || options.MaxSeqNumber > maxSeqNumber) { | |||
| throw new IdGeneratorException("MaxSeqNumber error. (range:[1, " + maxSeqNumber + "]"); | |||
| } | |||
| double maxValue = maxSeqNumber; | |||
| if (options.MinSeqNumber < 1 || options.MinSeqNumber > maxValue) { | |||
| throw new IdGeneratorException("MinSeqNumber error. (range:[1, " + maxValue + "]"); | |||
| } | |||
| switch (options.Method) { | |||
| case 1: | |||
| _SnowWorker = new SnowWorkerM1(options); | |||
| break; | |||
| case 2: | |||
| _SnowWorker = new SnowWorkerM2(options); | |||
| break; | |||
| default: | |||
| _SnowWorker = new SnowWorkerM1(options); | |||
| break; | |||
| } | |||
| if (options.Method == 1) { | |||
| try { | |||
| Thread.sleep(500); | |||
| } catch (InterruptedException e) { | |||
| e.printStackTrace(); | |||
| } | |||
| } | |||
| } | |||
| @Override | |||
| public long newLong() { | |||
| return _SnowWorker.nextId(); | |||
| } | |||
| } | |||
| /* | |||
| * 版权属于:yitter(yitter@126.com) | |||
| * 开源地址:https://gitee.com/yitter/idgenerator | |||
| */ | |||
| package com.yitter.idgen; | |||
| import com.yitter.contract.ISnowWorker; | |||
| import com.yitter.contract.IdGeneratorException; | |||
| import com.yitter.contract.IdGeneratorOptions; | |||
| import com.yitter.contract.IIdGenerator; | |||
| import com.yitter.core.SnowWorkerM1; | |||
| import com.yitter.core.SnowWorkerM2; | |||
| public class DefaultIdGenerator implements IIdGenerator { | |||
| private static ISnowWorker _SnowWorker = null; | |||
| public DefaultIdGenerator(IdGeneratorOptions options) throws IdGeneratorException { | |||
| if (options == null) { | |||
| throw new IdGeneratorException("options error."); | |||
| } | |||
| if (options.BaseTime < 315504000000L || options.BaseTime > System.currentTimeMillis()) { | |||
| throw new IdGeneratorException("BaseTime error."); | |||
| } | |||
| if (options.WorkerIdBitLength <= 0) { | |||
| throw new IdGeneratorException("WorkerIdBitLength error.(range:[1, 21])"); | |||
| } | |||
| if (options.SeqBitLength + options.WorkerIdBitLength > 22) { | |||
| throw new IdGeneratorException("error:WorkerIdBitLength + SeqBitLength <= 22"); | |||
| } | |||
| int maxWorkerIdNumber = (1 << options.WorkerIdBitLength) - 1; | |||
| if (options.WorkerId < 0 || options.WorkerId > maxWorkerIdNumber) { | |||
| throw new IdGeneratorException("WorkerId error. (range:[0, " + (maxWorkerIdNumber > 0 ? maxWorkerIdNumber : 63) + "]"); | |||
| } | |||
| if (options.SeqBitLength < 2 || options.SeqBitLength > 21) { | |||
| throw new IdGeneratorException("SeqBitLength error. (range:[2, 21])"); | |||
| } | |||
| int maxSeqNumber = (1 << options.SeqBitLength) - 1; | |||
| if (options.MaxSeqNumber < 0 || options.MaxSeqNumber > maxSeqNumber) { | |||
| throw new IdGeneratorException("MaxSeqNumber error. (range:[1, " + maxSeqNumber + "]"); | |||
| } | |||
| int maxValue = maxSeqNumber; | |||
| if (options.MinSeqNumber < 1 || options.MinSeqNumber > maxValue) { | |||
| throw new IdGeneratorException("MinSeqNumber error. (range:[1, " + maxValue + "]"); | |||
| } | |||
| switch (options.Method) { | |||
| case 1: | |||
| _SnowWorker = new SnowWorkerM1(options); | |||
| break; | |||
| case 2: | |||
| _SnowWorker = new SnowWorkerM2(options); | |||
| break; | |||
| default: | |||
| _SnowWorker = new SnowWorkerM1(options); | |||
| break; | |||
| } | |||
| if (options.Method == 1) { | |||
| try { | |||
| Thread.sleep(500); | |||
| } catch (InterruptedException e) { | |||
| e.printStackTrace(); | |||
| } | |||
| } | |||
| } | |||
| @Override | |||
| public long newLong() { | |||
| return _SnowWorker.nextId(); | |||
| } | |||
| } | |||
| @@ -2,7 +2,7 @@ | |||
| ## 介绍 | |||
| 1.用一种全新的雪花漂移算法,让ID更短、生成速度更快。 | |||
| 1.一个全新的雪花漂移算法,使生成的ID更短、速度更快。 | |||
| 2.核心在于缩短ID长度的同时,还能拥有极高瞬时并发处理量(保守值 50W/0.1s)。 | |||
| @@ -47,7 +47,7 @@ impl SnowWorkerM1 { | |||
| if options.BaseTime == 0 { | |||
| self.BaseTime = 1582136402000; | |||
| } else if options.BaseTime < 631123200000 || options.BaseTime > Utc::now().timestamp_millis() { | |||
| panic!("BaseTime error.") | |||
| panic!("BaseTime error."); | |||
| } else { | |||
| self.BaseTime = options.BaseTime; | |||
| } | |||
| @@ -58,39 +58,39 @@ impl SnowWorkerM1 { | |||
| panic!("WorkerIdBitLength error.(range:[1, 21])"); | |||
| } | |||
| if options.SeqBitLength + options.WorkerIdBitLength > 22 { | |||
| panic!("error:WorkerIdBitLength + SeqBitLength <= 22") | |||
| panic!("error:WorkerIdBitLength + SeqBitLength <= 22"); | |||
| } else { | |||
| self.WorkerIdBitLength = options.WorkerIdBitLength; | |||
| // self.WorkerIdBitLength = if options.WorkerIdBitLength == 0 { 6 } else { options.WorkerIdBitLength }; | |||
| // self.WorkerIdBitLength = options.WorkerIdBitLength; | |||
| self.WorkerIdBitLength = if options.WorkerIdBitLength <= 0 { 6 } else { options.WorkerIdBitLength }; | |||
| } | |||
| // WorkerId | |||
| let maxWorkerIdNumber = (2 as u32).pow(options.WorkerIdBitLength as u32) - 1; | |||
| let maxWorkerIdNumber = (1 << options.WorkerIdBitLength) - 1; | |||
| if options.WorkerId < 0 || options.WorkerId > maxWorkerIdNumber { | |||
| panic!("WorkerId error. (range:[0, {} ]", if maxWorkerIdNumber <= 0 { 63 } else { maxWorkerIdNumber }) | |||
| panic!("WorkerId error. (range:[0, {} ]", if maxWorkerIdNumber <= 0 { 63 } else { maxWorkerIdNumber }); | |||
| } else { | |||
| self.WorkerId = options.WorkerId; | |||
| } | |||
| // SeqBitLength | |||
| if options.SeqBitLength < 2 || options.SeqBitLength > 21 { | |||
| panic!("SeqBitLength error. (range:[2, 21])") | |||
| panic!("SeqBitLength error. (range:[2, 21])"); | |||
| } else { | |||
| self.SeqBitLength = options.SeqBitLength; | |||
| // self.SeqBitLength = if options.SeqBitLength == 0 { 6 } else { options.SeqBitLength }; | |||
| // self.SeqBitLength = options.SeqBitLength; | |||
| self.SeqBitLength = if options.SeqBitLength <= 0 { 6 } else { options.SeqBitLength }; | |||
| } | |||
| // MaxSeqNumber | |||
| let maxSeqNumber = (2 as u32).pow(options.SeqBitLength as u32) - 1; | |||
| let maxSeqNumber = (1 << options.SeqBitLength) - 1; | |||
| if options.MaxSeqNumber > maxSeqNumber { | |||
| panic!("MaxSeqNumber error. (range:[1, {}]", maxSeqNumber) | |||
| panic!("MaxSeqNumber error. (range:[1, {}]", maxSeqNumber); | |||
| } else { | |||
| self.MaxSeqNumber = if options.MaxSeqNumber <= 0 { (2 as u32).pow(options.SeqBitLength as u32) - 1 } else { options.MaxSeqNumber }; | |||
| self.MaxSeqNumber = if options.MaxSeqNumber <= 0 { maxSeqNumber } else { options.MaxSeqNumber }; | |||
| } | |||
| // MinSeqNumber | |||
| if options.MinSeqNumber > maxSeqNumber { | |||
| panic!("MinSeqNumber error. (range:[1, {}]", maxSeqNumber) | |||
| panic!("MinSeqNumber error. (range:[1, {}]", maxSeqNumber); | |||
| } else { | |||
| self.MinSeqNumber = options.MinSeqNumber; | |||
| } | |||
| @@ -100,7 +100,7 @@ impl SnowWorkerM1 { | |||
| self._CurrentSeqNumber = options.MinSeqNumber; | |||
| if options.Method == 1 { | |||
| sleep(std::time::Duration::from_millis(500)) | |||
| sleep(std::time::Duration::from_millis(500)); | |||
| } | |||
| } | |||