OperationTemplate.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * DBSyncer Copyright 2020-2023 All Rights Reserved.
  3. */
  4. package org.dbsyncer.parser.impl;
  5. import org.dbsyncer.parser.CacheService;
  6. import org.dbsyncer.common.util.CollectionUtils;
  7. import org.dbsyncer.common.util.StringUtil;
  8. import org.dbsyncer.parser.ParserException;
  9. import org.dbsyncer.parser.command.PersistenceCommand;
  10. import org.dbsyncer.parser.enums.CommandEnum;
  11. import org.dbsyncer.parser.enums.GroupStrategyEnum;
  12. import org.dbsyncer.parser.model.ConfigModel;
  13. import org.dbsyncer.parser.model.OperationConfig;
  14. import org.dbsyncer.parser.model.QueryConfig;
  15. import org.dbsyncer.parser.strategy.GroupStrategy;
  16. import org.dbsyncer.parser.util.ConfigModelUtil;
  17. import org.dbsyncer.storage.StorageService;
  18. import org.dbsyncer.storage.enums.StorageEnum;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21. import org.springframework.beans.BeanUtils;
  22. import org.springframework.stereotype.Component;
  23. import org.springframework.util.Assert;
  24. import javax.annotation.Resource;
  25. import java.util.ArrayList;
  26. import java.util.Collections;
  27. import java.util.LinkedList;
  28. import java.util.List;
  29. import java.util.Map;
  30. /**
  31. * 操作配置模板
  32. *
  33. * @author AE86
  34. * @version 1.0.0
  35. * @date 2019/9/16 23:59
  36. */
  37. @Component
  38. public final class OperationTemplate {
  39. private final Logger logger = LoggerFactory.getLogger(getClass());
  40. @Resource
  41. private StorageService storageService;
  42. @Resource
  43. private CacheService cacheService;
  44. public <T> List<T> queryAll(Class<T> valueType) {
  45. try {
  46. ConfigModel configModel = (ConfigModel) valueType.newInstance();
  47. return queryAll(new QueryConfig<T>(configModel));
  48. } catch (Exception e) {
  49. throw new ParserException(e);
  50. }
  51. }
  52. public <T> List<T> queryAll(QueryConfig<T> query) {
  53. ConfigModel model = query.getConfigModel();
  54. String groupId = getGroupId(model, query.getGroupStrategyEnum());
  55. Group group = cacheService.get(groupId, Group.class);
  56. if (null != group) {
  57. List<String> index = group.getIndex();
  58. if (!CollectionUtils.isEmpty(index)) {
  59. List<T> list = new ArrayList<>();
  60. Class<? extends ConfigModel> clazz = model.getClass();
  61. index.forEach(e -> {
  62. Object v = cacheService.get(e);
  63. if (null != v) {
  64. list.add((T) beanCopy(clazz, v));
  65. }
  66. });
  67. return list;
  68. }
  69. }
  70. return Collections.EMPTY_LIST;
  71. }
  72. public int queryCount(QueryConfig query) {
  73. ConfigModel model = query.getConfigModel();
  74. String groupId = getGroupId(model, query.getGroupStrategyEnum());
  75. Group group = cacheService.get(groupId, Group.class);
  76. return null != group ? group.getIndex().size() : 0;
  77. }
  78. public <T> T queryObject(Class<T> clazz, String id) {
  79. if (StringUtil.isBlank(id)) {
  80. return null;
  81. }
  82. Object o = cacheService.get(id, clazz);
  83. return beanCopy(clazz, o);
  84. }
  85. public String execute(OperationConfig config) {
  86. // 1、解析配置
  87. ConfigModel model = config.getModel();
  88. Assert.notNull(model, "ConfigModel can not be null.");
  89. // 2、持久化
  90. Map<String, Object> params = ConfigModelUtil.convertModelToMap(model);
  91. logger.debug("params:{}", params);
  92. CommandEnum cmd = config.getCommandEnum();
  93. Assert.notNull(cmd, "CommandEnum can not be null.");
  94. cmd.getCommandExecutor().execute(new PersistenceCommand(storageService, params));
  95. // 3、缓存
  96. cache(model, config.getGroupStrategyEnum());
  97. return model.getId();
  98. }
  99. public void cache(ConfigModel model, GroupStrategyEnum strategy) {
  100. // 1、缓存
  101. Assert.notNull(model, "ConfigModel can not be null.");
  102. String id = model.getId();
  103. cacheService.put(id, model);
  104. // 2、分组
  105. String groupId = getGroupId(model, strategy);
  106. cacheService.putIfAbsent(groupId, new Group());
  107. Group group = cacheService.get(groupId, Group.class);
  108. group.addIfAbsent(id);
  109. logger.debug("Put the model [{}] for {} group into cache.", id, groupId);
  110. }
  111. public void remove(OperationConfig config) {
  112. String id = config.getId();
  113. Assert.hasText(id, "ID can not be empty.");
  114. // 删除分组
  115. ConfigModel model = cacheService.get(id, ConfigModel.class);
  116. String groupId = getGroupId(model, config.getGroupStrategyEnum());
  117. Group group = cacheService.get(groupId, Group.class);
  118. if (null != group) {
  119. group.remove(id);
  120. if (0 >= group.size()) {
  121. cacheService.remove(groupId);
  122. }
  123. }
  124. cacheService.remove(id);
  125. storageService.remove(StorageEnum.CONFIG, id);
  126. }
  127. public String getGroupId(ConfigModel model, GroupStrategyEnum strategy) {
  128. Assert.notNull(model, "ConfigModel can not be null.");
  129. Assert.notNull(strategy, "GroupStrategyEnum can not be null.");
  130. GroupStrategy groupStrategy = strategy.getGroupStrategy();
  131. Assert.notNull(groupStrategy, "GroupStrategy can not be null.");
  132. String groupId = groupStrategy.getGroupId(model);
  133. Assert.hasText(groupId, "GroupId can not be empty.");
  134. return groupId;
  135. }
  136. private <T> T beanCopy(Class<T> clazz, Object o) {
  137. if (null == o || null == clazz) {
  138. return null;
  139. }
  140. try {
  141. T t = clazz.newInstance();
  142. BeanUtils.copyProperties(o, t);
  143. return t;
  144. } catch (InstantiationException e) {
  145. throw new ParserException(e);
  146. } catch (IllegalAccessException e) {
  147. throw new ParserException(e);
  148. }
  149. }
  150. public class Group {
  151. private List<String> index;
  152. public Group() {
  153. this.index = new LinkedList<>();
  154. }
  155. public synchronized void addIfAbsent(String e) {
  156. if (!index.contains(e)) {
  157. index.add(e);
  158. }
  159. }
  160. public synchronized void remove(String e) {
  161. index.remove(e);
  162. }
  163. public int size() {
  164. return index.size();
  165. }
  166. public List<String> getIndex() {
  167. return Collections.unmodifiableList(index);
  168. }
  169. public void setIndex(List<String> index) {
  170. this.index = index;
  171. }
  172. }
  173. }