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.

uSnowWorkerM3.pas 2.5 kB

2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. unit uSnowWorkerM3;
  2. interface
  3. uses System.SysUtils, System.DateUtils, uSnowWorkerM1, uIdGeneratorOptions, System.SyncObjs;
  4. /// <summary>
  5. /// ѩƯ㷨֧ID뼶ʱ
  6. /// </summary>
  7. type
  8. TSnowWorkerM3 = class(TSnowWorkerM1)
  9. protected
  10. FDataCenterId: Cardinal;
  11. FDataCenterIdBitLength: Byte;
  12. FTimestampType: Byte;
  13. protected
  14. /// <summary>
  15. /// IDĬ0
  16. /// </summary>
  17. property DataCenterId: Cardinal read FDataCenterId default 0;
  18. /// <summary>
  19. /// IDȣĬ0
  20. /// </summary>
  21. property DataCenterIdBitLength: Byte read FDataCenterIdBitLength default 0;
  22. /// <summary>
  23. /// ʱͣ0-룬1-룩Ĭ0
  24. /// </summary>
  25. property TimestampType: Byte read FTimestampType default 0;
  26. protected
  27. function CalcId(UseTimeTick: Int64): Int64; override;
  28. function CalcTurnBackId(UseTimeTick: Int64): Int64; override;
  29. function GetCurrentTimeTick(): Int64; override;
  30. public
  31. constructor Create(options: TIdGeneratorOptions); overload;
  32. end;
  33. implementation
  34. { TSnowWorkerM3 }
  35. constructor TSnowWorkerM3.Create(options: TIdGeneratorOptions);
  36. begin
  37. // 뼶ʱ
  38. FTimestampType := options.TimestampType;
  39. // DataCenter
  40. FDataCenterId := options.DataCenterId;
  41. FDataCenterIdBitLength := options.DataCenterIdBitLength;
  42. if (FTimestampType = 1) then
  43. begin
  44. FTopOverCostCount := 0;
  45. end;
  46. FTimestampShift := Byte(DataCenterIdBitLength + WorkerIdBitLength + SeqBitLength);
  47. inherited Create(options);
  48. end;
  49. function TSnowWorkerM3.CalcId(UseTimeTick: Int64): Int64;
  50. begin
  51. Result := ((UseTimeTick shl FTimestampShift) + (Int64(DataCenterId) shl DataCenterIdBitLength) +
  52. (Int64(WorkerId) shl SeqBitLength) + Int64(FCurrentSeqNumber));
  53. Inc(FCurrentSeqNumber);
  54. end;
  55. function TSnowWorkerM3.CalcTurnBackId(UseTimeTick: Int64): Int64;
  56. begin
  57. Result := ((UseTimeTick shl FTimestampShift) + (Int64(DataCenterId) shl DataCenterIdBitLength) +
  58. (Int64(WorkerId) shl SeqBitLength) + FTurnBackIndex);
  59. Dec(FTurnBackTimeTick);
  60. end;
  61. function TSnowWorkerM3.GetCurrentTimeTick: Int64;
  62. var
  63. Millis: Int64;
  64. begin
  65. if (TimestampType = 0) then
  66. begin
  67. Millis := GetMillisecondTimeStamp();
  68. Result := Millis - FBaseTime;
  69. end
  70. else
  71. begin
  72. Millis := GetSecondTimeStamp();
  73. Result := Millis - FBaseTime;
  74. end;
  75. end;
  76. end.