Delphi版雪花算法


本文整理自网络,侵删。

 
//作者:不得闲
//QQ: 75492895
unit DxSnowflake;

interface
uses System.SysUtils,System.SyncObjs,System.Generics.Collections,System.DateUtils;

type
  TWorkerID = 0..1023;
  TDxSnowflake = class
  private
    FWorkerID: TWorkerID;
    FLocker: TCriticalSection;
    fTime: Int64;
    fstep: int64;
  public
    constructor Create;
    destructor Destroy;override;
    property WorkerID: TWorkerID read FWorkerID write FWorkerID;
    function Generate: Int64;
  end;

implementation

const
  Epoch: int64 = 1539615188000; //北京时间2018-10-15号
  //工作站的节点位数
  WorkerNodeBits:Byte = 10;
  //序列号的节点数
StepBits: Byte = 12;
  timeShift: Byte = 22;
nodeShift: Byte = 12;
var
WorkerNodeMax: int64;
nodeMask:int64;

stepMask:int64;

procedure InitNodeInfo;
begin
WorkerNodeMax := -1 xor (-1 shl WorkerNodeBits);
nodeMask := WorkerNodeMax shl StepBits;
stepMask := -1 xor (-1 shl StepBits);
end;
{ TDxSnowflake }

constructor TDxSnowflake.Create;
begin
  FLocker := TCriticalSection.Create;
end;


destructor TDxSnowflake.Destroy;
begin
  FLocker.Free;
  inherited;
end;

function TDxSnowflake.Generate: Int64;
var
  curtime: Int64;
begin
  FLocker.Acquire;
  try
    curtime := DateTimeToUnix(Now) * 1000;
    if curtime = fTime then
    begin
      fstep := (fstep + 1) and stepMask;
      if fstep = 0 then
      begin
        while curtime <= fTime do
          curtime := DateTimeToUnix(Now) * 1000;
      end;
    end
    else fstep := 0;
    fTime := curtime;
    Result := (curtime - Epoch) shl timeShift or FWorkerID shl nodeShift  or fstep;
  finally
    FLocker.Release;
  end;
end;

initialization
  InitNodeInfo;
end.

相关阅读 >>

Delphi 10 seattle中的画布上绘制字符

Delphi与汇编杂谈

Delphi idhttp调用 rest

Delphi edit呈横线效果

Delphi console 清屏代码

Delphi 实现php的urlencode编码效果

Delphi 获得当前进程所使用的内存

Delphi monthcalendar1 获取选中日期

Delphi 如何判断字符串是否是有效email地址

Delphi 二进制转换为文本

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...