Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
commons
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
3
Merge Requests
3
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DevOps
commons
Commits
e745ae77
Commit
e745ae77
authored
Mar 06, 2018
by
zhiguo.liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 完善 parseUid 方法,支持解释 UID
parent
31cafb05
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
22 deletions
+34
-22
DefaultUidGenerator.java
.../java/cn/quantgroup/tech/util/id/DefaultUidGenerator.java
+33
-21
UidGenerator.java
...rc/main/java/cn/quantgroup/tech/util/id/UidGenerator.java
+1
-1
No files found.
commons-spring/src/main/java/cn/quantgroup/tech/util/id/DefaultUidGenerator.java
View file @
e745ae77
...
...
@@ -11,6 +11,7 @@ import org.springframework.beans.factory.annotation.Value;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.Assert
;
import
java.math.BigInteger
;
import
java.text.ParseException
;
import
java.util.Date
;
import
java.util.concurrent.TimeUnit
;
...
...
@@ -25,7 +26,6 @@ import java.util.concurrent.TimeUnit;
* <li>worker id: The next 22 bits, represents the worker's id which assigns based on database, max id is about 420W
* <li>sequence: The next 13 bits, represents a sequence within the same second, max for 8192/s<br><br>
* <p>
* The {@link DefaultUidGenerator#parseUID(long)} is a tool method to parse the bits
* <p>
* <pre>{@code
* +------+----------------------+----------------+-----------+
...
...
@@ -58,7 +58,7 @@ public class DefaultUidGenerator implements UidGenerator, InitializingBean {
protected
int
dataCenterIdBits
;
@Value
(
"${uid.workerBits:13}"
)
protected
int
workerBits
;
@Value
(
"${uid.seqBits:
2
3}"
)
@Value
(
"${uid.seqBits:
1
3}"
)
protected
int
seqBits
;
/**
...
...
@@ -90,12 +90,13 @@ public class DefaultUidGenerator implements UidGenerator, InitializingBean {
public
void
afterPropertiesSet
()
throws
Exception
{
// initialize bits allocator
bitsAllocator
=
new
BitsAllocator
(
timeBits
,
dataCenterIdBits
,
workerBits
,
seqBits
);
workerIdAssigner
=
new
DisposableWorkerIdAssigner
();
// initialize worker id
workerId
=
workerIdAssigner
.
assignWorkerId
(
dataCenterId
,
bitsAllocator
);
Assert
.
isTrue
(
workerId
<
bitsAllocator
.
getMaxWorkerId
(),
"workerId is too big"
);
Assert
.
isTrue
(
work
erId
<
bitsAllocator
.
getMaxDataCenterId
(),
"dataCenterId is too big"
);
Assert
.
isTrue
(
dataCent
erId
<
bitsAllocator
.
getMaxDataCenterId
(),
"dataCenterId is too big"
);
LOGGER
.
info
(
"Initialized bits dataCenterBits:{}, workerBits:{}, seqBits:{}"
,
dataCenterIdBits
,
workerBits
,
seqBits
);
LOGGER
.
info
(
"Initialized nodes, workerId:{}, dataCenterId:{}"
,
workerId
,
dataCenterId
);
}
@Override
...
...
@@ -110,28 +111,40 @@ public class DefaultUidGenerator implements UidGenerator, InitializingBean {
// TODO: 2018/3/5 反序列化 uid
@Override
public
String
parseUID
(
long
uid
)
{
int
totalBits
=
Long
.
toBinaryString
(
uid
).
length
();
long
timestampBits
=
bitsAllocator
.
getTimestampBits
();
public
String
parseUID
(
String
uidStr
)
{
BigInteger
bigInteger
=
new
BigInteger
(
uidStr
);
int
totalBits
=
bigInteger
.
bitLength
();
long
dataCenterIdBits
=
bitsAllocator
.
getDataCenterIdBits
();
long
workerIdBits
=
bitsAllocator
.
getWorkerIdBits
();
long
sequenceBits
=
bitsAllocator
.
getSequenceBits
();
if
(
totalBits
<
64
)
{
totalBits
=
64
;
long
uid
=
bigInteger
.
longValue
();
long
sequence
=
(
uid
<<
(
totalBits
-
sequenceBits
))
>>>
(
totalBits
-
sequenceBits
);
long
workerId
=
(
uid
<<
(
totalBits
-
workerIdBits
-
sequenceBits
))
>>>
(
totalBits
-
workerIdBits
);
long
dataCenterId
=
(
uid
<<
(
totalBits
-
dataCenterIdBits
-
workerIdBits
-
sequenceBits
))
>>>
(
totalBits
-
dataCenterIdBits
);
long
deltaSeconds
=
uid
>>>
(
dataCenterIdBits
+
workerIdBits
+
sequenceBits
);
Date
thatTime
=
new
Date
(
TimeUnit
.
SECONDS
.
toMillis
(
epochSeconds
+
deltaSeconds
));
String
thatTimeStr
=
DateFormatUtils
.
format
(
thatTime
,
DATETIME_PATTERN
);
return
String
.
format
(
"{\"UID\":\"%d\",\"timestamp\":\"%s\",\"dataCenterId\":\"%d\",\"workerId\":\"%d\",\"sequence\":\"%d\"}"
,
uid
,
thatTimeStr
,
dataCenterId
,
workerId
,
sequence
);
}
else
{
BigInteger
workerBig
=
getBigIntegerFromLength
(
workerIdBits
).
shiftLeft
((
int
)
sequenceBits
).
and
(
bigInteger
);
System
.
out
.
println
(
workerBig
);
long
sequence
=
getBigIntegerFromLength
(
sequenceBits
).
and
(
bigInteger
).
longValue
();
long
workerId
=
getBigIntegerFromLength
(
workerIdBits
).
and
(
bigInteger
.
shiftRight
((
int
)
sequenceBits
)).
longValue
();
long
dataCenterId
=
getBigIntegerFromLength
(
dataCenterIdBits
).
and
(
bigInteger
.
shiftRight
((
int
)
sequenceBits
+(
int
)
workerIdBits
)).
longValue
();
long
deltaSeconds
=
bigInteger
.
shiftRight
((
int
)
dataCenterIdBits
+
(
int
)
workerIdBits
+
(
int
)
sequenceBits
).
longValue
();
Date
thatTime
=
new
Date
(
TimeUnit
.
SECONDS
.
toMillis
(
epochSeconds
+
deltaSeconds
));
String
thatTimeStr
=
DateFormatUtils
.
format
(
thatTime
,
DATETIME_PATTERN
);
return
String
.
format
(
"{\"UID\":\"%d\",\"timestamp\":\"%s\",\"dataCenterId\":\"%d\",\"workerId\":\"%d\",\"sequence\":\"%d\"}"
,
bigInteger
,
thatTimeStr
,
dataCenterId
,
workerId
,
sequence
);
}
// parse UID
long
sequence
=
(
uid
<<
(
totalBits
-
sequenceBits
))
>>>
(
totalBits
-
sequenceBits
);
long
workerId
=
(
uid
<<
(
timestampBits
+
1
))
>>>
(
totalBits
-
workerIdBits
);
long
deltaSeconds
=
uid
>>>
(
dataCenterIdBits
+
workerIdBits
+
sequenceBits
);
Date
thatTime
=
new
Date
(
TimeUnit
.
SECONDS
.
toMillis
(
epochSeconds
+
deltaSeconds
));
String
thatTimeStr
=
DateFormatUtils
.
format
(
thatTime
,
DATETIME_PATTERN
);
}
// format as string
return
String
.
format
(
"{\"UID\":\"%d\",\"timestamp\":\"%s\",\"workerId\":\"%d\",\"sequence\":\"%d\"}"
,
uid
,
thatTimeStr
,
workerId
,
sequence
);
private
BigInteger
getBigIntegerFromLength
(
long
n
)
{
return
BigInteger
.
valueOf
(-
1
).
shiftLeft
((
int
)
n
).
not
();
}
/**
...
...
@@ -188,7 +201,6 @@ public class DefaultUidGenerator implements UidGenerator, InitializingBean {
return
TimeUnit
.
MILLISECONDS
.
toSeconds
(
System
.
currentTimeMillis
());
}
public
void
setTimeBits
(
int
timeBits
)
{
if
(
timeBits
>
0
)
{
this
.
timeBits
=
timeBits
;
...
...
commons-spring/src/main/java/cn/quantgroup/tech/util/id/UidGenerator.java
View file @
e745ae77
...
...
@@ -22,6 +22,6 @@ public interface UidGenerator {
* @param uid
* @return Parsed info
*/
String
parseUID
(
lo
ng
uid
);
String
parseUID
(
Stri
ng
uid
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment