添加删除公共组的接口
This commit is contained in:
@@ -2,11 +2,9 @@ package com.nis.web.service.restful;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -1886,138 +1884,8 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
|
||||
}
|
||||
}
|
||||
|
||||
private void addTmpReionold(List<Map<String, String>> regionMapList, MaatXmlExpr maatXmlExpr,
|
||||
Transaction transaction, int type, int idRelaRedisDBIndex, List<MaatXmlExpr> expressionList,
|
||||
Set<Integer> redisDB) throws Exception {
|
||||
Map<String, String> keyMap = new HashMap<>();// 记录15号库中的COMPILEGROUP,GROUPCOMPILE,避免每次去redis中查询,影响性能
|
||||
Map<Integer, Double> dbVersionMap = new HashMap<>();// 记录每个redisdb中的maat_version,避免每次去redis中查询,影响性能
|
||||
if (regionMapList != null && regionMapList.size() > 0) {
|
||||
String[] keySplit = maatXmlExpr.getKeyExpression().split(";");
|
||||
for (Map<String, String> map : regionMapList) {
|
||||
String maatKey = null;
|
||||
StringBuffer keyBF = new StringBuffer();
|
||||
for (String keyStr : keySplit) {
|
||||
if (!StringUtils.isEmpty(keyStr) && keyStr.trim().startsWith("[")) {
|
||||
keyStr = keyStr.trim().replace("[", "").replace("]", "");
|
||||
String keyVal = map.get(keyStr);
|
||||
if (keyVal != null && !keyVal.equals("")) {
|
||||
keyBF.append(keyVal);
|
||||
} else {
|
||||
throw new ServiceRuntimeException("未从map中获取到" + keyStr + "的值,请检查数据或配置文件是否正确",
|
||||
RestBusinessCode.NotFoundValueByKey.getValue());
|
||||
}
|
||||
} else if (!StringUtils.isEmpty(keyStr) && keyStr.trim().startsWith("{")) {
|
||||
keyStr = keyStr.trim().replace("{", "").replace("}", "");
|
||||
if (keyStr.toLowerCase().contains("table_name")) {
|
||||
String argTableName = map.get("table_name");
|
||||
if (argTableName == null) {
|
||||
throw new ServiceRuntimeException("添加分组复用域配置时,必须要传入表名,请检查参数,配置类型:" + type + ",对应的真实表名",
|
||||
RestBusinessCode.NotFoundTableName.getValue());
|
||||
} else {
|
||||
keyBF.append(argTableName);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
keyBF.append(keyStr.trim());
|
||||
}
|
||||
}
|
||||
String groupId = null;
|
||||
StringBuffer valBF = new StringBuffer();
|
||||
String[] valSplit = maatXmlExpr.getValueExpression().split(";");
|
||||
for (String valStr : valSplit) {
|
||||
if (!StringUtils.isEmpty(valStr) && valStr.trim().startsWith("[")) {
|
||||
valStr = valStr.trim().replace("[", "").replace("]", "");
|
||||
if (valStr.toLowerCase().equals("op_time") && type == 12) {
|
||||
String user_region = map.get("user_region");
|
||||
valBF.append(user_region + "\t");
|
||||
}
|
||||
String val = map.get(valStr);
|
||||
if (val != null) {
|
||||
valBF.append(val);
|
||||
if (valStr.equals("group_id")) {
|
||||
groupId = val;
|
||||
}
|
||||
} else {
|
||||
// 所有在maat.xml中配置的属性都不可以为空
|
||||
throw new ServiceRuntimeException("未从map中获取到" + valStr + "的值,无法拼接redisValue,请检查数据或配置文件是否正确",
|
||||
RestBusinessCode.NotFoundValueByKey.getValue());
|
||||
}
|
||||
} else if (valStr.equals(" ")) {
|
||||
valBF.append(" ");
|
||||
} else if (valStr.equals("\\t")) {// xml中是字符串的\t这里判断的时候需要转义为\\t,但是添加的时候需要添加\t不是\\t
|
||||
valBF.append("\t");
|
||||
} else if (valStr.equals("\\n")) {
|
||||
valBF.append("\n");
|
||||
} else {
|
||||
valBF.append(valStr.trim());
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
//
|
||||
// transaction.select(tmpStorageReuseRegionDB);
|
||||
// maatKey = keyBF.toString();
|
||||
// transaction.set(maatKey.toUpperCase(), valBF.toString());
|
||||
// logger.info("向{}号redis数据库添加了一条配置,key是{},value是{}", tmpStorageReuseRegionDB, maatKey.toUpperCase(),
|
||||
// valBF.toString());
|
||||
|
||||
String groupIdStr = "GROUPCOMPILE:" + groupId;
|
||||
String groupCompileVal = null;
|
||||
if (!keyMap.containsKey(groupIdStr)) {
|
||||
groupCompileVal = JedisUtils.get(groupIdStr, idRelaRedisDBIndex);
|
||||
keyMap.put(groupIdStr, groupCompileVal);
|
||||
} else {
|
||||
groupCompileVal = keyMap.get(groupIdStr);
|
||||
}
|
||||
|
||||
if (groupCompileVal != null && !groupCompileVal.trim().equals("")) {
|
||||
Set<Integer> redisDBSet = new HashSet<Integer>();
|
||||
String[] split = groupCompileVal.split(";");
|
||||
if (split != null && split.length > 0) {
|
||||
for (String compileStr : split) {
|
||||
if (compileStr != null && !compileStr.trim().equals("")) {
|
||||
String[] dbArr = compileStr.split("-")[1].split(",");
|
||||
for (String db : dbArr) {
|
||||
if (db != null && !db.trim().equals("")) {
|
||||
redisDBSet.add(Integer.parseInt(db.trim()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
String groupRegionStr = "GROUPREGION:" + groupId;
|
||||
String regionVal = null;
|
||||
if (!keyMap.containsKey(groupRegionStr)) {
|
||||
regionVal = JedisUtils.get(groupRegionStr, idRelaRedisDBIndex);
|
||||
keyMap.put(groupRegionStr, regionVal);
|
||||
} else {
|
||||
regionVal = keyMap.get(groupRegionStr);
|
||||
}
|
||||
transaction.append(groupRegionStr, ";" + maatKey.toUpperCase() + "-"
|
||||
+ redisDBSet.toString().replace("[", "").replace("]", ""));
|
||||
redisDB.addAll(redisDBSet);
|
||||
for (Integer redisDb : redisDBSet) {
|
||||
transaction.select(redisDb);
|
||||
transaction.set(maatKey.toUpperCase(), valBF.toString());
|
||||
logger.info("向{}号redis数据库添加了一条配置,key是{},value是{}", redisDb, maatKey.toUpperCase(),
|
||||
valBF.toString());
|
||||
Double maatVersion = null;
|
||||
if (!dbVersionMap.containsKey(redisDb)) {
|
||||
String maatVersionStr = JedisUtils.get("MAAT_VERSION", redisDb);
|
||||
if (maatVersionStr == null) {
|
||||
maatVersionStr = "0";
|
||||
}
|
||||
maatVersion = Double.valueOf(maatVersionStr) + 1D;
|
||||
dbVersionMap.put(redisDb, maatVersion);
|
||||
} else {
|
||||
maatVersion = dbVersionMap.get(redisDb);
|
||||
}
|
||||
updateMaatInfo(expressionList, maatKey, transaction, maatVersion, redisDb, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateGroupRegion(List<MaatConfig> configList, boolean isAdd) {
|
||||
@@ -2262,160 +2130,63 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从临时库删除分组复用域配置,如果该域被引用了,则去对应的redisdb中删除,并将关联库中记录的信息删除
|
||||
*
|
||||
* @param groupId
|
||||
* @param regionList
|
||||
* @param transaction
|
||||
* @param maatXmlConfig
|
||||
* @param tmpStorageReuseRegionDB
|
||||
* @param idRelaRedisDBIndex
|
||||
*/
|
||||
private void removeReuseReion(long groupId, List<String> regionList, Transaction transaction,
|
||||
MaatXmlConfig maatXmlConfig, int tmpStorageReuseRegionDB, int idRelaRedisDBIndex, Set<Integer> redisDB) {
|
||||
|
||||
if (regionList != null && regionList.size() > 0) {
|
||||
Set<String> regionSet = new HashSet<>(regionList);
|
||||
transaction.select(tmpStorageReuseRegionDB);
|
||||
Map<Integer, Double> dbVersionMap = new HashMap<>();
|
||||
Map<String, String> keyMap = new HashMap<>();
|
||||
// key是groupStr,value是删除region后剩下的region集合
|
||||
Map<String, Set<String>> regionKeyMap = new HashMap<>();
|
||||
|
||||
String groupStr = "GROUPREGION:" + groupId;
|
||||
String groupRegionVal = null;
|
||||
if (!keyMap.containsKey(groupStr)) {
|
||||
groupRegionVal = JedisUtils.get(groupStr, idRelaRedisDBIndex);
|
||||
keyMap.put(groupStr, groupRegionVal);
|
||||
} else {
|
||||
groupRegionVal = keyMap.get(groupStr);
|
||||
}
|
||||
if (groupRegionVal != null && !groupRegionVal.trim().equals("")) {
|
||||
Set<String> splitList = null;
|
||||
if (!regionKeyMap.containsKey(groupStr)) {
|
||||
String[] commonsplit = org.apache.commons.lang.StringUtils.split(groupRegionVal, ";");
|
||||
splitList = new HashSet<>(Arrays.asList(commonsplit));
|
||||
regionKeyMap.put(groupStr, splitList);
|
||||
} else {
|
||||
splitList = regionKeyMap.get(groupStr);
|
||||
}
|
||||
if (groupRegionVal != null && !groupRegionVal.trim().equals("")) {
|
||||
if (splitList != null && splitList.size() > 0) {
|
||||
for (Iterator iterator = splitList.iterator(); iterator.hasNext();) {
|
||||
String regionStr = (String) iterator.next();
|
||||
if (regionStr != null && !regionStr.trim().equals("")) {
|
||||
String regionKey = regionStr.split("-")[0];
|
||||
if (regionSet.contains(regionKey.replace("EFFECTIVE_RULE:", ""))) {
|
||||
try {
|
||||
regionSet.remove(regionKey.replace("EFFECTIVE_RULE:", ""));
|
||||
transaction.select(idRelaRedisDBIndex);
|
||||
transaction.del(regionKey);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new ServiceRuntimeException(
|
||||
"从" + tmpStorageReuseRegionDB + "号redis库中删除" + regionKey
|
||||
+ "失败,redis中不存在该值,请联系开发人员检查删除逻辑是否正确或redis数据是否出现了异常",
|
||||
RestBusinessCode.KeyNotExistsInRedis.getValue());
|
||||
}
|
||||
String[] dbArr = regionStr.split("-")[1].split(",");
|
||||
for (String db : dbArr) {
|
||||
if (db != null && !db.trim().equals("")) {
|
||||
int redisDb = Integer.parseInt(db.trim());
|
||||
redisDB.add(redisDb);
|
||||
transaction.select(redisDb);
|
||||
String isValidKey = regionKey.replace("EFFECTIVE_RULE", "OBSOLETE_RULE");
|
||||
transaction.rename(regionKey, isValidKey);
|
||||
Double maatVersion = null;
|
||||
if (!dbVersionMap.containsKey(redisDb)) {
|
||||
String maatVersionStr = JedisUtils.get("MAAT_VERSION", redisDb);
|
||||
if (maatVersionStr == null) {
|
||||
maatVersionStr = "0";
|
||||
}
|
||||
maatVersion = Double.valueOf(maatVersionStr) + 1D;
|
||||
dbVersionMap.put(redisDb, maatVersion);
|
||||
} else {
|
||||
maatVersion = dbVersionMap.get(redisDb);
|
||||
}
|
||||
updateMaatInfo(maatXmlConfig.getExpressionList(), isValidKey, transaction,
|
||||
maatVersion, redisDb, true);
|
||||
|
||||
}
|
||||
}
|
||||
if (splitList.size() == 1) {
|
||||
transaction.select(idRelaRedisDBIndex);
|
||||
try {
|
||||
transaction.del(groupStr);
|
||||
} catch (Exception e) {
|
||||
throw new ServiceRuntimeException(
|
||||
"从" + idRelaRedisDBIndex + "号redis库中删除" + groupStr
|
||||
+ "失败,redis中不存在该值,请联系开发人员检查删除逻辑是否正确或redis数据是否出现了异常",
|
||||
RestBusinessCode.KeyNotExistsInRedis.getValue());
|
||||
}
|
||||
}
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (regionSet.size() > 0) {
|
||||
throw new ServiceRuntimeException(
|
||||
"删除分组复用域配置时,域" + regionSet + "在关联关系中不存在,请联系开发人员检查删除逻辑是否正确或redis数据是否出现了异常",
|
||||
RestBusinessCode.KeyNotExistsInRedis.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (String groupRegionStr : regionKeyMap.keySet()) {
|
||||
transaction.select(idRelaRedisDBIndex);
|
||||
Set<String> set = regionKeyMap.get(groupRegionStr);
|
||||
if (set.size() > 0) {
|
||||
StringBuffer newGroupRegion = new StringBuffer();
|
||||
for (String str : set) {
|
||||
newGroupRegion.append(str);
|
||||
newGroupRegion.append(";");
|
||||
}
|
||||
transaction.set(groupStr, newGroupRegion.substring(0, newGroupRegion.length() - 1));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
throw new ServiceRuntimeException("删除分组复用域配置时,参数都为空,请检查配置参数是否正确",
|
||||
RestBusinessCode.ConfigSourceIsNull.getValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean delGroupReuseConfig(Map<Long, List<String>> groupAndRegionMap) {
|
||||
public boolean delCommonGroup(Set<Long> groupIdList) {
|
||||
Jedis resource = JedisUtils.getResource(0);
|
||||
Transaction transaction = resource.multi();
|
||||
try {
|
||||
if (groupAndRegionMap != null && groupAndRegionMap.size() > 0) {
|
||||
int tmpStorageReuseRegionDB = Configurations.getIntProperty("tmpStorageReuseRegionDB", 15);
|
||||
if (groupIdList != null && groupIdList.size() > 0) {
|
||||
int idRelaRedisDBIndex = Configurations.getIntProperty("idRelaRedisDBIndex", 15);
|
||||
transaction.select(tmpStorageReuseRegionDB);
|
||||
transaction.select(idRelaRedisDBIndex);
|
||||
// Map<String, Set<String>> groupReionKey = new HashMap<>();
|
||||
Set<String> set = new HashSet<>();
|
||||
for (Long groupId : groupIdList) {
|
||||
String commonGroup = "COMMONGROUPREGION:" + groupId;
|
||||
String regionKey = JedisUtils.get(commonGroup, idRelaRedisDBIndex);
|
||||
if (regionKey == null || regionKey.trim().equals("")) {
|
||||
throw new ServiceRuntimeException("无法删除公共组" + groupId + ",该组下未找到对应的域,请检查",
|
||||
RestBusinessCode.ConfigSourceIsNull.getValue());
|
||||
}
|
||||
String[] keyAndDBArr = org.apache.commons.lang.StringUtils.split(regionKey, ";");
|
||||
|
||||
for (String keyAndDB : keyAndDBArr) {
|
||||
String key = keyAndDB.substring(0, keyAndDB.indexOf("-"));
|
||||
set.add(key);
|
||||
}
|
||||
// groupReionKey.put(commonGroup, set);
|
||||
}
|
||||
|
||||
MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(37);// maat类配置的表达式都一样,这里因为没有service所以就随便取了一个
|
||||
Set<Integer> redisDB = new HashSet<Integer>();
|
||||
for (Long groupId : groupAndRegionMap.keySet()) {
|
||||
List<String> regionList = groupAndRegionMap.get(groupId);
|
||||
if (regionList != null && regionList.size() > 0) {
|
||||
removeReuseReion(groupId, regionList, transaction, maatXmlConfig, tmpStorageReuseRegionDB,
|
||||
idRelaRedisDBIndex, redisDB);
|
||||
List<MaatXmlExpr> expressionList = maatXmlConfig.getExpressionList();
|
||||
for (String db : Constants.COMMONGROUPDBARR) {
|
||||
int everyDb = Integer.parseInt(db);
|
||||
|
||||
Double maatVersion = null;
|
||||
String maatVersionStr = JedisUtils.get("MAAT_VERSION", everyDb);
|
||||
if (maatVersionStr == null) {
|
||||
maatVersionStr = "0";
|
||||
}
|
||||
maatVersion = Double.valueOf(maatVersionStr) + 1D;
|
||||
transaction.select(everyDb);
|
||||
for (String regionKey : set) {
|
||||
String obsKey = regionKey.replace("EFFECTIVE_RULE", "OBSOLETE_RULE");
|
||||
transaction.rename(regionKey,obsKey);
|
||||
updateMaatInfo(expressionList, obsKey, transaction, maatVersion, everyDb, true);
|
||||
}
|
||||
}
|
||||
for (Integer redisDb : redisDB) {
|
||||
transaction.select(redisDb);
|
||||
for (String db : Constants.COMMONGROUPDBARR) {
|
||||
transaction.select(Integer.parseInt(db));
|
||||
transaction.incrBy("MAAT_VERSION", 1l);
|
||||
logger.info("向{}号redis数据库更新了MAAT_VERSION", redisDb);
|
||||
logger.info("向{}号redis数据库更新了MAAT_VERSION", Integer.parseInt(db));
|
||||
// logger.info("向{}号redis数据库更新了MAAT_VERSION,更新后版本是{}", redisDb, maatVersion);
|
||||
}
|
||||
transaction.exec();
|
||||
return true;
|
||||
} else {
|
||||
throw new ServiceRuntimeException("单独删除分组复用配置的域配置时,参数为空,请检查",
|
||||
RestBusinessCode.ConfigSourceIsNull.getValue());
|
||||
throw new ServiceRuntimeException("删除公共组时,参数为空,请检查", RestBusinessCode.ConfigSourceIsNull.getValue());
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user