33import com .google .common .collect .Lists ;
44import com .intellij .openapi .project .Project ;
55import com .intellij .openapi .roots .ProjectRootManager ;
6- import com .intellij .openapi .vfs .VirtualFile ;
76import com .intellij .psi .PsiFile ;
87import com .intellij .psi .PsiFileSystemItem ;
98import com .intellij .psi .util .PsiUtil ;
109import com .intellij .psi .xml .XmlAttribute ;
1110import com .intellij .psi .xml .XmlFile ;
1211import com .intellij .psi .xml .XmlTag ;
1312import io .github .linyimin .plugin .utils .MapperDomUtils ;
14- import org .apache .commons .lang3 .StringUtils ;
1513
1614import 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}
0 commit comments