Skip to content

Commit

Permalink
Merge pull request #7 from dekuan/dev
Browse files Browse the repository at this point in the history
Upgrade ALGORITHM, change data center from 6 bits to 5 bits, data node from 6 bits to 5 bits.
  • Loading branch information
dekuan committed Sep 19, 2017
2 parents 44fa2b6 + 6a670be commit 7c4d787
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 81 deletions.
163 changes: 163 additions & 0 deletions README.chs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# dekuan/dedid
一种为分布式数据库而设计的全局唯一 ID(主键)生成器。
本算法的实现参考了 Twitter Snowflake,但是在最后的 12 位您不仅仅可以使用随机数字,也可以通过指定字符串来获取哈希值。


* [Documentation in English](README.md)



# 算法

### 结构体概况
本算法使用一个 64 位的 int 值作为 ID 的载体。

~~~
0 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx x xxxxxx xxxxxx xx xxxxxxxx
~~~

### 结构体说明

位置 | 长度 | 用途 | 备注
----------|----------|----------|----------
0 | 1 | 保留位 | 一直都是 0
1~41 | 41 | 以毫秒为单位的流淌时间 |0~69 年,即本 ID 算法最多可以使用 69 年
42~46 | 5 | 数据中心编号 | 编号范围 0~31,最多可容纳 32 个数据中心
47~51 | 5 | 数据中心下属数据节点编号 | 编号范围 0~31,每个数据中心最多可容纳 32 个数据节点。所以整个系统最多可容纳 1024 个数据节点
52~63 | 12 | 随机数/哈希值 | 取值范围 0~4095





# 位排列

### 数据中心编号
~~~
0 00000000 00000000 00000000 00000000 00000000 0 11111 00000 0000 00000000
00000000 00000000 00000000 00000000 00000000 00111110 00000000 00000000
00 00 00 00 00 3E 00 00
~~~

### 数据节点编号
~~~
0 00000000 00000000 00000000 00000000 00000000 0 00000 11111 0000 00000000
00000000 00000000 00000000 00000000 00000000 00000001 11110000 00000000
00 00 00 00 00 01 F0 00
~~~


### 流淌时间
~~~
0 11111111 11111111 11111111 11111111 11111111 1 00000 00000 0000 00000000
01111111 11111111 11111111 11111111 11111111 11000000 00000000 00000000
7F FF FF FF FF C0 00 00
~~~


### 随机数/哈希值
~~~
0 00000000 00000000 00000000 00000000 00000000 0 00000 00000 1111 11111111
00000000 00000000 00000000 00000000 00000000 00000000 00001111 11111111
00 00 00 00 00 00 0F FF
~~~


# 使用方法

### 创建一个普通的随机数 ID

~~~
$cDId = CDId::getInstance();
$nCenter = 0;
$nNode = 1;
$arrD = [];
$nNewId = $cDId->createId( $nCenter, $nNode, null, $arrD );
echo "new id = " . $nNewId . "\r\n";
print_r( $arrD );
~~~

##### 输出

~~~
new id = 114654484990270790
Array
(
[center] => 0
[node] => 1
[time] => 27335759399
[rand] => 3398
)
~~~


### 指定字符串,创建一个带有哈希值的 ID

~~~
$cDId = CDId::getInstance();
$nCenter = 0;
$nNode = 15;
$sSrc = "dekuan";
$arrD = [];
$nNewId = $cDId->createId( $nCenter, $nNode, $sSrc, $arrD );
echo "new id = " . $nNewId . "\r\n";
print_r( $arrD );
~~~

##### 输出

~~~
new id = 114654631304370386
Array
(
[center] => 0
[node] => 1
[time] => 27335794283
[rand] => 2258
)
~~~




### 解析 ID 获取详细信息

~~~
$cDId = CDId::getInstance();
$arrId = $cDId->parseId( 114654631304370386 );
print_r( $arrId );
~~~

##### 输出

~~~
Array
(
[center] => 0
[node] => 1
[time] => 27335794283
[rand] => 2258
)
~~~


