From e077b571cf13fa5744c4bf934fe96116e3635209 Mon Sep 17 00:00:00 2001 From: Dmitry Batkovich Date: Thu, 24 Oct 2019 10:17:59 +0200 Subject: [PATCH] add internal InspectStubIndexAction GitOrigin-RevId: 3302b2f3b8841918348e9949b96d10cf93f55346 --- .../internal/InspectStubIndexAction.java | 119 ++++++++++++++++++ resources/src/idea/JavaActions.xml | 3 + 2 files changed, 122 insertions(+) create mode 100644 java/java-impl/src/com/intellij/internal/InspectStubIndexAction.java diff --git a/java/java-impl/src/com/intellij/internal/InspectStubIndexAction.java b/java/java-impl/src/com/intellij/internal/InspectStubIndexAction.java new file mode 100644 index 000000000000..edbfc5684cd4 --- /dev/null +++ b/java/java-impl/src/com/intellij/internal/InspectStubIndexAction.java @@ -0,0 +1,119 @@ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.internal; + +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Trinity; +import com.intellij.openapi.vfs.VfsUtilCore; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.openapi.vfs.VirtualFileWithId; +import com.intellij.openapi.vfs.newvfs.ManagingFS; +import com.intellij.psi.search.*; +import com.intellij.psi.stubs.*; +import com.intellij.util.indexing.FileBasedIndex; +import com.intellij.util.indexing.FileBasedIndexImpl; +import com.intellij.util.indexing.IdIterator; +import com.intellij.util.indexing.StorageException; +import gnu.trove.TIntArrayList; +import org.jetbrains.annotations.NotNull; + +import java.io.File; +import java.util.*; + +public class InspectStubIndexAction extends AnAction { + private static final Logger LOG = Logger.getInstance(InspectStubIndexAction.class); + @Override + public void actionPerformed(@NotNull AnActionEvent e) { + Project project = e.getProject(); + if (project == null) return; + actionPerformed(project); + } + + public static void actionPerformed(Project project) { + Collection fileIds = FileBasedIndex.getInstance().getAllKeys(StubUpdatingIndex.INDEX_ID, project); + + TIntArrayList staleFileIds = new TIntArrayList(); + List staleFiles = new ArrayList<>(); + List> mismatchedKeys = new ArrayList<>(); + for (Integer id : fileIds) { + VirtualFile vFile; + try { + vFile = ManagingFS.getInstance().findFileById(id); + } + catch (Exception e) { + LOG.error(e); + continue; + } + if (vFile == null) { + try { + Map staleTree = + ((FileBasedIndexImpl)FileBasedIndex.getInstance()).getIndex(StubUpdatingIndex.INDEX_ID).getIndexedFileData(id); + + if (!staleTree.isEmpty()) { + staleFileIds.add(id); + } + } + catch (StorageException ex) { + LOG.error(ex); + } + continue; + } + + if (vFile.isInLocalFileSystem()) { + File ioFile = VfsUtilCore.virtualToIoFile(vFile); + if (!ioFile.exists()) { + staleFiles.add(vFile); + continue; + } + } + + Map data = FileBasedIndex.getInstance().getFileData(StubUpdatingIndex.INDEX_ID, vFile, project); + if (data.isEmpty()) { + continue; + } + if (!((FileBasedIndexImpl)FileBasedIndex.getInstance()).projectIndexableFiles(project).containsFileId(id)) { + continue; + } + try { + SerializedStubTree tree = data.values().iterator().next(); + for (Map.Entry> entry : tree.readStubIndicesValueMap().entrySet()) { + StubIndexKey stubIndexKey = entry.getKey(); + Set keys = entry.getValue().keySet(); + + for (Object key : keys) { + boolean found = false; + IdIterator ids = StubIndex.getInstance().getContainingIds(stubIndexKey, key, project, GlobalSearchScope.allScope(project)); + while (ids.hasNext()) { + if (ids.next() == id) { + found = true; + break; + } + } + + if (!found) { + mismatchedKeys.add(Trinity.create(vFile, stubIndexKey, key)); + } + } + } + } + catch (Exception ex) { + LOG.error(ex); + } + } + + LOG.info("Stub index analysis is finished for " + fileIds.size() + " records."); + if (!staleFileIds.isEmpty()) { + LOG.info("Stale file ids: " + staleFileIds); + } + if (!staleFiles.isEmpty()) { + LOG.info("Stale files: " + staleFiles); + } + if (!mismatchedKeys.isEmpty()) { + for (Trinity key : mismatchedKeys) { + LOG.info("mismatched key file = " + key.getFirst() + " id = " + ((VirtualFileWithId)key.getFirst()).getId() + " index = " + key.getSecond().getName() + " index key = " + key.getThird()); + } + } + } +} diff --git a/resources/src/idea/JavaActions.xml b/resources/src/idea/JavaActions.xml index b67849b90231..effeec567a8e 100644 --- a/resources/src/idea/JavaActions.xml +++ b/resources/src/idea/JavaActions.xml @@ -449,6 +449,9 @@ + +