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.

IdGenerator.c 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * 版权属于:yitter(yitter@126.com)
  3. * 开源地址:https://github.com/yitter/idgenerator
  4. */
  5. #include <stdio.h>
  6. #include <malloc.h>
  7. #include <pthread.h>
  8. #include <errno.h>
  9. #include <unistd.h>
  10. #include "IdGenerator.h"
  11. static inline uint64_t WorkerM1Id()
  12. {
  13. return WorkerM1NextId(_idGenerator->Worker);
  14. }
  15. static inline uint64_t WorkerM2Id()
  16. {
  17. return WorkerM2NextId(_idGenerator->Worker);
  18. }
  19. extern IdGenerator *GetIdGenInstance()
  20. {
  21. if (_idGenerator != NULL)
  22. return _idGenerator;
  23. else
  24. {
  25. _idGenerator = (IdGenerator *)malloc(sizeof(IdGenerator));
  26. _idGenerator->Worker = NewSnowFlakeWorker();
  27. return _idGenerator;
  28. }
  29. }
  30. extern void SetOptions(IdGeneratorOptions options)
  31. {
  32. if (GetIdGenInstance() == NULL)
  33. {
  34. exit(1);
  35. }
  36. // 1.BaseTime
  37. if (options.BaseTime == 0)
  38. {
  39. _idGenerator->Worker->BaseTime = 1582136402000;
  40. }
  41. else if (options.BaseTime < 631123200000 || options.BaseTime > GetCurrentTime())
  42. {
  43. perror("BaseTime error.");
  44. exit(1);
  45. }
  46. else
  47. {
  48. _idGenerator->Worker->BaseTime = options.BaseTime;
  49. }
  50. // 2.WorkerIdBitLength
  51. if (options.WorkerIdBitLength <= 0)
  52. {
  53. perror("WorkerIdBitLength error.(range:[1, 21])");
  54. exit(1);
  55. }
  56. if (options.SeqBitLength + options.WorkerIdBitLength > 22)
  57. {
  58. perror("error:WorkerIdBitLength + SeqBitLength <= 22");
  59. exit(1);
  60. }
  61. else
  62. {
  63. // _idGenerator->Worker->WorkerIdBitLength = options.WorkerIdBitLength;
  64. _idGenerator->Worker->WorkerIdBitLength = options.WorkerIdBitLength <= 0 ? 6 : options.WorkerIdBitLength;
  65. }
  66. // 3.WorkerId
  67. uint32_t maxWorkerIdNumber = (1 << options.WorkerIdBitLength) - 1;
  68. if (maxWorkerIdNumber == 0)
  69. {
  70. maxWorkerIdNumber = 63;
  71. }
  72. if (options.WorkerId < 0 || options.WorkerId > maxWorkerIdNumber)
  73. {
  74. perror("WorkerId error. (range:[0, {2^options.WorkerIdBitLength-1]}");
  75. exit(1);
  76. }
  77. else
  78. {
  79. _idGenerator->Worker->WorkerId = options.WorkerId;
  80. }
  81. // 4.SeqBitLength
  82. if (options.SeqBitLength < 2 || options.SeqBitLength > 21)
  83. {
  84. perror("SeqBitLength error. (range:[2, 21])");
  85. exit(1);
  86. }
  87. else
  88. {
  89. // _idGenerator->Worker->SeqBitLength = options.SeqBitLength;
  90. _idGenerator->Worker->SeqBitLength = options.SeqBitLength <= 0 ? 6 : options.SeqBitLength;
  91. }
  92. // 5.MaxSeqNumber
  93. uint32_t maxSeqNumber = (1 << options.SeqBitLength) - 1;
  94. if (maxSeqNumber == 0)
  95. {
  96. maxSeqNumber = 63;
  97. }
  98. if (options.MaxSeqNumber > maxSeqNumber)
  99. {
  100. perror("MaxSeqNumber error. (range:[1, {2^options.SeqBitLength-1}]");
  101. exit(1);
  102. }
  103. else
  104. {
  105. _idGenerator->Worker->MaxSeqNumber = options.MaxSeqNumber <= 0 ? maxSeqNumber : options.MaxSeqNumber;
  106. }
  107. // 6.MinSeqNumber
  108. if (options.MinSeqNumber < 5 || options.MinSeqNumber > maxSeqNumber)
  109. {
  110. perror("MinSeqNumber error. (range:[5, {options.MinSeqNumber}]");
  111. exit(1);
  112. }
  113. else
  114. {
  115. _idGenerator->Worker->MinSeqNumber = options.MinSeqNumber <= 0 ? 5 : options.MinSeqNumber;
  116. }
  117. // 7.TopOverCostCount
  118. if (options.TopOverCostCount < 0 || options.TopOverCostCount > 10000)
  119. {
  120. perror("TopOverCostCount error. (range:[0, 10000]");
  121. exit(1);
  122. }
  123. else
  124. {
  125. //_idGenerator->Worker->TopOverCostCount = options.TopOverCostCount <= 0 ? 2000 : options.TopOverCostCount;
  126. _idGenerator->Worker->TopOverCostCount = options.TopOverCostCount;
  127. }
  128. // 8.Others
  129. _idGenerator->Worker->_TimestampShift =
  130. _idGenerator->Worker->WorkerIdBitLength + _idGenerator->Worker->SeqBitLength;
  131. _idGenerator->Worker->_CurrentSeqNumber = _idGenerator->Worker->MinSeqNumber;
  132. _idGenerator->Worker->Method = options.Method;
  133. if (options.Method == 2)
  134. {
  135. _idGenerator->NextId = WorkerM2Id;
  136. }
  137. else
  138. {
  139. _idGenerator->NextId = WorkerM1Id;
  140. usleep(500 * 1000); // 暂停500ms
  141. }
  142. }