# 如何安装
~~~
# composer require dekuan/dedid
~~~
了解更多信息,敬请访问 [https://packagist.org/packages/dekuan/dedid](https://packagist.org/packages/dekuan/dedid)
78 changes: 42 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,54 +1,60 @@
# dekuan/dedid
An unique id generator for primary key of distributed database.
This implementation of the algorithm reference Twitter Snowflake,
but in the last 12 bits you can not only use random numbers, but also get a hash value by a specified string.




* [简体中文版文档](README.chs.md)


# ALGORITHM

### Bit structure
It's a 64 bits bigint.

~~~
0 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx x xxxxxx xxxxxx xx xxxxxxxx
0 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx x xxxxx xxxxx xxxx xxxxxxxx
~~~

### Details

Bits | Usage | Remark
----------|---------|--------
0 | Reserved| Always be 0
41 | Escaped Time (in millisecond)|0~69 years
6 | Data center|0~63
6 | Data node in the data center |0~63
10 | Random|0~1023
Position | Length | Usage | Remark
----------|----------|----------|----------
0 | 1 | Reserved | Always be 0
1~41 | 41 | Escaped Time (in millisecond) | 0~69 years
42~46 | 5 | Number of data center | 0~31
47~51 | 5 | Number of data node in the data center | 0~31
52~63 | 12 | Random / Hash | 0~4095




# Mark bits
# Bit marks

### Center
~~~
0 00000000 00000000 00000000 00000000 00000000 0 111111 000000 00 00000000
0 00000000 00000000 00000000 00000000 00000000 0 11111 00000 0000 00000000
00000000 00000000 00000000 00000000 00000000 00111111 00000000 00000000
00000000 00000000 00000000 00000000 00000000 00111110 00000000 00000000
00 00 00 00 00 3F 00 00
00 00 00 00 00 3E 00 00
~~~

### Node
~~~
0 00000000 00000000 00000000 00000000 00000000 0 000000 111111 00 00000000
0 00000000 00000000 00000000 00000000 00000000 0 00000 11111 0000 00000000
00000000 00000000 00000000 00000000 00000000 00000000 11111100 00000000
00000000 00000000 00000000 00000000 00000000 00000001 11110000 00000000
00 00 00 00 00 00 FC 00
00 00 00 00 00 01 F0 00
~~~


### Escaped Time
~~~
0 11111111 11111111 11111111 11111111 11111111 1 000000 000000 00 00000000
0 11111111 11111111 11111111 11111111 11111111 1 00000 00000 0000 00000000
01111111 11111111 11111111 11111111 11111111 11000000 00000000 00000000
Expand All @@ -58,11 +64,11 @@ Bits | Usage | Remark

### Random or Hash value
~~~
0 00000000 00000000 00000000 00000000 00000000 0 000000 000000 11 11111111
0 00000000 00000000 00000000 00000000 00000000 0 00000 00000 1111 11111111
00000000 00000000 00000000 00000000 00000000 00000000 00000011 11111111
00000000 00000000 00000000 00000000 00000000 00000000 00001111 11111111
00 00 00 00 00 00 03 FF
00 00 00 00 00 00 0F FF
~~~


Expand All @@ -72,8 +78,8 @@ Bits | Usage | Remark

~~~
$cDId = CDId::getInstance();
$nCenter = 61;
$nNode = 37;
$nCenter = 0;
$nNode = 1;
$arrD = [];
$nNewId = $cDId->createId( $nCenter, $nNode, null, $arrD );
Expand All @@ -86,18 +92,18 @@ print_r( $arrD );
##### output

~~~
new id = 98037672957548006
new id = 114654484990270790
Array
(
[center] => 61
[node] => 37
[time] => 23374002684
[rand] => 486
[center] => 0
[node] => 1
[time] => 27335759399
[rand] => 3398
)
~~~


### Create an new id with crc32 hash value
### Create an new id with crc32 hash value by a specified string

~~~
$cDId = CDId::getInstance();
Expand All @@ -116,13 +122,13 @@ print_r( $arrD );
##### output

~~~
new id = 99087647783270610
new id = 114654631304370386
Array
(
[center] => 0
[node] => 15
[time] => 23624336191
[rand] => 210
[node] => 1
[time] => 27335794283
[rand] => 2258
)
~~~

Expand All @@ -133,7 +139,7 @@ Array

~~~
$cDId = CDId::getInstance();
$arrId = $cDId->parseId( 98037672957548006 );
$arrId = $cDId->parseId( 114654631304370386 );
print_r( $arrId );
~~~
Expand All @@ -143,10 +149,10 @@ print_r( $arrId );
~~~
Array
(
[center] => 61
[node] => 37
[time] => 23374002684
[rand] => 486
[center] => 0
[node] => 1
[time] => 27335794283
[rand] => 2258
)
~~~

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
},
"require":
{
"php" : ">=5.3.0"
"php" : ">=7.0.0"
},
"require-dev" :
{
"phpunit/phpunit" : "~4.0"
"phpunit/phpunit": "~6.0"
},
"autoload":
{
Expand Down
Loading

0 comments on commit 7c4d787

Please sign in to comment.