ShardingSphereService.java
3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.taover.repository.shardingsphere;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.taover.repository.autoconfigure.ShardingSphereKeyGeneratorConfiguration;
@Service
public class ShardingSphereService {
private Map<Long, List<ShardingInfoEntity>> CACHED_TABLE_SUFFIX_BY_TENANT = null;
private Map<String, List<ShardingInfoEntity>> CACHED_TABLE_SUFFIX_BY_TABLE_NAME = null;
private Map<String, ShardingKeyGeneratorExt> GENERATOR_HOLDER = new HashMap<String, ShardingKeyGeneratorExt>();
@Resource
private ShardingInfoRepository shardingInfoRepository;
private ShardingSphereKeyGeneratorConfiguration config;
public ShardingSphereService(ShardingSphereKeyGeneratorConfiguration config) {
this.config = config;
}
public List<Long> generateKeyList(String tableName, int number){
if(!GENERATOR_HOLDER.containsKey(tableName)) {
loadShardingKeyGenerator(tableName);
}
return GENERATOR_HOLDER.get(tableName).generateKeyList(number);
}
public List<ShardingInfoEntity> getShardingInfoByTenantId(Long tenantId) {
if(CACHED_TABLE_SUFFIX_BY_TENANT == null) {
loadCacheTableShardingInfo();
}
return CACHED_TABLE_SUFFIX_BY_TENANT.get(tenantId);
}
private synchronized void loadCacheTableShardingInfo() {
if(CACHED_TABLE_SUFFIX_BY_TENANT != null && CACHED_TABLE_SUFFIX_BY_TABLE_NAME != null) {
return;
}
List<ShardingInfoEntity> dataList = this.shardingInfoRepository.findListBySql("1=1");
Map<Long, List<ShardingInfoEntity>> tempData = new HashMap<Long, List<ShardingInfoEntity>>();
Map<String, List<ShardingInfoEntity>> tempDataTableName = new HashMap<String, List<ShardingInfoEntity>>();
for(ShardingInfoEntity item: dataList) {
List<ShardingInfoEntity> tempItem = tempData.getOrDefault(item.getTenantId(), new ArrayList<ShardingInfoEntity>());
tempItem.add(item);
tempData.put(item.getTenantId(), tempItem);
List<ShardingInfoEntity> tempItemTableName = tempDataTableName.getOrDefault(item.getTableName(), new ArrayList<ShardingInfoEntity>());
tempItemTableName.add(item);
tempDataTableName.put(item.getTableName(), tempItemTableName);
}
CACHED_TABLE_SUFFIX_BY_TENANT = tempData;
CACHED_TABLE_SUFFIX_BY_TABLE_NAME = tempDataTableName;
}
private synchronized void loadShardingKeyGenerator(String tableName) {
if(GENERATOR_HOLDER.containsKey(tableName)) {
return;
}
ShardingKeyGeneratorExt generator = new ShardingKeyGeneratorImpl();
generator.loalConfig(config);
GENERATOR_HOLDER.put(tableName, generator);
}
public List<ShardingInfoEntity> getShardingInfoByTableNames(String[] broadcastTableNames) {
if(broadcastTableNames == null || broadcastTableNames.length == 0) {
return null;
}
if(CACHED_TABLE_SUFFIX_BY_TABLE_NAME == null) {
loadCacheTableShardingInfo();
}
List<ShardingInfoEntity> dataList = new ArrayList<ShardingInfoEntity>();
for(String item: broadcastTableNames) {
List<ShardingInfoEntity> tempData = CACHED_TABLE_SUFFIX_BY_TABLE_NAME.get(item);
if(tempData != null && !tempData.isEmpty()) {
dataList.addAll(tempData);
}
}
return dataList;
}
}