Skip to content

Commit 1339fef

Browse files
committed
support for default database identifier
1 parent 1202d1d commit 1339fef

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/LaraFlake.php

+22-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
class LaraFlake
88
{
99

10+
const INITIAL_EPOCH = 1451625443000;
11+
12+
const DEFAULT_SHARD_ID = 1;
13+
1014
/**
1115
* Generate the 64bit unique ID.
1216
* @return number BIGINT
@@ -19,10 +23,14 @@ public static function generateID()
1923
*/
2024
$curr_timestamp = floor(microtime(true) * 1000);
2125

26+
if ( ! is_int(config('laraflake.initial_epoch', self::INITIAL_EPOCH)) ) {
27+
throw new \InvalidArgumentException('Initial epoch is invalid. Must be an integer');
28+
}
29+
2230
/**
2331
* Subtract custom epoch from current time
2432
*/
25-
$curr_timestamp -= config('laraflake.initial_epoch');
33+
$curr_timestamp -= config('laraflake.initial_epoch', self::INITIAL_EPOCH);
2634

2735
/**
2836
* Create a initial base for ID
@@ -33,6 +41,13 @@ public static function generateID()
3341
* Get ID of database server (10 bits)
3442
* Up to 512 machines
3543
*/
44+
45+
$node = self::getServerShardId();
46+
47+
if ( ! is_int($node) || $node < 0 || $node > 1023 ) {
48+
throw new \InvalidArgumentException('The Shard ID identifier must be a 10 bit integer between 0 and 1023.');
49+
}
50+
3651
$shard_id = decbin(pow(2,9) - 1 + self::getServerShardId());
3752

3853
/**
@@ -59,6 +74,11 @@ public static function generateID()
5974
*/
6075
private static function getServerShardId()
6176
{
77+
78+
if(config('laraflake.provider', 'local') !== 'database'){
79+
return config('laraflake.shard_id', self::DEFAULT_SHARD_ID);
80+
}
81+
6282
try {
6383
$database_name = DB::getName();
6484
}catch (\PDOException $e){
@@ -90,6 +110,6 @@ private static function getMySqlServerId()
90110
*/
91111
public static function getTimeFromID($id)
92112
{
93-
return bindec(substr(decbin($id),0,41)) - pow(2,40) + 1 + config('laraflake.initial_epoch');
113+
return bindec(substr(decbin($id),0,41)) - pow(2,40) + 1 + config('laraflake.initial_epoch', self::INITIAL_EPOCH);
94114
}
95115
}

src/config/laraflake.php

+23
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,27 @@
1414
*/
1515
'initial_epoch' => env('INITIAL_EPOCH', 1451625443000),
1616

17+
/*
18+
|--------------------------------------------------------------------------
19+
| Provider
20+
|--------------------------------------------------------------------------
21+
|
22+
| The provider informs which driver will be used to obtain the database
23+
| node identification.
24+
| Available: "local" and "database".
25+
|
26+
*/
27+
'provider' => env('LARAFLAKE_PROVIDER', 'local'),
28+
29+
/*
30+
|--------------------------------------------------------------------------
31+
| Shard ID
32+
|--------------------------------------------------------------------------
33+
|
34+
| The shard id is the identifier of the node used when the provider is
35+
| local.
36+
|
37+
*/
38+
'shard_id' => env('LARAFLAKE_SHARD_ID', 1),
39+
1740
];

0 commit comments

Comments
 (0)