From adbdbefcdb4c8d603145b7a02b3fb5e9eb511246 Mon Sep 17 00:00:00 2001 From: "Maxim.Mossienko" Date: Thu, 3 Nov 2016 10:28:26 +0100 Subject: [PATCH] cleanup: extracted code related to vfs event filtering from FileBasedIndexImpl to IndexedFilesListener --- .../com/intellij/index/IndexTest.groovy | 10 ++-- .../util/indexing/FileBasedIndexImpl.java | 43 ++------------- .../util/indexing/IndexedFilesListener.java | 53 ++++++++++++++++++- 3 files changed, 58 insertions(+), 48 deletions(-) diff --git a/java/java-tests/testSrc/com/intellij/index/IndexTest.groovy b/java/java-tests/testSrc/com/intellij/index/IndexTest.groovy index 8c942956f1f1..55e6042ab5ca 100644 --- a/java/java-tests/testSrc/com/intellij/index/IndexTest.groovy +++ b/java/java-tests/testSrc/com/intellij/index/IndexTest.groovy @@ -565,15 +565,13 @@ class IndexTest extends JavaCodeInsightFixtureTestCase { }); } - @Override - protected void buildIndicesForFile(VirtualFile file, boolean contentChange) { - recordFileScheduledForIndexing(((VirtualFileWithId)file).id, file, contentChange) + protected void doInvalidateIndicesForFile(VirtualFile file, boolean contentChange) { + recordFileScheduledForInvalidation(((VirtualFileWithId)file).id, file, contentChange); } @Override - protected boolean invalidateIndicesForFile(VirtualFile file, boolean contentChange) { - recordFileScheduledForInvalidation(((VirtualFileWithId)file).id, file, contentChange); - return true + protected void buildIndicesForFile(VirtualFile file, boolean contentChange) { + recordFileScheduledForIndexing(((VirtualFileWithId)file).id, file, contentChange) } } ApplicationManager.getApplication().getMessageBus().connect(getTestRootDisposable()).subscribe( diff --git a/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java b/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java index 23369b051913..5fbe3c67ee5d 100644 --- a/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java +++ b/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java @@ -126,8 +126,6 @@ public class FileBasedIndexImpl extends FileBasedIndex { private final Set> myUpToDateIndicesForUnsavedOrTransactedDocuments = ContainerUtil.newConcurrentSet(); private volatile SmartFMap myTransactionMap = SmartFMap.emptyMap(); - @Nullable private final String myConfigPath; - @Nullable private final String myLogPath; private final boolean myIsUnitTestMode; @Nullable private ScheduledFuture myFlushingFuture; private volatile int myLocalModCount; @@ -166,8 +164,6 @@ public class FileBasedIndexImpl extends FileBasedIndex { myFileDocumentManager = fdm; myFileTypeManager = fileTypeManager; myIsUnitTestMode = ApplicationManager.getApplication().isUnitTestMode(); - myConfigPath = calcConfigPath(PathManager.getConfigPath()); - myLogPath = calcConfigPath(PathManager.getLogPath()); final MessageBusConnection connection = bus.connect(); connection.subscribe(PsiDocumentTransactionListener.TOPIC, new PsiDocumentTransactionListener() { @@ -313,18 +309,6 @@ public class FileBasedIndexImpl extends FileBasedIndex { } } - @Nullable - private static String calcConfigPath(@NotNull String path) { - try { - final String _path = FileUtil.toSystemIndependentName(new File(path).getCanonicalPath()); - return _path.endsWith("/") ? _path : _path + "/"; - } - catch (IOException e) { - LOG.info(e); - return null; - } - } - /** * @return true if registered index requires full rebuild for some reason, e.g. is just created or corrupted */ @@ -1823,9 +1807,6 @@ public class FileBasedIndexImpl extends FileBasedIndex { private final class ChangedFilesCollector extends IndexedFilesListener { private final ConcurrentIntObjectMap myFilesToUpdate = ContainerUtil.createConcurrentIntObjectMap(); - - private final ManagingFS myManagingFS = ManagingFS.getInstance(); - @Override protected void buildIndicesForFileRecursively(@NotNull VirtualFile file, boolean contentChange) { cleanProcessedFlag(file); @@ -1858,20 +1839,8 @@ public class FileBasedIndexImpl extends FileBasedIndex { } @Override - protected boolean invalidateIndicesForFile(VirtualFile file, boolean contentChange) { - if (isUnderConfigOrSystem(file)) { - return false; - } - if (file.isDirectory()) { - doInvalidateIndicesForFile(file, contentChange); - if (!isMock(file) && !myManagingFS.wereChildrenAccessed(file)) { - return false; - } - } - else { - doInvalidateIndicesForFile(file, contentChange); - } - return true; + protected void doInvalidateIndicesForFile(VirtualFile file, boolean contentChange) { + FileBasedIndexImpl.this.doInvalidateIndicesForFile(file, contentChange); } void scheduleForUpdate(VirtualFile file) { @@ -2071,13 +2040,7 @@ public class FileBasedIndexImpl extends FileBasedIndex { (isMock(file) || !getIndex(indexId).isIndexedStateForFile(((NewVirtualFile)file).getId(), file)); } - private boolean isUnderConfigOrSystem(@NotNull VirtualFile file) { - final String filePath = file.getPath(); - return myConfigPath != null && FileUtil.startsWith(filePath, myConfigPath) || - myLogPath != null && FileUtil.startsWith(filePath, myLogPath); - } - - private static boolean isMock(final VirtualFile file) { + static boolean isMock(final VirtualFile file) { return !(file instanceof NewVirtualFile); } diff --git a/platform/lang-impl/src/com/intellij/util/indexing/IndexedFilesListener.java b/platform/lang-impl/src/com/intellij/util/indexing/IndexedFilesListener.java index fd46e3d68caa..371be7a1f93f 100644 --- a/platform/lang-impl/src/com/intellij/util/indexing/IndexedFilesListener.java +++ b/platform/lang-impl/src/com/intellij/util/indexing/IndexedFilesListener.java @@ -16,21 +16,36 @@ package com.intellij.util.indexing; import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.application.PathManager; import com.intellij.openapi.roots.ContentIterator; +import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.vfs.*; import com.intellij.openapi.vfs.impl.BulkVirtualFileListenerAdapter; import com.intellij.openapi.vfs.newvfs.BulkFileListener; +import com.intellij.openapi.vfs.newvfs.ManagingFS; import com.intellij.openapi.vfs.newvfs.NewVirtualFile; import com.intellij.openapi.vfs.newvfs.events.VFileEvent; import com.intellij.util.Processor; import com.intellij.util.containers.ConcurrentIntObjectMap; import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.TestOnly; +import java.io.File; +import java.io.IOException; import java.util.List; public abstract class IndexedFilesListener extends VirtualFileAdapter implements BulkFileListener { + private final ManagingFS myManagingFS = ManagingFS.getInstance(); + @Nullable private final String myConfigPath; + @Nullable private final String myLogPath; + + public IndexedFilesListener() { + myConfigPath = calcConfigPath(PathManager.getConfigPath()); + myLogPath = calcConfigPath(PathManager.getLogPath()); + } + @Override public void fileMoved(@NotNull VirtualFileMoveEvent event) { buildIndicesForFileRecursively(event.getFile(), false); @@ -100,9 +115,25 @@ public abstract class IndexedFilesListener extends VirtualFileAdapter implements } } + protected boolean invalidateIndicesForFile(VirtualFile file, boolean contentChange) { + if (isUnderConfigOrSystem(file)) { + return false; + } + if (file.isDirectory()) { + doInvalidateIndicesForFile(file, contentChange); + if (!FileBasedIndexImpl.isMock(file) && !myManagingFS.wereChildrenAccessed(file)) { + return false; + } + } + else { + doInvalidateIndicesForFile(file, contentChange); + } + return true; + } + protected abstract void iterateIndexableFiles(VirtualFile file, ContentIterator iterator); protected abstract void buildIndicesForFile(VirtualFile file, boolean contentChange); - protected abstract boolean invalidateIndicesForFile(VirtualFile file, boolean contentChange); + protected abstract void doInvalidateIndicesForFile(VirtualFile file, boolean contentChange); protected void invalidateIndicesRecursively(@NotNull final VirtualFile file, final boolean contentChange) { VfsUtilCore.visitChildrenRecursively(file, new VirtualFileVisitor() { @@ -132,6 +163,24 @@ public abstract class IndexedFilesListener extends VirtualFileAdapter implements } } + @Nullable + private static String calcConfigPath(@NotNull String path) { + try { + final String _path = FileUtil.toSystemIndependentName(new File(path).getCanonicalPath()); + return _path.endsWith("/") ? _path : _path + "/"; + } + catch (IOException e) { + FileBasedIndexImpl.LOG.info(e); + return null; + } + } + + private boolean isUnderConfigOrSystem(@NotNull VirtualFile file) { + final String filePath = file.getPath(); + return myConfigPath != null && FileUtil.startsWith(filePath, myConfigPath) || + myLogPath != null && FileUtil.startsWith(filePath, myLogPath); + } + static final short ADD_TO_INDICES = 1; static final short REMOVE_FROM_INDICES = 2; static final short UPDATE_INDICES_CONTENT_CHANGED = 4; @@ -191,7 +240,7 @@ public abstract class IndexedFilesListener extends VirtualFileAdapter implements } private void updateChange(int fileId, VirtualFile file, short mask) { - if (DebugAssertions.DEBUG) assert ApplicationManager.getApplication().isWriteAccessAllowed(); + if (DebugAssertions.DEBUG) assert ApplicationManager.getApplication().isDispatchThread(); ChangeInfo changeInfo = myChangeInfos.get(fileId); if (changeInfo == null) myChangeInfos.put(fileId, new ChangeInfo(file, mask)); else changeInfo.changeOperation(mask);