Giter Club home page Giter Club logo

bits's Introduction

Bits

Bits is a PHP library for generating unique 64-bit identifiers for use in distributed computing. You can use Bits to create Twitter Snowflake IDs, Sonyflake IDs, or any other unique ID that uses bit sequences.

Installation

composer require glhd/bits

Usage

To get a new snowflake ID, simply call Snowflake::make(). This returns a new Snowflake object:

class Snowflake
{
    public readonly int $timestamp;
    public readonly int $datacenter_id;
    public readonly int $worker_id;
    public readonly int $sequence;
    
    public function id(): int;
    public function is(Snowflake $other): bool;
}

You can also use the snowflake() or sonyflake() global helper functions, if you prefer.

All Bits IDs implement __toString() and the Laravel Query\Expression interface so that you can easily pass them around without juggling types.

Usage with Eloquent Models

Bits provides a HasSnowflakes trait that behaves the same as Eloquent’s HasUuids and HasUlids traits. Simply add HasSnowflakes to your model, and whenever they're inserted or upserted, and new Snowflake will be generated for you.

You can also use Snowflake or Sonyflake as in your Eloquent $casts array to have that attribute automatically cast to a Bits instance.

use Glhd\Bits\Database\HasSnowflakes;
use Glhd\Bits\Snowflake;
use Illuminate\Database\Eloquent\Model;

class Example extends Model
{
    // Auto-generate Snowflake for new models
    use HasSnowflakes;
    
    // Any attribute can be cast to a `Snowflake` (or `Sonyflake`)
    protected $casts = [
        'id' => Snowflake::class,
    ];
}

$example = Example::create();

$example->id instanceof Snowflake; // true

echo $example->id; // 65898467809951744

About 64-bit Unique IDs

Snowflake format

0 0000001100100101110101100110111100101011 01011 01111 000000011101
┳ ━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━ ━━━┳━ ━┳━━━ ━┳━━━━━━━━━━
┗━ unused bit     ┗━ timestamp (41)           ┃   ┃     ┗━ sequence (12)
                              datacenter (5) ━┛   ┗━ worker (5)

Sonyflake format

0 000000011001001011101011001101111001010 11010110 1111000000011101
┳ ━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━━━┳━ ━┳━━━━━━━━━━━━━━
┗━ sign   ┗ timestamp (39)         sequence (8) ┛   ┗ machine (16)

Both of these IDs are represented by the same 64-bit integer, 56705782302306333, but convey different metadata. Depending on your scale and distribution needs, you may find one or the other format preferable, or choose to implement your own custom format.

Bits lets generate any kind of 64-bit unique ID you'd like, in the way that makes the most sense for your use-case.

bits's People

Contributors

actions-user avatar inxilpro 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.