From 489d176efff250eeef343ff60663a430d3bb9916 Mon Sep 17 00:00:00 2001 From: Eugene Zhuravlev Date: Tue, 6 Oct 2009 11:05:45 +0400 Subject: [PATCH] before accessing index data forcibly update only those 'dirty' files that can affect the result (update the file only if it is accepted by search scope) --- .../com/intellij/psi/stubs/StubIndexImpl.java | 4 +- .../BackgroundCacheUpdaterRunner.java | 36 +--------------- .../util/indexing/FileBasedIndex.java | 43 ++++++++++++------- 3 files changed, 31 insertions(+), 52 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/psi/stubs/StubIndexImpl.java b/platform/lang-impl/src/com/intellij/psi/stubs/StubIndexImpl.java index f6d6283ef069..556784eefaab 100644 --- a/platform/lang-impl/src/com/intellij/psi/stubs/StubIndexImpl.java +++ b/platform/lang-impl/src/com/intellij/psi/stubs/StubIndexImpl.java @@ -151,7 +151,7 @@ public class StubIndexImpl extends StubIndex implements ApplicationComponent, Pe final GlobalSearchScope scope) { checkRebuild(project); - FileBasedIndex.getInstance().ensureUpToDate(StubUpdatingIndex.INDEX_ID, project); + FileBasedIndex.getInstance().ensureUpToDate(StubUpdatingIndex.INDEX_ID, project, scope); final PersistentFS fs = (PersistentFS)ManagingFS.getInstance(); final PsiManager psiManager = PsiManager.getInstance(project); @@ -289,7 +289,7 @@ public class StubIndexImpl extends StubIndex implements ApplicationComponent, Pe public Collection getAllKeys(final StubIndexKey indexKey, @NotNull Project project) { checkRebuild(project); - FileBasedIndex.getInstance().ensureUpToDate(StubUpdatingIndex.INDEX_ID, project); + FileBasedIndex.getInstance().ensureUpToDate(StubUpdatingIndex.INDEX_ID, project, GlobalSearchScope.allScope(project)); final MyIndex index = (MyIndex)myIndices.get(indexKey); try { diff --git a/platform/lang-impl/src/com/intellij/util/indexing/BackgroundCacheUpdaterRunner.java b/platform/lang-impl/src/com/intellij/util/indexing/BackgroundCacheUpdaterRunner.java index 7ccf4b3a0554..75a623f21120 100644 --- a/platform/lang-impl/src/com/intellij/util/indexing/BackgroundCacheUpdaterRunner.java +++ b/platform/lang-impl/src/com/intellij/util/indexing/BackgroundCacheUpdaterRunner.java @@ -13,7 +13,6 @@ import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.progress.util.ProgressIndicatorBase; import com.intellij.openapi.project.DumbServiceImpl; import com.intellij.openapi.project.Project; -import com.intellij.openapi.project.ProjectManager; import com.intellij.openapi.project.ProjectUtil; import com.intellij.openapi.util.Key; import com.intellij.openapi.util.Ref; @@ -25,7 +24,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.*; -import java.util.concurrent.CountDownLatch; /** * @author Eugene Zhuravlev @@ -53,25 +51,6 @@ public class BackgroundCacheUpdaterRunner { } public void processFiles(final CacheUpdater updater) { - final CountDownLatch latch = new CountDownLatch(myProjectToFileMap.size()); - - for (Project project : ProjectManager.getInstance().getOpenProjects()) { - if (!myProjectToFileMap.containsKey(project)) { - // if the project is not affected still need to enter dumb mode to keep indices updating in background - // otherwise index access from this project may cause forceUpdate() which will index all the files in the calling thread - DumbServiceImpl.getInstance(project).queueIndexUpdate(new Consumer() { - public void consume(ProgressIndicator progressIndicator) { - try { - // just wait until update ends and exit dumb mode - latch.await(); - } - catch (InterruptedException ignored) { - } - } - }); - } - } - for (Map.Entry> entry : myProjectToFileMap.entrySet()) { final Project project = entry.getKey(); final Collection files = entry.getValue(); @@ -106,14 +85,8 @@ public class BackgroundCacheUpdaterRunner { } } finally { - latch.countDown(); queue.clear(); updater.updatingDone(); - try { - latch.await(); - } - catch (InterruptedException ignored) { - } } } }; @@ -121,13 +94,8 @@ public class BackgroundCacheUpdaterRunner { DumbServiceImpl.getInstance(project).queueIndexUpdate(action); } else { - try { - final ProgressIndicator currentIndicator = ProgressManager.getInstance().getProgressIndicator(); - action.consume(currentIndicator != null? currentIndicator : new EmptyProgressIndicator()); - } - finally { - latch.countDown(); - } + final ProgressIndicator currentIndicator = ProgressManager.getInstance().getProgressIndicator(); + action.consume(currentIndicator != null? currentIndicator : new EmptyProgressIndicator()); } } } diff --git a/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndex.java b/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndex.java index 62fc48811124..c38046e52735 100644 --- a/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndex.java +++ b/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndex.java @@ -418,7 +418,7 @@ public class FileBasedIndex implements ApplicationComponent { LOG.info("START INDEX SHUTDOWN"); try { - myChangedFilesUpdater.forceUpdate(); + myChangedFilesUpdater.forceUpdate(null); for (ID indexId : myIndices.keySet()) { final UpdatableIndex index = getIndex(indexId); @@ -458,6 +458,10 @@ public class FileBasedIndex implements ApplicationComponent { }); } + /** + * @param project it is guaranteeed to return data which is up-to-date withing the project + * Keys obtained from the files which do not belong to the project specified may not be up-to-date or even exist + */ @NotNull public Collection getAllKeys(final ID indexId, @NotNull Project project) { Set allKeys = new HashSet(); @@ -465,9 +469,13 @@ public class FileBasedIndex implements ApplicationComponent { return allKeys; } + /** + * @param project it is guaranteeed to return data which is up-to-date withing the project + * Keys obtained from the files which do not belong to the project specified may not be up-to-date or even exist + */ public boolean processAllKeys(final ID indexId, Processor processor, @NotNull Project project) { try { - ensureUpToDate(indexId, project); + ensureUpToDate(indexId, project, GlobalSearchScope.allScope(project)); final UpdatableIndex index = getIndex(indexId); if (index == null) return true; return index.processAllKeys(processor); @@ -524,7 +532,7 @@ public class FileBasedIndex implements ApplicationComponent { * DO NOT CALL DIRECTLY IN CLIENT CODE * The method is internal to indexing engine end is called internally. The method is public due to implementation details */ - public void ensureUpToDate(final ID indexId, @NotNull Project project) { + public void ensureUpToDate(final ID indexId, @NotNull Project project, @Nullable GlobalSearchScope filter) { if (isDumb(project)) { handleDumbMode(indexId, project); } @@ -540,6 +548,7 @@ public class FileBasedIndex implements ApplicationComponent { if (isUpToDateCheckEnabled()) { try { checkRebuild(indexId, false); + myChangedFilesUpdater.forceUpdate(filter); indexUnsavedDocuments(indexId, project); } catch (StorageException e) { @@ -636,7 +645,7 @@ public class FileBasedIndex implements ApplicationComponent { try { final Project project = filter.getProject(); assert project != null : "GlobalSearchScope#getProject() should be not-null for all index queries"; - ensureUpToDate(indexId, project); + ensureUpToDate(indexId, project, filter); final UpdatableIndex index = getIndex(indexId); if (index == null) { return true; @@ -709,7 +718,7 @@ public class FileBasedIndex implements ApplicationComponent { try { final Project project = filter.getProject(); assert project != null : "GlobalSearchScope#getProject() should be not-null for all index queries"; - ensureUpToDate(indexId, project); + ensureUpToDate(indexId, project, filter); final UpdatableIndex index = getIndex(indexId); if (index == null) { return true; @@ -794,7 +803,7 @@ public class FileBasedIndex implements ApplicationComponent { public void processAllValues(final ID indexId, AllValuesProcessor processor, @NotNull Project project) { try { - ensureUpToDate(indexId, project); + ensureUpToDate(indexId, project, null); final UpdatableIndex index = getIndex(indexId); if (index == null) { return; @@ -909,7 +918,6 @@ public class FileBasedIndex implements ApplicationComponent { } private void indexUnsavedDocuments(ID indexId, Project project) throws StorageException { - myChangedFilesUpdater.forceUpdate(); if (myUpToDateIndices.contains(indexId)) { return; // no need to index unsaved docs @@ -1526,19 +1534,22 @@ public class FileBasedIndex implements ApplicationComponent { private final Semaphore myForceUpdateSemaphore = new Semaphore(); - public void forceUpdate() { + public void forceUpdate(@Nullable GlobalSearchScope filter) { myChangedFilesUpdater.ensureAllInvalidateTasksCompleted(); final VirtualFile[] files = queryNeededFiles(); if (files.length > 0) { - myForceUpdateSemaphore.down(); - try { - for (VirtualFile file: files) { - processFileImpl(new com.intellij.ide.startup.FileContent(file)); + for (VirtualFile file: files) { + if (filter == null || filter.accept(file)) { + try { + myForceUpdateSemaphore.down(); + // process only files that can affect result + processFileImpl(new com.intellij.ide.startup.FileContent(file)); + } + finally { + myForceUpdateSemaphore.up(); + } } } - finally { - myForceUpdateSemaphore.up(); - } } // If several threads entered the method at the same time and there were files to update, @@ -1692,7 +1703,7 @@ public class FileBasedIndex implements ApplicationComponent { } public void removeIndexableSet(IndexableFileSet set) { - myChangedFilesUpdater.forceUpdate(); + myChangedFilesUpdater.forceUpdate(null); myIndexableSets.remove(set); }