ShardingSphereService.java 3.17 KB
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;
	}
}