Browse Source

优化逻辑

穿云 3 tháng trước cách đây
mục cha
commit
b9ec84e07d

+ 5 - 9
dbsyncer-manager/src/main/java/org/dbsyncer/manager/impl/PreloadTemplate.java

@@ -18,6 +18,7 @@ import org.dbsyncer.parser.enums.MetaEnum;
 import org.dbsyncer.parser.impl.OperationTemplate;
 import org.dbsyncer.parser.model.ConfigModel;
 import org.dbsyncer.parser.model.Connector;
+import org.dbsyncer.parser.model.Group;
 import org.dbsyncer.parser.model.Mapping;
 import org.dbsyncer.parser.model.Meta;
 import org.dbsyncer.parser.model.OperationConfig;
@@ -175,18 +176,13 @@ public final class PreloadTemplate implements ApplicationListener<ContextRefresh
         if (null == config) {
             return;
         }
-        OperationTemplate.Group group = JsonUtil.jsonToObj(config.toString(), OperationTemplate.Group.class);
-        if (null == group) {
+        Group group = JsonUtil.jsonToObj(config.toString(), Group.class);
+        if (null == group || group.isEmpty()) {
             return;
         }
 
-        List<String> index = group.getIndex();
-        if (CollectionUtils.isEmpty(index)) {
-            return;
-        }
-
-        for (String e : index) {
-            Map m = map.get(e);
+        for (String id : group.getIndex()) {
+            Map m = map.get(id);
             ConfigModel model = (ConfigModel) commandEnum.getCommandExecutor().execute(new PreloadCommand(profileComponent, m.toString()));
             operationTemplate.execute(new OperationConfig(model, CommandEnum.OPR_ADD, commandEnum.getGroupStrategyEnum()));
             // Load tableGroups

+ 2 - 4
dbsyncer-parser/src/main/java/org/dbsyncer/parser/CacheService.java

@@ -19,13 +19,11 @@ public interface CacheService {
     Object put(String key, Object value);
 
     /**
-     * 存放K-V,不存在k则写入
+     * 获取缓
      *
-     * @param key
-     * @param value
      * @return
      */
-    Object putIfAbsent(String key, Object value);
+    Map<String, Object> getCache();
 
     /**
      * 根据Key删除

+ 2 - 2
dbsyncer-parser/src/main/java/org/dbsyncer/parser/impl/CacheServiceImpl.java

@@ -23,8 +23,8 @@ public class CacheServiceImpl implements CacheService {
     }
 
     @Override
-    public Object putIfAbsent(String key, Object value) {
-        return cache.putIfAbsent(key, value);
+    public Map<String, Object> getCache() {
+        return cache;
     }
 
     @Override

+ 35 - 61
dbsyncer-parser/src/main/java/org/dbsyncer/parser/impl/OperationTemplate.java

@@ -3,7 +3,6 @@
  */
 package org.dbsyncer.parser.impl;
 
-import org.dbsyncer.common.util.CollectionUtils;
 import org.dbsyncer.common.util.StringUtil;
 import org.dbsyncer.parser.CacheService;
 import org.dbsyncer.parser.ParserException;
@@ -11,6 +10,7 @@ import org.dbsyncer.parser.command.impl.PersistenceCommand;
 import org.dbsyncer.parser.enums.CommandEnum;
 import org.dbsyncer.parser.enums.GroupStrategyEnum;
 import org.dbsyncer.parser.model.ConfigModel;
+import org.dbsyncer.parser.model.Group;
 import org.dbsyncer.parser.model.OperationConfig;
 import org.dbsyncer.parser.model.QueryConfig;
 import org.dbsyncer.parser.strategy.GroupStrategy;
@@ -24,10 +24,9 @@ import org.springframework.util.Assert;
 
 import javax.annotation.Resource;
 import java.util.ArrayList;
-import java.util.Collections;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
 
 /**
  * 操作配置模板
@@ -58,28 +57,30 @@ public final class OperationTemplate {
 
     public <T> List<T> queryAll(QueryConfig<T> query) {
         String groupId = getGroupId(query.getConfigModel(), query.getGroupStrategyEnum());
-        Group group = cacheService.get(groupId, Group.class);
-        if (null != group) {
-            List<String> index = group.getIndex();
-            if (!CollectionUtils.isEmpty(index)) {
-                List<T> list = new ArrayList<>();
-                index.forEach(e -> {
-                    Object v = cacheService.get(e);
-                    if (null != v) {
-                        list.add((T) v);
-                    }
-                });
-                return list;
-            }
-        }
-        return Collections.EMPTY_LIST;
+        List<T> list = new ArrayList<>();
+        cacheService.getCache().computeIfPresent(groupId, (k, v) -> {
+            Group group = (Group) v;
+            group.getIndex().forEach(id ->
+                cacheService.getCache().computeIfPresent(id, (x,y) -> {
+                    list.add((T) y);
+                    return y;
+                })
+            );
+            return group;
+        });
+        return list;
     }
 
     public int queryCount(QueryConfig query) {
         ConfigModel model = query.getConfigModel();
         String groupId = getGroupId(model, query.getGroupStrategyEnum());
-        Group group = cacheService.get(groupId, Group.class);
-        return null != group ? group.getIndex().size() : 0;
+        AtomicInteger count = new AtomicInteger();
+        cacheService.getCache().computeIfPresent(groupId, (k, v) -> {
+            Group group = (Group) v;
+            count.set(group.size());
+            return group;
+        });
+        return count.get();
     }
 
     public <T> T queryObject(Class<T> clazz, String id) {
@@ -114,10 +115,16 @@ public final class OperationTemplate {
 
         // 2、分组
         String groupId = getGroupId(model, strategy);
-        cacheService.putIfAbsent(groupId, new Group());
-        Group group = cacheService.get(groupId, Group.class);
-        group.addIfAbsent(id);
-        logger.debug("Put the model [{}] for {} group into cache.", id, groupId);
+        cacheService.getCache().compute(groupId, (k, v) -> {
+            Group group = (Group) v;
+            if (group == null) {
+                group = new Group();
+            }
+            if (!group.contains(id)) {
+                group.add(id);
+            }
+            return group;
+        });
     }
 
     public void remove(OperationConfig config) {
@@ -126,13 +133,11 @@ public final class OperationTemplate {
         // 删除分组
         ConfigModel model = cacheService.get(id, ConfigModel.class);
         String groupId = getGroupId(model, config.getGroupStrategyEnum());
-        Group group = cacheService.get(groupId, Group.class);
-        if (null != group) {
+        cacheService.getCache().computeIfPresent(groupId, (k, v) -> {
+            Group group = (Group) v;
             group.remove(id);
-            if (0 >= group.size()) {
-                cacheService.remove(groupId);
-            }
-        }
+            return group.isEmpty() ? null : group;
+        });
         cacheService.remove(id);
         storageService.remove(StorageEnum.CONFIG, id);
     }
@@ -148,35 +153,4 @@ public final class OperationTemplate {
         return groupId;
     }
 
-    public class Group {
-
-        private List<String> index;
-
-        public Group() {
-            this.index = new LinkedList<>();
-        }
-
-        public synchronized void addIfAbsent(String e) {
-            if (!index.contains(e)) {
-                index.add(e);
-            }
-        }
-
-        public synchronized void remove(String e) {
-            index.remove(e);
-        }
-
-        public int size() {
-            return index.size();
-        }
-
-        public List<String> getIndex() {
-            return Collections.unmodifiableList(index);
-        }
-
-        public void setIndex(List<String> index) {
-            this.index = index;
-        }
-    }
-
 }

+ 41 - 0
dbsyncer-parser/src/main/java/org/dbsyncer/parser/model/Group.java

@@ -0,0 +1,41 @@
+/**
+ * DBSyncer Copyright 2020-2025 All Rights Reserved.
+ */
+package org.dbsyncer.parser.model;
+
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * @Author 穿云
+ * @Version 1.0.0
+ * @Date 2025-01-26 21:36
+ */
+public final class Group {
+    private final List<String> index = new LinkedList<>();
+
+    public synchronized void remove(String e) {
+        index.remove(e);
+    }
+
+    public int size() {
+        return index.size();
+    }
+
+    public List<String> getIndex() {
+        return Collections.unmodifiableList(index);
+    }
+
+    public boolean contains(String id) {
+        return index.contains(id);
+    }
+
+    public void add(String id) {
+        index.add(id);
+    }
+
+    public boolean isEmpty() {
+        return index.isEmpty();
+    }
+}

+ 9 - 4
dbsyncer-web/src/main/java/org/dbsyncer/web/controller/openapi/OpenApiController.java

@@ -123,10 +123,15 @@ public class OpenApiController implements InitializingBean {
                 // 匹配解析包
                 for (int i = 0; i < length; i++) {
                     if (StringUtil.startsWith((String) array[i], obj.getKey())) {
-                        Object bean = applicationContext.getBean(v.getBeanType());
-                        InvocableHandlerMethod invocableHandlerMethod = new InvocableHandlerMethod(bean, v.getMethod());
-                        invocableHandlerMethod.setHandlerMethodArgumentResolvers(resolvers);
-                        handlers.putIfAbsent((String) array[i], invocableHandlerMethod);
+                        handlers.compute((String) array[i], (x, y) -> {
+                            if (y == null) {
+                                Object bean = applicationContext.getBean(v.getBeanType());
+                                InvocableHandlerMethod invocableHandlerMethod = new InvocableHandlerMethod(bean, v.getMethod());
+                                invocableHandlerMethod.setHandlerMethodArgumentResolvers(resolvers);
+                                return invocableHandlerMethod;
+                            }
+                            return y;
+                        });
                         filter = true;
                         break;
                     }