Skip to content

Commit b565db8

Browse files
committed
refactor: performance optimization, to avoid CPU occupancy
1 parent cc81e18 commit b565db8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+3117
-406
lines changed

build.gradle

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ plugins {
44
}
55

66
group 'org.linyimin'
7-
version '1.0.2'
7+
version '1.0.3'
88

99
repositories {
1010
maven { url 'https://maven.aliyun.com/repository/public/' }
1111
mavenCentral()
1212
}
1313

1414
dependencies {
15-
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
15+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
1616
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
17-
implementation 'org.mybatis:mybatis:3.3.1'
18-
implementation 'com.alibaba:fastjson:1.2.79'
19-
implementation 'mysql:mysql-connector-java:8.0.25'
17+
implementation 'com.alibaba:fastjson:2.0.19'
18+
implementation 'mysql:mysql-connector-java:8.0.30'
2019
implementation 'com.github.vertical-blank:sql-formatter:2.0.3'
20+
implementation group: 'ognl', name: 'ognl', version: '3.0.4'
2121
}
2222

2323
// See https://github.com/JetBrains/gradle-intellij-plugin/
@@ -28,6 +28,13 @@ intellij {
2828
}
2929
patchPluginXml {
3030
changeNotes = """
31+
<h4>1.0.3</h4>
32+
<ul>
33+
<li>Refactoring, performance optimization, to avoid CPU occupancy</li>
34+
</ul>
35+
<ul>
36+
<li>重构,性能优化避免CPU占满</li>
37+
</ul>
3138
<h4>1.0.2</h4>
3239
<ul>
3340
<li>Suppot without mybatis configuration file</li>

src/main/java/io/github/linyimin/plugin/cache/MybatisXmlContentCache.java

Lines changed: 19 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
import com.google.common.collect.Lists;
44
import com.intellij.openapi.project.Project;
55
import com.intellij.openapi.roots.ProjectRootManager;
6-
import com.intellij.openapi.vfs.VirtualFile;
76
import com.intellij.psi.PsiFile;
87
import com.intellij.psi.PsiFileSystemItem;
98
import com.intellij.psi.util.PsiUtil;
109
import com.intellij.psi.xml.XmlAttribute;
1110
import com.intellij.psi.xml.XmlFile;
1211
import com.intellij.psi.xml.XmlTag;
1312
import io.github.linyimin.plugin.utils.MapperDomUtils;
14-
import org.apache.commons.lang3.StringUtils;
1513

1614
import java.util.*;
1715

@@ -23,56 +21,50 @@ public class MybatisXmlContentCache {
2321

2422
private static final List<String> SUB_TAGS = Lists.newArrayList("insert", "update", "delete", "select");
2523

26-
private static final Map<Project, Map<String /* path */, String /* configuration */>> projectMybatisConfigurationMap = new HashMap<>();
27-
2824
private static final Map<Project, Map<String /* namespace */, List<String> /* method name list */>> projectMybatisMapperMap = new HashMap<>();
2925

3026
private static final Map<Project, Map<String /* namespace */, Set<XmlTag>>> projectMapperNamespaceMap = new HashMap<>();
3127

3228
private static final Map<Project, Map<String /* method qualified name */, Set<XmlTag>>> projectMapperMethodMap = new HashMap<>();
3329

34-
private static final Map<Project, Map<String /* method qualified name */, String /* mapper xml string */>> projectMethodToMapperFilePath = new HashMap<>();
35-
36-
37-
public static List<String> acquireConfigurations(Project project) {
38-
39-
addXmlCache(project);
40-
41-
Map<String, String> cacheMap = projectMybatisConfigurationMap.getOrDefault(project, Collections.emptyMap());
42-
43-
return new ArrayList<>(cacheMap.values());
44-
}
4530

4631
public static List<String> acquireByNamespace(Project project) {
4732

48-
addXmlCache(project);
49-
5033
Set<String> namespaces = projectMybatisMapperMap.getOrDefault(project, new HashMap<>()).keySet();
51-
return new ArrayList<>(namespaces);
52-
}
5334

54-
public static String acquireMapperPathByMethodName(Project project, String methodName) {
35+
if (!namespaces.isEmpty()) {
36+
return new ArrayList<>(namespaces);
37+
}
38+
5539
addXmlCache(project);
5640

57-
return projectMethodToMapperFilePath.getOrDefault(project, new HashMap<>()).get(methodName);
41+
return new ArrayList<>(projectMybatisMapperMap.getOrDefault(project, new HashMap<>()).keySet());
5842
}
5943

6044
public static Set<XmlTag> acquireByNamespace(Project project, String namespace) {
6145

62-
addXmlCache(project);
63-
6446
Map<String /* namespace */, Set<XmlTag>> cache = projectMapperNamespaceMap.getOrDefault(project, new HashMap<>());
6547

66-
return cache.getOrDefault(namespace, new HashSet<>());
48+
if (cache.containsKey(namespace)) {
49+
return cache.getOrDefault(namespace, new HashSet<>());
50+
}
51+
52+
addXmlCache(project);
53+
54+
return projectMapperNamespaceMap.getOrDefault(project, new HashMap<>()).getOrDefault(namespace, new HashSet<>());
6755
}
6856

6957
public static Set<XmlTag> acquireByMethodName(Project project, String methodQualifiedName) {
7058

71-
addXmlCache(project);
72-
7359
Map<String /* namespace */, Set<XmlTag>> cache = projectMapperMethodMap.getOrDefault(project, new HashMap<>());
7460

75-
return cache.getOrDefault(methodQualifiedName, new HashSet<>());
61+
if (cache.containsKey(methodQualifiedName)) {
62+
return cache.get(methodQualifiedName);
63+
}
64+
65+
addXmlCache(project);
66+
67+
return projectMapperMethodMap.getOrDefault(project, new HashMap<>()).getOrDefault(methodQualifiedName, new HashSet<>());
7668
}
7769

7870
private static void addXmlCache(Project project) {
@@ -87,10 +79,6 @@ private static void addXmlCache(Project project) {
8779

8880
PsiFile psiFile = item.getContainingFile();
8981

90-
if (MapperDomUtils.isMybatisConfigurationFile(psiFile)) {
91-
addConfigurationCache(project, fileOrDir, psiFile);
92-
}
93-
9482
if (MapperDomUtils.isMybatisMapperFile(psiFile)) {
9583
addMapperCache(project, psiFile);
9684
}
@@ -133,8 +121,6 @@ private static void addMapperCache(Project project, PsiFile psiFile) {
133121

134122
String id = subAttribute.getValue();
135123

136-
addMethodToMapperCache(project, namespace, id, psiFile);
137-
138124
addMethodXmlTagCache(project, namespace, id, subTag);
139125

140126
addNamespaceCache(project, namespace, id);
@@ -143,25 +129,6 @@ private static void addMapperCache(Project project, PsiFile psiFile) {
143129

144130
}
145131

146-
private static void addMethodToMapperCache(Project project, String namespace, String id, PsiFile psiFile) {
147-
Map<String, String> methodCacheMap = projectMethodToMapperFilePath.getOrDefault(project, new HashMap<>());
148-
149-
String methodQualifiedName = namespace + "." + id;
150-
151-
String path = psiFile.getVirtualFile().getPath();
152-
153-
if (StringUtils.isBlank(path)) {
154-
return;
155-
}
156-
157-
path = path.substring(path.indexOf("resources/") + "resources/".length());
158-
159-
methodCacheMap.put(methodQualifiedName, path);
160-
161-
projectMethodToMapperFilePath.put(project, methodCacheMap);
162-
163-
}
164-
165132
private static void addNamespaceXmlTagCache(Project project, String namespace, XmlTag xmlTag) {
166133

167134
Map<String, Set<XmlTag>> namespaceCacheMap = projectMapperNamespaceMap.getOrDefault(project, new HashMap<>());
@@ -200,14 +167,4 @@ private static void addNamespaceCache(Project project, String namespace, String
200167

201168
projectMybatisMapperMap.put(project, cacheMap);
202169
}
203-
204-
private static void addConfigurationCache(Project project, VirtualFile fileOrDir, PsiFile psiFile) {
205-
Map<String, String> cacheMap = projectMybatisConfigurationMap.getOrDefault(project, new HashMap<>());
206-
207-
String path = fileOrDir.getPath();
208-
String configuration = psiFile.getText();
209-
210-
cacheMap.put(path, configuration);
211-
projectMybatisConfigurationMap.put(project, cacheMap);
212-
}
213170
}

src/main/java/io/github/linyimin/plugin/compile/MybatisPojoCompile.java

Lines changed: 0 additions & 107 deletions
This file was deleted.

src/main/java/io/github/linyimin/plugin/compile/ProjectLoader.java

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.github.linyimin.plugin.mybatis.mapping;
2+
3+
/**
4+
* @author Clinton Begin
5+
*/
6+
public enum SqlCommandType {
7+
UNKNOWN, INSERT, UPDATE, DELETE, SELECT, FLUSH
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.github.linyimin.plugin.mybatis.mapping;
2+
3+
/**
4+
* @author banzhe
5+
* @date 2022/11/13 14:59
6+
**/
7+
public interface SqlSource {
8+
String getSql(Object parameterObject);
9+
}

0 commit comments

Comments
 (0)