ts 生成雪花id

class Snowflake {
  private twepoch;
  private workerIdBits;
  private dataCenterIdBits;
  private maxWrokerId;
  private maxDataCenterId;
  private sequenceBits;
  private workerIdShift;
  private dataCenterIdShift;
  private timestampLeftShift;
  private sequenceMask;
  private lastTimestamp;
  private workerId;
  private dataCenterId;
  private sequence;
  constructor(_workerId: any, _dataCenterId: any, _sequence: any) {
    this.twepoch = 1288834974657n;
    this.workerIdBits = 5n;
    this.dataCenterIdBits = 5n;
    this.maxWrokerId = -1n ^ (-1n << this.workerIdBits); // 值为:31
    this.maxDataCenterId = -1n ^ (-1n << this.dataCenterIdBits); // 值为:31
    this.sequenceBits = 12n;
    this.workerIdShift = this.sequenceBits; // 值为:12
    this.dataCenterIdShift = this.sequenceBits + this.workerIdBits; // 值为:17
    this.timestampLeftShift =
      this.sequenceBits + this.workerIdBits + this.dataCenterIdBits; // 值为:22
    this.sequenceMask = -1n ^ (-1n << this.sequenceBits); // 值为:4095
    this.lastTimestamp = -1n;
    //设置默认值,从环境变量取
    this.workerId = 1n;
    this.dataCenterId = 1n;
    this.sequence = 0n;
    if (this.workerId > this.maxWrokerId || this.workerId < 0) {
      throw new Error(
        `_workerId must max than 0 and small than maxWrokerId-[${this.maxWrokerId}]`
      );
    }
    if (this.dataCenterId > this.maxDataCenterId || this.dataCenterId < 0) {
      throw new Error(
        `_dataCenterId must max than 0 and small than maxDataCenterId-[${this.maxDataCenterId}]`
      );
    }

    this.workerId = BigInt(_workerId);
    this.dataCenterId = BigInt(_dataCenterId);
    this.sequence = BigInt(_sequence);
  }

  tilNextMillis(lastTimestamp: any) {
    let timestamp = this.timeGen();
    while (timestamp <= lastTimestamp) {
      timestamp = this.timeGen();
    }
    return BigInt(timestamp);
  }

  timeGen() {
    return BigInt(Date.now());
  }

  nextId() {
    let timestamp = this.timeGen();
    if (timestamp < this.lastTimestamp) {
      throw new Error(
        `Clock moved backwards. Refusing to generate id for ${
          this.lastTimestamp - timestamp
        }`
      );
    }
    if (this.lastTimestamp === timestamp) {
      this.sequence = (this.sequence + 1n) & this.sequenceMask;
      if (this.sequence === 0n) {
        timestamp = this.tilNextMillis(this.lastTimestamp);
      }
    } else {
      this.sequence = 0n;
    }
    this.lastTimestamp = timestamp;
    return (
      ((timestamp - this.twepoch) << this.timestampLeftShift) |
      (this.dataCenterId << this.dataCenterIdShift) |
      (this.workerId << this.workerIdShift) |
      this.sequence
    );
  }
}
export { Snowflake };

import { Snowflake } from "../hooks/snowflake";
const snowflake = new Snowflake(1n, 1n, 0n);
const id = snowflake.nextId().toString();
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,以下是 TypeScript 实现的雪花 ID 生成器: ```typescript class SnowflakeGenerator { private workerIdBits = 5; private datacenterIdBits = 5; private sequenceBits = 12; private maxWorkerId = -1 ^ (-1 << this.workerIdBits); private maxDatacenterId = -1 ^ (-1 << this.datacenterIdBits); private sequenceMask = -1 ^ (-1 << this.sequenceBits); private workerIdShift = this.sequenceBits; private datacenterIdShift = this.sequenceBits + this.workerIdBits; private timestampLeftShift = this.sequenceBits + this.workerIdBits + this.datacenterIdBits; private lastTimestamp = -1; private sequence = 0; private workerId: number; private datacenterId: number; constructor(workerId: number, datacenterId: number) { if (workerId > this.maxWorkerId || workerId < 0) { throw new Error(`Worker ID must be between 0 and ${this.maxWorkerId}`); } if (datacenterId > this.maxDatacenterId || datacenterId < 0) { throw new Error(`Datacenter ID must be between 0 and ${this.maxDatacenterId}`); } this.workerId = workerId; this.datacenterId = datacenterId; } private timeGen(): number { return Date.now(); } private tilNextMillis(lastTimestamp: number): number { let timestamp = this.timeGen(); while (timestamp <= lastTimestamp) { timestamp = this.timeGen(); } return timestamp; } public nextId(): bigint { let timestamp = this.timeGen(); if (timestamp < this.lastTimestamp) { throw new Error(`Clock moved backwards. Refusing to generate id for ${this.lastTimestamp - timestamp} milliseconds`); } if (timestamp === this.lastTimestamp) { this.sequence = (this.sequence + 1) & this.sequenceMask; if (this.sequence === 0) { timestamp = this.tilNextMillis(this.lastTimestamp); } } else { this.sequence = 0; } this.lastTimestamp = timestamp; return ( BigInt(timestamp) << BigInt(this.timestampLeftShift) | BigInt(this.datacenterId) << BigInt(this.datacenterIdShift) | BigInt(this.workerId) << BigInt(this.workerIdShift) | BigInt(this.sequence) ); } } ``` 使用示例: ```typescript const generator = new SnowflakeGenerator(1, 1); console.log(generator.nextId()); // 输出类似 1076853241037248n 的大整数 ``` 这个实现是基于 Twitter 的雪花算法,使用了 TypeScript 语言。在构造函数中传入 workerId 和 datacenterId 两个参数,用于生成唯一的 ID。调用 `nextId()` 方法可以获得一个新的 ID
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值