码迷,mamicode.com
首页 > 编程语言 > 详细

雪花算法

时间:2019-07-06 19:29:16      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:演示   The   utils   ide   epo   唯一值   ima   info   har   

雪花算法

 

unit Snowflake;

 (*Delphi版雪花算法
 作者:不得闲 QQ:75492895
 用于生成Int64位的唯一值ID,WorkerID用于区分工作站,
 ID会随着时间增加位数,每毫秒可生成4096个ID
 
 用法:
 创建全局变量:snow: TSnowflake;
 创建对象:snow := TSnowflake.Create; // 不要忘了在退出时释放snow.Free;
 调用:
 snow.WorkerID:=100;
 mmo1.Lines.Add( FormatFloat(‘#0‘,snow.Generate));
 *)

interface

uses
  SysUtils, SyncObjs, DateUtils;

type
  TWorkerID = 0..1023;

  TSnowflake = 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;
{ TSnowflake }

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

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

function TSnowflake.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.

  演示效果:

技术图片

雪花算法

标签:演示   The   utils   ide   epo   唯一值   ima   info   har   

原文地址:https://www.cnblogs.com/hnxxcxg/p/11143681.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!