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 4.2 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
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
3 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
3 years ago
4 years ago
3 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * 版权属于:yitter(yitter@126.com)
  3. * 开源地址:https://github.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. // 1.BaseTime
  33. if (options.BaseTime < DateTime.Now.AddYears(-50) || options.BaseTime > DateTime.Now)
  34. {
  35. throw new ApplicationException("BaseTime error.");
  36. }
  37. // 2.WorkerIdBitLength
  38. if (options.WorkerIdBitLength <= 0)
  39. {
  40. throw new ApplicationException("WorkerIdBitLength error.(range:[1, 21])");
  41. }
  42. if (options.DataCenterIdBitLength + options.WorkerIdBitLength + options.SeqBitLength > 22)
  43. {
  44. throw new ApplicationException("error:DataCenterIdBitLength + WorkerIdBitLength + SeqBitLength <= 22");
  45. }
  46. // 3.WorkerId & DataCenterId
  47. var maxWorkerIdNumber = (1 << options.WorkerIdBitLength) - 1;
  48. if (maxWorkerIdNumber == 0)
  49. {
  50. maxWorkerIdNumber = 63;
  51. }
  52. if (options.WorkerId < 0 || options.WorkerId > maxWorkerIdNumber)
  53. {
  54. throw new ApplicationException("WorkerId error. (range:[0, " + maxWorkerIdNumber + "]");
  55. }
  56. var maxDataCenterIdNumber = (1 << options.DataCenterIdBitLength) - 1;
  57. if (options.DataCenterId < 0 || options.DataCenterId > maxDataCenterIdNumber)
  58. {
  59. throw new ApplicationException("DataCenterId error. (range:[0, " + maxDataCenterIdNumber + "]");
  60. }
  61. // 4.SeqBitLength
  62. if (options.SeqBitLength < 2 || options.SeqBitLength > 21)
  63. {
  64. throw new ApplicationException("SeqBitLength error. (range:[2, 21])");
  65. }
  66. // 5.MaxSeqNumber
  67. var maxSeqNumber = (1 << options.SeqBitLength) - 1;
  68. if (maxSeqNumber == 0)
  69. {
  70. maxSeqNumber = 63;
  71. }
  72. if (options.MaxSeqNumber < 0 || options.MaxSeqNumber > maxSeqNumber)
  73. {
  74. throw new ApplicationException("MaxSeqNumber error. (range:[1, " + maxSeqNumber + "]");
  75. }
  76. // 6.MinSeqNumber
  77. if (options.MinSeqNumber < 5 || options.MinSeqNumber > maxSeqNumber)
  78. {
  79. throw new ApplicationException("MinSeqNumber error. (range:[5, " + maxSeqNumber + "]");
  80. }
  81. switch (options.Method)
  82. {
  83. case 2:
  84. _SnowWorker = new SnowWorkerM2(options);
  85. break;
  86. default:
  87. if (options.DataCenterIdBitLength == 0 && options.TimestampType == 0)
  88. {
  89. _SnowWorker = new SnowWorkerM1(options);
  90. }
  91. else
  92. {
  93. _SnowWorker = new SnowWorkerM3(options);
  94. }
  95. break;
  96. }
  97. if (options.Method != 2)
  98. {
  99. Thread.Sleep(500);
  100. }
  101. }
  102. public long NewLong()
  103. {
  104. return _SnowWorker.NextId();
  105. }
  106. }
  107. }