Skip to content

Commit

Permalink
fix sequence generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Veal98 committed May 22, 2024
1 parent f8a50d9 commit 007cdd8
Showing 1 changed file with 11 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@

import cn.itmtx.ddd.ezlink.component.bloomfilter.BloomFilterHelper;
import cn.itmtx.ddd.ezlink.domain.domainobject.SequenceAndCodeDO;
import cn.itmtx.ddd.ezlink.domain.domainobject.UrlMapDO;
import cn.itmtx.ddd.ezlink.domain.domainservice.cache.UrlMapCacheManager;
import cn.itmtx.ddd.ezlink.domain.domainservice.enums.SequenceGeneratorStrategyEnum;
import cn.itmtx.ddd.ezlink.domain.domainservice.util.ConversionUtils;
import com.google.common.hash.BloomFilter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

Expand All @@ -24,11 +18,7 @@
@Slf4j
public class SequenceGenerator {

private final static DecimalSequenceGenerator DECIMAL_SEQUENCE_GENERATOR;

static {
DECIMAL_SEQUENCE_GENERATOR = loadGenerator();
}
private DecimalSequenceGenerator decimalSequenceGenerator;

/**
* 62 进制压缩码的长度
Expand All @@ -40,13 +30,17 @@ public class SequenceGenerator {
* 压缩码生成策略
*/
@Value("${ezlink.generate.sequence-generator.type}")
private static String strategy;
private String strategy;

/**
* 加载 10 进制压缩码生成器
* @return
*/
private static DecimalSequenceGenerator loadGenerator() {
private DecimalSequenceGenerator loadGenerator() {
if (Objects.nonNull(decimalSequenceGenerator)) {
return decimalSequenceGenerator;
}

SequenceGeneratorStrategyEnum strategyEnum = SequenceGeneratorStrategyEnum.findBy(strategy);
// 如果找不到相应的 strategy 配置,则走 spi 机制查找用户自定义的 generator
if (Objects.isNull(strategyEnum)) {
Expand Down Expand Up @@ -84,15 +78,17 @@ private static DecimalSequenceGenerator loadGenerator() {
* @return
*/
public SequenceAndCodeDO generate(String longUrl) {
decimalSequenceGenerator = this.loadGenerator();

// 生成 10 进制压缩码
long sequence = DECIMAL_SEQUENCE_GENERATOR.generateDecimalSequence(longUrl);
long sequence = decimalSequenceGenerator.generateDecimalSequence(longUrl);
// 10 进制转 62 进制
String compressionCode = ConversionUtils.X.encode62(sequence, compressionCodeLength);

// 处理冲突
boolean isInBloomFilter = BloomFilterHelper.mightContain(compressionCode);
if (isInBloomFilter) {
sequence = DECIMAL_SEQUENCE_GENERATOR.fixConflict(longUrl, compressionCodeLength);
sequence = decimalSequenceGenerator.fixConflict(longUrl, compressionCodeLength);
compressionCode = ConversionUtils.X.encode62(sequence, compressionCodeLength);
}

Expand Down

0 comments on commit 007cdd8

Please sign in to comment.