From c2500ca316b637ea6a421a9804188f557518401b Mon Sep 17 00:00:00 2001 From: "Maxim.Mossienko" Date: Thu, 2 Jul 2015 12:48:41 +0200 Subject: [PATCH] fix tests, less garbage during index updates (IDEA-142179) --- .../psi/impl/cache/impl/IdCacheTest.java | 11 +-- .../intellij/psi/search/UpdateCacheTest.java | 11 +-- .../util/indexing/FileBasedIndexImpl.java | 96 +++++++++---------- 3 files changed, 53 insertions(+), 65 deletions(-) diff --git a/java/java-tests/testSrc/com/intellij/psi/impl/cache/impl/IdCacheTest.java b/java/java-tests/testSrc/com/intellij/psi/impl/cache/impl/IdCacheTest.java index 34a66d93c262..a1c4fbba4a23 100644 --- a/java/java-tests/testSrc/com/intellij/psi/impl/cache/impl/IdCacheTest.java +++ b/java/java-tests/testSrc/com/intellij/psi/impl/cache/impl/IdCacheTest.java @@ -27,13 +27,13 @@ import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiFile; import com.intellij.psi.impl.cache.CacheManager; import com.intellij.psi.impl.cache.TodoCacheManager; -import com.intellij.psi.impl.cache.impl.id.IdIndex; -import com.intellij.psi.impl.cache.impl.todo.TodoIndex; -import com.intellij.psi.search.*; +import com.intellij.psi.search.GlobalSearchScope; +import com.intellij.psi.search.TodoAttributesUtil; +import com.intellij.psi.search.TodoPattern; +import com.intellij.psi.search.UsageSearchContext; import com.intellij.testFramework.IdeaTestUtil; import com.intellij.testFramework.PsiTestUtil; import com.intellij.util.ArrayUtil; -import com.intellij.util.indexing.FileBasedIndex; import java.io.File; import java.util.Arrays; @@ -48,9 +48,6 @@ public class IdCacheTest extends CodeInsightTestCase{ protected void setUp() throws Exception { super.setUp(); - FileBasedIndex.getInstance().requestRebuild(IdIndex.NAME); - FileBasedIndex.getInstance().requestRebuild(TodoIndex.NAME); - String root = JavaTestUtil.getJavaTestDataPath()+ "/psi/impl/cache/"; PsiTestUtil.removeAllRoots(myModule, IdeaTestUtil.getMockJdk17()); diff --git a/java/java-tests/testSrc/com/intellij/psi/search/UpdateCacheTest.java b/java/java-tests/testSrc/com/intellij/psi/search/UpdateCacheTest.java index 3a4f928505e7..14830b0a7402 100644 --- a/java/java-tests/testSrc/com/intellij/psi/search/UpdateCacheTest.java +++ b/java/java-tests/testSrc/com/intellij/psi/search/UpdateCacheTest.java @@ -34,7 +34,6 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.*; import com.intellij.psi.impl.JavaPsiFacadeEx; import com.intellij.psi.impl.PsiManagerImpl; -import com.intellij.psi.impl.cache.impl.id.IdIndex; import com.intellij.psi.impl.cache.impl.todo.TodoIndex; import com.intellij.psi.impl.cache.impl.todo.TodoIndexEntry; import com.intellij.psi.impl.source.tree.injected.InjectedLanguageManagerImpl; @@ -52,15 +51,7 @@ import java.io.File; import java.util.*; @PlatformTestCase.WrapInCommand -public class UpdateCacheTest extends PsiTestCase{ - @Override - protected void setUp() throws Exception { - super.setUp(); - - FileBasedIndex.getInstance().requestRebuild(IdIndex.NAME); - FileBasedIndex.getInstance().requestRebuild(TodoIndex.NAME); - } - +public class UpdateCacheTest extends PsiTestCase { @Override protected void setUpProject() throws Exception { myProjectManager = ProjectManagerEx.getInstanceEx(); 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 e327cf9e2d6f..93a4f5a0e133 100644 --- a/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java +++ b/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java @@ -1716,9 +1716,11 @@ public class FileBasedIndexImpl extends FileBasedIndex { fc.putUserData(IndexingDataKeys.PROJECT, project); } + static final Key ourPhysicalContentKey = Key.create("physical.content.flag"); + private void updateSingleIndex(@NotNull ID indexId, final int inputId, @Nullable FileContent currentFC) throws StorageException { - if (ourRebuildStatus.get(indexId).get() == REQUIRES_REBUILD) { + if (ourRebuildStatus.get(indexId).get() == REQUIRES_REBUILD && !myIsUnitTestMode) { return; // the index is scheduled for rebuild, no need to update } myLocalModCount++; @@ -1726,7 +1728,8 @@ public class FileBasedIndexImpl extends FileBasedIndex { final UpdatableIndex index = getIndex(indexId); assert index != null; - if (currentFC != null && currentFC.getUserData(ourPhysicalContentKey) == null) { + boolean hasContent = currentFC != null; + if (hasContent && currentFC.getUserData(ourPhysicalContentKey) == null) { currentFC.putUserData(ourPhysicalContentKey, Boolean.TRUE); } @@ -1735,10 +1738,7 @@ public class FileBasedIndexImpl extends FileBasedIndex { final Computable update = index.update(inputId, currentFC); try { - scheduleUpdate(indexId, - createUpdateComputableWithBufferingDisabled(update), - createIndexedStampUpdateRunnable(indexId, inputId, currentFC != null) - ); + scheduleUpdate(indexId, update, inputId, hasContent); } catch (RuntimeException exception) { Throwable causeToRebuildIndex = getCauseToRebuildIndex(exception); if (causeToRebuildIndex != null) { @@ -1750,51 +1750,51 @@ public class FileBasedIndexImpl extends FileBasedIndex { } } - static final Key ourPhysicalContentKey = Key.create("physical.content.flag"); - - @NotNull - private Runnable createIndexedStampUpdateRunnable(@NotNull final ID indexId, - final int fileId, - final boolean hasContent) { - return new Runnable() { - @Override - public void run() { - if (hasContent) { - IndexingStamp.setFileIndexedStateCurrent(fileId, indexId); - } - else { - IndexingStamp.setFileIndexedStateUnindexed(fileId, indexId); - } - if (myNotRequiringContentIndices.contains(indexId)) IndexingStamp.flushCache(fileId); - } - }; - } - - @NotNull - private Computable createUpdateComputableWithBufferingDisabled(@NotNull final Computable update) { - return new Computable() { - @Override - public Boolean compute() { - Boolean result; - final StorageGuard.StorageModeExitHandler lock = setDataBufferingEnabled(false); - try { - result = update.compute(); - } - finally { - lock.leave(); - } - return result; - } - }; - } - - private void scheduleUpdate(@NotNull ID indexId, @NotNull Computable update, @NotNull Runnable successRunnable) { + private void scheduleUpdate(@NotNull final ID indexId, final Computable update, final int inputId, final boolean hasContent) { if (myNotRequiringContentIndices.contains(indexId) && !Registry.is("idea.concurrent.scanning.files.to.index")) { - myContentlessIndicesUpdateQueue.submit(update, successRunnable); + myContentlessIndicesUpdateQueue.submit( + new Computable() { + @Override + public Boolean compute() { + return updateWithBufferingEnabled(update); + } + }, + new Runnable() { + @Override + public void run() { + indexedStampUpdate(indexId, inputId, hasContent); + } + }); } else { - Boolean result = update.compute(); - if (result == Boolean.TRUE) ApplicationManager.getApplication().runReadAction(successRunnable); + if (updateWithBufferingEnabled(update)) { + AccessToken accessToken = ReadAction.start(); + try { + indexedStampUpdate(indexId, inputId, hasContent); + } finally { + accessToken.finish(); + } + } + } + } + + protected void indexedStampUpdate(@NotNull ID indexId, int fileId, boolean hasContent) { + if (hasContent) { + IndexingStamp.setFileIndexedStateCurrent(fileId, indexId); + } + else { + IndexingStamp.setFileIndexedStateUnindexed(fileId, indexId); + } + if (myNotRequiringContentIndices.contains(indexId)) IndexingStamp.flushCache(fileId); + } + + protected boolean updateWithBufferingEnabled(@NotNull final Computable update) { + final StorageGuard.StorageModeExitHandler lock = setDataBufferingEnabled(false); + try { + return update.compute(); + } + finally { + lock.leave(); } }