You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultIdGenerator.cs 3.3 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * 版权属于:yitter(yitter@126.com)
  3. * 开源地址:https://gitee.com/yitter/idgenerator
  4. * 版权协议:MIT
  5. * 版权说明:只要保留本版权,你可以免费使用、修改、分发本代码。
  6. * 免责条款:任何因为本代码产生的系统、法律、政治、宗教问题,均与版权所有者无关。
  7. *
  8. */
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Text;
  12. using System.Threading;
  13. namespace Yitter.IdGenerator
  14. {
  15. /// <summary>
  16. /// 默认实现
  17. /// </summary>
  18. public class DefaultIdGenerator : IIdGenerator
  19. {
  20. private ISnowWorker _SnowWorker { get; set; }
  21. public Action<OverCostActionArg> GenIdActionAsync
  22. {
  23. get => _SnowWorker.GenAction;
  24. set => _SnowWorker.GenAction = value;
  25. }
  26. public DefaultIdGenerator(IdGeneratorOptions options)
  27. {
  28. if (options == null)
  29. {
  30. throw new ApplicationException("options error.");
  31. }
  32. if (options.BaseTime < DateTime.Now.AddYears(-50) || options.BaseTime > DateTime.Now)
  33. {
  34. throw new ApplicationException("BaseTime error.");
  35. }
  36. if (options.WorkerIdBitLength <= 0)
  37. {
  38. throw new ApplicationException("WorkerIdBitLength error.(range:[1, 21])");
  39. }
  40. if (options.SeqBitLength + options.WorkerIdBitLength > 22)
  41. {
  42. throw new ApplicationException("error:WorkerIdBitLength + SeqBitLength <= 22");
  43. }
  44. var maxWorkerIdNumber = (1 << options.WorkerIdBitLength) - 1;
  45. if (options.WorkerId < 0 || options.WorkerId > maxWorkerIdNumber)
  46. {
  47. throw new ApplicationException("WorkerId error. (range:[0, " + (maxWorkerIdNumber > 0 ? maxWorkerIdNumber : 63) + "]");
  48. }
  49. if (options.SeqBitLength < 2 || options.SeqBitLength > 21)
  50. {
  51. throw new ApplicationException("SeqBitLength error. (range:[2, 21])");
  52. }
  53. var maxSeqNumber = (1 << options.SeqBitLength) - 1;
  54. if (options.MaxSeqNumber < 0 || options.MaxSeqNumber > maxSeqNumber)
  55. {
  56. throw new ApplicationException("MaxSeqNumber error. (range:[1, " + maxSeqNumber + "]");
  57. }
  58. var maxValue = maxSeqNumber;
  59. if (options.MinSeqNumber < 1 || options.MinSeqNumber > maxValue)
  60. {
  61. throw new ApplicationException("MinSeqNumber error. (range:[1, " + maxValue + "]");
  62. }
  63. switch (options.Method)
  64. {
  65. case 1:
  66. _SnowWorker = new SnowWorkerM1(options);
  67. break;
  68. case 2:
  69. _SnowWorker = new SnowWorkerM2(options);
  70. break;
  71. default:
  72. _SnowWorker = new SnowWorkerM1(options);
  73. break;
  74. }
  75. if (options.Method == 1)
  76. {
  77. Thread.Sleep(500);
  78. }
  79. }
  80. public long NewLong()
  81. {
  82. return _SnowWorker.NextId();
  83. }
  84. }
  85. }

雪花算法中非常好用的数字ID生成器