雪花算法(snowflake)delphi版

雪花算法简单描述:
+ 最高位是符号位,始终为0,不可用。
+ 41位的时间序列,精确到毫秒级,41位的长度可以使用69年。时间位还有一个很重要的作用是可以根据时间进行排序。
+ 10位的机器标识,10位的长度最多支持部署1024个节点。
+ 12位的计数序列号,序列号即一系列的自增id,可以支持同一节点同一毫秒生成多个ID序号,12位的计数序列号支持每个节点每毫秒产生4096个ID序号。
看的出来,这个算法很简洁也很简单,但依旧是一个很好的ID生成策略。其中,10位器标识符一般是5位IDC+5位machine编号,唯一确定一台机器。
delphi版本是不得闲折腾的,QQ75492895,我这算是转载,源码如下:
(*
 Delphi版雪花算法
 作者:不得闲 QQ:75492895
 用于生成Int64位的唯一值ID,WorkerID用于区分工作站,
 ID会随着时间增加位数,每毫秒可生成4096个ID
 
 用法:
 创建全局变量:snow: TDxSnowflake;
 创建对象:snow := TDxSnowflake.Create; // 不要忘了在退出时释放snow.Free;
 调用:
 snow.WorkerID:=100;
 mmo1.Lines.Add( FormatFloat('#0',snow.Generate));
*)
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.

说明和注释,我写在最上面的注释里了,这个拿来生成业务流水号真的很方便。

WebPascal脚本模型教程 - 网页...

猜你喜欢

转载自www.cnblogs.com/westsoft/p/10206795.html