OperationTemplate.java 6.3 KB

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