2 * Copyright 2000-2014 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.util.indexing;
18 import com.intellij.ProjectTopics;
19 import com.intellij.ide.IdeBundle;
20 import com.intellij.ide.caches.FileContent;
21 import com.intellij.ide.startup.impl.StartupManagerImpl;
22 import com.intellij.openapi.application.ApplicationManager;
23 import com.intellij.openapi.diagnostic.Logger;
24 import com.intellij.openapi.progress.ProcessCanceledException;
25 import com.intellij.openapi.progress.ProgressIndicator;
26 import com.intellij.openapi.project.CacheUpdateRunner;
27 import com.intellij.openapi.project.DumbModeTask;
28 import com.intellij.openapi.project.DumbService;
29 import com.intellij.openapi.project.Project;
30 import com.intellij.openapi.roots.CollectingContentIterator;
31 import com.intellij.openapi.roots.ModuleRootAdapter;
32 import com.intellij.openapi.roots.ModuleRootEvent;
33 import com.intellij.openapi.roots.impl.PushedFilePropertiesUpdater;
34 import com.intellij.openapi.startup.StartupManager;
35 import com.intellij.openapi.vfs.VirtualFile;
36 import com.intellij.util.Consumer;
37 import org.jetbrains.annotations.NotNull;
39 import java.util.List;
42 * @author Eugene Zhuravlev
45 public class UnindexedFilesUpdater extends DumbModeTask {
46 private static final Logger LOG = Logger.getInstance("#com.intellij.util.indexing.UnindexedFilesUpdater");
48 private final FileBasedIndexImpl myIndex = (FileBasedIndexImpl)FileBasedIndex.getInstance();
49 private final Project myProject;
50 private final boolean myOnStartup;
52 public UnindexedFilesUpdater(final Project project, boolean onStartup) {
54 myOnStartup = onStartup;
55 project.getMessageBus().connect(this).subscribe(ProjectTopics.PROJECT_ROOTS, new ModuleRootAdapter() {
57 public void rootsChanged(ModuleRootEvent event) {
58 DumbService.getInstance(project).cancelTask(UnindexedFilesUpdater.this);
63 private void updateUnindexedFiles(ProgressIndicator indicator) {
64 long started = System.currentTimeMillis();
65 PushedFilePropertiesUpdater.getInstance(myProject).pushAllPropertiesNow();
66 LOG.info("Pushed properties in " + (System.currentTimeMillis() - started) + " ms");
68 indicator.setIndeterminate(true);
69 indicator.setText(IdeBundle.message("progress.indexing.scanning"));
71 CollectingContentIterator finder = myIndex.createContentIterator(indicator);
72 long l = System.currentTimeMillis();
73 myIndex.iterateIndexableFiles(finder, myProject, indicator);
74 myIndex.filesUpdateEnumerationFinished();
76 LOG.info("Indexable files iterated in " + (System.currentTimeMillis() - l) + " ms");
77 List<VirtualFile> files = finder.getFiles();
79 if (myOnStartup && !ApplicationManager.getApplication().isUnitTestMode()) {
80 // full VFS refresh makes sense only after it's loaded, i.e. after scanning files to index is finished
81 ((StartupManagerImpl)StartupManager.getInstance(myProject)).scheduleInitialVfsRefresh();
84 if (files.isEmpty()) {
88 started = System.currentTimeMillis();
89 LOG.info("Unindexed files update started: " + files.size() + " files to update");
91 indicator.setIndeterminate(false);
92 indicator.setText(IdeBundle.message("progress.indexing.updating"));
94 indexFiles(indicator, files);
95 LOG.info("Unindexed files update done in " + (System.currentTimeMillis() - started) + " ms");
98 private void indexFiles(ProgressIndicator indicator, List<VirtualFile> files) {
99 CacheUpdateRunner.processFiles(indicator, true, files, myProject, new Consumer<FileContent>() {
101 public void consume(FileContent content) {
103 myIndex.indexFileContent(myProject, content);
106 IndexingStamp.flushCache(content.getVirtualFile());
113 public void performInDumbMode(@NotNull ProgressIndicator indicator) {
114 myIndex.filesUpdateStarted(myProject);
116 updateUnindexedFiles(indicator);
118 catch (ProcessCanceledException e) {
119 LOG.info("Unindexed files update canceled");
122 myIndex.filesUpdateFinished(myProject);