|
| 1 | +package io.github.linyimin.plugin.cache; |
| 2 | + |
| 3 | +import com.google.common.collect.Lists; |
| 4 | +import com.intellij.openapi.project.Project; |
| 5 | +import com.intellij.openapi.roots.ProjectRootManager; |
| 6 | +import com.intellij.openapi.vfs.VirtualFile; |
| 7 | +import com.intellij.psi.PsiFile; |
| 8 | +import com.intellij.psi.PsiFileSystemItem; |
| 9 | +import com.intellij.psi.util.PsiUtil; |
| 10 | +import com.intellij.psi.xml.XmlAttribute; |
| 11 | +import com.intellij.psi.xml.XmlFile; |
| 12 | +import com.intellij.psi.xml.XmlTag; |
| 13 | +import io.github.linyimin.plugin.utils.MapperDomUtils; |
| 14 | + |
| 15 | +import java.util.*; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author banzhe |
| 19 | + * @date 2022/08/10 23:28 |
| 20 | + **/ |
| 21 | +public class MybatisXmlContentCache { |
| 22 | + |
| 23 | + private static final List<String> SUB_TAGS = Lists.newArrayList("insert", "update", "delete", "select"); |
| 24 | + |
| 25 | + private static final Map<Project, Map<String /* path */, String /* configuration */>> projectMybatisConfigurationMap = new HashMap<>(); |
| 26 | + |
| 27 | + private static final Map<Project, Map<String /* namespace */, List<String> /* method name list */>> projectMybatisMapperMap = new HashMap<>(); |
| 28 | + |
| 29 | + private static final Map<Project, Map<String /* namespace */, List<XmlTag>>> projectMapperNamespaceMap = new HashMap<>(); |
| 30 | + |
| 31 | + private static final Map<Project, Map<String /* method qualified name */, List<XmlTag>>> projectMapperMethodMap = new HashMap<>(); |
| 32 | + |
| 33 | + |
| 34 | + public static List<String> acquireConfigurations(Project project) { |
| 35 | + |
| 36 | + addXmlCache(project); |
| 37 | + |
| 38 | + Map<String, String> cacheMap = projectMybatisConfigurationMap.getOrDefault(project, Collections.emptyMap()); |
| 39 | + |
| 40 | + return new ArrayList<>(cacheMap.values()); |
| 41 | + } |
| 42 | + |
| 43 | + public static List<String> acquireByNamespace(Project project) { |
| 44 | + |
| 45 | + addXmlCache(project); |
| 46 | + |
| 47 | + Set<String> namespaces = projectMybatisMapperMap.getOrDefault(project, new HashMap<>()).keySet(); |
| 48 | + return new ArrayList<>(namespaces); |
| 49 | + } |
| 50 | + |
| 51 | + public static List<XmlTag> acquireByNamespace(Project project, String namespace) { |
| 52 | + |
| 53 | + addXmlCache(project); |
| 54 | + |
| 55 | + Map<String /* namespace */, List<XmlTag>> cache = projectMapperNamespaceMap.getOrDefault(project, new HashMap<>()); |
| 56 | + |
| 57 | + return cache.getOrDefault(namespace, new ArrayList<>()); |
| 58 | + } |
| 59 | + |
| 60 | + public static List<XmlTag> acquireByMethodName(Project project, String methodQualifiedName) { |
| 61 | + |
| 62 | + addXmlCache(project); |
| 63 | + |
| 64 | + Map<String /* namespace */, List<XmlTag>> cache = projectMapperMethodMap.getOrDefault(project, new HashMap<>()); |
| 65 | + |
| 66 | + return cache.getOrDefault(methodQualifiedName, new ArrayList<>()); |
| 67 | + } |
| 68 | + |
| 69 | + private static void addXmlCache(Project project) { |
| 70 | + |
| 71 | + ProjectRootManager.getInstance(project).getFileIndex().iterateContent(fileOrDir -> { |
| 72 | + |
| 73 | + PsiFileSystemItem item = PsiUtil.findFileSystemItem(project, fileOrDir); |
| 74 | + |
| 75 | + if (item == null) { |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + PsiFile psiFile = item.getContainingFile(); |
| 80 | + |
| 81 | + if (MapperDomUtils.isMybatisConfigurationFile(psiFile)) { |
| 82 | + addConfigurationCache(project, fileOrDir, psiFile); |
| 83 | + } |
| 84 | + |
| 85 | + if (MapperDomUtils.isMybatisMapperFile(psiFile)) { |
| 86 | + addMapperCache(project, psiFile); |
| 87 | + } |
| 88 | + |
| 89 | + return true; |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + private static void addMapperCache(Project project, PsiFile psiFile) { |
| 94 | + |
| 95 | + XmlTag rootTag = ((XmlFile) psiFile).getRootTag(); |
| 96 | + |
| 97 | + if (rootTag == null || rootTag.getAttribute("namespace") == null) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + XmlAttribute namespaceAttribute = rootTag.getAttribute("namespace"); |
| 103 | + |
| 104 | + if (namespaceAttribute == null) { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + String namespace = namespaceAttribute.getValue(); |
| 109 | + |
| 110 | + addNamespaceXmlTagCache(project, namespace, rootTag); |
| 111 | + |
| 112 | + XmlTag[] subTags = rootTag.getSubTags(); |
| 113 | + |
| 114 | + for (XmlTag subTag : subTags) { |
| 115 | + if (!SUB_TAGS.contains(subTag.getName())) { |
| 116 | + continue; |
| 117 | + } |
| 118 | + |
| 119 | + XmlAttribute subAttribute = subTag.getAttribute("id"); |
| 120 | + |
| 121 | + if (subAttribute == null) { |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + String id = subAttribute.getValue(); |
| 126 | + |
| 127 | + addMethodXmlTagCache(project, namespace, id, subTag); |
| 128 | + |
| 129 | + addNamespaceCache(project, namespace, id); |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + } |
| 134 | + |
| 135 | + private static void addNamespaceXmlTagCache(Project project, String namespace, XmlTag xmlTag) { |
| 136 | + |
| 137 | + Map<String, List<XmlTag>> namespaceCacheMap = projectMapperNamespaceMap.getOrDefault(project, new HashMap<>()); |
| 138 | + |
| 139 | + List<XmlTag> tags = namespaceCacheMap.getOrDefault(namespace, new ArrayList<>()); |
| 140 | + tags.add(xmlTag); |
| 141 | + |
| 142 | + namespaceCacheMap.put(namespace, tags); |
| 143 | + |
| 144 | + projectMapperNamespaceMap.put(project, namespaceCacheMap); |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | + private static void addMethodXmlTagCache(Project project, String namespace, String id, XmlTag xmlTag) { |
| 149 | + |
| 150 | + Map<String, List<XmlTag>> methodCacheMap = projectMapperMethodMap.getOrDefault(project, new HashMap<>()); |
| 151 | + |
| 152 | + String methodQualifiedName = namespace + "." + id; |
| 153 | + |
| 154 | + List<XmlTag> tags = methodCacheMap.getOrDefault(methodQualifiedName, new ArrayList<>()); |
| 155 | + tags.add(xmlTag); |
| 156 | + |
| 157 | + methodCacheMap.put(methodQualifiedName, tags); |
| 158 | + |
| 159 | + projectMapperMethodMap.put(project, methodCacheMap); |
| 160 | + } |
| 161 | + |
| 162 | + private static void addNamespaceCache(Project project, String namespace, String id) { |
| 163 | + |
| 164 | + Map<String, List<String>> cacheMap = projectMybatisMapperMap.getOrDefault(project, new HashMap<>()); |
| 165 | + |
| 166 | + List<String> ids = cacheMap.getOrDefault(namespace, new ArrayList<>()); |
| 167 | + ids.add(id); |
| 168 | + |
| 169 | + cacheMap.put(namespace, ids); |
| 170 | + |
| 171 | + projectMybatisMapperMap.put(project, cacheMap); |
| 172 | + } |
| 173 | + |
| 174 | + private static void addConfigurationCache(Project project, VirtualFile fileOrDir, PsiFile psiFile) { |
| 175 | + Map<String, String> cacheMap = projectMybatisConfigurationMap.getOrDefault(project, new HashMap<>()); |
| 176 | + |
| 177 | + String path = fileOrDir.getPath(); |
| 178 | + String configuration = psiFile.getText(); |
| 179 | + |
| 180 | + cacheMap.put(path, configuration); |
| 181 | + projectMybatisConfigurationMap.put(project, cacheMap); |
| 182 | + } |
| 183 | +} |
0 commit comments