package com.xuecheng.framework.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;
/**
* java edition of Twitter <b>Snowflake</b>, a network service for generating
* unique ID numbers at high scale with some simple guarantees.
*
* https://github.com/twitter/snowflake
*/
public class Snowflake {
private static Logger log = LoggerFactory.getLogger(Snowflake.class);
// private static Logger
/*
* bits allocations for timeStamp, datacenterId, workerId and sequence
*/
private final long unusedBits = 1L;
/**
* 'time stamp' here is defined as the number of millisecond that have
* elapsed since the {@link #epoch} given by users on {@link Snowflake}
* instance initialization
*/
private final long timestampBits = 41L;
private final long datacenterIdBits = 5L;
private final long workerIdBits = 5L;
private final long sequenceBits = 12L;
/*
* max values of timeStamp, workerId, datacenterId and sequence
*/
private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); // 2^5-1
private final long maxWorkerId = -1L ^ (-1L << workerIdBits); // 2^5-1
private final long maxSequence = -1L ^ (-1L << sequenceBits); // 2^12-1
/**
* left shift bits of timeStamp, workerId and datacenterId
*/
private final long timestampShift = sequenceBits + datacenterIdBits + workerIdBits;
private final long datacenterIdShift = sequenceBits + workerIdBits;
private final long workerIdShift = sequenceBits;
/*
* object status variables
*/
/**
* reference material of 'time stamp' is '2016-01-01'. its value can't be
* modified after initialization.
*/
private final long epoch = 1451606400000L;
/**
* data center number the process running on, its value can't be modified
* after initialization.
* <p>
* max: 2^5-1 range: [0,31]
*/
private final long datacenterId;
/**
* machine or process number, its value can't be modified after
* initialization.
* <p>
* max: 2^5-1 range: [0,31]
*
*/
private final long workerId;
/**
* the unique and incrementing sequence number scoped in only one
* period/unit (here is ONE millisecond). its value will be increased by 1
* in the same specified period and then reset to 0 for next period.
* <p>
* max: 2^12-1 range: [0,4095]
*/
private long sequence = 0L;
/** the time stamp last snowflake ID generated */
private long lastTimestamp = -1L;
/**
* generate an unique and incrementing id
*
* @return id
*/
public synchronized long nextId() {
long currTimestamp = timestampGen();
if (currTimestamp < lastTimestamp) {
throw new IllegalStateException(
String.format("Clock moved backwards. Refusing to generate id for %d milliseconds",
lastTimestamp - currTimestamp));
}
if (currTimestamp == lastTimestamp) {
sequence = (sequence + 1) & maxSequence;
if (sequence == 0) { // overflow: greater than max sequence
currTimestamp = waitNextMillis(currTimestamp);
}
} else { // reset to 0 for next period/millisecond
sequence = 0L;
}
// track and memo the time stamp last snowflake ID generated
lastTimestamp = currTimestamp;
return ((currTimestamp - epoch) << timestampShift) | //
(datacenterId << datacenterIdShift) | //
(workerId << workerIdShift) | // new line for nice looking
sequence;
}
/**
* @param datacenterId
* data center number the process running on, value range: [0,31]
* @param workerId
* machine or process number, value range: [0,31]
*/
public Snowflake(long datacenterId, long workerId) {
if (datacenterId > maxDatacenterId || datacenterId < 0) {
throw new IllegalArgumentException(
String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
}
if (workerId > maxWorkerId || workerId < 0) {
throw new IllegalArgumentException(
String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
}
this.datacenterId = datacenterId;
this.workerId = workerId;
}
/**
* track the amount of calling {@link #waitNextMillis(long)} method
*/
private final AtomicLong waitCount = new AtomicLong(0);
/**
* @return the amount of calling {@link #waitNextMillis(long)} method
*/
public long getWaitCount() {
return waitCount.get();
}
/**
* running loop blocking until next millisecond
*
* @param currTimestamp current time stamp
* @return current time stamp in millisecond
*/
protected long waitNextMillis(long currTimestamp) {
waitCount.incrementAndGet();
while (currTimestamp <= lastTimestamp) {
currTimestamp = timestampGen();
}
return currTimestamp;
}
/**
* get current time stamp
*
* @return current time stamp in millisecond
*/
protected long timestampGen() {
return System.currentTimeMillis();
}
/**
* show settings of Snowflake
*/
@Override
public String toString() {
return "Snowflake Settings [timestampBits=" + timestampBits + ", datacenterIdBits=" + datacenterIdBits
+ ", workerIdBits=" + workerIdBits + ", sequenceBits=" + sequenceBits + ", epoch=" + epoch
+ ", datacenterId=" + datacenterId + ", workerId=" + workerId + "]";
}
public long getEpoch() {
return this.epoch;
}
/**
* extract time stamp, datacenterId, workerId and sequence number
* information from the given id
*
* @param id
* a snowflake id generated by this object
* @return an array containing time stamp, datacenterId, workerId and
* sequence number
*/
public long[] parseId(long id) {
long[] arr = new long[5];
arr[4] = ((id & diode(unusedBits, timestampBits)) >> timestampShift);
arr[0] = arr[4] + epoch;
arr[1] = (id & diode(unusedBits + timestampBits, datacenterIdBits)) >> datacenterIdShift;
arr[2] = (id & diode(unusedBits + timestampBits + datacenterIdBits, workerIdBits)) >> workerIdShift;
arr[3] = (id & diode(unusedBits + timestampBits + datacenterIdBits + workerIdBits, sequenceBits));
return arr;
}
/**
* extract and display time stamp, datacenterId, workerId and sequence
* number information from the given id in humanization format
*
* @param id snowflake id in Long format
* @return snowflake id in String format
*/
public String formatId(long id) {
long[] arr = parseId(id);
String tmf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date(arr[0]));
return String.format("%s, #%d, @(%d,%d)", tmf, arr[3], arr[1], arr[2]);
}
/**
* a diode is a long value whose left and right margin are ZERO, while
* middle bits are ONE in binary string layout. it looks like a diode in
* shape.
*
* @param offset
* left margin position
* @param length
* offset+length is right margin position
* @return a long value
*/
private long diode(long offset, long length) {
int lb = (int) (64 - offset);
int rb = (int) (64 - (of
没有合适的资源?快使用搜索试试~ 我知道了~
xcEduService01.zip_forget9uu_sendheo_xc-service_代码
共441个文件
xml:162个
class:122个
java:120个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 130 浏览量
2022-09-24
10:39:52
上传
评论
收藏 667KB ZIP 举报
温馨提示
后端学成网站代码,教育网站模型,提供了后端的基础框架的搭建等
资源推荐
资源详情
资源评论
收起资源包目录
xcEduService01.zip_forget9uu_sendheo_xc-service_代码 (441个子文件)
CoursePub.class 10KB
CourseIndex.class 10KB
CourseOff.class 9KB
CoursePre.class 9KB
CmsPage.class 9KB
weixinPayInformRequest.class 9KB
XcUser.class 8KB
MediaFile.class 7KB
XcMenu.class 7KB
HttpClient.class 7KB
XcTask.class 7KB
CourseBase.class 7KB
Teachplan.class 6KB
XcCompany.class 6KB
XcTaskHis.class 6KB
FileSystem.class 6KB
MediaVideoCourse.class 5KB
XcOrdersDetail.class 5KB
XcOrders.class 5KB
CourseMarket.class 5KB
PreViewCourse.class 5KB
ViewCourse.class 5KB
Snowflake.class 5KB
HlsVideoUtil.class 5KB
XcLearningCourse.class 5KB
XcRole.class 5KB
CmsSiteServer.class 5KB
VideoUtil.class 4KB
CourseSearchParam.class 4KB
Category.class 4KB
XcLearningList.class 4KB
CourseCode.class 4KB
OrderCode.class 4KB
ReportCourse.class 4KB
ReportCompany.class 4KB
UserBasicInfo.class 4KB
FileSystemCode.class 4KB
CmsSite.class 4KB
UcenterCode.class 4KB
XcTeacher.class 4KB
MediaCode.class 4KB
AuthCode.class 4KB
MD5Util.class 4KB
TeachplanMedia.class 4KB
PreViewCourseMedia.class 4KB
ViewCourseMedia.class 4KB
XcUserRole.class 4KB
CmsTemplate.class 3KB
Mp4VideoUtil.class 3KB
PortalViewCode.class 3KB
PayOrderResult.class 3KB
SysDictionary.class 3KB
XcOauth2Util$UserJwt.class 3KB
QueryPageRequest.class 3KB
XcPermission.class 3KB
XcOrdersPay.class 3KB
CmsConfigModel.class 3KB
ResponseResult.class 3KB
CmsConfig.class 3KB
XcCompanyUser.class 3KB
CmsPageRepositoryTest.class 3KB
CmsCode.class 3KB
SnowflakeIdWorker.class 3KB
MediaCourseResult.class 3KB
PayQrcodeResult.class 3KB
UserTest.class 3KB
QueryMediaFileRequest.class 2KB
SysDictionaryValue.class 2KB
TeachplanExt.class 2KB
CommonCode.class 2KB
LoginRequest.class 2KB
AuthToken.class 2KB
CookieUtil.class 2KB
XcUserExt.class 2KB
UserTokenStore.class 2KB
QueryResult.class 2KB
PageService.class 2KB
UserToken.class 2KB
CoursePic.class 2KB
TeachplanParameter.class 2KB
CategoryParameter.class 2KB
UploadFileResult.class 2KB
Swagger2Configuration.class 2KB
MediaFileResult.class 2KB
GenerateOrderNum.class 2KB
QueryResponseResult.class 2KB
CreateOrderResult.class 2KB
OrderResult.class 2KB
CmsPageParam.class 2KB
CmsPageResult.class 2KB
TeachplanNode.class 2KB
MediaVideoCourseSimple.class 2KB
CategoryNode.class 2KB
XcMenuExt.class 2KB
Oauth2Util.class 2KB
CheckChunkResult.class 2KB
CoursePreviewResult.class 2KB
MediaFileProcess_m3u8.class 2KB
LoginResult.class 2KB
DeleteCourseResult.class 2KB
共 441 条
- 1
- 2
- 3
- 4
- 5
资源评论
刘良运
- 粉丝: 68
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- keil2 + proteus + 8051.exe
- 1961ee27df03bd4595d28e24b00dde4e_744c805f7e4fb4d40fa3f695bfbab035_8(1).c
- mediapipe-0.9.0.1-cp37-cp37m-win-amd64.whl.zip
- windows注册表编辑工具
- mediapipe-0.9.0.1-cp37-cp37m-win-amd64.whl.zip
- 校园通行码预约管理系统20240522075502
- 车类型数据集6250张VOC+YOLO格式.zip
- The PyTorch implementation of STGCN.STGCN-main.zip
- 092300108.cpp
- 车类型数据集6000张VOC+YOLO格式.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功