GithubHelp home page GithubHelp logo

sue602 / lua-snowflake Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stuartcarnie/lua-snowflake

0.0 1.0 0.0 180 KB

An implementation of Snowflake for Lua. Snowflake is an algorithm which supports ordered, distributed id generation

License: MIT License

Lua 2.82% C 97.18%

lua-snowflake's Introduction

lua-snowflake

An implementation of Snowflake for Lua. Snowflake is an algorithm which supports ordered, distributed id generation.

Installation

$ luarocks install snowflake

Usage

local sf = require "snowflake"
sf.init(0x01, 0x01)
local uuid = sf.next_id()

uuid will be a 64-bit number represented as a string that is structured as follows:

6  6         5         4         3         2         1         
3210987654321098765432109876543210987654321098765432109876543210

tttttttttttttttttttttttttttttttttttttttttdddddnnnnnsssssssssssss

where

  • s is a 12-bit integer that increments when next_id() is called multiple times for the same millisecond
  • n is a 5-bit integer representing the node within a given data center
  • d is a 5-bit integer representing a unique data center or group of servers
  • t is a 42-bit integer representing the current timestamp in milliseconds
    • the number of milliseconds to have elapsed since 1413817200000 or 2014-10-20T15:00:00.000Z

sf.init(datacenter_id, node_id) is used to initialize snowflake and set values for ddddd and nnnnn as follows:

  • datacenter_id is an integer n, where 0 ≤ n ≤ 0x1f and specifies the ddddd portion of the id
  • node_id is an integer n, where 0 ≤ n ≤ 0x1f and specifies the nnnnn portion of the id

lua-snowflake's People

Contributors

lowks avatar stuartcarnie avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.