Skip to content

Commit

Permalink
support for default database identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
hafael committed Dec 16, 2017
1 parent 1202d1d commit 1339fef
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/LaraFlake.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
class LaraFlake
{

const INITIAL_EPOCH = 1451625443000;

const DEFAULT_SHARD_ID = 1;

/**
* Generate the 64bit unique ID.
* @return number BIGINT
Expand All @@ -19,10 +23,14 @@ public static function generateID()
*/
$curr_timestamp = floor(microtime(true) * 1000);

if ( ! is_int(config('laraflake.initial_epoch', self::INITIAL_EPOCH)) ) {
throw new \InvalidArgumentException('Initial epoch is invalid. Must be an integer');
}

/**
* Subtract custom epoch from current time
*/
$curr_timestamp -= config('laraflake.initial_epoch');
$curr_timestamp -= config('laraflake.initial_epoch', self::INITIAL_EPOCH);

/**
* Create a initial base for ID
Expand All @@ -33,6 +41,13 @@ public static function generateID()
* Get ID of database server (10 bits)
* Up to 512 machines
*/

$node = self::getServerShardId();

if ( ! is_int($node) || $node < 0 || $node > 1023 ) {
throw new \InvalidArgumentException('The Shard ID identifier must be a 10 bit integer between 0 and 1023.');
}

$shard_id = decbin(pow(2,9) - 1 + self::getServerShardId());

/**
Expand All @@ -59,6 +74,11 @@ public static function generateID()
*/
private static function getServerShardId()
{

if(config('laraflake.provider', 'local') !== 'database'){
return config('laraflake.shard_id', self::DEFAULT_SHARD_ID);
}

try {
$database_name = DB::getName();
}catch (\PDOException $e){
Expand Down Expand Up @@ -90,6 +110,6 @@ private static function getMySqlServerId()
*/
public static function getTimeFromID($id)
{
return bindec(substr(decbin($id),0,41)) - pow(2,40) + 1 + config('laraflake.initial_epoch');
return bindec(substr(decbin($id),0,41)) - pow(2,40) + 1 + config('laraflake.initial_epoch', self::INITIAL_EPOCH);
}
}
23 changes: 23 additions & 0 deletions src/config/laraflake.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,27 @@
*/
'initial_epoch' => env('INITIAL_EPOCH', 1451625443000),

/*
|--------------------------------------------------------------------------
| Provider
|--------------------------------------------------------------------------
|
| The provider informs which driver will be used to obtain the database
| node identification.
| Available: "local" and "database".
|
*/
'provider' => env('LARAFLAKE_PROVIDER', 'local'),

/*
|--------------------------------------------------------------------------
| Shard ID
|--------------------------------------------------------------------------
|
| The shard id is the identifier of the node used when the provider is
| local.
|
*/
'shard_id' => env('LARAFLAKE_SHARD_ID', 1),

];

0 comments on commit 1339fef

Please sign in to comment.