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
08302269
Commit
08302269
authored
May 15, 2018
by
xiaoguang.xu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: 少提交了两个类
parent
e8f5fbb8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
173 additions
and
0 deletions
+173
-0
BitsAllocator.java
...main/java/cn/quantgroup/tech/generator/BitsAllocator.java
+129
-0
IDGenerateException.java
...ava/cn/quantgroup/tech/generator/IDGenerateException.java
+44
-0
No files found.
idgenerator-spring-boot-starter/src/main/java/cn/quantgroup/tech/generator/BitsAllocator.java
0 → 100644
View file @
08302269
package
cn
.
quantgroup
.
tech
.
generator
;
import
org.apache.commons.lang3.builder.ToStringBuilder
;
import
org.apache.commons.lang3.builder.ToStringStyle
;
import
org.springframework.util.Assert
;
import
java.math.BigInteger
;
/**
* Allocate 64 bits for the UID(long)<br>
* sign (fixed 1bit) -> deltaSecond -> workerId -> sequence(within the same second)
*/
public
class
BitsAllocator
{
/**
* Total 64 bits
* dataCenterIdBits + workerIdBits + sequenceBits
*/
public
static
final
int
TOTAL_BITS
=
1
<<
6
;
private
final
int
timestampBits
;
private
final
int
dataCenterIdBits
;
private
final
int
workerIdBits
;
private
final
int
sequenceBits
;
/**
* Max value for dataCenterId & workerId & sequence
*/
private
final
long
maxDeltaSeconds
;
private
final
long
maxDataCenterId
;
private
final
long
maxWorkerId
;
private
final
long
maxSequence
;
/**
* Shift for dataCenterId & workerId & sequence
*/
private
final
int
timestampShift
;
private
final
int
dataCenterIdShift
;
private
final
int
workerIdShift
;
/**
* Constructor with timestampBits, workerIdBits, sequenceBits<br>
* The highest bit used for sign, so <code>63</code> bits for timestampBits, workerIdBits, sequenceBits
*/
public
BitsAllocator
(
int
timestampBits
,
int
dataCenterIdBits
,
int
workerIdBits
,
int
sequenceBits
)
{
// make sure allocated 64 bits
int
allocateTotalBits
=
dataCenterIdBits
+
workerIdBits
+
sequenceBits
;
Assert
.
isTrue
(
allocateTotalBits
+
1
<
TOTAL_BITS
,
"allocate greater than 64 bits"
);
// initialize bits
this
.
timestampBits
=
timestampBits
;
this
.
dataCenterIdBits
=
dataCenterIdBits
;
this
.
workerIdBits
=
workerIdBits
;
this
.
sequenceBits
=
sequenceBits
;
// initialize max value
this
.
maxDeltaSeconds
=
~(-
1L
<<
timestampBits
);
this
.
maxDataCenterId
=
~(-
1L
<<
dataCenterIdBits
);
this
.
maxWorkerId
=
~(-
1L
<<
workerIdBits
);
this
.
maxSequence
=
~(-
1L
<<
sequenceBits
);
// initialize shift
this
.
timestampShift
=
dataCenterIdBits
+
workerIdBits
+
sequenceBits
;
this
.
dataCenterIdShift
=
workerIdBits
+
sequenceBits
;
this
.
workerIdShift
=
sequenceBits
;
}
/**
* Allocate bits for UID according to delta seconds & workerId & sequence<br>
*
* @param deltaSeconds
* @param workerId
* @param sequence
* @return
*/
public
long
allocate
(
long
deltaSeconds
,
long
dataCenterId
,
long
workerId
,
long
sequence
)
{
return
(
deltaSeconds
<<
timestampShift
)
|
(
dataCenterId
<<
dataCenterIdShift
)
|
(
workerId
<<
workerIdShift
)
|
sequence
;
}
public
BigInteger
allocateBigInteger
(
long
deltaSeconds
,
long
dataCenterId
,
long
workerId
,
long
sequence
)
{
return
BigInteger
.
ZERO
.
or
(
BigInteger
.
valueOf
(
deltaSeconds
).
shiftLeft
(
timestampShift
))
.
or
(
BigInteger
.
valueOf
(
dataCenterId
).
shiftLeft
(
dataCenterIdShift
))
.
or
(
BigInteger
.
valueOf
(
workerId
).
shiftLeft
(
workerIdShift
))
.
or
(
BigInteger
.
valueOf
(
sequence
));
}
public
int
getTimestampBits
()
{
return
timestampBits
;
}
public
int
getDataCenterIdBits
()
{
return
dataCenterIdBits
;
}
public
int
getWorkerIdBits
()
{
return
workerIdBits
;
}
public
int
getSequenceBits
()
{
return
sequenceBits
;
}
public
long
getMaxDeltaSeconds
()
{
return
maxDeltaSeconds
;
}
public
long
getMaxDataCenterId
()
{
return
maxDataCenterId
;
}
public
long
getMaxWorkerId
()
{
return
maxWorkerId
;
}
public
long
getMaxSequence
()
{
return
maxSequence
;
}
public
int
getTimestampShift
()
{
return
timestampShift
;
}
public
int
getWorkerIdShift
()
{
return
workerIdShift
;
}
@Override
public
String
toString
()
{
return
ToStringBuilder
.
reflectionToString
(
this
,
ToStringStyle
.
SHORT_PREFIX_STYLE
);
}
}
\ No newline at end of file
idgenerator-spring-boot-starter/src/main/java/cn/quantgroup/tech/generator/IDGenerateException.java
0 → 100644
View file @
08302269
/*
* Copyright (c) 2017 Baidu, Inc. All Rights Reserve.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
cn
.
quantgroup
.
tech
.
generator
;
/**
* IDGenerateException
*/
public
class
IDGenerateException
extends
RuntimeException
{
/**
* Serial Version UID
*/
private
static
final
long
serialVersionUID
=
-
27048199131316992L
;
/**
* Default constructor
*/
public
IDGenerateException
()
{
super
();
}
/**
* Constructor with cause
*
* @param cause
*/
public
IDGenerateException
(
Throwable
cause
)
{
super
(
cause
);
}
}
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